From 80f9a49a061a61f86c483e6a8525581f0d7349cb Mon Sep 17 00:00:00 2001 From: David Zbarsky Date: Tue, 10 Sep 2013 19:03:56 -0400 Subject: [PATCH] Bug 911603 - Convert MozPowerManager to WebIDL r=Ms2ger --HG-- rename : dom/power/nsIDOMPowerManager.idl => dom/webidl/MozPowerManager.webidl --- dom/base/Navigator.cpp | 2 +- dom/base/Navigator.h | 2 +- dom/base/nsDOMClassInfo.cpp | 8 -- dom/base/nsDOMClassInfoClasses.h | 1 - dom/bindings/Bindings.conf | 10 +- dom/interfaces/base/domstubs.idl | 1 - dom/permission/tests/test_power.html | 2 +- dom/power/PowerManager.cpp | 116 +++++++++--------- dom/power/PowerManager.h | 41 +++++-- dom/power/moz.build | 2 +- .../MozPowerManager.webidl} | 17 +-- dom/webidl/Navigator.webidl | 1 - dom/webidl/moz.build | 1 + 13 files changed, 110 insertions(+), 94 deletions(-) rename dom/{power/nsIDOMPowerManager.idl => webidl/MozPowerManager.webidl} (85%) diff --git a/dom/base/Navigator.cpp b/dom/base/Navigator.cpp index 4bd98064baef..1091c5001516 100644 --- a/dom/base/Navigator.cpp +++ b/dom/base/Navigator.cpp @@ -1100,7 +1100,7 @@ Navigator::GetBattery(ErrorResult& aRv) return mBatteryManager; } -nsIDOMMozPowerManager* +power::PowerManager* Navigator::GetMozPower(ErrorResult& aRv) { if (!mPowerManager) { diff --git a/dom/base/Navigator.h b/dom/base/Navigator.h index d01c035e2feb..3d10eb000bf6 100644 --- a/dom/base/Navigator.h +++ b/dom/base/Navigator.h @@ -199,7 +199,7 @@ public: { aRv = GetBuildID(aBuildID); } - nsIDOMMozPowerManager* GetMozPower(ErrorResult& aRv); + power::PowerManager* GetMozPower(ErrorResult& aRv); bool JavaEnabled(ErrorResult& aRv); bool TaintEnabled() { diff --git a/dom/base/nsDOMClassInfo.cpp b/dom/base/nsDOMClassInfo.cpp index 9cca51b39f2c..d09e098fc9a9 100644 --- a/dom/base/nsDOMClassInfo.cpp +++ b/dom/base/nsDOMClassInfo.cpp @@ -158,7 +158,6 @@ #include "nsWrapperCacheInlines.h" #include "mozilla/dom/HTMLCollectionBinding.h" -#include "nsIDOMPowerManager.h" #include "nsIDOMWakeLock.h" #include "nsIDOMMobileMessageManager.h" #include "nsIDOMMozSmsMessage.h" @@ -464,9 +463,6 @@ static nsDOMClassInfoData sClassInfoData[] = { DEFAULT_SCRIPTABLE_FLAGS | WINDOW_SCRIPTABLE_FLAGS) - NS_DEFINE_CLASSINFO_DATA(MozPowerManager, nsDOMGenericSH, - DOM_DEFAULT_SCRIPTABLE_FLAGS) - NS_DEFINE_CLASSINFO_DATA(MozWakeLock, nsDOMGenericSH, DOM_DEFAULT_SCRIPTABLE_FLAGS) @@ -1280,10 +1276,6 @@ nsDOMClassInfo::Init() #endif DOM_CLASSINFO_MAP_END - DOM_CLASSINFO_MAP_BEGIN(MozPowerManager, nsIDOMMozPowerManager) - DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozPowerManager) - DOM_CLASSINFO_MAP_END - DOM_CLASSINFO_MAP_BEGIN(MozWakeLock, nsIDOMMozWakeLock) DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozWakeLock) DOM_CLASSINFO_MAP_END diff --git a/dom/base/nsDOMClassInfoClasses.h b/dom/base/nsDOMClassInfoClasses.h index 394769a116a0..78c08c707d22 100644 --- a/dom/base/nsDOMClassInfoClasses.h +++ b/dom/base/nsDOMClassInfoClasses.h @@ -88,7 +88,6 @@ DOMCI_CLASS(File) // DOM modal content window class, almost identical to Window DOMCI_CLASS(ModalContentWindow) -DOMCI_CLASS(MozPowerManager) DOMCI_CLASS(MozWakeLock) DOMCI_CLASS(MozMobileMessageManager) diff --git a/dom/bindings/Bindings.conf b/dom/bindings/Bindings.conf index 95df5dbacbcd..f2d53e899d79 100644 --- a/dom/bindings/Bindings.conf +++ b/dom/bindings/Bindings.conf @@ -776,8 +776,8 @@ DOMInterfaces = { 'nativeType': 'nsDOMAttributeMap', }, -'MozTimeManager': { - 'nativeType': 'mozilla::dom::time::TimeManager', +'MozPowerManager': { + 'nativeType': 'mozilla::dom::power::PowerManager', }, 'MozStkCommandEvent' : { @@ -785,6 +785,10 @@ DOMInterfaces = { 'headerFile': 'StkCommandEvent.h', }, +'MozTimeManager': { + 'nativeType': 'mozilla::dom::time::TimeManager', +}, + 'MozVoicemail': { 'nativeType': 'mozilla::dom::Voicemail', }, @@ -1799,7 +1803,6 @@ addExternalIface('MozMediaStreamOptions', nativeType='nsIMediaStreamOptions', addExternalIface('MozMobileConnection', headerFile='nsIDOMMobileConnection.h') addExternalIface('MozMobileMessageManager', headerFile='nsIDOMMobileMessageManager.h') addExternalIface('MozObserver', nativeType='nsIObserver', notflattened=True) -addExternalIface('MozPowerManager', headerFile='nsIDOMPowerManager.h') addExternalIface('MozRDFCompositeDataSource', nativeType='nsIRDFCompositeDataSource', notflattened=True) addExternalIface('MozRDFResource', nativeType='nsIRDFResource', notflattened=True) @@ -1810,6 +1813,7 @@ addExternalIface('MozTreeColumn', nativeType='nsITreeColumn', headerFile='nsITreeColumns.h') addExternalIface('MozVoicemailStatus') addExternalIface('MozWakeLock', headerFile='nsIDOMWakeLock.h') +addExternalIface('MozWakeLockListener', headerFile='nsIDOMWakeLockListener.h') addExternalIface('MozXULTemplateBuilder', nativeType='nsIXULTemplateBuilder') addExternalIface('nsIControllers', nativeType='nsIControllers') addExternalIface('nsIInputStreamCallback', nativeType='nsIInputStreamCallback', diff --git a/dom/interfaces/base/domstubs.idl b/dom/interfaces/base/domstubs.idl index d8421825dc10..db6156f1eaf3 100644 --- a/dom/interfaces/base/domstubs.idl +++ b/dom/interfaces/base/domstubs.idl @@ -85,5 +85,4 @@ interface nsIDOMFontFace; interface nsIDOMFontFaceList; // Power -interface nsIDOMMozPowerManager; interface nsIDOMMozWakeLock; diff --git a/dom/permission/tests/test_power.html b/dom/permission/tests/test_power.html index f7d7629dce01..51c2e8b538da 100644 --- a/dom/permission/tests/test_power.html +++ b/dom/permission/tests/test_power.html @@ -20,7 +20,7 @@ var gData = [ { perm: ["power"], obj: "mozPower", - idl: "nsIDOMMozPowerManager", + webidl: "MozPowerManager", }, ] diff --git a/dom/power/PowerManager.cpp b/dom/power/PowerManager.cpp index 6d04b1fb1c32..fdb7a287639e 100644 --- a/dom/power/PowerManager.cpp +++ b/dom/power/PowerManager.cpp @@ -15,29 +15,33 @@ #include "nsPIDOMWindow.h" #include "nsServiceManagerUtils.h" #include "nsError.h" - -DOMCI_DATA(MozPowerManager, mozilla::dom::power::PowerManager) +#include "mozilla/dom/MozPowerManagerBinding.h" namespace mozilla { namespace dom { namespace power { NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PowerManager) - NS_INTERFACE_MAP_ENTRY(nsIDOMMozPowerManager) - NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMMozPowerManager) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) NS_INTERFACE_MAP_ENTRY(nsIDOMMozWakeLockListener) - NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozPowerManager) NS_INTERFACE_MAP_END -NS_IMPL_CYCLE_COLLECTION_1(PowerManager, mListeners) +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(PowerManager, mListeners, mWindow) NS_IMPL_CYCLE_COLLECTING_ADDREF(PowerManager) NS_IMPL_CYCLE_COLLECTING_RELEASE(PowerManager) +/* virtual */ JSObject* +PowerManager::WrapObject(JSContext* aCx, JS::Handle aScope) +{ + return MozPowerManagerBinding::Wrap(aCx, aScope, this); +} + nsresult PowerManager::Init(nsIDOMWindow *aWindow) { - mWindow = do_GetWeakReference(aWindow); + mWindow = aWindow; nsCOMPtr pmService = do_GetService(POWERMANAGERSERVICE_CONTRACTID); @@ -60,63 +64,62 @@ PowerManager::Shutdown() return NS_OK; } -NS_IMETHODIMP -PowerManager::Reboot() +void +PowerManager::Reboot(ErrorResult& aRv) { nsCOMPtr pmService = do_GetService(POWERMANAGERSERVICE_CONTRACTID); - NS_ENSURE_STATE(pmService); - - pmService->Reboot(); - - return NS_OK; + if (pmService) { + pmService->Reboot(); + } else { + aRv.Throw(NS_ERROR_UNEXPECTED); + } } -NS_IMETHODIMP +void PowerManager::FactoryReset() { hal::FactoryReset(); - return NS_OK; } -NS_IMETHODIMP -PowerManager::PowerOff() +void +PowerManager::PowerOff(ErrorResult& aRv) { nsCOMPtr pmService = do_GetService(POWERMANAGERSERVICE_CONTRACTID); - NS_ENSURE_STATE(pmService); - - pmService->PowerOff(); - - return NS_OK; + if (pmService) { + pmService->PowerOff(); + } else { + aRv.Throw(NS_ERROR_UNEXPECTED); + } } -NS_IMETHODIMP +void PowerManager::AddWakeLockListener(nsIDOMMozWakeLockListener *aListener) { - // already added? bail out. - if (mListeners.Contains(aListener)) - return NS_OK; - - mListeners.AppendElement(aListener); - return NS_OK; + if (!mListeners.Contains(aListener)) { + mListeners.AppendElement(aListener); + } } -NS_IMETHODIMP +void PowerManager::RemoveWakeLockListener(nsIDOMMozWakeLockListener *aListener) { mListeners.RemoveElement(aListener); - return NS_OK; } -NS_IMETHODIMP -PowerManager::GetWakeLockState(const nsAString &aTopic, nsAString &aState) +void +PowerManager::GetWakeLockState(const nsAString& aTopic, + nsAString& aState, + ErrorResult& aRv) { nsCOMPtr pmService = do_GetService(POWERMANAGERSERVICE_CONTRACTID); - NS_ENSURE_STATE(pmService); - - return pmService->GetWakeLockState(aTopic, aState); + if (pmService) { + aRv = pmService->GetWakeLockState(aTopic, aState); + } else { + aRv.Throw(NS_ERROR_UNEXPECTED); + } } NS_IMETHODIMP @@ -138,47 +141,44 @@ PowerManager::Callback(const nsAString &aTopic, const nsAString &aState) return NS_OK; } -NS_IMETHODIMP -PowerManager::GetScreenEnabled(bool *aEnabled) +bool +PowerManager::ScreenEnabled() { - *aEnabled = hal::GetScreenEnabled(); - return NS_OK; + return hal::GetScreenEnabled(); } -NS_IMETHODIMP +void PowerManager::SetScreenEnabled(bool aEnabled) { hal::SetScreenEnabled(aEnabled); - return NS_OK; } -NS_IMETHODIMP -PowerManager::GetScreenBrightness(double *aBrightness) +double +PowerManager::ScreenBrightness() { - *aBrightness = hal::GetScreenBrightness(); - return NS_OK; + return hal::GetScreenBrightness(); } -NS_IMETHODIMP -PowerManager::SetScreenBrightness(double aBrightness) +void +PowerManager::SetScreenBrightness(double aBrightness, ErrorResult& aRv) { - NS_ENSURE_TRUE(0 <= aBrightness && aBrightness <= 1, NS_ERROR_INVALID_ARG); - hal::SetScreenBrightness(aBrightness); - return NS_OK; + if (0 <= aBrightness && aBrightness <= 1) { + hal::SetScreenBrightness(aBrightness); + } else { + aRv.Throw(NS_ERROR_INVALID_ARG); + } } -NS_IMETHODIMP -PowerManager::GetCpuSleepAllowed(bool *aAllowed) +bool +PowerManager::CpuSleepAllowed() { - *aAllowed = hal::GetCpuSleepAllowed(); - return NS_OK; + return hal::GetCpuSleepAllowed(); } -NS_IMETHODIMP +void PowerManager::SetCpuSleepAllowed(bool aAllowed) { hal::SetCpuSleepAllowed(aAllowed); - return NS_OK; } bool diff --git a/dom/power/PowerManager.h b/dom/power/PowerManager.h index 90538f82fd1c..492d0a889305 100644 --- a/dom/power/PowerManager.h +++ b/dom/power/PowerManager.h @@ -7,30 +7,32 @@ #include "nsCOMPtr.h" #include "nsTArray.h" -#include "nsIDOMPowerManager.h" #include "nsIDOMWakeLockListener.h" #include "nsIDOMWindow.h" #include "nsWeakReference.h" #include "nsCycleCollectionParticipant.h" +#include "nsWrapperCache.h" class nsPIDOMWindow; namespace mozilla { +class ErrorResult; + namespace dom { namespace power { -class PowerManager - : public nsIDOMMozPowerManager - , public nsIDOMMozWakeLockListener +class PowerManager MOZ_FINAL : public nsIDOMMozWakeLockListener + , public nsWrapperCache { public: NS_DECL_CYCLE_COLLECTING_ISUPPORTS - NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(PowerManager, nsIDOMMozPowerManager) - NS_DECL_NSIDOMMOZPOWERMANAGER + NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(PowerManager) NS_DECL_NSIDOMMOZWAKELOCKLISTENER - PowerManager() {}; - virtual ~PowerManager() {}; + PowerManager() + { + SetIsDOMBinding(); + } nsresult Init(nsIDOMWindow *aWindow); nsresult Shutdown(); @@ -39,9 +41,28 @@ public: static already_AddRefed CreateInstance(nsPIDOMWindow*); -private: + // WebIDL + nsIDOMWindow* GetParentObject() const + { + return mWindow; + } + virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aScope) MOZ_OVERRIDE; + void Reboot(ErrorResult& aRv); + void FactoryReset(); + void PowerOff(ErrorResult& aRv); + void AddWakeLockListener(nsIDOMMozWakeLockListener* aListener); + void RemoveWakeLockListener(nsIDOMMozWakeLockListener* aListener); + void GetWakeLockState(const nsAString& aTopic, nsAString& aState, + ErrorResult& aRv); + bool ScreenEnabled(); + void SetScreenEnabled(bool aEnabled); + double ScreenBrightness(); + void SetScreenBrightness(double aBrightness, ErrorResult& aRv); + bool CpuSleepAllowed(); + void SetCpuSleepAllowed(bool aAllowed); - nsWeakPtr mWindow; +private: + nsCOMPtr mWindow; nsTArray > mListeners; }; diff --git a/dom/power/moz.build b/dom/power/moz.build index aa8df6acd0a2..267c6aca3cf2 100644 --- a/dom/power/moz.build +++ b/dom/power/moz.build @@ -8,7 +8,6 @@ if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'gonk': TEST_DIRS += ['test'] XPIDL_SOURCES += [ - 'nsIDOMPowerManager.idl', 'nsIDOMWakeLock.idl', 'nsIDOMWakeLockListener.idl', 'nsIPowerManagerService.idl', @@ -17,6 +16,7 @@ XPIDL_SOURCES += [ XPIDL_MODULE = 'dom_power' EXPORTS.mozilla.dom.power += [ + 'PowerManager.h', 'PowerManagerService.h', 'Types.h', ] diff --git a/dom/power/nsIDOMPowerManager.idl b/dom/webidl/MozPowerManager.webidl similarity index 85% rename from dom/power/nsIDOMPowerManager.idl rename to dom/webidl/MozPowerManager.webidl index 514e8893ec0d..4ab38858fd9a 100644 --- a/dom/power/nsIDOMPowerManager.idl +++ b/dom/webidl/MozPowerManager.webidl @@ -3,17 +3,16 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include "nsISupports.idl" - -interface nsIDOMMozWakeLockListener; +interface MozWakeLockListener; /** * This interface implements navigator.mozPower */ -[scriptable, uuid(7b181fef-2757-4198-89a0-8c426b8439ea)] -interface nsIDOMMozPowerManager : nsISupports +interface MozPowerManager { + [Throws] void powerOff(); + [Throws] void reboot(); void factoryReset(); @@ -23,8 +22,8 @@ interface nsIDOMMozPowerManager : nsISupports * - locked but not visible * - locked and visible */ - void addWakeLockListener(in nsIDOMMozWakeLockListener aListener); - void removeWakeLockListener(in nsIDOMMozWakeLockListener aListener); + void addWakeLockListener(MozWakeLockListener aListener); + void removeWakeLockListener(MozWakeLockListener aListener); /** * Query the wake lock state of the topic. @@ -41,7 +40,8 @@ interface nsIDOMMozPowerManager : nsISupports * * @param aTopic The resource name related to the wake lock. */ - DOMString getWakeLockState(in DOMString aTopic); + [Throws] + DOMString getWakeLockState(DOMString aTopic); /** * Is the device's screen currently enabled? This attribute controls the @@ -64,6 +64,7 @@ interface nsIDOMMozPowerManager : nsISupports * * @throw NS_ERROR_INVALID_ARG if brightness is not in the range [0, 1]. */ + [SetterThrows] attribute double screenBrightness; /** diff --git a/dom/webidl/Navigator.webidl b/dom/webidl/Navigator.webidl index aebe69329467..b612830f8d37 100644 --- a/dom/webidl/Navigator.webidl +++ b/dom/webidl/Navigator.webidl @@ -17,7 +17,6 @@ * and create derivative works of this document. */ -interface MozPowerManager; interface MozWakeLock; // http://www.whatwg.org/specs/web-apps/current-work/#the-navigator-object diff --git a/dom/webidl/moz.build b/dom/webidl/moz.build index 8f092631bb55..7f6004a5f327 100644 --- a/dom/webidl/moz.build +++ b/dom/webidl/moz.build @@ -211,6 +211,7 @@ WEBIDL_FILES = [ 'MozActivity.webidl', 'MozMmsMessage.webidl', 'MozNamedAttrMap.webidl', + 'MozPowerManager.webidl', 'MozTimeManager.webidl', 'MutationEvent.webidl', 'MutationObserver.webidl',