Bug 678694 - (4/7) Dummy BatteryManager DOM code. r=sicking
authorMounir Lamouri <mounir.lamouri@gmail.com>
Wed, 02 Nov 2011 16:01:32 +0100
changeset 79672 5d24f6d3df66b2cfb8618f403aaeb72f35c8550f
parent 79671 09da9af7e93485547a0587a470f70168f9a56e7c
child 79673 718e0171d5d2cd0f76bba9f72bac754d1327ae0e
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerssicking
bugs678694
milestone10.0a1
Bug 678694 - (4/7) Dummy BatteryManager DOM code. r=sicking Returns default values only and doesn't send events.
dom/base/Navigator.cpp
dom/base/Navigator.h
dom/base/nsDOMClassInfo.cpp
dom/base/nsDOMClassInfoClasses.h
dom/battery/BatteryManager.cpp
dom/battery/BatteryManager.h
dom/battery/Constants.h
dom/battery/Makefile.in
dom/battery/test/test_battery_basics.html
--- a/dom/base/Navigator.cpp
+++ b/dom/base/Navigator.cpp
@@ -63,16 +63,17 @@
 #include "nsICookiePermission.h"
 #include "nsIScriptSecurityManager.h"
 #include "nsIJSContextStack.h"
 #include "nsCharSeparatedTokenizer.h"
 #include "nsContentUtils.h"
 #include "nsUnicharUtils.h"
 #include "mozilla/Preferences.h"
 #include "mozilla/Telemetry.h"
+#include "BatteryManager.h"
 
 // This should not be in the namespace.
 DOMCI_DATA(Navigator, mozilla::dom::Navigator)
 
 namespace mozilla {
 namespace dom {
 
 static const char sJSStackContractID[] = "@mozilla.org/js/xpc/ContextStack;1";
@@ -506,16 +507,19 @@ Navigator::LoadingNewDocument()
     mGeolocation = nsnull;
   }
 
   if (mNotification) {
     mNotification->Shutdown();
     mNotification = nsnull;
   }
 
+  if (mBatteryManager) {
+    mBatteryManager = nsnull;
+  }
 }
 
 nsresult
 Navigator::RefreshMIMEArray()
 {
   if (mMimeTypes) {
     return mMimeTypes->Refresh();
   }
@@ -729,17 +733,21 @@ NS_IMETHODIMP Navigator::GetMozNotificat
 
 //*****************************************************************************
 //    Navigator::nsIDOMNavigatorBattery
 //*****************************************************************************
 
 NS_IMETHODIMP
 Navigator::GetMozBattery(nsIDOMBatteryManager** aBattery)
 {
-  *aBattery = nsnull;
+  if (!mBatteryManager) {
+    mBatteryManager = new battery::BatteryManager();
+  }
+
+  NS_ADDREF(*aBattery = mBatteryManager);
 
   return NS_OK;
 }
 
 PRInt64
 Navigator::SizeOf() const
 {
   PRInt64 size = sizeof(*this);
--- a/dom/base/Navigator.h
+++ b/dom/base/Navigator.h
@@ -44,16 +44,17 @@
 #define mozilla_dom_Navigator_h
 
 #include "nsIDOMNavigator.h"
 #include "nsIDOMNavigatorGeolocation.h"
 #include "nsIDOMNavigatorDesktopNotification.h"
 #include "nsIDOMClientInformation.h"
 #include "nsIDOMNavigatorBattery.h"
 #include "nsAutoPtr.h"
+#include "nsIDOMBatteryManager.h"
 
 class nsPluginArray;
 class nsMimeTypeArray;
 class nsGeolocation;
 class nsDesktopNotificationCenter;
 class nsIDocShell;
 
 //*****************************************************************************
@@ -97,16 +98,17 @@ public:
 
 private:
   static bool sDoNotTrackEnabled;
 
   nsRefPtr<nsMimeTypeArray> mMimeTypes;
   nsRefPtr<nsPluginArray> mPlugins;
   nsRefPtr<nsGeolocation> mGeolocation;
   nsRefPtr<nsDesktopNotificationCenter> mNotification;
+  nsCOMPtr<nsIDOMBatteryManager> mBatteryManager;
   nsIDocShell* mDocShell; // weak reference
 };
 
 } // namespace dom
 } // namespace mozilla
 
 nsresult NS_GetNavigatorUserAgent(nsAString& aUserAgent);
 nsresult NS_GetNavigatorPlatform(nsAString& aPlatform);
--- a/dom/base/nsDOMClassInfo.cpp
+++ b/dom/base/nsDOMClassInfo.cpp
@@ -506,16 +506,18 @@
 #include "nsIDOMMediaQueryList.h"
 
 #include "nsDOMTouchEvent.h"
 #include "nsIDOMCustomEvent.h"
 
 #include "nsWrapperCacheInlines.h"
 #include "dombindings.h"
 
+#include "nsIDOMBatteryManager.h"
+
 using namespace mozilla;
 using namespace mozilla::dom;
 
 static NS_DEFINE_CID(kDOMSOF_CID, NS_DOM_SCRIPT_OBJECT_FACTORY_CID);
 
 static const char kDOMStringBundleURL[] =
   "chrome://global/locale/dom/dom.properties";
 
@@ -1380,17 +1382,20 @@ static nsDOMClassInfoData sClassInfoData
   NS_DEFINE_CLASSINFO_DATA(GeoPositionCoords, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
 
   NS_DEFINE_CLASSINFO_DATA(GeoPositionAddress, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
 
   NS_DEFINE_CLASSINFO_DATA(GeoPositionError, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
-  
+
+  NS_DEFINE_CLASSINFO_DATA(BatteryManager, nsDOMGenericSH,
+                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
+
   NS_DEFINE_CLASSINFO_DATA(CSSFontFaceRule, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
   NS_DEFINE_CLASSINFO_DATA(CSSFontFaceStyleDecl, nsCSSStyleDeclSH,
                            ARRAY_SCRIPTABLE_FLAGS)
 
 #if defined(MOZ_MEDIA)
   NS_DEFINE_CLASSINFO_DATA(HTMLVideoElement, nsElementSH,
                            ELEMENT_SCRIPTABLE_FLAGS)
@@ -3860,16 +3865,21 @@ nsDOMClassInfo::Init()
   DOM_CLASSINFO_MAP_BEGIN(GeoPositionAddress, nsIDOMGeoPositionAddress)
      DOM_CLASSINFO_MAP_ENTRY(nsIDOMGeoPositionAddress)
   DOM_CLASSINFO_MAP_END
 
   DOM_CLASSINFO_MAP_BEGIN(GeoPositionError, nsIDOMGeoPositionError)
      DOM_CLASSINFO_MAP_ENTRY(nsIDOMGeoPositionError)
   DOM_CLASSINFO_MAP_END
 
+  DOM_CLASSINFO_MAP_BEGIN(BatteryManager, nsIDOMBatteryManager)
+     DOM_CLASSINFO_MAP_ENTRY(nsIDOMBatteryManager)
+     DOM_CLASSINFO_MAP_ENTRY(nsIDOMEventTarget)
+  DOM_CLASSINFO_MAP_END
+
   DOM_CLASSINFO_MAP_BEGIN(CSSFontFaceRule, nsIDOMCSSFontFaceRule)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSFontFaceRule)
   DOM_CLASSINFO_MAP_END
 
   DOM_CLASSINFO_MAP_BEGIN_NO_CLASS_IF(CSSFontFaceStyleDecl,
                                       nsIDOMCSSStyleDeclaration)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSStyleDeclaration)
   DOM_CLASSINFO_MAP_END
--- a/dom/base/nsDOMClassInfoClasses.h
+++ b/dom/base/nsDOMClassInfoClasses.h
@@ -423,16 +423,18 @@ DOMCI_CLASS(MessageEvent)
 
 // Geolocation
 DOMCI_CLASS(GeoGeolocation)
 DOMCI_CLASS(GeoPosition)
 DOMCI_CLASS(GeoPositionCoords)
 DOMCI_CLASS(GeoPositionAddress)
 DOMCI_CLASS(GeoPositionError)
 
+DOMCI_CLASS(BatteryManager)
+
 // @font-face in CSS
 DOMCI_CLASS(CSSFontFaceRule)
 DOMCI_CLASS(CSSFontFaceStyleDecl)
 
 #if defined(MOZ_MEDIA)
 // WhatWG Video Element
 DOMCI_CLASS(HTMLVideoElement)
 DOMCI_CLASS(HTMLSourceElement)
new file mode 100644
--- /dev/null
+++ b/dom/battery/BatteryManager.cpp
@@ -0,0 +1,127 @@
+/* -*- 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 ***** */
+
+#include "BatteryManager.h"
+#include "nsIDOMClassInfo.h"
+#include "Constants.h"
+
+DOMCI_DATA(BatteryManager, mozilla::dom::battery::BatteryManager)
+
+namespace mozilla {
+namespace dom {
+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_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_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_IMPL_ADDREF_INHERITED(BatteryManager, nsDOMEventTargetHelper)
+NS_IMPL_RELEASE_INHERITED(BatteryManager, nsDOMEventTargetHelper)
+
+BatteryManager::BatteryManager()
+  : mLevel(kDefaultLevel)
+  , mCharging(kDefaultCharging)
+{
+}
+
+BatteryManager::~BatteryManager()
+{
+  if (mListenerManager) {
+    mListenerManager->Disconnect();
+  }
+}
+
+NS_IMETHODIMP
+BatteryManager::GetCharging(bool* aCharging)
+{
+  *aCharging = mCharging;
+
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+BatteryManager::GetLevel(float* aLevel)
+{
+  *aLevel = mLevel;
+
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+BatteryManager::GetOnlevelchange(nsIDOMEventListener** aOnlevelchange)
+{
+  return GetInnerEventListener(mOnLevelChangeListener, aOnlevelchange);
+}
+
+NS_IMETHODIMP
+BatteryManager::SetOnlevelchange(nsIDOMEventListener* aOnlevelchange)
+{
+  return RemoveAddEventListener(NS_LITERAL_STRING("levelchange"),
+                                mOnLevelChangeListener, aOnlevelchange);
+}
+
+NS_IMETHODIMP
+BatteryManager::GetOnchargingchange(nsIDOMEventListener** aOnchargingchange)
+{
+  return GetInnerEventListener(mOnChargingChangeListener, aOnchargingchange);
+}
+
+NS_IMETHODIMP
+BatteryManager::SetOnchargingchange(nsIDOMEventListener* aOnchargingchange)
+{
+  return RemoveAddEventListener(NS_LITERAL_STRING("chargingchange"),
+                                mOnChargingChangeListener, aOnchargingchange);
+}
+
+} // namespace battery
+} // namespace dom
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/battery/BatteryManager.h
@@ -0,0 +1,75 @@
+/* -*- 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_BatteryManager_h
+#define mozilla_dom_battery_BatteryManager_h
+
+#include "nsIDOMBatteryManager.h"
+#include "nsDOMEventTargetHelper.h"
+#include "nsCycleCollectionParticipant.h"
+
+namespace mozilla {
+namespace dom {
+namespace battery {
+
+class BatteryManager : public nsIDOMBatteryManager
+                     , public nsDOMEventTargetHelper
+{
+public:
+  NS_DECL_ISUPPORTS
+  NS_DECL_NSIDOMBATTERYMANAGER
+  NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::)
+
+  BatteryManager();
+  virtual ~BatteryManager();
+
+  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(BatteryManager,
+                                           nsDOMEventTargetHelper)
+
+private:
+  float mLevel;
+  bool  mCharging;
+
+  nsRefPtr<nsDOMEventListenerWrapper> mOnLevelChangeListener;
+  nsRefPtr<nsDOMEventListenerWrapper> mOnChargingChangeListener;
+};
+
+} // namespace battery
+} // namespace dom
+} // namespace mozilla
+
+#endif // mozilla_dom_battery_BatteryManager_h
new file mode 100644
--- /dev/null
+++ b/dom/battery/Constants.h
@@ -0,0 +1,56 @@
+/* -*- 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_Constants_h__
+#define mozilla_dom_battery_Constants_h__
+
+/**
+ * A set of constants that might need to be used by battery backends.
+ * It's not part of BatteryManager.h to prevent those backends to include it.
+ */
+namespace mozilla {
+namespace dom {
+namespace battery {
+
+  static const float kDefaultLevel    = 1.0f;
+  static const bool  kDefaultCharging = true;
+
+} // namespace battery
+} // namespace dom
+} // namespace mozilla
+
+#endif // mozilla_dom_battery_Constants_h__
--- a/dom/battery/Makefile.in
+++ b/dom/battery/Makefile.in
@@ -43,25 +43,28 @@ include $(DEPTH)/config/autoconf.mk
 
 LIBRARY_NAME     = dom_battery_s
 XPIDL_MODULE     = dom_battery
 LIBXUL_LIBRARY   = 1
 FORCE_STATIC_LIB = 1
 
 include $(topsrcdir)/dom/dom-config.mk
 
-#EXPORTS_NAMESPACES = mozilla/dom/battery
+EXPORTS_NAMESPACES = mozilla/dom/battery
 
 EXPORTS_mozilla/dom/battery = \
+  Constants.h \
   $(NULL)
 
 CPPSRCS = \
+  BatteryManager.cpp \
   $(NULL)
 
 LOCAL_INCLUDES = \
+  -I$(topsrcdir)/content/events/src \
   $(NULL)
 
 XPIDLSRCS = \
   nsIDOMBatteryManager.idl \
   nsIDOMNavigatorBattery.idl \
   $(NULL)
 
 ifdef ENABLE_TESTS
--- a/dom/battery/test/test_battery_basics.html
+++ b/dom/battery/test/test_battery_basics.html
@@ -11,12 +11,16 @@
 </div>
 <pre id="test">
 <script type="application/javascript">
 
 /** Test for Battery API **/
 
 ok('mozBattery' in navigator, "navigator.mozBattery should exist");
 
+var battery = navigator.mozBattery;
+is(battery.level, 1.0, "Default battery level should be 1.0");
+is(battery.charging, true, "Default charging value should be true");
+
 </script>
 </pre>
 </body>
 </html>