|
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* vim: set ts=2 et sw=2 tw=80: */ |
|
3 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 #include "CheckQuotaHelper.h" |
|
8 |
|
9 #include "nsIDOMWindow.h" |
|
10 #include "nsIObserverService.h" |
|
11 #include "nsIPermissionManager.h" |
|
12 #include "nsIPrincipal.h" |
|
13 #include "nsIScriptObjectPrincipal.h" |
|
14 #include "nsIURI.h" |
|
15 #include "nsPIDOMWindow.h" |
|
16 |
|
17 #include "mozilla/dom/quota/QuotaManager.h" |
|
18 #include "mozilla/Services.h" |
|
19 #include "nsContentUtils.h" |
|
20 #include "nsNetUtil.h" |
|
21 #include "nsThreadUtils.h" |
|
22 #include "nsXULAppAPI.h" |
|
23 |
|
24 #define TOPIC_QUOTA_PROMPT "indexedDB-quota-prompt" |
|
25 #define TOPIC_QUOTA_RESPONSE "indexedDB-quota-response" |
|
26 #define TOPIC_QUOTA_CANCEL "indexedDB-quota-cancel" |
|
27 |
|
28 USING_QUOTA_NAMESPACE |
|
29 using namespace mozilla::services; |
|
30 using mozilla::MutexAutoLock; |
|
31 |
|
32 namespace { |
|
33 |
|
34 inline |
|
35 uint32_t |
|
36 GetQuotaPermissionFromWindow(nsIDOMWindow* aWindow) |
|
37 { |
|
38 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
|
39 |
|
40 nsCOMPtr<nsIScriptObjectPrincipal> sop(do_QueryInterface(aWindow)); |
|
41 NS_ENSURE_TRUE(sop, nsIPermissionManager::DENY_ACTION); |
|
42 |
|
43 return CheckQuotaHelper::GetQuotaPermission(sop->GetPrincipal()); |
|
44 } |
|
45 |
|
46 } // anonymous namespace |
|
47 |
|
48 CheckQuotaHelper::CheckQuotaHelper(nsPIDOMWindow* aWindow, |
|
49 mozilla::Mutex& aMutex) |
|
50 : mWindow(aWindow), |
|
51 mMutex(aMutex), |
|
52 mCondVar(mMutex, "CheckQuotaHelper::mCondVar"), |
|
53 mPromptResult(0), |
|
54 mWaiting(true), |
|
55 mHasPrompted(false) |
|
56 { |
|
57 NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!"); |
|
58 mMutex.AssertCurrentThreadOwns(); |
|
59 } |
|
60 |
|
61 bool |
|
62 CheckQuotaHelper::PromptAndReturnQuotaIsDisabled() |
|
63 { |
|
64 NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!"); |
|
65 mMutex.AssertCurrentThreadOwns(); |
|
66 |
|
67 while (mWaiting) { |
|
68 mCondVar.Wait(); |
|
69 } |
|
70 |
|
71 NS_ASSERTION(!mWindow, "This should always be null here!"); |
|
72 |
|
73 NS_ASSERTION(mPromptResult == nsIPermissionManager::ALLOW_ACTION || |
|
74 mPromptResult == nsIPermissionManager::DENY_ACTION || |
|
75 mPromptResult == nsIPermissionManager::UNKNOWN_ACTION, |
|
76 "Unknown permission!"); |
|
77 |
|
78 return mPromptResult == nsIPermissionManager::ALLOW_ACTION; |
|
79 } |
|
80 |
|
81 void |
|
82 CheckQuotaHelper::Cancel() |
|
83 { |
|
84 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
|
85 mMutex.AssertCurrentThreadOwns(); |
|
86 |
|
87 if (mWaiting && !mHasPrompted) { |
|
88 MutexAutoUnlock unlock(mMutex); |
|
89 |
|
90 // First close any prompts that are open for this window. |
|
91 nsCOMPtr<nsIObserverService> obs = GetObserverService(); |
|
92 NS_WARN_IF_FALSE(obs, "Failed to get observer service!"); |
|
93 if (obs && NS_FAILED(obs->NotifyObservers(static_cast<nsIRunnable*>(this), |
|
94 TOPIC_QUOTA_CANCEL, nullptr))) { |
|
95 NS_WARNING("Failed to notify observers!"); |
|
96 } |
|
97 |
|
98 // If that didn't trigger an Observe callback (maybe the window had already |
|
99 // died?) then go ahead and do it manually. |
|
100 if (!mHasPrompted) { |
|
101 nsAutoString response; |
|
102 response.AppendInt(nsIPermissionManager::UNKNOWN_ACTION); |
|
103 |
|
104 if (NS_SUCCEEDED(Observe(nullptr, TOPIC_QUOTA_RESPONSE, response.get()))) { |
|
105 NS_ASSERTION(mHasPrompted, "Should have set this in Observe!"); |
|
106 } |
|
107 else { |
|
108 NS_WARNING("Failed to notify!"); |
|
109 } |
|
110 } |
|
111 } |
|
112 } |
|
113 |
|
114 // static |
|
115 uint32_t |
|
116 CheckQuotaHelper::GetQuotaPermission(nsIPrincipal* aPrincipal) |
|
117 { |
|
118 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
|
119 NS_ASSERTION(aPrincipal, "Null principal!"); |
|
120 |
|
121 NS_ASSERTION(!nsContentUtils::IsSystemPrincipal(aPrincipal), |
|
122 "Chrome windows shouldn't track quota!"); |
|
123 |
|
124 nsCOMPtr<nsIPermissionManager> pm = |
|
125 do_GetService(NS_PERMISSIONMANAGER_CONTRACTID); |
|
126 NS_ENSURE_TRUE(pm, nsIPermissionManager::DENY_ACTION); |
|
127 |
|
128 uint32_t permission; |
|
129 nsresult rv = pm->TestPermissionFromPrincipal(aPrincipal, |
|
130 PERMISSION_STORAGE_UNLIMITED, |
|
131 &permission); |
|
132 NS_ENSURE_SUCCESS(rv, nsIPermissionManager::DENY_ACTION); |
|
133 |
|
134 return permission; |
|
135 } |
|
136 |
|
137 NS_IMPL_ISUPPORTS(CheckQuotaHelper, nsIRunnable, |
|
138 nsIInterfaceRequestor, |
|
139 nsIObserver) |
|
140 |
|
141 NS_IMETHODIMP |
|
142 CheckQuotaHelper::Run() |
|
143 { |
|
144 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
|
145 |
|
146 nsresult rv = NS_OK; |
|
147 |
|
148 if (NS_SUCCEEDED(rv)) { |
|
149 if (!mHasPrompted) { |
|
150 mPromptResult = GetQuotaPermissionFromWindow(mWindow); |
|
151 } |
|
152 |
|
153 if (mHasPrompted) { |
|
154 // Add permissions to the database, but only if we are in the parent |
|
155 // process (if we are in the child process, we have already |
|
156 // set the permission when the prompt was shown in the parent, as |
|
157 // we cannot set the permission from the child). |
|
158 if (mPromptResult != nsIPermissionManager::UNKNOWN_ACTION && |
|
159 XRE_GetProcessType() == GeckoProcessType_Default) { |
|
160 nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(mWindow); |
|
161 NS_ENSURE_TRUE(sop, NS_ERROR_FAILURE); |
|
162 |
|
163 nsCOMPtr<nsIPermissionManager> permissionManager = |
|
164 do_GetService(NS_PERMISSIONMANAGER_CONTRACTID); |
|
165 NS_ENSURE_STATE(permissionManager); |
|
166 |
|
167 rv = permissionManager->AddFromPrincipal(sop->GetPrincipal(), |
|
168 PERMISSION_STORAGE_UNLIMITED, |
|
169 mPromptResult, |
|
170 nsIPermissionManager::EXPIRE_NEVER, 0); |
|
171 NS_ENSURE_SUCCESS(rv, rv); |
|
172 } |
|
173 } |
|
174 else if (mPromptResult == nsIPermissionManager::UNKNOWN_ACTION) { |
|
175 uint32_t quota = QuotaManager::GetStorageQuotaMB(); |
|
176 |
|
177 nsString quotaString; |
|
178 quotaString.AppendInt(quota); |
|
179 |
|
180 nsCOMPtr<nsIObserverService> obs = GetObserverService(); |
|
181 NS_ENSURE_STATE(obs); |
|
182 |
|
183 // We have to watch to make sure that the window doesn't go away without |
|
184 // responding to us. Otherwise our database threads will hang. |
|
185 rv = obs->AddObserver(this, DOM_WINDOW_DESTROYED_TOPIC, false); |
|
186 NS_ENSURE_SUCCESS(rv, rv); |
|
187 |
|
188 rv = obs->NotifyObservers(static_cast<nsIRunnable*>(this), |
|
189 TOPIC_QUOTA_PROMPT, quotaString.get()); |
|
190 NS_ENSURE_SUCCESS(rv, rv); |
|
191 |
|
192 return NS_OK; |
|
193 } |
|
194 } |
|
195 |
|
196 MutexAutoLock lock(mMutex); |
|
197 |
|
198 NS_ASSERTION(mWaiting, "Huh?!"); |
|
199 |
|
200 // This should never be used again. |
|
201 mWindow = nullptr; |
|
202 |
|
203 mWaiting = false; |
|
204 mCondVar.NotifyAll(); |
|
205 |
|
206 return NS_OK; |
|
207 } |
|
208 |
|
209 NS_IMETHODIMP |
|
210 CheckQuotaHelper::GetInterface(const nsIID& aIID, |
|
211 void** aResult) |
|
212 { |
|
213 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
|
214 |
|
215 if (aIID.Equals(NS_GET_IID(nsIObserver))) { |
|
216 return QueryInterface(aIID, aResult); |
|
217 } |
|
218 |
|
219 if (aIID.Equals(NS_GET_IID(nsIDOMWindow))) { |
|
220 return mWindow->QueryInterface(aIID, aResult); |
|
221 } |
|
222 |
|
223 *aResult = nullptr; |
|
224 return NS_ERROR_NOT_AVAILABLE; |
|
225 } |
|
226 |
|
227 NS_IMETHODIMP |
|
228 CheckQuotaHelper::Observe(nsISupports* aSubject, |
|
229 const char* aTopic, |
|
230 const char16_t* aData) |
|
231 { |
|
232 NS_ASSERTION(NS_IsMainThread(), "Wrong thread!"); |
|
233 |
|
234 nsresult rv; |
|
235 |
|
236 if (!strcmp(aTopic, TOPIC_QUOTA_RESPONSE)) { |
|
237 if (!mHasPrompted) { |
|
238 mHasPrompted = true; |
|
239 |
|
240 mPromptResult = nsDependentString(aData).ToInteger(&rv); |
|
241 NS_ENSURE_SUCCESS(rv, rv); |
|
242 |
|
243 rv = NS_DispatchToCurrentThread(this); |
|
244 NS_ENSURE_SUCCESS(rv, rv); |
|
245 |
|
246 // We no longer care about the window here. |
|
247 nsCOMPtr<nsIObserverService> obs = GetObserverService(); |
|
248 NS_ENSURE_STATE(obs); |
|
249 |
|
250 rv = obs->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC); |
|
251 NS_ENSURE_SUCCESS(rv, rv); |
|
252 } |
|
253 return NS_OK; |
|
254 } |
|
255 |
|
256 if (!strcmp(aTopic, DOM_WINDOW_DESTROYED_TOPIC)) { |
|
257 NS_ASSERTION(!mHasPrompted, "Should have removed observer before now!"); |
|
258 NS_ASSERTION(mWindow, "This should never be null!"); |
|
259 |
|
260 nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aSubject)); |
|
261 NS_ENSURE_STATE(window); |
|
262 |
|
263 if (mWindow->GetSerial() == window->GetSerial()) { |
|
264 // This is our window, dying, without responding to our prompt! Fake one. |
|
265 mHasPrompted = true; |
|
266 mPromptResult = nsIPermissionManager::UNKNOWN_ACTION; |
|
267 |
|
268 rv = NS_DispatchToCurrentThread(this); |
|
269 NS_ENSURE_SUCCESS(rv, rv); |
|
270 |
|
271 // We no longer care about the window here. |
|
272 nsCOMPtr<nsIObserverService> obs = GetObserverService(); |
|
273 NS_ENSURE_STATE(obs); |
|
274 |
|
275 rv = obs->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC); |
|
276 NS_ENSURE_SUCCESS(rv, rv); |
|
277 } |
|
278 return NS_OK; |
|
279 } |
|
280 |
|
281 NS_NOTREACHED("Unexpected topic!"); |
|
282 return NS_ERROR_UNEXPECTED; |
|
283 } |