Cherry-pick: [Android WebView] Add a compatibility quirk for returning screen sizes in physical pixels

Cherry-pick of Blink http://crrev.com/26373006

Bug: 11026947

Original description:

[Android WebView] Add a compatibility quirk for returning screen sizes in physical pixels

This enables the browser to return screen.width / availWidth (and height),
@media device-width (and height), window.outerWidth (and height),
but not window.innerWidth (and height) in physical rather than CSS
pixels.

BUG=305236
Committed: https://src.chromium.org/viewvc/blink?view=rev&revision=159299

Change-Id: Ib0f4e7dafd696c2188cc84cb61d2040484d2d4f9
diff --git a/Source/core/css/MediaQueryEvaluator.cpp b/Source/core/css/MediaQueryEvaluator.cpp
index 413c80e..327f6e0 100644
--- a/Source/core/css/MediaQueryEvaluator.cpp
+++ b/Source/core/css/MediaQueryEvaluator.cpp
@@ -365,6 +365,8 @@
         FloatRect sg = screenRect(frame->page()->mainFrame()->view());
         int length;
         long height = sg.height();
+        if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+            height = lroundf(height * frame->page()->deviceScaleFactor());
         InspectorInstrumentation::applyScreenHeightOverride(frame, &height);
         return computeLength(value, !frame->document()->inQuirksMode(), style, length) && compareValue(static_cast<int>(height), length, op);
     }
@@ -379,6 +381,8 @@
         FloatRect sg = screenRect(frame->page()->mainFrame()->view());
         int length;
         long width = sg.width();
+        if (frame->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+            width = lroundf(width * frame->page()->deviceScaleFactor());
         InspectorInstrumentation::applyScreenWidthOverride(frame, &width);
         return computeLength(value, !frame->document()->inQuirksMode(), style, length) && compareValue(static_cast<int>(width), length, op);
     }
diff --git a/Source/core/page/DOMWindow.cpp b/Source/core/page/DOMWindow.cpp
index 2bbe490..ad6f4bd 100644
--- a/Source/core/page/DOMWindow.cpp
+++ b/Source/core/page/DOMWindow.cpp
@@ -963,6 +963,8 @@
     if (!page)
         return 0;
 
+    if (page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        return lroundf(page->chrome().windowRect().height() * page->deviceScaleFactor());
     return static_cast<int>(page->chrome().windowRect().height());
 }
 
@@ -975,6 +977,8 @@
     if (!page)
         return 0;
 
+    if (page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        return lroundf(page->chrome().windowRect().width() * page->deviceScaleFactor());
     return static_cast<int>(page->chrome().windowRect().width());
 }
 
@@ -1023,6 +1027,8 @@
     if (!page)
         return 0;
 
+    if (page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        return lroundf(page->chrome().windowRect().x() * page->deviceScaleFactor());
     return static_cast<int>(page->chrome().windowRect().x());
 }
 
@@ -1035,6 +1041,8 @@
     if (!page)
         return 0;
 
+    if (page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        return lroundf(page->chrome().windowRect().y() * page->deviceScaleFactor());
     return static_cast<int>(page->chrome().windowRect().y());
 }
 
diff --git a/Source/core/page/Screen.cpp b/Source/core/page/Screen.cpp
index 7b7b4d3..801401c 100644
--- a/Source/core/page/Screen.cpp
+++ b/Source/core/page/Screen.cpp
@@ -33,6 +33,8 @@
 #include "core/inspector/InspectorInstrumentation.h"
 #include "core/page/Frame.h"
 #include "core/page/FrameView.h"
+#include "core/page/Page.h"
+#include "core/page/Settings.h"
 #include "core/platform/PlatformScreen.h"
 #include "core/platform/graphics/FloatRect.h"
 
@@ -49,6 +51,9 @@
     if (!m_frame)
         return 0;
     long height = static_cast<long>(screenRect(m_frame->view()).height());
+    Page* page = m_frame->page();
+    if (page && page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        height = lroundf(height * page->deviceScaleFactor());
     InspectorInstrumentation::applyScreenHeightOverride(m_frame, &height);
     return static_cast<unsigned>(height);
 }
@@ -58,6 +63,9 @@
     if (!m_frame)
         return 0;
     long width = static_cast<long>(screenRect(m_frame->view()).width());
+    Page* page = m_frame->page();
+    if (page && page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        width = lroundf(width * page->deviceScaleFactor());
     InspectorInstrumentation::applyScreenWidthOverride(m_frame, &width);
     return static_cast<unsigned>(width);
 }
@@ -80,6 +88,9 @@
 {
     if (!m_frame)
         return 0;
+    Page* page = m_frame->page();
+    if (page && page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        return lroundf(screenAvailableRect(m_frame->view()).x() * page->deviceScaleFactor());
     return static_cast<int>(screenAvailableRect(m_frame->view()).x());
 }
 
@@ -87,6 +98,9 @@
 {
     if (!m_frame)
         return 0;
+    Page* page = m_frame->page();
+    if (page && page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        return lroundf(screenAvailableRect(m_frame->view()).y() * page->deviceScaleFactor());
     return static_cast<int>(screenAvailableRect(m_frame->view()).y());
 }
 
@@ -94,6 +108,9 @@
 {
     if (!m_frame)
         return 0;
+    Page* page = m_frame->page();
+    if (page && page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        return lroundf(screenAvailableRect(m_frame->view()).height() * page->deviceScaleFactor());
     return static_cast<unsigned>(screenAvailableRect(m_frame->view()).height());
 }
 
@@ -101,6 +118,9 @@
 {
     if (!m_frame)
         return 0;
+    Page* page = m_frame->page();
+    if (page && page->settings()->reportScreenSizeInPhysicalPixelsQuirk())
+        return lroundf(screenAvailableRect(m_frame->view()).width() * page->deviceScaleFactor());
     return static_cast<unsigned>(screenAvailableRect(m_frame->view()).width());
 }
 
diff --git a/Source/core/page/Settings.in b/Source/core/page/Settings.in
index 75d0fab..5689692 100644
--- a/Source/core/page/Settings.in
+++ b/Source/core/page/Settings.in
@@ -140,6 +140,9 @@
 viewportMetaZeroValuesQuirk initial=false
 # Another Android SDK <= 18 quirk, removable 2015.  http://crbug.com/295287
 ignoreMainFrameOverflowHiddenQuirk initial=false
+# Yet another Android SDK <= 18 quirk, removable 2015.
+# See http://crbug.com/305236
+reportScreenSizeInPhysicalPixelsQuirk initial=false
 # This quirk is to maintain compatibility with Android apps.
 # It will be possible to remove it once WebSettings.{get|set}UseWideViewPort
 # API function will be removed. See http://crbug.com/288037.
diff --git a/Source/web/WebSettingsImpl.cpp b/Source/web/WebSettingsImpl.cpp
index abb7b3c..57c5be3 100644
--- a/Source/web/WebSettingsImpl.cpp
+++ b/Source/web/WebSettingsImpl.cpp
@@ -198,6 +198,11 @@
     m_settings->setIgnoreMainFrameOverflowHiddenQuirk(ignoreMainFrameOverflowHiddenQuirk);
 }
 
+void WebSettingsImpl::setReportScreenSizeInPhysicalPixelsQuirk(bool reportScreenSizeInPhysicalPixelsQuirk)
+{
+    m_settings->setReportScreenSizeInPhysicalPixelsQuirk(reportScreenSizeInPhysicalPixelsQuirk);
+}
+
 void WebSettingsImpl::setSupportsMultipleWindows(bool supportsMultipleWindows)
 {
     m_settings->setSupportsMultipleWindows(supportsMultipleWindows);
diff --git a/Source/web/WebSettingsImpl.h b/Source/web/WebSettingsImpl.h
index ad329e1..a943390 100644
--- a/Source/web/WebSettingsImpl.h
+++ b/Source/web/WebSettingsImpl.h
@@ -133,6 +133,7 @@
     virtual void setPluginsEnabled(bool);
     virtual void setPrivilegedWebGLExtensionsEnabled(bool);
     virtual void setRenderVSyncNotificationEnabled(bool);
+    virtual void setReportScreenSizeInPhysicalPixelsQuirk(bool);
     virtual void setSansSerifFontFamily(const WebString&, UScriptCode = USCRIPT_COMMON);
     virtual void setSelectTrailingWhitespaceEnabled(bool);
     virtual void setSelectionIncludesAltImageText(bool);
diff --git a/public/web/WebSettings.h b/public/web/WebSettings.h
index 9a69c1b..b8e0363 100644
--- a/public/web/WebSettings.h
+++ b/public/web/WebSettings.h
@@ -140,6 +140,7 @@
     virtual void setPluginsEnabled(bool) = 0;
     virtual void setPrivilegedWebGLExtensionsEnabled(bool) = 0;
     virtual void setRenderVSyncNotificationEnabled(bool) = 0;
+    virtual void setReportScreenSizeInPhysicalPixelsQuirk(bool) = 0;
     virtual void setSansSerifFontFamily(const WebString&, UScriptCode = USCRIPT_COMMON) = 0;
     virtual void setSelectTrailingWhitespaceEnabled(bool) = 0;
     virtual void setSelectionIncludesAltImageText(bool) = 0;