Tue, 06 Jan 2015 21:39:09 +0100
Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.
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 "Hal.h" |
michael@0 | 7 | #include "HalImpl.h" |
michael@0 | 8 | #include "nsITimer.h" |
michael@0 | 9 | #include "mozilla/Preferences.h" |
michael@0 | 10 | #include "mozilla/dom/battery/Constants.h" |
michael@0 | 11 | #include "nsComponentManagerUtils.h" |
michael@0 | 12 | |
michael@0 | 13 | #include <windows.h> |
michael@0 | 14 | #include "mozilla/WindowsVersion.h" |
michael@0 | 15 | |
michael@0 | 16 | using namespace mozilla::dom::battery; |
michael@0 | 17 | |
michael@0 | 18 | namespace mozilla { |
michael@0 | 19 | namespace hal_impl { |
michael@0 | 20 | |
michael@0 | 21 | static nsCOMPtr<nsITimer> sUpdateTimer; |
michael@0 | 22 | |
michael@0 | 23 | /* Power Event API is Vista or later */ |
michael@0 | 24 | static decltype(RegisterPowerSettingNotification)* sRegisterPowerSettingNotification = nullptr; |
michael@0 | 25 | static decltype(UnregisterPowerSettingNotification)* sUnregisterPowerSettingNotification = nullptr; |
michael@0 | 26 | static HPOWERNOTIFY sPowerHandle = nullptr; |
michael@0 | 27 | static HPOWERNOTIFY sCapacityHandle = nullptr; |
michael@0 | 28 | static HWND sHWnd = nullptr; |
michael@0 | 29 | |
michael@0 | 30 | static void |
michael@0 | 31 | UpdateHandler(nsITimer* aTimer, void* aClosure) { |
michael@0 | 32 | NS_ASSERTION(!IsVistaOrLater(), |
michael@0 | 33 | "We shouldn't call this function for Vista or later version!"); |
michael@0 | 34 | |
michael@0 | 35 | static hal::BatteryInformation sLastInfo; |
michael@0 | 36 | hal::BatteryInformation currentInfo; |
michael@0 | 37 | |
michael@0 | 38 | hal_impl::GetCurrentBatteryInformation(¤tInfo); |
michael@0 | 39 | if (sLastInfo.level() != currentInfo.level() || |
michael@0 | 40 | sLastInfo.charging() != currentInfo.charging() || |
michael@0 | 41 | sLastInfo.remainingTime() != currentInfo.remainingTime()) { |
michael@0 | 42 | hal::NotifyBatteryChange(currentInfo); |
michael@0 | 43 | sLastInfo = currentInfo; |
michael@0 | 44 | } |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | static |
michael@0 | 48 | LRESULT CALLBACK |
michael@0 | 49 | BatteryWindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { |
michael@0 | 50 | if (msg != WM_POWERBROADCAST || wParam != PBT_POWERSETTINGCHANGE) { |
michael@0 | 51 | return DefWindowProc(hwnd, msg, wParam, lParam); |
michael@0 | 52 | } |
michael@0 | 53 | |
michael@0 | 54 | hal::BatteryInformation currentInfo; |
michael@0 | 55 | |
michael@0 | 56 | // Since we need update remainingTime, we cannot use LPARAM. |
michael@0 | 57 | hal_impl::GetCurrentBatteryInformation(¤tInfo); |
michael@0 | 58 | |
michael@0 | 59 | hal::NotifyBatteryChange(currentInfo); |
michael@0 | 60 | return TRUE; |
michael@0 | 61 | } |
michael@0 | 62 | |
michael@0 | 63 | void |
michael@0 | 64 | EnableBatteryNotifications() |
michael@0 | 65 | { |
michael@0 | 66 | if (IsVistaOrLater()) { |
michael@0 | 67 | // RegisterPowerSettingNotification is from Vista or later. |
michael@0 | 68 | // Use this API if available. |
michael@0 | 69 | HMODULE hUser32 = GetModuleHandleW(L"USER32.DLL"); |
michael@0 | 70 | if (!sRegisterPowerSettingNotification) |
michael@0 | 71 | sRegisterPowerSettingNotification = (decltype(RegisterPowerSettingNotification)*) |
michael@0 | 72 | GetProcAddress(hUser32, "RegisterPowerSettingNotification"); |
michael@0 | 73 | if (!sUnregisterPowerSettingNotification) |
michael@0 | 74 | sUnregisterPowerSettingNotification = (decltype(UnregisterPowerSettingNotification)*) |
michael@0 | 75 | GetProcAddress(hUser32, "UnregisterPowerSettingNotification"); |
michael@0 | 76 | |
michael@0 | 77 | if (!sRegisterPowerSettingNotification || |
michael@0 | 78 | !sUnregisterPowerSettingNotification) { |
michael@0 | 79 | NS_ASSERTION(false, "Canot find PowerSettingNotification functions."); |
michael@0 | 80 | return; |
michael@0 | 81 | } |
michael@0 | 82 | |
michael@0 | 83 | // Create custom window to watch battery event |
michael@0 | 84 | // If we can get Gecko's window handle, this is unnecessary. |
michael@0 | 85 | |
michael@0 | 86 | if (sHWnd == nullptr) { |
michael@0 | 87 | WNDCLASSW wc; |
michael@0 | 88 | HMODULE hSelf = GetModuleHandle(nullptr); |
michael@0 | 89 | |
michael@0 | 90 | if (!GetClassInfoW(hSelf, L"MozillaBatteryClass", &wc)) { |
michael@0 | 91 | ZeroMemory(&wc, sizeof(WNDCLASSW)); |
michael@0 | 92 | wc.hInstance = hSelf; |
michael@0 | 93 | wc.lpfnWndProc = BatteryWindowProc; |
michael@0 | 94 | wc.lpszClassName = L"MozillaBatteryClass"; |
michael@0 | 95 | RegisterClassW(&wc); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | sHWnd = CreateWindowW(L"MozillaBatteryClass", L"Battery Watcher", |
michael@0 | 99 | 0, 0, 0, 0, 0, |
michael@0 | 100 | nullptr, nullptr, hSelf, nullptr); |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | if (sHWnd == nullptr) { |
michael@0 | 104 | return; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | sPowerHandle = |
michael@0 | 108 | sRegisterPowerSettingNotification(sHWnd, |
michael@0 | 109 | &GUID_ACDC_POWER_SOURCE, |
michael@0 | 110 | DEVICE_NOTIFY_WINDOW_HANDLE); |
michael@0 | 111 | sCapacityHandle = |
michael@0 | 112 | sRegisterPowerSettingNotification(sHWnd, |
michael@0 | 113 | &GUID_BATTERY_PERCENTAGE_REMAINING, |
michael@0 | 114 | DEVICE_NOTIFY_WINDOW_HANDLE); |
michael@0 | 115 | } else |
michael@0 | 116 | { |
michael@0 | 117 | // for Windows XP. If we remove Windows XP support, |
michael@0 | 118 | // we should remove timer-based power notification |
michael@0 | 119 | sUpdateTimer = do_CreateInstance(NS_TIMER_CONTRACTID); |
michael@0 | 120 | if (sUpdateTimer) { |
michael@0 | 121 | sUpdateTimer->InitWithFuncCallback(UpdateHandler, |
michael@0 | 122 | nullptr, |
michael@0 | 123 | Preferences::GetInt("dom.battery.timer", |
michael@0 | 124 | 30000 /* 30s */), |
michael@0 | 125 | nsITimer::TYPE_REPEATING_SLACK); |
michael@0 | 126 | } |
michael@0 | 127 | } |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | void |
michael@0 | 131 | DisableBatteryNotifications() |
michael@0 | 132 | { |
michael@0 | 133 | if (IsVistaOrLater()) { |
michael@0 | 134 | if (sPowerHandle) { |
michael@0 | 135 | sUnregisterPowerSettingNotification(sPowerHandle); |
michael@0 | 136 | sPowerHandle = nullptr; |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | if (sCapacityHandle) { |
michael@0 | 140 | sUnregisterPowerSettingNotification(sCapacityHandle); |
michael@0 | 141 | sCapacityHandle = nullptr; |
michael@0 | 142 | } |
michael@0 | 143 | |
michael@0 | 144 | if (sHWnd) { |
michael@0 | 145 | DestroyWindow(sHWnd); |
michael@0 | 146 | sHWnd = nullptr; |
michael@0 | 147 | } |
michael@0 | 148 | } else |
michael@0 | 149 | { |
michael@0 | 150 | if (sUpdateTimer) { |
michael@0 | 151 | sUpdateTimer->Cancel(); |
michael@0 | 152 | sUpdateTimer = nullptr; |
michael@0 | 153 | } |
michael@0 | 154 | } |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | void |
michael@0 | 158 | GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo) |
michael@0 | 159 | { |
michael@0 | 160 | SYSTEM_POWER_STATUS status; |
michael@0 | 161 | if (!GetSystemPowerStatus(&status)) { |
michael@0 | 162 | aBatteryInfo->level() = kDefaultLevel; |
michael@0 | 163 | aBatteryInfo->charging() = kDefaultCharging; |
michael@0 | 164 | aBatteryInfo->remainingTime() = kDefaultRemainingTime; |
michael@0 | 165 | return; |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | aBatteryInfo->level() = |
michael@0 | 169 | status.BatteryLifePercent == 255 ? kDefaultLevel |
michael@0 | 170 | : ((double)status.BatteryLifePercent) / 100.0; |
michael@0 | 171 | aBatteryInfo->charging() = (status.ACLineStatus != 0); |
michael@0 | 172 | if (status.ACLineStatus != 0) { |
michael@0 | 173 | if (aBatteryInfo->level() == 1.0) { |
michael@0 | 174 | // GetSystemPowerStatus API may returns -1 for BatteryFullLifeTime. |
michael@0 | 175 | // So, if battery is 100%, set kDefaultRemainingTime at force. |
michael@0 | 176 | aBatteryInfo->remainingTime() = kDefaultRemainingTime; |
michael@0 | 177 | } else { |
michael@0 | 178 | aBatteryInfo->remainingTime() = |
michael@0 | 179 | status.BatteryFullLifeTime == (DWORD)-1 ? kUnknownRemainingTime |
michael@0 | 180 | : status.BatteryFullLifeTime; |
michael@0 | 181 | } |
michael@0 | 182 | } else { |
michael@0 | 183 | aBatteryInfo->remainingTime() = |
michael@0 | 184 | status.BatteryLifeTime == (DWORD)-1 ? kUnknownRemainingTime |
michael@0 | 185 | : status.BatteryLifeTime; |
michael@0 | 186 | } |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | } // hal_impl |
michael@0 | 190 | } // mozilla |