dom/quota/CheckQuotaHelper.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     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/. */
     7 #include "CheckQuotaHelper.h"
     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"
    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"
    24 #define TOPIC_QUOTA_PROMPT "indexedDB-quota-prompt"
    25 #define TOPIC_QUOTA_RESPONSE "indexedDB-quota-response"
    26 #define TOPIC_QUOTA_CANCEL "indexedDB-quota-cancel"
    28 USING_QUOTA_NAMESPACE
    29 using namespace mozilla::services;
    30 using mozilla::MutexAutoLock;
    32 namespace {
    34 inline
    35 uint32_t
    36 GetQuotaPermissionFromWindow(nsIDOMWindow* aWindow)
    37 {
    38   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
    40   nsCOMPtr<nsIScriptObjectPrincipal> sop(do_QueryInterface(aWindow));
    41   NS_ENSURE_TRUE(sop, nsIPermissionManager::DENY_ACTION);
    43   return CheckQuotaHelper::GetQuotaPermission(sop->GetPrincipal());
    44 }
    46 } // anonymous namespace
    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 }
    61 bool
    62 CheckQuotaHelper::PromptAndReturnQuotaIsDisabled()
    63 {
    64   NS_ASSERTION(!NS_IsMainThread(), "Wrong thread!");
    65   mMutex.AssertCurrentThreadOwns();
    67   while (mWaiting) {
    68     mCondVar.Wait();
    69   }
    71   NS_ASSERTION(!mWindow, "This should always be null here!");
    73   NS_ASSERTION(mPromptResult == nsIPermissionManager::ALLOW_ACTION ||
    74                mPromptResult == nsIPermissionManager::DENY_ACTION ||
    75                mPromptResult == nsIPermissionManager::UNKNOWN_ACTION,
    76                "Unknown permission!");
    78   return mPromptResult == nsIPermissionManager::ALLOW_ACTION;
    79 }
    81 void
    82 CheckQuotaHelper::Cancel()
    83 {
    84   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
    85   mMutex.AssertCurrentThreadOwns();
    87   if (mWaiting && !mHasPrompted) {
    88     MutexAutoUnlock unlock(mMutex);
    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     }
    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);
   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 }
   114 // static
   115 uint32_t
   116 CheckQuotaHelper::GetQuotaPermission(nsIPrincipal* aPrincipal)
   117 {
   118   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   119   NS_ASSERTION(aPrincipal, "Null principal!");
   121   NS_ASSERTION(!nsContentUtils::IsSystemPrincipal(aPrincipal),
   122                "Chrome windows shouldn't track quota!");
   124   nsCOMPtr<nsIPermissionManager> pm =
   125     do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
   126   NS_ENSURE_TRUE(pm, nsIPermissionManager::DENY_ACTION);
   128   uint32_t permission;
   129   nsresult rv = pm->TestPermissionFromPrincipal(aPrincipal,
   130                                                 PERMISSION_STORAGE_UNLIMITED,
   131                                                 &permission);
   132   NS_ENSURE_SUCCESS(rv, nsIPermissionManager::DENY_ACTION);
   134   return permission;
   135 }
   137 NS_IMPL_ISUPPORTS(CheckQuotaHelper, nsIRunnable,
   138                   nsIInterfaceRequestor,
   139                   nsIObserver)
   141 NS_IMETHODIMP
   142 CheckQuotaHelper::Run()
   143 {
   144   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   146   nsresult rv = NS_OK;
   148   if (NS_SUCCEEDED(rv)) {
   149     if (!mHasPrompted) {
   150       mPromptResult = GetQuotaPermissionFromWindow(mWindow);
   151     }
   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);
   163         nsCOMPtr<nsIPermissionManager> permissionManager =
   164           do_GetService(NS_PERMISSIONMANAGER_CONTRACTID);
   165         NS_ENSURE_STATE(permissionManager);
   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();
   177       nsString quotaString;
   178       quotaString.AppendInt(quota);
   180       nsCOMPtr<nsIObserverService> obs = GetObserverService();
   181       NS_ENSURE_STATE(obs);
   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);
   188       rv = obs->NotifyObservers(static_cast<nsIRunnable*>(this),
   189                                 TOPIC_QUOTA_PROMPT, quotaString.get());
   190       NS_ENSURE_SUCCESS(rv, rv);
   192       return NS_OK;
   193     }
   194   }
   196   MutexAutoLock lock(mMutex);
   198   NS_ASSERTION(mWaiting, "Huh?!");
   200   // This should never be used again.
   201   mWindow = nullptr;
   203   mWaiting = false;
   204   mCondVar.NotifyAll();
   206   return NS_OK;
   207 }
   209 NS_IMETHODIMP
   210 CheckQuotaHelper::GetInterface(const nsIID& aIID,
   211                                void** aResult)
   212 {
   213   NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");
   215   if (aIID.Equals(NS_GET_IID(nsIObserver))) {
   216     return QueryInterface(aIID, aResult);
   217   }
   219   if (aIID.Equals(NS_GET_IID(nsIDOMWindow))) {
   220     return mWindow->QueryInterface(aIID, aResult);
   221   }
   223   *aResult = nullptr;
   224   return NS_ERROR_NOT_AVAILABLE;
   225 }
   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!");
   234   nsresult rv;
   236   if (!strcmp(aTopic, TOPIC_QUOTA_RESPONSE)) {
   237     if (!mHasPrompted) {
   238       mHasPrompted = true;
   240       mPromptResult = nsDependentString(aData).ToInteger(&rv);
   241       NS_ENSURE_SUCCESS(rv, rv);
   243       rv = NS_DispatchToCurrentThread(this);
   244       NS_ENSURE_SUCCESS(rv, rv);
   246       // We no longer care about the window here.
   247       nsCOMPtr<nsIObserverService> obs = GetObserverService();
   248       NS_ENSURE_STATE(obs);
   250       rv = obs->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
   251       NS_ENSURE_SUCCESS(rv, rv);
   252     }
   253     return NS_OK;
   254   }
   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!");
   260     nsCOMPtr<nsPIDOMWindow> window(do_QueryInterface(aSubject));
   261     NS_ENSURE_STATE(window);
   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;
   268       rv = NS_DispatchToCurrentThread(this);
   269       NS_ENSURE_SUCCESS(rv, rv);
   271       // We no longer care about the window here.
   272       nsCOMPtr<nsIObserverService> obs = GetObserverService();
   273       NS_ENSURE_STATE(obs);
   275       rv = obs->RemoveObserver(this, DOM_WINDOW_DESTROYED_TOPIC);
   276       NS_ENSURE_SUCCESS(rv, rv);
   277     }
   278     return NS_OK;
   279   }
   281   NS_NOTREACHED("Unexpected topic!");
   282   return NS_ERROR_UNEXPECTED;
   283 }

mercurial