This commit is contained in:
Mounir Lamouri 2011-11-19 11:37:21 +01:00
Родитель e2e6d5823c
Коммит beaf6dbbef
3 изменённых файлов: 50 добавлений и 78 удалений

Просмотреть файл

@ -749,8 +749,22 @@ NS_IMETHODIMP
Navigator::GetMozBattery(nsIDOMBatteryManager** aBattery) Navigator::GetMozBattery(nsIDOMBatteryManager** aBattery)
{ {
if (!mBatteryManager) { if (!mBatteryManager) {
*aBattery = nsnull;
nsCOMPtr<nsPIDOMWindow> window = do_GetInterface(mDocShell);
NS_ENSURE_TRUE(window, NS_OK);
nsCOMPtr<nsIDocument> 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 = new battery::BatteryManager();
mBatteryManager->Init(); mBatteryManager->Init(window->GetCurrentInnerWindow(), scx);
} }
NS_ADDREF(*aBattery = mBatteryManager); NS_ADDREF(*aBattery = mBatteryManager);

Просмотреть файл

@ -61,26 +61,28 @@ namespace battery {
NS_IMPL_CYCLE_COLLECTION_CLASS(BatteryManager) NS_IMPL_CYCLE_COLLECTION_CLASS(BatteryManager)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BatteryManager, NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(BatteryManager,
nsDOMEventTargetHelper) nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnLevelChangeListener) NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(levelchange)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnChargingChangeListener) NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(chargingchange)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mOnDischargingTimeChangeListener) NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(chargingtimechange)
NS_CYCLE_COLLECTION_TRAVERSE_EVENT_HANDLER(dischargingtimechange)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BatteryManager, NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(BatteryManager,
nsDOMEventTargetHelper) nsDOMEventTargetWrapperCache)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnLevelChangeListener) NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(levelchange)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnChargingChangeListener) NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(chargingchange)
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mOnDischargingTimeChangeListener) NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(chargingtimechange)
NS_CYCLE_COLLECTION_UNLINK_EVENT_HANDLER(dischargingtimechange)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BatteryManager) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(BatteryManager)
NS_INTERFACE_MAP_ENTRY(nsIDOMBatteryManager) NS_INTERFACE_MAP_ENTRY(nsIDOMBatteryManager)
NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(BatteryManager) 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_ADDREF_INHERITED(BatteryManager, nsDOMEventTargetWrapperCache)
NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetHelper) NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetWrapperCache)
BatteryManager::BatteryManager() BatteryManager::BatteryManager()
: mLevel(kDefaultLevel) : mLevel(kDefaultLevel)
@ -97,8 +99,12 @@ BatteryManager::~BatteryManager()
} }
void void
BatteryManager::Init() BatteryManager::Init(nsPIDOMWindow *aWindow, nsIScriptContext* aScriptContext)
{ {
// Those vars come from nsDOMEventTargetHelper.
mOwner = aWindow;
mScriptContext = aScriptContext;
hal::RegisterBatteryObserver(this); hal::RegisterBatteryObserver(this);
hal::BatteryInformation* batteryInfo = new hal::BatteryInformation(); hal::BatteryInformation* batteryInfo = new hal::BatteryInformation();
@ -157,61 +163,10 @@ BatteryManager::GetChargingTime(double* aChargingTime)
return NS_OK; return NS_OK;
} }
NS_IMETHODIMP NS_IMPL_EVENT_HANDLER(BatteryManager, levelchange)
BatteryManager::GetOnlevelchange(nsIDOMEventListener** aOnlevelchange) NS_IMPL_EVENT_HANDLER(BatteryManager, chargingchange)
{ NS_IMPL_EVENT_HANDLER(BatteryManager, chargingtimechange)
return GetInnerEventListener(mOnLevelChangeListener, aOnlevelchange); NS_IMPL_EVENT_HANDLER(BatteryManager, dischargingtimechange)
}
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);
}
nsresult nsresult
BatteryManager::DispatchTrustedEventToSelf(const nsAString& aEventName) BatteryManager::DispatchTrustedEventToSelf(const nsAString& aEventName)

Просмотреть файл

@ -39,11 +39,14 @@
#define mozilla_dom_battery_BatteryManager_h #define mozilla_dom_battery_BatteryManager_h
#include "nsIDOMBatteryManager.h" #include "nsIDOMBatteryManager.h"
#include "nsDOMEventTargetHelper.h" #include "nsDOMEventTargetWrapperCache.h"
#include "nsCycleCollectionParticipant.h" #include "nsCycleCollectionParticipant.h"
#include "mozilla/Observer.h" #include "mozilla/Observer.h"
#include "Types.h" #include "Types.h"
class nsPIDOMWindow;
class nsIScriptContext;
namespace mozilla { namespace mozilla {
namespace hal { namespace hal {
@ -53,26 +56,26 @@ class BatteryInformation;
namespace dom { namespace dom {
namespace battery { namespace battery {
class BatteryManager : public nsIDOMBatteryManager class BatteryManager : public nsDOMEventTargetWrapperCache
, public nsDOMEventTargetHelper , public nsIDOMBatteryManager
, public BatteryObserver , public BatteryObserver
{ {
public: public:
NS_DECL_ISUPPORTS NS_DECL_ISUPPORTS
NS_DECL_NSIDOMBATTERYMANAGER NS_DECL_NSIDOMBATTERYMANAGER
NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::) NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetWrapperCache::)
BatteryManager(); BatteryManager();
virtual ~BatteryManager(); virtual ~BatteryManager();
void Init(); void Init(nsPIDOMWindow *aWindow, nsIScriptContext* aScriptContext);
void Shutdown(); void Shutdown();
// For IObserver. // For IObserver.
void Notify(const hal::BatteryInformation& aBatteryInfo); void Notify(const hal::BatteryInformation& aBatteryInfo);
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BatteryManager, NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BatteryManager,
nsDOMEventTargetHelper) nsDOMEventTargetWrapperCache)
/** /**
* Returns whether the battery api is supported (ie. not disabled by the user) * Returns whether the battery api is supported (ie. not disabled by the user)
@ -101,10 +104,10 @@ private:
*/ */
double mRemainingTime; double mRemainingTime;
nsRefPtr<nsDOMEventListenerWrapper> mOnLevelChangeListener; NS_DECL_EVENT_HANDLER(levelchange);
nsRefPtr<nsDOMEventListenerWrapper> mOnChargingChangeListener; NS_DECL_EVENT_HANDLER(chargingchange);
nsRefPtr<nsDOMEventListenerWrapper> mOnDischargingTimeChangeListener; NS_DECL_EVENT_HANDLER(chargingtimechange);
nsRefPtr<nsDOMEventListenerWrapper> mOnChargingTimeChangeListener; NS_DECL_EVENT_HANDLER(dischargingtimechange);
}; };
} // namespace battery } // namespace battery