Sat, 03 Jan 2015 20:18:00 +0100
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.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "mozilla/dom/PowerManager.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "mozilla/Hal.h" |
michael@0 | 9 | #include "WakeLock.h" |
michael@0 | 10 | #include "nsDOMClassInfoID.h" |
michael@0 | 11 | #include "nsIDOMWakeLockListener.h" |
michael@0 | 12 | #include "nsIDocument.h" |
michael@0 | 13 | #include "nsIPermissionManager.h" |
michael@0 | 14 | #include "nsIPowerManagerService.h" |
michael@0 | 15 | #include "nsIPrincipal.h" |
michael@0 | 16 | #include "nsPIDOMWindow.h" |
michael@0 | 17 | #include "nsServiceManagerUtils.h" |
michael@0 | 18 | #include "nsError.h" |
michael@0 | 19 | #include "mozilla/dom/MozPowerManagerBinding.h" |
michael@0 | 20 | |
michael@0 | 21 | namespace mozilla { |
michael@0 | 22 | namespace dom { |
michael@0 | 23 | |
michael@0 | 24 | NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PowerManager) |
michael@0 | 25 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
michael@0 | 26 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
michael@0 | 27 | NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener) |
michael@0 | 28 | NS_INTERFACE_MAP_END |
michael@0 | 29 | |
michael@0 | 30 | NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(PowerManager, mListeners, mWindow) |
michael@0 | 31 | |
michael@0 | 32 | NS_IMPL_CYCLE_COLLECTING_ADDREF(PowerManager) |
michael@0 | 33 | NS_IMPL_CYCLE_COLLECTING_RELEASE(PowerManager) |
michael@0 | 34 | |
michael@0 | 35 | /* virtual */ JSObject* |
michael@0 | 36 | PowerManager::WrapObject(JSContext* aCx) |
michael@0 | 37 | { |
michael@0 | 38 | return MozPowerManagerBinding::Wrap(aCx, this); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | nsresult |
michael@0 | 42 | PowerManager::Init(nsIDOMWindow *aWindow) |
michael@0 | 43 | { |
michael@0 | 44 | mWindow = aWindow; |
michael@0 | 45 | |
michael@0 | 46 | nsCOMPtr<nsIPowerManagerService> pmService = |
michael@0 | 47 | do_GetService(POWERMANAGERSERVICE_CONTRACTID); |
michael@0 | 48 | NS_ENSURE_STATE(pmService); |
michael@0 | 49 | |
michael@0 | 50 | // Add ourself to the global notification list. |
michael@0 | 51 | pmService->AddWakeLockListener(this); |
michael@0 | 52 | return NS_OK; |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | nsresult |
michael@0 | 56 | PowerManager::Shutdown() |
michael@0 | 57 | { |
michael@0 | 58 | nsCOMPtr<nsIPowerManagerService> pmService = |
michael@0 | 59 | do_GetService(POWERMANAGERSERVICE_CONTRACTID); |
michael@0 | 60 | NS_ENSURE_STATE(pmService); |
michael@0 | 61 | |
michael@0 | 62 | // Remove ourself from the global notification list. |
michael@0 | 63 | pmService->RemoveWakeLockListener(this); |
michael@0 | 64 | return NS_OK; |
michael@0 | 65 | } |
michael@0 | 66 | |
michael@0 | 67 | void |
michael@0 | 68 | PowerManager::Reboot(ErrorResult& aRv) |
michael@0 | 69 | { |
michael@0 | 70 | nsCOMPtr<nsIPowerManagerService> pmService = |
michael@0 | 71 | do_GetService(POWERMANAGERSERVICE_CONTRACTID); |
michael@0 | 72 | if (pmService) { |
michael@0 | 73 | pmService->Reboot(); |
michael@0 | 74 | } else { |
michael@0 | 75 | aRv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 76 | } |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | void |
michael@0 | 80 | PowerManager::FactoryReset() |
michael@0 | 81 | { |
michael@0 | 82 | hal::FactoryReset(); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | void |
michael@0 | 86 | PowerManager::PowerOff(ErrorResult& aRv) |
michael@0 | 87 | { |
michael@0 | 88 | nsCOMPtr<nsIPowerManagerService> pmService = |
michael@0 | 89 | do_GetService(POWERMANAGERSERVICE_CONTRACTID); |
michael@0 | 90 | if (pmService) { |
michael@0 | 91 | pmService->PowerOff(); |
michael@0 | 92 | } else { |
michael@0 | 93 | aRv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 94 | } |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | void |
michael@0 | 98 | PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener) |
michael@0 | 99 | { |
michael@0 | 100 | if (!mListeners.Contains(aListener)) { |
michael@0 | 101 | mListeners.AppendElement(aListener); |
michael@0 | 102 | } |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void |
michael@0 | 106 | PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener) |
michael@0 | 107 | { |
michael@0 | 108 | mListeners.RemoveElement(aListener); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | void |
michael@0 | 112 | PowerManager::GetWakeLockState(const nsAString& aTopic, |
michael@0 | 113 | nsAString& aState, |
michael@0 | 114 | ErrorResult& aRv) |
michael@0 | 115 | { |
michael@0 | 116 | nsCOMPtr<nsIPowerManagerService> pmService = |
michael@0 | 117 | do_GetService(POWERMANAGERSERVICE_CONTRACTID); |
michael@0 | 118 | if (pmService) { |
michael@0 | 119 | aRv = pmService->GetWakeLockState(aTopic, aState); |
michael@0 | 120 | } else { |
michael@0 | 121 | aRv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | NS_IMETHODIMP |
michael@0 | 126 | PowerManager::Callback(const nsAString &aTopic, const nsAString &aState) |
michael@0 | 127 | { |
michael@0 | 128 | /** |
michael@0 | 129 | * We maintain a local listener list instead of using the global |
michael@0 | 130 | * list so that when the window is destroyed we don't have to |
michael@0 | 131 | * cleanup the mess. |
michael@0 | 132 | * Copy the listeners list before we walk through the callbacks |
michael@0 | 133 | * because the callbacks may install new listeners. We expect no |
michael@0 | 134 | * more than one listener per window, so it shouldn't be too long. |
michael@0 | 135 | */ |
michael@0 | 136 | nsAutoTArray<nsCOMPtr<nsIDOMMozWakeLockListener>, 2> listeners(mListeners); |
michael@0 | 137 | for (uint32_t i = 0; i < listeners.Length(); ++i) { |
michael@0 | 138 | listeners[i]->Callback(aTopic, aState); |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | return NS_OK; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | bool |
michael@0 | 145 | PowerManager::ScreenEnabled() |
michael@0 | 146 | { |
michael@0 | 147 | return hal::GetScreenEnabled(); |
michael@0 | 148 | } |
michael@0 | 149 | |
michael@0 | 150 | void |
michael@0 | 151 | PowerManager::SetScreenEnabled(bool aEnabled) |
michael@0 | 152 | { |
michael@0 | 153 | hal::SetScreenEnabled(aEnabled); |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | double |
michael@0 | 157 | PowerManager::ScreenBrightness() |
michael@0 | 158 | { |
michael@0 | 159 | return hal::GetScreenBrightness(); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | void |
michael@0 | 163 | PowerManager::SetScreenBrightness(double aBrightness, ErrorResult& aRv) |
michael@0 | 164 | { |
michael@0 | 165 | if (0 <= aBrightness && aBrightness <= 1) { |
michael@0 | 166 | hal::SetScreenBrightness(aBrightness); |
michael@0 | 167 | } else { |
michael@0 | 168 | aRv.Throw(NS_ERROR_INVALID_ARG); |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | bool |
michael@0 | 173 | PowerManager::CpuSleepAllowed() |
michael@0 | 174 | { |
michael@0 | 175 | return hal::GetCpuSleepAllowed(); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | void |
michael@0 | 179 | PowerManager::SetCpuSleepAllowed(bool aAllowed) |
michael@0 | 180 | { |
michael@0 | 181 | hal::SetCpuSleepAllowed(aAllowed); |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | bool |
michael@0 | 185 | PowerManager::CheckPermission(nsPIDOMWindow* aWindow) |
michael@0 | 186 | { |
michael@0 | 187 | nsCOMPtr<nsIPermissionManager> permMgr = |
michael@0 | 188 | do_GetService(NS_PERMISSIONMANAGER_CONTRACTID); |
michael@0 | 189 | NS_ENSURE_TRUE(permMgr, false); |
michael@0 | 190 | |
michael@0 | 191 | uint32_t permission = nsIPermissionManager::DENY_ACTION; |
michael@0 | 192 | permMgr->TestPermissionFromWindow(aWindow, "power", &permission); |
michael@0 | 193 | |
michael@0 | 194 | return permission == nsIPermissionManager::ALLOW_ACTION; |
michael@0 | 195 | } |
michael@0 | 196 | |
michael@0 | 197 | already_AddRefed<PowerManager> |
michael@0 | 198 | PowerManager::CreateInstance(nsPIDOMWindow* aWindow) |
michael@0 | 199 | { |
michael@0 | 200 | nsRefPtr<PowerManager> powerManager = new PowerManager(); |
michael@0 | 201 | if (NS_FAILED(powerManager->Init(aWindow))) { |
michael@0 | 202 | powerManager = nullptr; |
michael@0 | 203 | } |
michael@0 | 204 | |
michael@0 | 205 | return powerManager.forget(); |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | } // dom |
michael@0 | 209 | } // mozilla |