Bug 740188 - Screen Orientation API locking (1/3): DOM/HAL boilerplate. r=smaug sr=sicking
authorMounir Lamouri <mounir.lamouri@gmail.com>
Thu, 29 Mar 2012 12:43:16 -0700
changeset 94005 a1ba687fd66a73673fc6b8da781ae4f772fefaf4
parent 94004 945faa47b6277c3337a8693e9ce4933e0912487d
child 94006 d8aa32fa55317e94c58dcf6a216790fc03cdca3b
push id886
push userlsblakk@mozilla.com
push dateMon, 04 Jun 2012 19:57:52 +0000
treeherdermozilla-beta@bbd8d5efd6d1 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, sicking
bugs740188
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 740188 - Screen Orientation API locking (1/3): DOM/HAL boilerplate. r=smaug sr=sicking
dom/base/ScreenOrientation.h
dom/base/nsScreen.cpp
dom/interfaces/base/nsIDOMScreen.idl
hal/Hal.cpp
hal/Hal.h
hal/android/AndroidHal.cpp
hal/fallback/ScreenOrientationFallback.cpp
hal/sandbox/PHal.ipdl
hal/sandbox/SandboxHal.cpp
--- a/dom/base/ScreenOrientation.h
+++ b/dom/base/ScreenOrientation.h
@@ -7,17 +7,17 @@
 
 namespace mozilla {
 namespace dom {
 
 // Make sure that any change here is also made in
 // * mobile/android/base/GeckoScreenOrientationListener.java
 // * embedding/android/GeckoScreenOrientationListener.java
 enum ScreenOrientation {
-  eScreenOrientation_Current            = 0,
+  eScreenOrientation_None               = 0,
   eScreenOrientation_PortraitPrimary    = 1,  // 00000001
   eScreenOrientation_PortraitSecondary  = 2,  // 00000010
   eScreenOrientation_Portrait           = 3,  // 00000011
   eScreenOrientation_LandscapePrimary   = 4,  // 00000100
   eScreenOrientation_LandscapeSecondary = 8,  // 00001000
   eScreenOrientation_Landscape          = 12, // 00001100
   eScreenOrientation_EndGuard
 };
@@ -25,17 +25,17 @@ enum ScreenOrientation {
 /**
  * ScreenOrientationWrapper is a class wrapping ScreenOrientation so it can be
  * used with Observer<T> which is taking a class, not an enum.
  * C++11 should make this useless.
  */
 class ScreenOrientationWrapper {
 public:
   ScreenOrientationWrapper()
-    : orientation(eScreenOrientation_Current)
+    : orientation(eScreenOrientation_None)
   {}
 
   ScreenOrientationWrapper(ScreenOrientation aOrientation)
     : orientation(aOrientation)
   {}
 
   ScreenOrientation orientation;
 };
@@ -48,15 +48,15 @@ namespace IPC {
 /**
  * Screen orientation serializer.
  * Note that technically, 5, 6, 7, 9, 10 and 11 are illegal values but will
  * not make the serializer to fail. We might want to write our own serializer.
  */
 template <>
 struct ParamTraits<mozilla::dom::ScreenOrientation>
   : public EnumSerializer<mozilla::dom::ScreenOrientation,
-                          mozilla::dom::eScreenOrientation_Current,
+                          mozilla::dom::eScreenOrientation_None,
                           mozilla::dom::eScreenOrientation_EndGuard>
 {};
 
 } // namespace IPC
 
 #endif // mozilla_dom_ScreenOrientation_h
--- a/dom/base/nsScreen.cpp
+++ b/dom/base/nsScreen.cpp
@@ -375,17 +375,17 @@ nsScreen::SetMozBrightness(double aBrigh
 }
 
 void
 nsScreen::Notify(const ScreenOrientationWrapper& aOrientation)
 {
   ScreenOrientation previousOrientation = mOrientation;
   mOrientation = aOrientation.orientation;
 
-  NS_ASSERTION(mOrientation != eScreenOrientation_Current &&
+  NS_ASSERTION(mOrientation != eScreenOrientation_None &&
                mOrientation != eScreenOrientation_EndGuard &&
                mOrientation != eScreenOrientation_Portrait &&
                mOrientation != eScreenOrientation_Landscape,
                "Invalid orientation value passed to notify method!");
 
   if (mOrientation != previousOrientation) {
     // TODO: use an helper method, see bug 720768.
     nsRefPtr<nsDOMEvent> event = new nsDOMEvent(nsnull, nsnull);
@@ -406,17 +406,17 @@ nsScreen::Notify(const ScreenOrientation
     }
   }
 }
 
 NS_IMETHODIMP
 nsScreen::GetMozOrientation(nsAString& aOrientation)
 {
   switch (mOrientation) {
-    case eScreenOrientation_Current:
+    case eScreenOrientation_None:
     case eScreenOrientation_EndGuard:
     case eScreenOrientation_Portrait:
     case eScreenOrientation_Landscape:
       NS_ASSERTION(false, "Shouldn't be used when getting value!");
       return NS_ERROR_FAILURE;
     case eScreenOrientation_PortraitPrimary:
       aOrientation.AssignLiteral("portrait-primary");
       break;
@@ -428,8 +428,41 @@ nsScreen::GetMozOrientation(nsAString& a
       break;
     case eScreenOrientation_LandscapeSecondary:
       aOrientation.AssignLiteral("landscape-secondary");
       break;
   }
 
   return NS_OK;
 }
+
+NS_IMETHODIMP
+nsScreen::MozLockOrientation(const nsAString& aOrientation, bool* aReturn)
+{
+  ScreenOrientation orientation;
+
+  if (aOrientation.EqualsLiteral("portrait")) {
+    orientation = eScreenOrientation_Portrait;
+  } else if (aOrientation.EqualsLiteral("portrait-primary")) {
+    orientation = eScreenOrientation_PortraitPrimary;
+  } else if (aOrientation.EqualsLiteral("portrait-secondary")) {
+    orientation = eScreenOrientation_PortraitSecondary;
+  } else if (aOrientation.EqualsLiteral("landscape")) {
+    orientation = eScreenOrientation_Landscape;
+  } else if (aOrientation.EqualsLiteral("landscape-primary")) {
+    orientation = eScreenOrientation_LandscapePrimary;
+  } else if (aOrientation.EqualsLiteral("landscape-secondary")) {
+    orientation = eScreenOrientation_LandscapeSecondary;
+  } else {
+    *aReturn = false;
+    return NS_OK;
+  }
+
+  *aReturn = hal::LockScreenOrientation(orientation);
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsScreen::MozUnlockOrientation()
+{
+  hal::UnlockScreenOrientation();
+  return NS_OK;
+}
--- a/dom/interfaces/base/nsIDOMScreen.idl
+++ b/dom/interfaces/base/nsIDOMScreen.idl
@@ -34,17 +34,17 @@
  * 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 "nsIDOMEventTarget.idl"
 
-[scriptable, uuid(6366afc9-0072-4231-a4ec-98cd65f350ef)]
+[scriptable, uuid(8a66b30c-9a32-4b17-ab4e-ca8b7b588243)]
 interface nsIDOMScreen : nsIDOMEventTarget
 {
   readonly attribute long             top;
   readonly attribute long             left;
   readonly attribute long             width;
   readonly attribute long             height;
   readonly attribute long             pixelDepth;
   readonly attribute long             colorDepth;
@@ -79,9 +79,19 @@ interface nsIDOMScreen : nsIDOMEventTarg
   /**
    * Returns the current screen orientation.
    * Can be: landscape-primary, landscape-secondary,
    *         portrait-primary or portrait-secondary.
    */
   readonly attribute DOMString       mozOrientation;
 
   attribute nsIDOMEventListener      onmozorientationchange;
+
+  /**
+   * Lock screen orientation to the specified type.
+   */
+  boolean mozLockOrientation(in DOMString orientation);
+
+  /**
+   * Unlock the screen orientation.
+   */
+  void mozUnlockOrientation();
 };
--- a/hal/Hal.cpp
+++ b/hal/Hal.cpp
@@ -561,10 +561,24 @@ GetCurrentScreenOrientation(dom::ScreenO
 
 void
 NotifyScreenOrientationChange(const dom::ScreenOrientation& aScreenOrientation)
 {
   sScreenOrientationObservers.CacheInformation(dom::ScreenOrientationWrapper(aScreenOrientation));
   sScreenOrientationObservers.BroadcastCachedInformation();
 }
 
+bool
+LockScreenOrientation(const dom::ScreenOrientation& aOrientation)
+{
+  AssertMainThread();
+  RETURN_PROXY_IF_SANDBOXED(LockScreenOrientation(aOrientation));
+}
+
+void
+UnlockScreenOrientation()
+{
+  AssertMainThread();
+  PROXY_IF_SANDBOXED(UnlockScreenOrientation());
+}
+
 } // namespace hal
 } // namespace mozilla
--- a/hal/Hal.h
+++ b/hal/Hal.h
@@ -318,16 +318,27 @@ void UnregisterScreenOrientationObserver
 void GetCurrentScreenOrientation(dom::ScreenOrientation* aScreenOrientation);
 
 /**
  * Notify of a change in the screen orientation.
  * @param aScreenOrientation The new screen orientation.
  */
 void NotifyScreenOrientationChange(const dom::ScreenOrientation& aScreenOrientation);
 
+/**
+ * Lock the screen orientation to the specific orientation.
+ * @return Whether the lock has been accepted.
+ */
+bool LockScreenOrientation(const dom::ScreenOrientation& aOrientation);
+
+/**
+ * Unlock the screen orientation.
+ */
+void UnlockScreenOrientation();
+
 } // namespace MOZ_HAL_NAMESPACE
 } // namespace mozilla
 
 #ifdef MOZ_DEFINED_HAL_NAMESPACE
 # undef MOZ_DEFINED_HAL_NAMESPACE
 # undef MOZ_HAL_NAMESPACE
 #endif
 
--- a/hal/android/AndroidHal.cpp
+++ b/hal/android/AndroidHal.cpp
@@ -212,11 +212,22 @@ GetCurrentScreenOrientation(dom::ScreenO
     return;
   }
 
   dom::ScreenOrientationWrapper orientationWrapper;
   bridge->GetScreenOrientation(orientationWrapper);
   *aScreenOrientation = orientationWrapper.orientation;
 }
 
+bool
+LockScreenOrientation(const dom::ScreenOrientation& aOrientation)
+{
+  return false;
+}
+
+void
+UnlockScreenOrientation()
+{
+}
+
 } // hal_impl
 } // mozilla
 
--- a/hal/fallback/ScreenOrientationFallback.cpp
+++ b/hal/fallback/ScreenOrientationFallback.cpp
@@ -36,10 +36,21 @@ GetCurrentScreenOrientation(dom::ScreenO
   screenMgr->GetPrimaryScreen(getter_AddRefs(screen));
   screen->GetRect(&screenLeft, &screenTop, &screenWidth, &screenHeight);
 
   *aScreenOrientation = screenWidth >= screenHeight
                           ? dom::eScreenOrientation_LandscapePrimary
                           : dom::eScreenOrientation_PortraitPrimary;
 }
 
+bool
+LockScreenOrientation(const dom::ScreenOrientation& aOrientation)
+{
+  return false;
+}
+
+void
+UnlockScreenOrientation()
+{
+}
+
 } // hal_impl
 } // mozilla
--- a/hal/sandbox/PHal.ipdl
+++ b/hal/sandbox/PHal.ipdl
@@ -139,16 +139,19 @@ parent:
     DisableWakeLockNotifications();
     sync GetWakeLockInfo(nsString aTopic)
       returns (WakeLockInformation aWakeLockInfo);
 
     EnableScreenOrientationNotifications();
     DisableScreenOrientationNotifications();
     sync GetCurrentScreenOrientation()
       returns (ScreenOrientation aScreenOrientation);
+    sync LockScreenOrientation(ScreenOrientation aOrientation)
+      returns (bool allowed);
+    UnlockScreenOrientation();
 
 child:
     NotifySensorChange(SensorData aSensorData);
 
 parent:    
     EnableSensorNotifications(SensorType aSensor);
     DisableSensorNotifications(SensorType aSensor);
 
--- a/hal/sandbox/SandboxHal.cpp
+++ b/hal/sandbox/SandboxHal.cpp
@@ -106,16 +106,30 @@ DisableScreenOrientationNotifications()
 
 void
 GetCurrentScreenOrientation(ScreenOrientation* aScreenOrientation)
 {
   Hal()->SendGetCurrentScreenOrientation(aScreenOrientation);
 }
 
 bool
+LockScreenOrientation(const dom::ScreenOrientation& aOrientation)
+{
+  bool allowed;
+  Hal()->SendLockScreenOrientation(aOrientation, &allowed);
+  return allowed;
+}
+
+void
+UnlockScreenOrientation()
+{
+  Hal()->SendUnlockScreenOrientation();
+}
+
+bool
 GetScreenEnabled()
 {
   bool enabled = false;
   Hal()->SendGetScreenEnabled(&enabled);
   return enabled;
 }
 
 void
@@ -316,16 +330,30 @@ public:
   }
 
   NS_OVERRIDE virtual bool
   RecvGetCurrentScreenOrientation(ScreenOrientation* aScreenOrientation) {
     hal::GetCurrentScreenOrientation(aScreenOrientation);
     return true;
   }
 
+  NS_OVERRIDE virtual bool
+  RecvLockScreenOrientation(const dom::ScreenOrientation& aOrientation, bool* aAllowed)
+  {
+    *aAllowed = hal::LockScreenOrientation(aOrientation);
+    return true;
+  }
+
+  NS_OVERRIDE virtual bool
+  RecvUnlockScreenOrientation()
+  {
+    hal::UnlockScreenOrientation();
+    return true;
+  }
+
   void Notify(const ScreenOrientationWrapper& aScreenOrientation) {
     unused << SendNotifyScreenOrientationChange(aScreenOrientation.orientation);
   }
 
   NS_OVERRIDE virtual bool
   RecvGetScreenEnabled(bool *enabled)
   {
     *enabled = hal::GetScreenEnabled();