Bug 678694 - (7/7) Interaction between DOM and hal. r=sicking

The battery status is coming from hal and when there is an update, hal informs
the DOM which then dispatch the appropriate event.
This commit is contained in:
Mounir Lamouri 2011-11-02 16:27:06 +01:00
Родитель f2fd1ece99
Коммит 087411f650
5 изменённых файлов: 115 добавлений и 5 удалений

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

@ -102,6 +102,10 @@ Navigator::~Navigator()
if (mPlugins) {
mPlugins->Invalidate();
}
if (mBatteryManager) {
mBatteryManager->Shutdown();
}
}
NS_INTERFACE_MAP_BEGIN(Navigator)
@ -136,6 +140,11 @@ Navigator::SetDocShell(nsIDocShell* aDocShell)
mNotification->Shutdown();
mNotification = nsnull;
}
if (mBatteryManager) {
mBatteryManager->Shutdown();
mBatteryManager = nsnull;
}
}
//*****************************************************************************
@ -513,6 +522,7 @@ Navigator::LoadingNewDocument()
}
if (mBatteryManager) {
mBatteryManager->Shutdown();
mBatteryManager = nsnull;
}
}
@ -740,6 +750,7 @@ Navigator::GetMozBattery(nsIDOMBatteryManager** aBattery)
{
if (!mBatteryManager) {
mBatteryManager = new battery::BatteryManager();
mBatteryManager->Init();
}
NS_ADDREF(*aBattery = mBatteryManager);

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

@ -49,7 +49,6 @@
#include "nsIDOMClientInformation.h"
#include "nsIDOMNavigatorBattery.h"
#include "nsAutoPtr.h"
#include "nsIDOMBatteryManager.h"
class nsPluginArray;
class nsMimeTypeArray;
@ -64,6 +63,10 @@ class nsIDocShell;
namespace mozilla {
namespace dom {
namespace battery {
class BatteryManager;
} // namespace battery
class Navigator : public nsIDOMNavigator,
public nsIDOMClientInformation,
public nsIDOMNavigatorGeolocation,
@ -103,7 +106,7 @@ private:
nsRefPtr<nsPluginArray> mPlugins;
nsRefPtr<nsGeolocation> mGeolocation;
nsRefPtr<nsDesktopNotificationCenter> mNotification;
nsCOMPtr<nsIDOMBatteryManager> mBatteryManager;
nsRefPtr<battery::BatteryManager> mBatteryManager;
nsIDocShell* mDocShell; // weak reference
};

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

@ -35,9 +35,18 @@
*
* ***** END LICENSE BLOCK ***** */
#include "mozilla/Hal.h"
#include "BatteryManager.h"
#include "nsIDOMClassInfo.h"
#include "Constants.h"
#include "nsDOMEvent.h"
/**
* We have to use macros here because our leak analysis tool things we are
* leaking strings when we have |static const nsString|. Sad :(
*/
#define LEVELCHANGE_EVENT_NAME NS_LITERAL_STRING("levelchange")
#define CHARGINGCHANGE_EVENT_NAME NS_LITERAL_STRING("chargingchange")
DOMCI_DATA(BatteryManager, mozilla::dom::battery::BatteryManager)
@ -80,6 +89,25 @@ BatteryManager::~BatteryManager()
}
}
void
BatteryManager::Init()
{
hal::RegisterBatteryObserver(this);
hal::BatteryInformation* batteryInfo = new hal::BatteryInformation();
hal::GetCurrentBatteryInformation(batteryInfo);
UpdateFromBatteryInfo(*batteryInfo);
delete batteryInfo;
}
void
BatteryManager::Shutdown()
{
hal::UnregisterBatteryObserver(this);
}
NS_IMETHODIMP
BatteryManager::GetCharging(bool* aCharging)
{
@ -105,8 +133,8 @@ BatteryManager::GetOnlevelchange(nsIDOMEventListener** aOnlevelchange)
NS_IMETHODIMP
BatteryManager::SetOnlevelchange(nsIDOMEventListener* aOnlevelchange)
{
return RemoveAddEventListener(NS_LITERAL_STRING("levelchange"),
mOnLevelChangeListener, aOnlevelchange);
return RemoveAddEventListener(LEVELCHANGE_EVENT_NAME, mOnLevelChangeListener,
aOnlevelchange);
}
NS_IMETHODIMP
@ -118,10 +146,51 @@ BatteryManager::GetOnchargingchange(nsIDOMEventListener** aOnchargingchange)
NS_IMETHODIMP
BatteryManager::SetOnchargingchange(nsIDOMEventListener* aOnchargingchange)
{
return RemoveAddEventListener(NS_LITERAL_STRING("chargingchange"),
return RemoveAddEventListener(CHARGINGCHANGE_EVENT_NAME,
mOnChargingChangeListener, aOnchargingchange);
}
nsresult
BatteryManager::DispatchTrustedEventToSelf(const nsAString& aEventName)
{
nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nsnull, nsnull);
nsresult rv = event->InitEvent(aEventName, false, false);
NS_ENSURE_SUCCESS(rv, rv);
rv = event->SetTrusted(PR_TRUE);
NS_ENSURE_SUCCESS(rv, rv);
bool dummy;
rv = DispatchEvent(event, &dummy);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
void
BatteryManager::UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo)
{
mLevel = aBatteryInfo.level();
mCharging = aBatteryInfo.charging();
}
void
BatteryManager::Notify(const hal::BatteryInformation& aBatteryInfo)
{
float previousLevel = mLevel;
bool previousCharging = mCharging;
UpdateFromBatteryInfo(aBatteryInfo);
if (previousCharging != mCharging) {
DispatchTrustedEventToSelf(CHARGINGCHANGE_EVENT_NAME);
}
if (previousLevel != mLevel) {
DispatchTrustedEventToSelf(LEVELCHANGE_EVENT_NAME);
}
}
} // namespace battery
} // namespace dom
} // namespace mozilla

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

@ -41,13 +41,21 @@
#include "nsIDOMBatteryManager.h"
#include "nsDOMEventTargetHelper.h"
#include "nsCycleCollectionParticipant.h"
#include "mozilla/Observer.h"
#include "Types.h"
namespace mozilla {
namespace hal {
class BatteryInformation;
} // namespace hal
namespace dom {
namespace battery {
class BatteryManager : public nsIDOMBatteryManager
, public nsDOMEventTargetHelper
, public BatteryObserver
{
public:
NS_DECL_ISUPPORTS
@ -57,10 +65,27 @@ public:
BatteryManager();
virtual ~BatteryManager();
void Init();
void Shutdown();
// For IObserver.
void Notify(const hal::BatteryInformation& aBatteryInfo);
NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BatteryManager,
nsDOMEventTargetHelper)
private:
/**
* Dispatch a trusted non-cancellable and non-bubbling event to itself.
*/
nsresult DispatchTrustedEventToSelf(const nsAString& aEventName);
/**
* Update the battery information stored in the battery manager object using
* a battery information object.
*/
void UpdateFromBatteryInfo(const hal::BatteryInformation& aBatteryInfo);
float mLevel;
bool mCharging;

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

@ -72,4 +72,6 @@ ifdef ENABLE_TESTS
DIRS += test
endif
include $(topsrcdir)/config/config.mk
include $(topsrcdir)/ipc/chromium/chromium-config.mk
include $(topsrcdir)/config/rules.mk