Bug 678694 - (6/7) hal code for the Battery API. r=cjones

This commit is contained in:
Mounir Lamouri 2011-11-02 16:14:01 +01:00
parent b81187bcec
commit a1fdb4f396
8 changed files with 281 additions and 2 deletions

View File

@ -52,6 +52,7 @@ EXPORTS_NAMESPACES = mozilla/dom/battery
EXPORTS_mozilla/dom/battery = \
Constants.h \
Types.h \
$(NULL)
CPPSRCS = \

54
dom/battery/Types.h Normal file
View File

@ -0,0 +1,54 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Mounir Lamouri <mounir.lamouri@mozilla.com> (Original Author)
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef mozilla_dom_battery_Types_h
#define mozilla_dom_battery_Types_h
namespace mozilla {
namespace hal {
class BatteryInformation;
} // namespace hal
template <class T>
class Observer;
typedef Observer<hal::BatteryInformation> BatteryObserver;
} // namespace mozilla
#endif // mozilla_dom_battery_Types_h

View File

@ -41,6 +41,7 @@
#include "mozilla/Util.h"
#include "nsThreadUtils.h"
#include "nsXULAppAPI.h"
#include "mozilla/Observer.h"
#define PROXY_IF_SANDBOXED(_call) \
do { \
@ -73,5 +74,99 @@ Vibrate(const nsTArray<uint32>& pattern)
PROXY_IF_SANDBOXED(Vibrate(pattern));
}
class BatteryObserversManager
{
public:
void AddObserver(BatteryObserver* aObserver) {
if (!mObservers) {
mObservers = new ObserverList<BatteryInformation>();
}
mObservers->AddObserver(aObserver);
if (mObservers->Length() == 1) {
PROXY_IF_SANDBOXED(EnableBatteryNotifications());
}
}
void RemoveObserver(BatteryObserver* aObserver) {
mObservers->RemoveObserver(aObserver);
if (mObservers->Length() == 0) {
PROXY_IF_SANDBOXED(DisableBatteryNotifications());
delete mObservers;
mObservers = 0;
delete mBatteryInfo;
mBatteryInfo = 0;
}
}
void CacheBatteryInformation(const BatteryInformation& aBatteryInfo) {
if (mBatteryInfo) {
delete mBatteryInfo;
}
mBatteryInfo = new BatteryInformation(aBatteryInfo);
}
bool HasCachedBatteryInformation() const {
return mBatteryInfo;
}
void GetCachedBatteryInformation(BatteryInformation* aBatteryInfo) const {
*aBatteryInfo = *mBatteryInfo;
}
void Broadcast(const BatteryInformation& aBatteryInfo) {
MOZ_ASSERT(mObservers);
mObservers->Broadcast(aBatteryInfo);
}
private:
ObserverList<BatteryInformation>* mObservers;
BatteryInformation* mBatteryInfo;
};
static BatteryObserversManager sBatteryObservers;
void
RegisterBatteryObserver(BatteryObserver* aBatteryObserver)
{
AssertMainThread();
sBatteryObservers.AddObserver(aBatteryObserver);
}
void
UnregisterBatteryObserver(BatteryObserver* aBatteryObserver)
{
AssertMainThread();
sBatteryObservers.RemoveObserver(aBatteryObserver);
}
// EnableBatteryNotifications isn't defined on purpose.
// DisableBatteryNotifications isn't defined on purpose.
void
GetCurrentBatteryInformation(BatteryInformation* aBatteryInfo)
{
AssertMainThread();
if (sBatteryObservers.HasCachedBatteryInformation()) {
sBatteryObservers.GetCachedBatteryInformation(aBatteryInfo);
} else {
PROXY_IF_SANDBOXED(GetCurrentBatteryInformation(aBatteryInfo));
sBatteryObservers.CacheBatteryInformation(*aBatteryInfo);
}
}
void NotifyBatteryChange(const BatteryInformation& aBatteryInfo)
{
AssertMainThread();
sBatteryObservers.CacheBatteryInformation(aBatteryInfo);
sBatteryObservers.Broadcast(aBatteryInfo);
}
} // namespace hal
} // namespace mozilla

View File

@ -43,6 +43,7 @@
#include "base/basictypes.h"
#include "mozilla/Types.h"
#include "nsTArray.h"
#include "mozilla/dom/battery/Types.h"
#ifndef MOZ_HAL_NAMESPACE
// This goop plays some cpp tricks to ensure a uniform API across the
@ -63,6 +64,11 @@
#endif
namespace mozilla {
namespace hal {
class BatteryInformation;
} // namespace hal
namespace MOZ_HAL_NAMESPACE /*hal*/ {
/**
@ -75,6 +81,47 @@ namespace MOZ_HAL_NAMESPACE /*hal*/ {
*/
void Vibrate(const nsTArray<uint32>& pattern);
/**
* Inform the battery backend there is a new battery observer.
* @param aBatteryObserver The observer that should be added.
*/
void RegisterBatteryObserver(BatteryObserver* aBatteryObserver);
/**
* Inform the battery backend a battery observer unregistered.
* @param aBatteryObserver The observer that should be removed.
*/
void UnregisterBatteryObserver(BatteryObserver* aBatteryObserver);
/**
* Enables battery notifications from the backend.
*
* This method is semi-private in the sense of it is visible in the hal
* namespace but should not be used. Calls to this method from the hal
* namespace will produce a link error because it is not defined.
*/
void EnableBatteryNotifications();
/**
* Disables battery notifications from the backend.
*
* This method is semi-private in the sense of it is visible in the hal
* namespace but should not be used. Calls to this method from the hal
* namespace will produce a link error because it is not defined.
*/
void DisableBatteryNotifications();
/**
* Returns the current battery information.
*/
void GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo);
/**
* Notify of a change in the battery state.
* @param aBatteryInfo The new battery information.
*/
void NotifyBatteryChange(const hal::BatteryInformation& aBatteryInfo);
}
}

View File

@ -44,4 +44,5 @@
#undef mozilla_Hal_h
#define MOZ_HAL_NAMESPACE hal_sandbox
#include "Hal.h"
#include "mozilla/hal_sandbox/PHal.h"
#undef MOZ_HAL_NAMESPACE

View File

@ -38,6 +38,7 @@
* ***** END LICENSE BLOCK ***** */
#include "Hal.h"
#include "mozilla/dom/battery/Constants.h"
namespace mozilla {
namespace hal_impl {
@ -46,5 +47,20 @@ void
Vibrate(const nsTArray<uint32>& pattern)
{}
void
EnableBatteryNotifications()
{}
void
DisableBatteryNotifications()
{}
void
GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo)
{
aBatteryInfo->level() = dom::battery::kDefaultLevel;
aBatteryInfo->charging() = dom::battery::kDefaultCharging;
}
} // hal_impl
} // namespace mozilla

View File

@ -40,14 +40,30 @@
include protocol PContent;
namespace mozilla {
namespace hal {
struct BatteryInformation {
float level;
bool charging;
};
}
namespace hal_sandbox {
protocol PHal {
sync protocol PHal {
manager PContent;
child:
NotifyBatteryChange(BatteryInformation aBatteryInfo);
parent:
Vibrate(uint32[] pattern);
EnableBatteryNotifications();
DisableBatteryNotifications();
sync GetCurrentBatteryInformation()
returns (BatteryInformation aBatteryInfo);
__delete__();
};

View File

@ -41,6 +41,9 @@
#include "mozilla/dom/ContentChild.h"
#include "mozilla/hal_sandbox/PHalChild.h"
#include "mozilla/hal_sandbox/PHalParent.h"
#include "mozilla/dom/battery/Types.h"
#include "mozilla/Observer.h"
#include "mozilla/unused.h"
using namespace mozilla;
using namespace mozilla::dom;
@ -66,7 +69,26 @@ Vibrate(const nsTArray<uint32>& pattern)
Hal()->SendVibrate(p);
}
class HalParent : public PHalParent {
void
EnableBatteryNotifications()
{
Hal()->SendEnableBatteryNotifications();
}
void
DisableBatteryNotifications()
{
Hal()->SendDisableBatteryNotifications();
}
void
GetCurrentBatteryInformation(BatteryInformation* aBatteryInfo)
{
Hal()->SendGetCurrentBatteryInformation(aBatteryInfo);
}
class HalParent : public PHalParent
, public BatteryObserver {
public:
NS_OVERRIDE virtual bool
RecvVibrate(const InfallibleTArray<unsigned int>& pattern) {
@ -76,10 +98,37 @@ public:
hal::Vibrate(pattern);
return true;
}
NS_OVERRIDE virtual bool
RecvEnableBatteryNotifications() {
hal::RegisterBatteryObserver(this);
return true;
}
NS_OVERRIDE virtual bool
RecvDisableBatteryNotifications() {
hal::UnregisterBatteryObserver(this);
return true;
}
NS_OVERRIDE virtual bool
RecvGetCurrentBatteryInformation(BatteryInformation* aBatteryInfo) {
hal::GetCurrentBatteryInformation(aBatteryInfo);
return true;
}
void Notify(const BatteryInformation& aBatteryInfo) {
unused << SendNotifyBatteryChange(aBatteryInfo);
}
};
class HalChild : public PHalChild {
public:
NS_OVERRIDE virtual bool
RecvNotifyBatteryChange(const BatteryInformation& aBatteryInfo) {
hal::NotifyBatteryChange(aBatteryInfo);
return true;
}
};
PHalChild* CreateHalChild() {