Bug 1369815 - Add display mode to nsIDocShell and use it for media queries r=heycam draft
authorJames Willcox <snorp@snorp.net>
Wed, 09 Aug 2017 08:54:06 -0500
changeset 644222 ece86a8f06cbb8cdec337456a03d7e133a824a8f
parent 644221 1730364a43b6e1028588f94832da67ae6069d2be
child 644223 4ebb62b66671bf2e487d185fa633b255e24ee0a7
child 644302 1ccce4d53dd0c7dede5b910fceb5ad4739e7e78b
child 644328 0d903cef39931e6ab8330aef354eef809fe7a2f6
push id73351
push userbmo:snorp@snorp.net
push dateThu, 10 Aug 2017 17:15:02 +0000
reviewersheycam
bugs1369815
milestone57.0a1
Bug 1369815 - Add display mode to nsIDocShell and use it for media queries r=heycam MozReview-Commit-ID: EVT0fTk6GfC
docshell/base/nsDocShell.cpp
docshell/base/nsDocShell.h
docshell/base/nsIDocShell.idl
layout/style/nsMediaFeatures.cpp
--- a/docshell/base/nsDocShell.cpp
+++ b/docshell/base/nsDocShell.cpp
@@ -835,16 +835,17 @@ nsDocShell::nsDocShell()
   , mBlankTiming(false)
   , mCreatingDocument(false)
 #ifdef DEBUG
   , mInEnsureScriptEnv(false)
 #endif
   , mDefaultLoadFlags(nsIRequest::LOAD_NORMAL)
   , mFrameType(FRAME_TYPE_REGULAR)
   , mPrivateBrowsingId(0)
+  , mDisplayMode(nsIDocShell::DISPLAY_MODE_BROWSER)
   , mForcedCharset(nullptr)
   , mParentCharset(nullptr)
   , mParentCharsetSource(0)
   , mJSRunToCompletionDepth(0)
   , mTouchEventsOverride(nsIDocShell::TOUCHEVENTS_OVERRIDE_NONE)
 {
   AssertOriginAttributesMatchPrivateBrowsing();
 
@@ -15058,8 +15059,30 @@ nsDocShell::GetAwaitingLargeAlloc(bool* 
   return NS_OK;
 }
 
 NS_IMETHODIMP_(void)
 nsDocShell::GetOriginAttributes(mozilla::OriginAttributes& aAttrs)
 {
   aAttrs = mOriginAttributes;
 }
+
+NS_IMETHODIMP
+nsDocShell::GetDisplayMode(uint32_t* aDisplayMode)
+{
+  NS_ENSURE_ARG_POINTER(aDisplayMode);
+  *aDisplayMode = mDisplayMode;
+  return NS_OK;
+}
+
+NS_IMETHODIMP
+nsDocShell::SetDisplayMode(uint32_t aDisplayMode)
+{
+  if (!(aDisplayMode == nsIDocShell::DISPLAY_MODE_BROWSER ||
+        aDisplayMode == nsIDocShell::DISPLAY_MODE_STANDALONE ||
+        aDisplayMode == nsIDocShell::DISPLAY_MODE_FULLSCREEN ||
+        aDisplayMode == nsIDocShell::DISPLAY_MODE_MINIMAL_UI)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  mDisplayMode = aDisplayMode;
+  return NS_OK;
+}
\ No newline at end of file
--- a/docshell/base/nsDocShell.h
+++ b/docshell/base/nsDocShell.h
@@ -1045,16 +1045,18 @@ protected:
   // Currently treated as a binary value: 1 - in private mode, 0 - not private mode
   // On content docshells mPrivateBrowsingId == mOriginAttributes.mPrivateBrowsingId
   // On chrome docshells this value will be set, but not have the corresponding
   // origin attribute set.
   uint32_t mPrivateBrowsingId;
 
   nsString mInterceptedDocumentId;
 
+  uint32_t mDisplayMode;
+
 private:
   const Encoding* mForcedCharset;
   const Encoding* mParentCharset;
   int32_t mParentCharsetSource;
   nsCOMPtr<nsIPrincipal> mParentCharsetPrincipal;
   nsTObserverArray<nsWeakPtr> mPrivacyObservers;
   nsTObserverArray<nsWeakPtr> mReflowObservers;
   nsTObserverArray<nsWeakPtr> mScrollObservers;
--- a/docshell/base/nsIDocShell.idl
+++ b/docshell/base/nsIDocShell.idl
@@ -1149,9 +1149,19 @@ interface nsIDocShell : nsIDocShellTreeI
    * Attribute that determines whether tracking protection is enabled.
    */
   attribute boolean useTrackingProtection;
 
  /**
   * Fire a dummy location change event asynchronously.
   */
   [noscript] void dispatchLocationChangeEvent();
+
+  const unsigned long DISPLAY_MODE_BROWSER = 1;
+  const unsigned long DISPLAY_MODE_STANDALONE = 2;
+  const unsigned long DISPLAY_MODE_FULLSCREEN = 3;
+  const unsigned long DISPLAY_MODE_MINIMAL_UI = 4;
+
+  /**
+   * Display mode for this document. Defaults to DISPLAY_MODE_BROWSER
+   */
+  attribute unsigned long displayMode;
 };
--- a/layout/style/nsMediaFeatures.cpp
+++ b/layout/style/nsMediaFeatures.cpp
@@ -13,16 +13,17 @@
 #include "nsPresContext.h"
 #include "nsCSSValue.h"
 #ifdef XP_WIN
 #include "mozilla/LookAndFeel.h"
 #endif
 #include "nsCSSRuleProcessor.h"
 #include "nsDeviceContext.h"
 #include "nsIBaseWindow.h"
+#include "nsIDocShell.h"
 #include "nsIDocument.h"
 #include "nsIWidget.h"
 #include "nsContentUtils.h"
 #include "mozilla/StyleSheet.h"
 #include "mozilla/StyleSheetInlines.h"
 
 using namespace mozilla;
 
@@ -299,43 +300,63 @@ GetScan(nsPresContext* aPresContext, con
   aResult.Reset();
 }
 
 static void
 GetDisplayMode(nsPresContext* aPresContext, const nsMediaFeature*,
                nsCSSValue& aResult)
 {
   nsCOMPtr<nsISupports> container;
+  RefPtr<nsIDocShell> docShell;
+
+  if (!aPresContext) {
+    return;
+  }
+
   if (aPresContext) {
     // Calling GetRootPresContext() can be slow, so make sure to call it
     // just once.
     nsRootPresContext* root = aPresContext->GetRootPresContext();
     if (root && root->Document()) {
       container = root->Document()->GetContainer();
+      docShell = root->GetDocShell();
+    }
+  }
+
+  nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(container);
+  if (baseWindow) {
+    nsCOMPtr<nsIWidget> mainWidget;
+    baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
+    nsSizeMode mode = mainWidget ? mainWidget->SizeMode() : nsSizeMode_Normal;
+
+    if (mode == nsSizeMode_Fullscreen) {
+      aResult.SetIntValue(NS_STYLE_DISPLAY_MODE_FULLSCREEN, eCSSUnit_Enumerated);
+      return;
     }
   }
-  nsCOMPtr<nsIBaseWindow> baseWindow = do_QueryInterface(container);
-  if (!baseWindow) {
-    aResult.SetIntValue(NS_STYLE_DISPLAY_MODE_BROWSER, eCSSUnit_Enumerated);
-    return;
+
+  int32_t displayMode = NS_STYLE_DISPLAY_MODE_BROWSER;
+  uint32_t mode = nsIDocShell::DISPLAY_MODE_BROWSER;
+  if (docShell) {
+    docShell->GetDisplayMode(&mode);
   }
-  nsCOMPtr<nsIWidget> mainWidget;
-  baseWindow->GetMainWidget(getter_AddRefs(mainWidget));
-  int32_t displayMode;
-  nsSizeMode mode = mainWidget ? mainWidget->SizeMode() : nsSizeMode_Normal;
-  // Background tabs are always in 'browser' mode for now.
-  // If new modes are supported, please ensure not cause the regression in
-  // Bug 1259641.
+
   switch (mode) {
-    case nsSizeMode_Fullscreen:
+    case nsIDocShell::DISPLAY_MODE_STANDALONE:
+      displayMode = NS_STYLE_DISPLAY_MODE_STANDALONE;
+      break;
+    case nsIDocShell::DISPLAY_MODE_FULLSCREEN:
       displayMode = NS_STYLE_DISPLAY_MODE_FULLSCREEN;
       break;
+    case nsIDocShell::DISPLAY_MODE_MINIMAL_UI:
+      displayMode = NS_STYLE_DISPLAY_MODE_MINIMAL_UI;
+      break;
+    case nsIDocShell::DISPLAY_MODE_BROWSER:
     default:
       displayMode = NS_STYLE_DISPLAY_MODE_BROWSER;
-      break;
   }
 
   aResult.SetIntValue(displayMode, eCSSUnit_Enumerated);
 }
 
 static void
 GetGrid(nsPresContext* aPresContext, const nsMediaFeature*,
         nsCSSValue& aResult)