Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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 <limits> |
michael@0 | 7 | #include "BatteryManager.h" |
michael@0 | 8 | #include "Constants.h" |
michael@0 | 9 | #include "mozilla/DOMEventTargetHelper.h" |
michael@0 | 10 | #include "mozilla/Hal.h" |
michael@0 | 11 | #include "mozilla/Preferences.h" |
michael@0 | 12 | #include "mozilla/dom/BatteryManagerBinding.h" |
michael@0 | 13 | #include "nsIDOMClassInfo.h" |
michael@0 | 14 | |
michael@0 | 15 | /** |
michael@0 | 16 | * We have to use macros here because our leak analysis tool things we are |
michael@0 | 17 | * leaking strings when we have |static const nsString|. Sad :( |
michael@0 | 18 | */ |
michael@0 | 19 | #define LEVELCHANGE_EVENT_NAME NS_LITERAL_STRING("levelchange") |
michael@0 | 20 | #define CHARGINGCHANGE_EVENT_NAME NS_LITERAL_STRING("chargingchange") |
michael@0 | 21 | #define DISCHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("dischargingtimechange") |
michael@0 | 22 | #define CHARGINGTIMECHANGE_EVENT_NAME NS_LITERAL_STRING("chargingtimechange") |
michael@0 | 23 | |
michael@0 | 24 | namespace mozilla { |
michael@0 | 25 | namespace dom { |
michael@0 | 26 | namespace battery { |
michael@0 | 27 | |
michael@0 | 28 | BatteryManager::BatteryManager(nsPIDOMWindow* aWindow) |
michael@0 | 29 | : DOMEventTargetHelper(aWindow) |
michael@0 | 30 | , mLevel(kDefaultLevel) |
michael@0 | 31 | , mCharging(kDefaultCharging) |
michael@0 | 32 | , mRemainingTime(kDefaultRemainingTime) |
michael@0 | 33 | { |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | void |
michael@0 | 37 | BatteryManager::Init() |
michael@0 | 38 | { |
michael@0 | 39 | hal::RegisterBatteryObserver(this); |
michael@0 | 40 | |
michael@0 | 41 | hal::BatteryInformation batteryInfo; |
michael@0 | 42 | hal::GetCurrentBatteryInformation(&batteryInfo); |
michael@0 | 43 | |
michael@0 | 44 | UpdateFromBatteryInfo(batteryInfo); |
michael@0 | 45 | } |
michael@0 | 46 | |
michael@0 | 47 | void |
michael@0 | 48 | BatteryManager::Shutdown() |
michael@0 | 49 | { |
michael@0 | 50 | hal::UnregisterBatteryObserver(this); |
michael@0 | 51 | } |
michael@0 | 52 | |
michael@0 | 53 | JSObject* |
michael@0 | 54 | BatteryManager::WrapObject(JSContext* aCx) |
michael@0 | 55 | { |
michael@0 | 56 | return BatteryManagerBinding::Wrap(aCx, this); |
michael@0 | 57 | } |
michael@0 | 58 | |
michael@0 | 59 | double |
michael@0 | 60 | BatteryManager::DischargingTime() const |
michael@0 | 61 | { |
michael@0 | 62 | if (mCharging || mRemainingTime == kUnknownRemainingTime) { |
michael@0 | 63 | return std::numeric_limits<double>::infinity(); |
michael@0 | 64 | } |
michael@0 | 65 | |
michael@0 | 66 | return mRemainingTime; |
michael@0 | 67 | } |
michael@0 | 68 | |
michael@0 | 69 | double |
michael@0 | 70 | BatteryManager::ChargingTime() const |
michael@0 | 71 | { |
michael@0 | 72 | if (!mCharging || mRemainingTime == kUnknownRemainingTime) { |
michael@0 | 73 | return std::numeric_limits<double>::infinity(); |
michael@0 | 74 | } |
michael@0 | 75 | |
michael@0 | 76 | return mRemainingTime; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | void |
michael@0 | 80 | BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo) |
michael@0 | 81 | { |
michael@0 | 82 | mLevel = aBatteryInfo.level(); |
michael@0 | 83 | mCharging = aBatteryInfo.charging(); |
michael@0 | 84 | mRemainingTime = aBatteryInfo.remainingTime(); |
michael@0 | 85 | |
michael@0 | 86 | // Add some guards to make sure the values are coherent. |
michael@0 | 87 | if (mLevel == 1.0 && mCharging == true && |
michael@0 | 88 | mRemainingTime != kDefaultRemainingTime) { |
michael@0 | 89 | mRemainingTime = kDefaultRemainingTime; |
michael@0 | 90 | NS_ERROR("Battery API: When charging and level at 1.0, remaining time " |
michael@0 | 91 | "should be 0. Please fix your backend!"); |
michael@0 | 92 | } |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | void |
michael@0 | 96 | BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo) |
michael@0 | 97 | { |
michael@0 | 98 | double previousLevel = mLevel; |
michael@0 | 99 | bool previousCharging = mCharging; |
michael@0 | 100 | double previousRemainingTime = mRemainingTime; |
michael@0 | 101 | |
michael@0 | 102 | UpdateFromBatteryInfo(aBatteryInfo); |
michael@0 | 103 | |
michael@0 | 104 | if (previousCharging != mCharging) { |
michael@0 | 105 | DispatchTrustedEvent(CHARGINGCHANGE_EVENT_NAME); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | if (previousLevel != mLevel) { |
michael@0 | 109 | DispatchTrustedEvent(LEVELCHANGE_EVENT_NAME); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | /* |
michael@0 | 113 | * There are a few situations that could happen here: |
michael@0 | 114 | * 1. Charging state changed: |
michael@0 | 115 | * a. Previous remaining time wasn't unkwonw, we have to fire an event for |
michael@0 | 116 | * the change. |
michael@0 | 117 | * b. New remaining time isn't unkwonw, we have to fire an event for it. |
michael@0 | 118 | * 2. Charging state didn't change but remainingTime did, we have to fire |
michael@0 | 119 | * the event that correspond to the current charging state. |
michael@0 | 120 | */ |
michael@0 | 121 | if (mCharging != previousCharging) { |
michael@0 | 122 | if (previousRemainingTime != kUnknownRemainingTime) { |
michael@0 | 123 | DispatchTrustedEvent(previousCharging ? CHARGINGTIMECHANGE_EVENT_NAME |
michael@0 | 124 | : DISCHARGINGTIMECHANGE_EVENT_NAME); |
michael@0 | 125 | } |
michael@0 | 126 | if (mRemainingTime != kUnknownRemainingTime) { |
michael@0 | 127 | DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME |
michael@0 | 128 | : DISCHARGINGTIMECHANGE_EVENT_NAME); |
michael@0 | 129 | } |
michael@0 | 130 | } else if (previousRemainingTime != mRemainingTime) { |
michael@0 | 131 | DispatchTrustedEvent(mCharging ? CHARGINGTIMECHANGE_EVENT_NAME |
michael@0 | 132 | : DISCHARGINGTIMECHANGE_EVENT_NAME); |
michael@0 | 133 | } |
michael@0 | 134 | } |
michael@0 | 135 | |
michael@0 | 136 | /* static */ bool |
michael@0 | 137 | BatteryManager::HasSupport() |
michael@0 | 138 | { |
michael@0 | 139 | return Preferences::GetBool("dom.battery.enabled", true); |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | } // namespace battery |
michael@0 | 143 | } // namespace dom |
michael@0 | 144 | } // namespace mozilla |