toolkit/components/diskspacewatcher/DiskSpaceWatcher.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 /* This Source Code Form is subject to the terms of the Mozilla Public
     2  * License, v. 2.0. If a copy of the MPL was not distributed with this
     3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     5 #include "DiskSpaceWatcher.h"
     6 #include "nsIObserverService.h"
     7 #include "nsXULAppAPI.h"
     8 #include "mozilla/Hal.h"
     9 #include "mozilla/ModuleUtils.h"
    10 #include "mozilla/Preferences.h"
    11 #include "mozilla/ClearOnShutdown.h"
    12 #include "mozilla/Services.h"
    14 #define NS_DISKSPACEWATCHER_CID \
    15   { 0xab218518, 0xf197, 0x4fb4, { 0x8b, 0x0f, 0x8b, 0xb3, 0x4d, 0xf2, 0x4b, 0xf4 } }
    17 using namespace mozilla;
    19 StaticRefPtr<DiskSpaceWatcher> gDiskSpaceWatcher;
    21 NS_IMPL_ISUPPORTS(DiskSpaceWatcher, nsIDiskSpaceWatcher, nsIObserver)
    23 uint64_t DiskSpaceWatcher::sFreeSpace = 0;
    24 bool DiskSpaceWatcher::sIsDiskFull = false;
    26 DiskSpaceWatcher::DiskSpaceWatcher()
    27 {
    28   MOZ_ASSERT(NS_IsMainThread());
    29   MOZ_ASSERT(!gDiskSpaceWatcher);
    30 }
    32 DiskSpaceWatcher::~DiskSpaceWatcher()
    33 {
    34   MOZ_ASSERT(!gDiskSpaceWatcher);
    35 }
    37 already_AddRefed<DiskSpaceWatcher>
    38 DiskSpaceWatcher::FactoryCreate()
    39 {
    40   if (XRE_GetProcessType() != GeckoProcessType_Default) {
    41     return nullptr;
    42   }
    44   MOZ_ASSERT(NS_IsMainThread());
    46   if (!Preferences::GetBool("disk_space_watcher.enabled", false)) {
    47     return nullptr;
    48   }
    50   if (!gDiskSpaceWatcher) {
    51     gDiskSpaceWatcher = new DiskSpaceWatcher();
    52     ClearOnShutdown(&gDiskSpaceWatcher);
    53   }
    55   nsRefPtr<DiskSpaceWatcher> service = gDiskSpaceWatcher.get();
    56   return service.forget();
    57 }
    59 NS_IMETHODIMP
    60 DiskSpaceWatcher::Observe(nsISupports* aSubject, const char* aTopic,
    61                           const char16_t* aData)
    62 {
    63   MOZ_ASSERT(NS_IsMainThread());
    65   if (!strcmp(aTopic, "profile-after-change")) {
    66     nsCOMPtr<nsIObserverService> observerService =
    67       mozilla::services::GetObserverService();
    68     observerService->AddObserver(this, "profile-before-change", false);
    69     mozilla::hal::StartDiskSpaceWatcher();
    70     return NS_OK;
    71   }
    73   if (!strcmp(aTopic, "profile-before-change")) {
    74     nsCOMPtr<nsIObserverService> observerService =
    75       mozilla::services::GetObserverService();
    76     observerService->RemoveObserver(this, "profile-before-change");
    77     mozilla::hal::StopDiskSpaceWatcher();
    78     return NS_OK;
    79   }
    81   MOZ_ASSERT(false, "DiskSpaceWatcher got unexpected topic!");
    82   return NS_ERROR_UNEXPECTED;
    83 }
    85 /* readonly attribute bool isDiskFull; */
    86 NS_IMETHODIMP DiskSpaceWatcher::GetIsDiskFull(bool* aIsDiskFull)
    87 {
    88   *aIsDiskFull = sIsDiskFull;
    89   return NS_OK;
    90 }
    92 // GetFreeSpace is a macro on windows, and that messes up with the c++
    93 // compiler.
    94 #ifdef XP_WIN
    95 #undef GetFreeSpace
    96 #endif
    97 /* readonly attribute long freeSpace; */
    98 NS_IMETHODIMP DiskSpaceWatcher::GetFreeSpace(uint64_t* aFreeSpace)
    99 {
   100   *aFreeSpace = sFreeSpace;
   101   return NS_OK;
   102 }
   104 // static
   105 void DiskSpaceWatcher::UpdateState(bool aIsDiskFull, uint64_t aFreeSpace)
   106 {
   107   MOZ_ASSERT(NS_IsMainThread());
   108   if (!gDiskSpaceWatcher) {
   109     return;
   110   }
   112   nsCOMPtr<nsIObserverService> observerService =
   113     mozilla::services::GetObserverService();
   115   sIsDiskFull = aIsDiskFull;
   116   sFreeSpace = aFreeSpace;
   118   if (!observerService) {
   119     return;
   120   }
   122   const char16_t stateFull[] = { 'f', 'u', 'l', 'l', 0 };
   123   const char16_t stateFree[] = { 'f', 'r', 'e', 'e', 0 };
   125   nsCOMPtr<nsISupports> subject;
   126   CallQueryInterface(gDiskSpaceWatcher.get(), getter_AddRefs(subject));
   127   MOZ_ASSERT(subject);
   128   observerService->NotifyObservers(subject,
   129                                    DISKSPACEWATCHER_OBSERVER_TOPIC,
   130                                    sIsDiskFull ? stateFull : stateFree);
   131   return;
   132 }
   134 NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(DiskSpaceWatcher,
   135                                          DiskSpaceWatcher::FactoryCreate)
   137 NS_DEFINE_NAMED_CID(NS_DISKSPACEWATCHER_CID);
   139 static const mozilla::Module::CIDEntry kDiskSpaceWatcherCIDs[] = {
   140   { &kNS_DISKSPACEWATCHER_CID, false, nullptr, DiskSpaceWatcherConstructor },
   141   { nullptr }
   142 };
   144 static const mozilla::Module::ContractIDEntry kDiskSpaceWatcherContracts[] = {
   145   { "@mozilla.org/toolkit/disk-space-watcher;1", &kNS_DISKSPACEWATCHER_CID },
   146   { nullptr }
   147 };
   149 static const mozilla::Module::CategoryEntry kDiskSpaceWatcherCategories[] = {
   150 #ifdef MOZ_WIDGET_GONK
   151   { "profile-after-change", "Disk Space Watcher Service", DISKSPACEWATCHER_CONTRACTID },
   152 #endif
   153   { nullptr }
   154 };
   156 static const mozilla::Module kDiskSpaceWatcherModule = {
   157   mozilla::Module::kVersion,
   158   kDiskSpaceWatcherCIDs,
   159   kDiskSpaceWatcherContracts,
   160   kDiskSpaceWatcherCategories
   161 };
   163 NSMODULE_DEFN(DiskSpaceWatcherModule) = &kDiskSpaceWatcherModule;

mercurial