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