Bug 875784 - Move MozTimeManager to WebIDL; r=smaug
authorMs2ger <ms2ger@gmail.com>
Wed, 12 Jun 2013 09:00:07 +0200
changeset 134707 4eba1004b5ebaf48aef4cb96caab9d079fdd06b1
parent 134706 7880cbf6200f482cc37aa9ff715813adf5e33eeb
child 134708 67a6841cbe3d784191d9f4fd2557749b3a630e91
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewerssmaug
bugs875784
milestone24.0a1
Bug 875784 - Move MozTimeManager to WebIDL; r=smaug
dom/base/Navigator.cpp
dom/base/nsDOMClassInfo.cpp
dom/base/nsDOMClassInfoClasses.h
dom/bindings/Bindings.conf
dom/permission/tests/test_time.html
dom/time/TimeManager.cpp
dom/time/TimeManager.h
dom/time/moz.build
dom/time/nsIDOMNavigatorTime.idl
dom/time/nsIDOMTimeManager.idl
dom/webidl/MozTimeManager.webidl
dom/webidl/WebIDL.mk
--- a/dom/base/Navigator.cpp
+++ b/dom/base/Navigator.cpp
@@ -1475,26 +1475,26 @@ Navigator::MozSetMessageHandler(const ns
   return mMessagesManager->MozSetMessageHandler(aType, aCallback);
 }
 
 //*****************************************************************************
 //    Navigator::nsIDOMNavigatorTime
 //*****************************************************************************
 #ifdef MOZ_TIME_MANAGER
 NS_IMETHODIMP
-Navigator::GetMozTime(nsIDOMMozTimeManager** aTime)
+Navigator::GetMozTime(nsISupports** aTime)
 {
   *aTime = nullptr;
 
   if (!CheckPermission("time")) {
     return NS_ERROR_DOM_SECURITY_ERR;
   }
 
   if (!mTimeManager) {
-    mTimeManager = new time::TimeManager();
+    mTimeManager = new time::TimeManager(GetWindow());
   }
 
   NS_ADDREF(*aTime = mTimeManager);
   return NS_OK;
 }
 #endif
 
 //*****************************************************************************
--- a/dom/base/nsDOMClassInfo.cpp
+++ b/dom/base/nsDOMClassInfo.cpp
@@ -792,21 +792,16 @@ static nsDOMClassInfoData sClassInfoData
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
   NS_DEFINE_CLASSINFO_DATA(AsyncScrollEventDetail, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
 
   NS_DEFINE_CLASSINFO_DATA(LockedFile, nsEventTargetSH,
                            EVENTTARGET_SCRIPTABLE_FLAGS)
   NS_DEFINE_CLASSINFO_DATA(CSSFontFeatureValuesRule, nsDOMGenericSH,
                            DOM_DEFAULT_SCRIPTABLE_FLAGS)
-
-#ifdef MOZ_TIME_MANAGER
-  NS_DEFINE_CLASSINFO_DATA(MozTimeManager, nsDOMGenericSH,
-                           DOM_DEFAULT_SCRIPTABLE_FLAGS)
-#endif
 };
 
 #define NS_DEFINE_CONTRACT_CTOR(_class, _contract_id)                           \
   static nsresult                                                               \
   _class##Ctor(nsISupports** aInstancePtrResult)                                \
   {                                                                             \
     nsresult rv = NS_OK;                                                        \
     nsCOMPtr<nsISupports> native = do_CreateInstance(_contract_id, &rv);        \
@@ -1920,22 +1915,16 @@ nsDOMClassInfo::Init()
   DOM_CLASSINFO_MAP_BEGIN(LockedFile, nsIDOMLockedFile)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMLockedFile)
   DOM_CLASSINFO_MAP_END
 
   DOM_CLASSINFO_MAP_BEGIN(CSSFontFeatureValuesRule, nsIDOMCSSFontFeatureValuesRule)
     DOM_CLASSINFO_MAP_ENTRY(nsIDOMCSSFontFeatureValuesRule)
   DOM_CLASSINFO_MAP_END
 
-#ifdef MOZ_TIME_MANAGER
-  DOM_CLASSINFO_MAP_BEGIN(MozTimeManager, nsIDOMMozTimeManager)
-    DOM_CLASSINFO_MAP_ENTRY(nsIDOMMozTimeManager)
-  DOM_CLASSINFO_MAP_END
-#endif
-
   MOZ_STATIC_ASSERT(MOZ_ARRAY_LENGTH(sClassInfoData) == eDOMClassInfoIDCount,
                     "The number of items in sClassInfoData doesn't match the "
                     "number of nsIDOMClassInfo ID's, this is bad! Fix it!");
 
 #ifdef DEBUG
   for (size_t i = 0; i < eDOMClassInfoIDCount; i++) {
     if (!sClassInfoData[i].u.mConstructorFptr ||
         sClassInfoData[i].mDebugID != i) {
--- a/dom/base/nsDOMClassInfoClasses.h
+++ b/dom/base/nsDOMClassInfoClasses.h
@@ -186,15 +186,8 @@ DOMCI_CLASS(CameraControl)
 DOMCI_CLASS(CameraCapabilities)
 
 DOMCI_CLASS(OpenWindowEventDetail)
 DOMCI_CLASS(AsyncScrollEventDetail)
 
 DOMCI_CLASS(LockedFile)
 
 DOMCI_CLASS(CSSFontFeatureValuesRule)
-
-#ifdef MOZ_TIME_MANAGER
-DOMCI_CLASS(MozTimeManager)
-#endif
-
-#ifdef MOZ_WEBRTC
-#endif
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -647,16 +647,24 @@ DOMInterfaces = {
 {
     'nativeType': 'nsIChannel',
     'notflattened': True
 },
 {
     'workers': True,
 }],
 
+'MozNamedAttrMap': {
+    'nativeType': 'nsDOMAttributeMap',
+},
+
+'MozTimeManager': {
+    'nativeType': 'mozilla::dom::time::TimeManager',
+},
+
 'MozStkCommandEvent' : {
     'nativeType': 'mozilla::dom::icc::StkCommandEvent',
     'headerFile': 'StkCommandEvent.h',
 },
 
 'MutationEvent': {
     'nativeType': 'nsDOMMutationEvent',
 },
@@ -667,20 +675,16 @@ DOMInterfaces = {
 
 'MutationRecord': {
     'nativeType': 'nsDOMMutationRecord',
     'headerFile': 'nsDOMMutationObserver.h',
     'resultNotAddRefed': [ 'target', 'addedNodes', 'removedNodes',
                            'previousSibling', 'nextSibling' ]
 },
 
-'MozNamedAttrMap': {
-    'nativeType': 'nsDOMAttributeMap',
-},
-
 'Node': {
     'nativeType': 'nsINode',
     'concrete': False,
     'hasXPConnectImpls': True,
     'resultNotAddRefed': [ 'ownerDocument', 'parentNode', 'parentElement',
                            'childNodes', 'firstChild', 'lastChild',
                            'previousSibling', 'nextSibling', 'insertBefore',
                            'appendChild', 'replaceChild', 'removeChild',
--- a/dom/permission/tests/test_time.html
+++ b/dom/permission/tests/test_time.html
@@ -15,16 +15,16 @@ https://bugzilla.mozilla.org/show_bug.cg
 <div id="content" style="display: none"></div>
 <pre id="test">
 <script type="application/javascript;version=1.8" src="file_framework.js"></script>
 <script type="application/javascript;version=1.8">
 var gData = [
   {
     perm: ["time"],
     obj: "mozTime",
-    idl: "nsIDOMMozTimeManager",
+    webidl: "MozTimeManager",
   },
 ]
 </script>
 </pre>
 </body>
 </html>
 
--- a/dom/time/TimeManager.cpp
+++ b/dom/time/TimeManager.cpp
@@ -1,59 +1,49 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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 "jsapi.h"
-#include "nsIDOMClassInfo.h"
-#include "nsITimeService.h"
 #include "TimeManager.h"
 
-#ifdef MOZ_TIME_MANAGER
-DOMCI_DATA(MozTimeManager, mozilla::dom::time::TimeManager)
-#endif
+#include "mozilla/dom/MozTimeManagerBinding.h"
+#include "nsContentUtils.h"
+#include "nsITimeService.h"
+#include "nsServiceManagerUtils.h"
 
 namespace mozilla {
 namespace dom {
 namespace time {
 
-NS_INTERFACE_MAP_BEGIN(TimeManager)
-  NS_INTERFACE_MAP_ENTRY(nsIDOMMozTimeManager)
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TimeManager)
+  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
   NS_INTERFACE_MAP_ENTRY(nsISupports)
-#ifdef MOZ_TIME_MANAGER
-  NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(MozTimeManager)
-#endif
 NS_INTERFACE_MAP_END
 
-NS_IMPL_ADDREF(TimeManager)
-NS_IMPL_RELEASE(TimeManager)
+NS_IMPL_CYCLE_COLLECTING_ADDREF(TimeManager)
+NS_IMPL_CYCLE_COLLECTING_RELEASE(TimeManager)
 
-nsresult
-TimeManager::Set(const JS::Value& date, JSContext* ctx) {
-  double dateMSec;
-
-  if (date.isObject()) {
-    JS::Rooted<JSObject*> dateObj(ctx, date.toObjectOrNull());
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(TimeManager, mWindow)
 
-    if (JS_ObjectIsDate(ctx, dateObj) && js_DateIsValid(dateObj)) {
-      dateMSec = js_DateGetMsecSinceEpoch(dateObj);
-    }
-    else {
-      NS_WARN_IF_FALSE(JS_ObjectIsDate(ctx, dateObj), "This is not a Date object");
-      NS_WARN_IF_FALSE(js_DateIsValid(dateObj), "Date is not valid");
-      return NS_ERROR_INVALID_ARG;
-    }
-  } else if (date.isNumber()) {
-    dateMSec = date.toNumber();
-  } else {
-    return NS_ERROR_INVALID_ARG;
-  }
+JSObject*
+TimeManager::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
+{
+  return MozTimeManagerBinding::Wrap(aCx, aScope, this);
+}
 
+void
+TimeManager::Set(Date& aDate)
+{
+  Set(aDate.TimeStamp());
+}
+
+void
+TimeManager::Set(double aTime)
+{
   nsCOMPtr<nsITimeService> timeService = do_GetService(TIMESERVICE_CONTRACTID);
   if (timeService) {
-    return timeService->Set(dateMSec);
+    timeService->Set(aTime);
   }
-  return NS_OK;
 }
 
 } // namespace time
 } // namespace dom
 } // namespace mozilla
--- a/dom/time/TimeManager.h
+++ b/dom/time/TimeManager.h
@@ -1,26 +1,59 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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/. */
 
 #ifndef mozilla_dom_time_TimeManager_h
 #define mozilla_dom_time_TimeManager_h
 
-#include "nsIDOMTimeManager.h"
 #include "mozilla/Attributes.h"
+#include "nsISupports.h"
+#include "nsPIDOMWindow.h"
+#include "nsWrapperCache.h"
 
 namespace mozilla {
 namespace dom {
+
+class Date;
+
 namespace time {
 
-class TimeManager MOZ_FINAL : public nsIDOMMozTimeManager
+class TimeManager MOZ_FINAL : public nsISupports
+                            , public nsWrapperCache
 {
 public:
-  NS_DECL_ISUPPORTS
-  NS_DECL_NSIDOMMOZTIMEMANAGER
+  static bool PrefEnabled()
+  {
+#ifdef MOZ_TIME_MANAGER
+    return true;
+#else
+    return false;
+#endif
+  }
+
+  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(TimeManager)
+
+  explicit TimeManager(nsPIDOMWindow* aWindow)
+    : mWindow(aWindow)
+  {
+    SetIsDOMBinding();
+  }
+
+  nsPIDOMWindow* GetParentObject() const
+  {
+    return mWindow;
+  }
+  JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope);
+
+  void Set(Date& aDate);
+  void Set(double aTime);
+
+private:
+  nsCOMPtr<nsPIDOMWindow> mWindow;
 };
 
 } // namespace time
 } // namespace dom
 } // namespace mozilla
 
 #endif //mozilla_dom_time_TimeManager_h
--- a/dom/time/moz.build
+++ b/dom/time/moz.build
@@ -1,27 +1,27 @@
 # -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # 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/.
 
 XPIDL_SOURCES += [
     'nsIDOMNavigatorTime.idl',
-    'nsIDOMTimeManager.idl',
     'nsITimeService.idl',
 ]
 
 XPIDL_MODULE = 'dom_time'
 
 MODULE = 'dom'
 
 EXPORTS.mozilla.dom.time += [
     'DateCacheCleaner.h',
     'TimeChangeObserver.h',
+    'TimeManager.h',
     'TimeService.h',
 ]
 
 CPP_SOURCES += [
     'DateCacheCleaner.cpp',
     'TimeChangeObserver.cpp',
     'TimeManager.cpp',
     'TimeService.cpp',
--- a/dom/time/nsIDOMNavigatorTime.idl
+++ b/dom/time/nsIDOMNavigatorTime.idl
@@ -1,13 +1,11 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * 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 nsIDOMMozTimeManager;
-
-[scriptable, uuid(befc186d-c249-4acb-8e70-8080f7b45e5c)]
+[scriptable, uuid(88df63f0-814d-4424-b1f9-9184149639e5)]
 interface nsIDOMMozNavigatorTime : nsISupports
 {
-  readonly attribute nsIDOMMozTimeManager mozTime;
+  readonly attribute nsISupports /* MozTimeManager */ mozTime;
 };
deleted file mode 100644
--- a/dom/time/nsIDOMTimeManager.idl
+++ /dev/null
@@ -1,20 +0,0 @@
-/* This Source Code Form is subject to the terms of the Mozilla Public
- * 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"
-
-[scriptable, builtinclass, uuid(d29beaaa-bd54-4fd5-9f18-e0eedb1dc96d)]
-interface nsIDOMMozTimeManager : nsISupports
-{
-  /* Set the system time.
-   *
-   * The |time| argument can be either a Date object or a number.
-   *
-   * - If |time| is a number, it's interpreted as milliseconds
-   *   since the epoch (midnight UTC on January 1, 1970).
-   * - If |time| is a Date object, |set(time)| is equivalent to
-   *   |set(time.getTime())|.
-   */
-  [implicit_jscontext] void set(in jsval time);
-};
new file mode 100644
--- /dev/null
+++ b/dom/webidl/MozTimeManager.webidl
@@ -0,0 +1,18 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * 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/. */
+
+[PrefControlled]
+interface MozTimeManager {
+  /* Set the system time.
+   *
+   * The |time| argument can be either a Date object or a number.
+   *
+   * - If |time| is a number, it's interpreted as milliseconds
+   *   since the epoch (midnight UTC on January 1, 1970).
+   * - If |time| is a Date object, |set(time)| is equivalent to
+   *   |set(time.getTime())|.
+   */
+  void set(Date time);
+  void set(double time);
+};
--- a/dom/webidl/WebIDL.mk
+++ b/dom/webidl/WebIDL.mk
@@ -176,16 +176,17 @@ webidl_files = \
   MediaStreamTrack.webidl \
   MessageEvent.webidl \
   MobileMessageManager.webidl \
   MouseEvent.webidl \
   MouseScrollEvent.webidl \
   MozActivity.webidl \
   MozMmsMessage.webidl \
   MozNamedAttrMap.webidl \
+  MozTimeManager.webidl \
   MutationEvent.webidl \
   MutationObserver.webidl \
   NetDashboard.webidl \
   Node.webidl \
   NodeFilter.webidl \
   NodeIterator.webidl \
   NodeList.webidl \
   Notification.webidl \