diff --git a/dom/base/Navigator.cpp b/dom/base/Navigator.cpp index 22030558035..a71dc99abdd 100644 --- a/dom/base/Navigator.cpp +++ b/dom/base/Navigator.cpp @@ -749,8 +749,22 @@ NS_IMETHODIMP Navigator::GetMozBattery(nsIDOMBatteryManager** aBattery) { if (!mBatteryManager) { + *aBattery = nsnull; + + nsCOMPtr window = do_GetInterface(mDocShell); + NS_ENSURE_TRUE(window, NS_OK); + + nsCOMPtr document = do_GetInterface(mDocShell); + NS_ENSURE_TRUE(document, NS_OK); + + nsIScriptGlobalObject* sgo = document->GetScopeObject(); + NS_ENSURE_TRUE(sgo, NS_OK); + + nsIScriptContext* scx = sgo->GetContext(); + NS_ENSURE_TRUE(scx, NS_OK); + mBatteryManager = new battery::BatteryManager(); - mBatteryManager->Init(); + mBatteryManager->Init(window->GetCurrentInnerWindow(), scx); } NS_ADDREF(*aBattery = mBatteryManager); diff --git a/dom/battery/BatteryManager.cpp b/dom/battery/BatteryManager.cpp index b5fdfee9f07..2e7fb298c61 100644 --- a/dom/battery/BatteryManager.cpp +++ b/dom/battery/BatteryManager.cpp @@ -61,26 +61,28 @@ namespace battery { NS_IMPL_CYCLE_COLLECTION_CLASS(BatteryManager) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BatteryManager, - nsDOMEventTargetHelper) - NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnLevelChangeListener) - NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnChargingChangeListener) - NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnDischargingTimeChangeListener) + nsDOMEventTargetWrapperCache) + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(levelchange) + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(chargingchange) + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(chargingtimechange) + NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(dischargingtimechange) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BatteryManager, - nsDOMEventTargetHelper) - NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnLevelChangeListener) - NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnChargingChangeListener) - NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnDischargingTimeChangeListener) + nsDOMEventTargetWrapperCache) + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(levelchange) + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(chargingchange) + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(chargingtimechange) + NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(dischargingtimechange) NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BatteryManager) NS_INTERFACE_MAP_ENTRY(nsIDOMBatteryManager) NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BatteryManager) -NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper) +NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetWrapperCache) -NS_IMPL_ADDREF_INHERITED(BatteryManager, nsDOMEventTargetHelper) -NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetHelper) +NS_IMPL_ADDREF_INHERITED(BatteryManager, nsDOMEventTargetWrapperCache) +NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetWrapperCache) BatteryManager::BatteryManager() : mLevel(kDefaultLevel) @@ -97,8 +99,12 @@ BatteryManager::~BatteryManager() } void -BatteryManager::Init() +BatteryManager::Init(nsPIDOMWindow *aWindow, nsIScriptContext* aScriptContext) { + // Those vars come from nsDOMEventTargetHelper. + mOwner = aWindow; + mScriptContext = aScriptContext; + hal::RegisterBatteryObserver(this); hal::BatteryInformation* batteryInfo = new hal::BatteryInformation(); @@ -157,61 +163,10 @@ BatteryManager::GetChargingTime(double* aChargingTime) return NS_OK; } -NS_IMETHODIMP -BatteryManager::GetOnlevelchange(nsIDOMEventListener** aOnlevelchange) -{ - return GetInnerEventListener(mOnLevelChangeListener, aOnlevelchange); -} - -NS_IMETHODIMP -BatteryManager::SetOnlevelchange(nsIDOMEventListener* aOnlevelchange) -{ - return RemoveAddEventListener(LEVELCHANGE_EVENT_NAME, mOnLevelChangeListener, - aOnlevelchange); -} - -NS_IMETHODIMP -BatteryManager::GetOnchargingchange(nsIDOMEventListener** aOnchargingchange) -{ - return GetInnerEventListener(mOnChargingChangeListener, aOnchargingchange); -} - -NS_IMETHODIMP -BatteryManager::SetOnchargingchange(nsIDOMEventListener* aOnchargingchange) -{ - return RemoveAddEventListener(CHARGINGCHANGE_EVENT_NAME, - mOnChargingChangeListener, aOnchargingchange); -} - -NS_IMETHODIMP -BatteryManager::GetOndischargingtimechange(nsIDOMEventListener** aOndischargingtimechange) -{ - return GetInnerEventListener(mOnDischargingTimeChangeListener, - aOndischargingtimechange); -} - -NS_IMETHODIMP -BatteryManager::SetOndischargingtimechange(nsIDOMEventListener* aOndischargingtimechange) -{ - return RemoveAddEventListener(DISCHARGINGTIMECHANGE_EVENT_NAME, - mOnDischargingTimeChangeListener, - aOndischargingtimechange); -} - -NS_IMETHODIMP -BatteryManager::GetOnchargingtimechange(nsIDOMEventListener** aOnchargingtimechange) -{ - return GetInnerEventListener(mOnChargingTimeChangeListener, - aOnchargingtimechange); -} - -NS_IMETHODIMP -BatteryManager::SetOnchargingtimechange(nsIDOMEventListener* aOnchargingtimechange) -{ - return RemoveAddEventListener(CHARGINGTIMECHANGE_EVENT_NAME, - mOnChargingTimeChangeListener, - aOnchargingtimechange); -} +NS_IMPL_EVENT_HANDLER(BatteryManager, levelchange) +NS_IMPL_EVENT_HANDLER(BatteryManager, chargingchange) +NS_IMPL_EVENT_HANDLER(BatteryManager, chargingtimechange) +NS_IMPL_EVENT_HANDLER(BatteryManager, dischargingtimechange) nsresult BatteryManager::DispatchTrustedEventToSelf(const nsAString& aEventName) diff --git a/dom/battery/BatteryManager.h b/dom/battery/BatteryManager.h index e4a4072df2f..2b34ca22511 100644 --- a/dom/battery/BatteryManager.h +++ b/dom/battery/BatteryManager.h @@ -39,11 +39,14 @@ #define mozilla_dom_battery_BatteryManager_h #include "nsIDOMBatteryManager.h" -#include "nsDOMEventTargetHelper.h" +#include "nsDOMEventTargetWrapperCache.h" #include "nsCycleCollectionParticipant.h" #include "mozilla/Observer.h" #include "Types.h" +class nsPIDOMWindow; +class nsIScriptContext; + namespace mozilla { namespace hal { @@ -53,26 +56,26 @@ class BatteryInformation; namespace dom { namespace battery { -class BatteryManager : public nsIDOMBatteryManager - , public nsDOMEventTargetHelper +class BatteryManager : public nsDOMEventTargetWrapperCache + , public nsIDOMBatteryManager , public BatteryObserver { public: NS_DECL_ISUPPORTS NS_DECL_NSIDOMBATTERYMANAGER - NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::) + NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetWrapperCache::) BatteryManager(); virtual ~BatteryManager(); - void Init(); + void Init(nsPIDOMWindow *aWindow, nsIScriptContext* aScriptContext); void Shutdown(); // For IObserver. void Notify(const hal::BatteryInformation& aBatteryInfo); NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BatteryManager, - nsDOMEventTargetHelper) + nsDOMEventTargetWrapperCache) /** * Returns whether the battery api is supported (ie. not disabled by the user) @@ -101,10 +104,10 @@ private: */ double mRemainingTime; - nsRefPtr mOnLevelChangeListener; - nsRefPtr mOnChargingChangeListener; - nsRefPtr mOnDischargingTimeChangeListener; - nsRefPtr mOnChargingTimeChangeListener; + NS_DECL_EVENT_HANDLER(levelchange); + NS_DECL_EVENT_HANDLER(chargingchange); + NS_DECL_EVENT_HANDLER(chargingtimechange); + NS_DECL_EVENT_HANDLER(dischargingtimechange); }; } // namespace battery