Bug 793244 - Part b: Convert Screen to WebIDL; r=bz
authorMs2ger <ms2ger@gmail.com>
Sun, 14 Oct 2012 09:40:11 +0200
changeset 110311 b740202ee0b8bcc989b7c538797b66c9b075bfe7
parent 110310 5468a80570b24af65ff86851fe0b58846df6084b
child 110312 02053bbdc0327518104f39e14843e73f48ae2f5a
push id93
push usernmatsakis@mozilla.com
push dateWed, 31 Oct 2012 21:26:57 +0000
reviewersbz
bugs793244
milestone19.0a1
Bug 793244 - Part b: Convert Screen to WebIDL; r=bz
dom/base/nsScreen.cpp
dom/base/nsScreen.h
dom/bindings/Bindings.conf
dom/bindings/Makefile.in
dom/interfaces/base/nsIDOMScreen.idl
dom/webidl/Screen.webidl
dom/webidl/WebIDL.mk
--- a/dom/base/nsScreen.cpp
+++ b/dom/base/nsScreen.cpp
@@ -9,16 +9,17 @@
 #include "nsPresContext.h"
 #include "nsCOMPtr.h"
 #include "nsDOMClassInfoID.h"
 #include "nsIDocShellTreeItem.h"
 #include "nsLayoutUtils.h"
 #include "nsDOMEvent.h"
 #include "nsGlobalWindow.h"
 #include "nsJSUtils.h"
+#include "mozilla/dom/ScreenBinding.h"
 
 using namespace mozilla;
 using namespace mozilla::dom;
 
 namespace {
 
 bool
 IsChromeType(nsIDocShell *aDocShell)
@@ -106,130 +107,52 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_
   NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(Screen)
 NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
 
 NS_IMPL_ADDREF_INHERITED(nsScreen, nsDOMEventTargetHelper)
 NS_IMPL_RELEASE_INHERITED(nsScreen, nsDOMEventTargetHelper)
 
 NS_IMPL_EVENT_HANDLER(nsScreen, mozorientationchange)
 
-NS_IMETHODIMP
-nsScreen::GetTop(int32_t* aTop)
-{
-  nsRect rect;
-  nsresult rv = GetRect(rect);
-
-  *aTop = rect.y;
-
-  return rv;
-}
-
-
-NS_IMETHODIMP
-nsScreen::GetLeft(int32_t* aLeft)
-{
-  nsRect rect;
-  nsresult rv = GetRect(rect);
-
-  *aLeft = rect.x;
-
-  return rv;
-}
-
-
-NS_IMETHODIMP
-nsScreen::GetWidth(int32_t* aWidth)
-{
-  nsRect rect;
-  nsresult rv = GetRect(rect);
-
-  *aWidth = rect.width;
-
-  return rv;
-}
-
-NS_IMETHODIMP
-nsScreen::GetHeight(int32_t* aHeight)
-{
-  nsRect rect;
-  nsresult rv = GetRect(rect);
-
-  *aHeight = rect.height;
-
-  return rv;
-}
-
-NS_IMETHODIMP
-nsScreen::GetPixelDepth(int32_t* aPixelDepth)
+int32_t
+nsScreen::GetPixelDepth(ErrorResult& aRv)
 {
   nsDeviceContext* context = GetDeviceContext();
 
   if (!context) {
-    *aPixelDepth = -1;
-
-    return NS_ERROR_FAILURE;
+    aRv.Throw(NS_ERROR_FAILURE);
+    return -1;
   }
 
   uint32_t depth;
   context->GetDepth(depth);
-
-  *aPixelDepth = depth;
-
-  return NS_OK;
-}
-
-NS_IMETHODIMP
-nsScreen::GetColorDepth(int32_t* aColorDepth)
-{
-  return GetPixelDepth(aColorDepth);
-}
-
-NS_IMETHODIMP
-nsScreen::GetAvailWidth(int32_t* aAvailWidth)
-{
-  nsRect rect;
-  nsresult rv = GetAvailRect(rect);
-
-  *aAvailWidth = rect.width;
-
-  return rv;
+  return depth;
 }
 
-NS_IMETHODIMP
-nsScreen::GetAvailHeight(int32_t* aAvailHeight)
-{
-  nsRect rect;
-  nsresult rv = GetAvailRect(rect);
-
-  *aAvailHeight = rect.height;
-
-  return rv;
-}
+#define FORWARD_LONG_GETTER(_name)                                              \
+  NS_IMETHODIMP                                                                 \
+  nsScreen::Get ## _name(int32_t* aOut)                                         \
+  {                                                                             \
+    ErrorResult rv;                                                             \
+    *aOut = Get ## _name(rv);                                                   \
+    return rv.ErrorCode();                                                      \
+  }
 
-NS_IMETHODIMP
-nsScreen::GetAvailLeft(int32_t* aAvailLeft)
-{
-  nsRect rect;
-  nsresult rv = GetAvailRect(rect);
-
-  *aAvailLeft = rect.x;
-
-  return rv;
-}
+FORWARD_LONG_GETTER(AvailWidth)
+FORWARD_LONG_GETTER(AvailHeight)
+FORWARD_LONG_GETTER(Width)
+FORWARD_LONG_GETTER(Height)
 
-NS_IMETHODIMP
-nsScreen::GetAvailTop(int32_t* aAvailTop)
-{
-  nsRect rect;
-  nsresult rv = GetAvailRect(rect);
+FORWARD_LONG_GETTER(Top)
+FORWARD_LONG_GETTER(Left)
+FORWARD_LONG_GETTER(AvailTop)
+FORWARD_LONG_GETTER(AvailLeft)
 
-  *aAvailTop = rect.y;
-
-  return rv;
-}
+FORWARD_LONG_GETTER(PixelDepth)
+FORWARD_LONG_GETTER(ColorDepth)
 
 nsDeviceContext*
 nsScreen::GetDeviceContext()
 {
   return nsLayoutUtils::GetDeviceContextForScreenInfo(GetOwner());
 }
 
 nsresult
@@ -282,38 +205,44 @@ nsScreen::Notify(const hal::ScreenConfig
                mOrientation == eScreenOrientation_LandscapeSecondary,
                "Invalid orientation value passed to notify method!");
 
   if (mOrientation != previousOrientation) {
     DispatchTrustedEvent(NS_LITERAL_STRING("mozorientationchange"));
   }
 }
 
-NS_IMETHODIMP
-nsScreen::GetMozOrientation(nsAString& aOrientation)
+void
+nsScreen::GetMozOrientation(nsString& aOrientation)
 {
   switch (mOrientation) {
-    case eScreenOrientation_PortraitPrimary:
-      aOrientation.AssignLiteral("portrait-primary");
-      break;
-    case eScreenOrientation_PortraitSecondary:
-      aOrientation.AssignLiteral("portrait-secondary");
-      break;
-    case eScreenOrientation_LandscapePrimary:
-      aOrientation.AssignLiteral("landscape-primary");
-      break;
-    case eScreenOrientation_LandscapeSecondary:
-      aOrientation.AssignLiteral("landscape-secondary");
-      break;
-    case eScreenOrientation_None:
-    default:
-      MOZ_ASSERT(false);
-      return NS_ERROR_FAILURE;
+  case eScreenOrientation_PortraitPrimary:
+    aOrientation.AssignLiteral("portrait-primary");
+    break;
+  case eScreenOrientation_PortraitSecondary:
+    aOrientation.AssignLiteral("portrait-secondary");
+    break;
+  case eScreenOrientation_LandscapePrimary:
+    aOrientation.AssignLiteral("landscape-primary");
+    break;
+  case eScreenOrientation_LandscapeSecondary:
+    aOrientation.AssignLiteral("landscape-secondary");
+    break;
+  case eScreenOrientation_None:
+  default:
+    MOZ_NOT_REACHED("Unacceptable mOrientation value");
   }
+}
 
+NS_IMETHODIMP
+nsScreen::GetSlowMozOrientation(nsAString& aOrientation)
+{
+  nsString orientation;
+  GetMozOrientation(orientation);
+  aOrientation = orientation;
   return NS_OK;
 }
 
 nsScreen::LockPermission
 nsScreen::GetLockOrientationPermission() const
 {
   nsCOMPtr<nsPIDOMWindow> owner = GetOwner();
   if (!owner) {
@@ -473,23 +402,36 @@ nsScreen::MozLockOrientation(const Seque
   }
 
   // This is only for compilers that don't understand that the previous switch
   // will always return.
   MOZ_NOT_REACHED();
   return false;
 }
 
-NS_IMETHODIMP
+void
 nsScreen::MozUnlockOrientation()
 {
   hal::UnlockScreenOrientation();
+}
+
+NS_IMETHODIMP
+nsScreen::SlowMozUnlockOrientation()
+{
+  MozUnlockOrientation();
   return NS_OK;
 }
 
+/* virtual */
+JSObject*
+nsScreen::WrapObject(JSContext* aCx, JSObject* aScope, bool* aTriedToWrap)
+{
+  return ScreenBinding::Wrap(aCx, aScope, this, aTriedToWrap);
+}
+
 NS_IMPL_ISUPPORTS1(nsScreen::FullScreenEventListener, nsIDOMEventListener)
 
 NS_IMETHODIMP
 nsScreen::FullScreenEventListener::HandleEvent(nsIDOMEvent* aEvent)
 {
 #ifdef DEBUG
   nsAutoString eventType;
   aEvent->GetType(eventType);
--- a/dom/base/nsScreen.h
+++ b/dom/base/nsScreen.h
@@ -10,42 +10,127 @@
 #include "mozilla/dom/ScreenOrientation.h"
 #include "mozilla/ErrorResult.h"
 #include "mozilla/Hal.h"
 #include "nsIDOMScreen.h"
 #include "nsISupports.h"
 #include "nsIScriptContext.h"
 #include "nsCOMPtr.h"
 #include "nsDOMEventTargetHelper.h"
+#include "nsRect.h"
 
 class nsIDocShell;
 class nsDeviceContext;
-struct nsRect;
 
 // Script "screen" object
 class nsScreen : public nsDOMEventTargetHelper
                , public nsIDOMScreen
                , public mozilla::hal::ScreenConfigurationObserver
 {
   typedef mozilla::ErrorResult ErrorResult;
 public:
   static already_AddRefed<nsScreen> Create(nsPIDOMWindow* aWindow);
 
   void Reset();
 
-  NS_DECL_ISUPPORTS
+  NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_NSIDOMSCREEN
   NS_FORWARD_NSIDOMEVENTTARGET(nsDOMEventTargetHelper::)
 
+  nsPIDOMWindow* GetParentObject() const
+  {
+    return GetOwner();
+  }
+
+  int32_t GetTop(ErrorResult& aRv)
+  {
+    nsRect rect;
+    aRv = GetRect(rect);
+    return rect.y;
+  }
+
+  int32_t GetLeft(ErrorResult& aRv)
+  {
+    nsRect rect;
+    aRv = GetRect(rect);
+    return rect.x;
+  }
+
+  int32_t GetWidth(ErrorResult& aRv)
+  {
+    nsRect rect;
+    aRv = GetRect(rect);
+    return rect.width;
+  }
+
+  int32_t GetHeight(ErrorResult& aRv)
+  {
+    nsRect rect;
+    aRv = GetRect(rect);
+    return rect.height;
+  }
+
+  int32_t GetPixelDepth(ErrorResult& aRv);
+  int32_t GetColorDepth(ErrorResult& aRv)
+  {
+    return GetPixelDepth(aRv);
+  }
+
+  int32_t GetAvailTop(ErrorResult& aRv)
+  {
+    nsRect rect;
+    aRv = GetAvailRect(rect);
+    return rect.y;
+  }
+
+  int32_t GetAvailLeft(ErrorResult& aRv)
+  {
+    nsRect rect;
+    aRv = GetAvailRect(rect);
+    return rect.x;
+  }
+
+  int32_t GetAvailWidth(ErrorResult& aRv)
+  {
+    nsRect rect;
+    aRv = GetAvailRect(rect);
+    return rect.width;
+  }
+
+  int32_t GetAvailHeight(ErrorResult& aRv)
+  {
+    nsRect rect;
+    aRv = GetAvailRect(rect);
+    return rect.height;
+  }
+
+  void GetMozOrientation(nsString& aOrientation);
+
+  JSObject* GetOnmozorientationchange(JSContext* aCx)
+  {
+    JS::Value val;
+    nsresult rv = GetOnmozorientationchange(aCx, &val);
+    return NS_SUCCEEDED(rv) ? val.toObjectOrNull() : nullptr;
+  }
+  void SetOnmozorientationchange(JSContext* aCx, JSObject* aCallback,
+                                 ErrorResult& aRv)
+  {
+    aRv = SetOnmozorientationchange(aCx, JS::ObjectOrNullValue(aCallback));
+  }
+
   bool MozLockOrientation(const nsAString& aOrientation, ErrorResult& aRv);
   bool MozLockOrientation(const mozilla::dom::Sequence<nsString>& aOrientations, ErrorResult& aRv);
+  void MozUnlockOrientation();
 
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(nsScreen,
                                            nsDOMEventTargetHelper)
 
+  virtual JSObject* WrapObject(JSContext* aCx, JSObject* aScope,
+                               bool* aTriedToWrap);
+
   void Notify(const mozilla::hal::ScreenConfiguration& aConfiguration);
 
 protected:
   nsDeviceContext* GetDeviceContext();
   nsresult GetRect(nsRect& aRect);
   nsresult GetAvailRect(nsRect& aRect);
 
   mozilla::dom::ScreenOrientation mOrientation;
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -287,16 +287,21 @@ DOMInterfaces = {
 
 'PropertyNodeList': [
 {
     'headerFile': 'HTMLPropertiesCollection.h',
     'prefable': True,
     'resultNotAddRefed': [ 'item' ]
 }],
 
+'Screen': {
+    'nativeType': 'nsScreen',
+    'prefable': True,
+},
+
 'SVGLengthList': [
 {
     'nativeType': 'mozilla::DOMSVGLengthList',
     'headerFile': 'DOMSVGLengthList.h',
     'prefable': True,
     'resultNotAddRefed': [ 'getItem' ]
 }],
 
--- a/dom/bindings/Makefile.in
+++ b/dom/bindings/Makefile.in
@@ -9,16 +9,17 @@ VPATH            = @srcdir@
 
 MODULE           = dom
 LIBRARY_NAME     = dombindings_s
 LIBXUL_LIBRARY   = 1
 FORCE_STATIC_LIB = 1
 EXPORT_LIBRARY   = 1
 
 include $(topsrcdir)/config/config.mk
+include $(topsrcdir)/ipc/chromium/chromium-config.mk
 
 # Need this to find all our DOM source files.
 include $(topsrcdir)/dom/dom-config.mk
 
 include $(topsrcdir)/dom/webidl/WebIDL.mk
 
 binding_include_path := mozilla/dom
 all_webidl_files = $(webidl_files) $(generated_webidl_files)
@@ -67,17 +68,19 @@ EXPORTS_$(binding_include_path) = \
   UnionConversions.h \
   UnionTypes.h \
   $(exported_binding_headers) \
   $(NULL)
 
 LOCAL_INCLUDES += -I$(topsrcdir)/js/xpconnect/src \
   -I$(topsrcdir)/js/xpconnect/wrappers \
   -I$(topsrcdir)/content/canvas/src \
-  -I$(topsrcdir)/content/html/content/src
+  -I$(topsrcdir)/content/html/content/src \
+  -I$(topsrcdir)/dom/base \
+  $(NULL)
 
 include $(topsrcdir)/config/rules.mk
 
 # If you change bindinggen_dependencies here, change it in
 # dom/bindings/test/Makefile.in too.
 bindinggen_dependencies := \
   BindingGen.py \
   Bindings.conf \
--- a/dom/interfaces/base/nsIDOMScreen.idl
+++ b/dom/interfaces/base/nsIDOMScreen.idl
@@ -19,16 +19,17 @@ interface nsIDOMScreen : nsIDOMEventTarg
   readonly attribute long             availLeft;
   readonly attribute long             availTop;
 
   /**
    * Returns the current screen orientation.
    * Can be: landscape-primary, landscape-secondary,
    *         portrait-primary or portrait-secondary.
    */
+  [binaryname(SlowMozOrientation)]
   readonly attribute DOMString       mozOrientation;
 
   [implicit_jscontext] attribute jsval      onmozorientationchange;
 
   /**
    * Lock the screen to the specified orientations(s).  This method returns true
    * if the lock was acquired successfully, and false otherwise.
    *
@@ -52,10 +53,11 @@ interface nsIDOMScreen : nsIDOMEventTarg
    * "landscape" orientation similarly corresponds to the set
    * ['landscape-primary', 'landscape-secondary'].
    */
   [implicit_jscontext] boolean mozLockOrientation(in jsval orientation);
 
   /**
    * Unlock the screen orientation.
    */
+  [binaryname(SlowMozUnlockOrientation)]
   void mozUnlockOrientation();
 };
new file mode 100644
--- /dev/null
+++ b/dom/webidl/Screen.webidl
@@ -0,0 +1,52 @@
+/* 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/. */
+
+interface Screen : EventTarget {
+  // CSSOM-View
+  // http://dev.w3.org/csswg/cssom-view/#the-screen-interface
+  [Throws]
+  readonly attribute long availWidth;
+  [Throws]
+  readonly attribute long availHeight;
+  [Throws]
+  readonly attribute long width;
+  [Throws]
+  readonly attribute long height;
+  [Throws]
+  readonly attribute long colorDepth;
+  [Throws]
+  readonly attribute long pixelDepth;
+
+  [Throws]
+  readonly attribute long top;
+  [Throws]
+  readonly attribute long left;
+  [Throws]
+  readonly attribute long availTop;
+  [Throws]
+  readonly attribute long availLeft;
+
+  /**
+   * Returns the current screen orientation.
+   * Can be: landscape-primary, landscape-secondary,
+   *         portrait-primary or portrait-secondary.
+   */
+  readonly attribute DOMString mozOrientation;
+
+  [SetterThrows]
+  attribute EventHandler onmozorientationchange;
+
+  /**
+   * Lock screen orientation to the specified type.
+   */
+  [Throws]
+  boolean mozLockOrientation(DOMString orientation);
+  [Throws]
+  boolean mozLockOrientation(sequence<DOMString> orientation);
+
+  /**
+   * Unlock the screen orientation.
+   */
+  void mozUnlockOrientation();
+};
--- a/dom/webidl/WebIDL.mk
+++ b/dom/webidl/WebIDL.mk
@@ -30,16 +30,17 @@ webidl_files = \
   HTMLCollection.webidl \
   HTMLOptionsCollection.webidl \
   HTMLPropertiesCollection.webidl \
   NodeList.webidl \
   PaintRequestList.webidl \
   Performance.webidl \
   PerformanceNavigation.webidl \
   PerformanceTiming.webidl \
+  Screen.webidl \
   SVGLengthList.webidl \
   SVGNumberList.webidl \
   SVGPathSegList.webidl \
   SVGPointList.webidl \
   SVGTransformList.webidl \
   TextDecoder.webidl \
   TextEncoder.webidl \
   WebSocket.webidl \