Bug 1196290 - Do not update orientation lock when app docshell is activated. r=smaug
authorWilliam Chen <wchen@mozilla.com>
Thu, 20 Aug 2015 17:25:05 -0700
changeset 287176 bc43f663e5e16b7a5e5a6d77b4a96b7c27accafa
parent 287175 25d46daf76f71e0d81242e6fbf020d45dfefc055
child 287177 bdb54665c61c46ab4770f921dd3385d771989496
push id4660
push usermartin.thomson@gmail.com
push dateFri, 21 Aug 2015 22:37:38 +0000
reviewerssmaug
bugs1196290
milestone43.0a1
Bug 1196290 - Do not update orientation lock when app docshell is activated. r=smaug
docshell/base/nsDocShell.cpp
dom/base/nsScreen.cpp
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -5931,17 +5931,20 @@ nsDocShell::SetIsActive(bool aIsActive)
   if (pshell) {
     pshell->SetIsActive(aIsActive);
   }
 
   // Tell the window about it
   if (mScriptGlobal) {
     mScriptGlobal->SetIsBackground(!aIsActive);
     if (nsCOMPtr<nsIDocument> doc = mScriptGlobal->GetExtantDoc()) {
-      if (aIsActive) {
+      // Update orientation when the top-level browsing context becomes active.
+      // We make an exception for apps because they currently rely on
+      // orientation locks persisting across browsing contexts.
+      if (aIsActive && !GetIsApp()) {
         nsCOMPtr<nsIDocShellTreeItem> parent;
         GetSameTypeParent(getter_AddRefs(parent));
         if (!parent) {
           // We only care about the top-level browsing context.
           uint16_t orientation = OrientationLock();
           ScreenOrientation::UpdateActiveOrientationLock(orientation);
         }
       }
@@ -10066,17 +10069,19 @@ nsDocShell::InternalLoad(nsIURI* aURI,
   if (mTiming && timeBeforeUnload) {
     mTiming->NotifyUnloadAccepted(mCurrentURI);
   }
 
   // Whenever a top-level browsing context is navigated, the user agent MUST
   // lock the orientation of the document to the document's default
   // orientation. We don't explicitly check for a top-level browsing context
   // here because orientation is only set on top-level browsing contexts.
-  if (OrientationLock() != eScreenOrientation_None) {
+  // We make an exception for apps because they currently rely on
+  // orientation locks persisting across browsing contexts.
+  if (OrientationLock() != eScreenOrientation_None && !GetIsApp()) {
 #ifdef DEBUG
     nsCOMPtr<nsIDocShellTreeItem> parent;
     GetSameTypeParent(getter_AddRefs(parent));
     MOZ_ASSERT(!parent);
 #endif
     SetOrientationLock(eScreenOrientation_None);
     if (mIsActive) {
       ScreenOrientation::UpdateActiveOrientationLock(eScreenOrientation_None);
--- a/dom/base/nsScreen.cpp
+++ b/dom/base/nsScreen.cpp
@@ -192,16 +192,39 @@ NS_IMETHODIMP
 nsScreen::GetSlowMozOrientation(nsAString& aOrientation)
 {
   nsString orientation;
   GetMozOrientation(orientation);
   aOrientation = orientation;
   return NS_OK;
 }
 
+static void
+UpdateDocShellOrientationLock(nsPIDOMWindow* aWindow,
+                              ScreenOrientationInternal aOrientation)
+{
+  if (!aWindow) {
+    return;
+  }
+
+  nsCOMPtr<nsIDocShell> docShell = aWindow->GetDocShell();
+  if (!docShell) {
+    return;
+  }
+
+  nsCOMPtr<nsIDocShellTreeItem> root;
+  docShell->GetSameTypeRootTreeItem(getter_AddRefs(root));
+  nsCOMPtr<nsIDocShell> rootShell(do_QueryInterface(root));
+  if (!rootShell) {
+    return;
+  }
+
+  rootShell->SetOrientationLock(aOrientation);
+}
+
 bool
 nsScreen::MozLockOrientation(const nsAString& aOrientation, ErrorResult& aRv)
 {
   nsString orientation(aOrientation);
   Sequence<nsString> orientations;
   if (!orientations.AppendElement(orientation, fallible)) {
     aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
     return false;
@@ -240,29 +263,32 @@ nsScreen::MozLockOrientation(const Seque
       return false;
     }
   }
 
   switch (mScreenOrientation->GetLockOrientationPermission(false)) {
     case ScreenOrientation::LOCK_DENIED:
       return false;
     case ScreenOrientation::LOCK_ALLOWED:
+      UpdateDocShellOrientationLock(GetOwner(), orientation);
       return mScreenOrientation->LockDeviceOrientation(orientation, false, aRv);
     case ScreenOrientation::FULLSCREEN_LOCK_ALLOWED:
+      UpdateDocShellOrientationLock(GetOwner(), orientation);
       return mScreenOrientation->LockDeviceOrientation(orientation, true, aRv);
   }
 
   // This is only for compilers that don't understand that the previous switch
   // will always return.
   MOZ_CRASH("unexpected lock orientation permission value");
 }
 
 void
 nsScreen::MozUnlockOrientation()
 {
+  UpdateDocShellOrientationLock(GetOwner(), eScreenOrientation_None);
   mScreenOrientation->UnlockDeviceOrientation();
 }
 
 bool
 nsScreen::IsDeviceSizePageSize()
 {
   nsPIDOMWindow* owner = GetOwner();
   if (owner) {