Web Inspector: Implement backend for device metrics emulation
authorapavlov@chromium.org <apavlov@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 2 Apr 2012 12:20:44 +0000 (12:20 +0000)
committerapavlov@chromium.org <apavlov@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 2 Apr 2012 12:20:44 +0000 (12:20 +0000)
https://bugs.webkit.org/show_bug.cgi?id=82827

Source/WebCore:

This change implements the inspector backend for the device metrics override feature,
as well as the respective InspectorClient capability detection.
When a navigation occurs in the override mode, the page auto-zooming ("fit width")
is initiated upon the first layout after the DOMContentLoaded event.

Reviewed by Pavel Feldman.

* inspector/Inspector.json:
* inspector/InspectorClient.h:
(WebCore::InspectorClient::canOverrideDeviceMetrics):
(WebCore::InspectorClient::overrideDeviceMetrics):
(WebCore::InspectorClient::autoZoomPageToFitWidth):
(InspectorClient):
* inspector/InspectorInstrumentation.cpp:
(WebCore::InspectorInstrumentation::didLayoutImpl):
* inspector/InspectorPageAgent.cpp:
(PageAgentState):
(WebCore::InspectorPageAgent::InspectorPageAgent):
(WebCore::InspectorPageAgent::restore):
(WebCore::InspectorPageAgent::disable):
(WebCore::InspectorPageAgent::canOverrideDeviceMetrics):
(WebCore):
(WebCore::InspectorPageAgent::setDeviceMetricsOverride):
(WebCore::InspectorPageAgent::domContentEventFired):
(WebCore::InspectorPageAgent::loadEventFired):
(WebCore::InspectorPageAgent::didLayout):
(WebCore::InspectorPageAgent::updateViewMetrics):
* inspector/InspectorPageAgent.h:
* inspector/InspectorState.cpp:
(WebCore::InspectorState::getDouble):
(WebCore):
* inspector/InspectorState.h:
(InspectorState):
(WebCore::InspectorState::setDouble):

LayoutTests:

Follow the Web Inspector Protocol change.

Reviewed by Pavel Feldman.

* inspector/styles/override-screen-size.html:

git-svn-id: http://svn.webkit.org/repository/webkit/trunk@112864 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/inspector/styles/override-screen-size.html
Source/WebCore/ChangeLog
Source/WebCore/inspector/Inspector.json
Source/WebCore/inspector/InspectorClient.h
Source/WebCore/inspector/InspectorInstrumentation.cpp
Source/WebCore/inspector/InspectorPageAgent.cpp
Source/WebCore/inspector/InspectorPageAgent.h
Source/WebCore/inspector/InspectorState.cpp
Source/WebCore/inspector/InspectorState.h

index 4034e36..555f30a 100644 (file)
@@ -1,3 +1,14 @@
+2012-03-31  Alexander Pavlov  <apavlov@chromium.org>
+
+        Web Inspector: Implement backend for device metrics emulation
+        https://bugs.webkit.org/show_bug.cgi?id=82827
+
+        Follow the Web Inspector Protocol change.
+
+        Reviewed by Pavel Feldman.
+
+        * inspector/styles/override-screen-size.html:
+
 2012-04-02  Sheriff Bot  <webkit.review.bot@gmail.com>
 
         Unreviewed, rolling out r112813 and r112837.
index 224800f..71c2b59 100644 (file)
@@ -77,7 +77,7 @@ function test()
         }
 
         // Disable overrides.
-        PageAgent.setScreenSizeOverride(0, 0, overrideCallback);
+        PageAgent.setDeviceMetricsOverride(0, 0, 1, overrideCallback);
     }
 
     function step2()
@@ -153,7 +153,7 @@ function test()
                 InspectorTest.addResult("Override: " + width + "x" + height + " => ERROR");
             userCallback();
         }
-        PageAgent.setScreenSizeOverride(width, height, callback);
+        PageAgent.setDeviceMetricsOverride(width, height, 1, callback);
     }
 
     function overrideAndDumpData(width, height, callback)
index b7c2aa8..02b2f63 100644 (file)
@@ -1,3 +1,43 @@
+2012-03-31  Alexander Pavlov  <apavlov@chromium.org>
+
+        Web Inspector: Implement backend for device metrics emulation
+        https://bugs.webkit.org/show_bug.cgi?id=82827
+
+        This change implements the inspector backend for the device metrics override feature,
+        as well as the respective InspectorClient capability detection.
+        When a navigation occurs in the override mode, the page auto-zooming ("fit width")
+        is initiated upon the first layout after the DOMContentLoaded event.
+
+        Reviewed by Pavel Feldman.
+
+        * inspector/Inspector.json:
+        * inspector/InspectorClient.h:
+        (WebCore::InspectorClient::canOverrideDeviceMetrics):
+        (WebCore::InspectorClient::overrideDeviceMetrics):
+        (WebCore::InspectorClient::autoZoomPageToFitWidth):
+        (InspectorClient):
+        * inspector/InspectorInstrumentation.cpp:
+        (WebCore::InspectorInstrumentation::didLayoutImpl):
+        * inspector/InspectorPageAgent.cpp:
+        (PageAgentState):
+        (WebCore::InspectorPageAgent::InspectorPageAgent):
+        (WebCore::InspectorPageAgent::restore):
+        (WebCore::InspectorPageAgent::disable):
+        (WebCore::InspectorPageAgent::canOverrideDeviceMetrics):
+        (WebCore):
+        (WebCore::InspectorPageAgent::setDeviceMetricsOverride):
+        (WebCore::InspectorPageAgent::domContentEventFired):
+        (WebCore::InspectorPageAgent::loadEventFired):
+        (WebCore::InspectorPageAgent::didLayout):
+        (WebCore::InspectorPageAgent::updateViewMetrics):
+        * inspector/InspectorPageAgent.h:
+        * inspector/InspectorState.cpp:
+        (WebCore::InspectorState::getDouble):
+        (WebCore):
+        * inspector/InspectorState.h:
+        (InspectorState):
+        (WebCore::InspectorState::setDouble):
+
 2012-04-02  Sheriff Bot  <webkit.review.bot@gmail.com>
 
         Unreviewed, rolling out r112813 and r112837.
index ba1d7bd..93d4f85 100644 (file)
                 "hidden": true
             },
             {
-                "name": "setScreenSizeOverride",
-                "description": "Overrides the values of window.screen.width, window.screen.height, window.innerWidth, window.innerHeight, and \"device-width\"/\"device-height\"-related CSS media query results",
+                "name": "canOverrideDeviceMetrics",
+                "description": "Checks whether <code>setDeviceMetricsOverride</code> can be invoked.",
+                "returns": [
+                    { "name": "result", "type": "boolean", "description": "If true, <code>setDeviceMetricsOverride</code> can safely be invoked on the agent." }
+                ],
+                "hidden": true
+            },
+            {
+                "name": "setDeviceMetricsOverride",
+                "description": "Overrides the values of device screen dimensions (window.screen.width, window.screen.height, window.innerWidth, window.innerHeight, and \"device-width\"/\"device-height\"-related CSS media query results) and the font scale factor.",
                 "parameters": [
                     { "name": "width", "type": "integer", "description": "Overriding width value in pixels (minimum 0, maximum 10000000). 0 disables the override." },
-                    { "name": "height", "type": "integer", "description": "Overriding height value in pixels (minimum 0, maximum 10000000). 0 disables the override." }
+                    { "name": "height", "type": "integer", "description": "Overriding height value in pixels (minimum 0, maximum 10000000). 0 disables the override." },
+                    { "name": "fontScaleFactor", "type": "number", "description": "Overriding font scale factor value (must be positive). 1 disables the override." }
                 ],
                 "hidden": true
             },
index e4da688..612acd6 100644 (file)
@@ -56,6 +56,14 @@ public:
     virtual bool canClearBrowserCookies() { return false; }
     virtual void clearBrowserCookies() { }
 
+    virtual bool canOverrideDeviceMetrics() { return false; }
+    virtual void overrideDeviceMetrics(int /*width*/, int /*height*/, float /*fontScaleFactor*/) {
+        // FIXME: Platforms may want to implement this (see https://bugs.webkit.org/show_bug.cgi?id=82886).
+    }
+    virtual void autoZoomPageToFitWidth() {
+        // FIXME: Platforms may want to implement this (see https://bugs.webkit.org/show_bug.cgi?id=82886).
+    }
+
     bool doDispatchMessageOnFrontendPage(Page* frontendPage, const String& message);
 };
 
index b8d841f..5a53160 100644 (file)
@@ -407,8 +407,14 @@ InspectorInstrumentationCookie InspectorInstrumentation::willLayoutImpl(Instrume
 
 void InspectorInstrumentation::didLayoutImpl(const InspectorInstrumentationCookie& cookie)
 {
+    if (!cookie.first)
+        return;
+
     if (InspectorTimelineAgent* timelineAgent = retrieveTimelineAgent(cookie))
         timelineAgent->didLayout();
+
+    if (InspectorPageAgent* pageAgent = cookie.first->inspectorPageAgent())
+        pageAgent->didLayout();
 }
 
 InspectorInstrumentationCookie InspectorInstrumentation::willLoadXHRImpl(InstrumentingAgents* instrumentingAgents, XMLHttpRequest* request)
index 4582e8b..90d4f24 100644 (file)
@@ -84,6 +84,7 @@ static const char pageAgentEnabled[] = "pageAgentEnabled";
 static const char pageAgentScriptsToEvaluateOnLoad[] = "pageAgentScriptsToEvaluateOnLoad";
 static const char pageAgentScreenWidthOverride[] = "pageAgentScreenWidthOverride";
 static const char pageAgentScreenHeightOverride[] = "pageAgentScreenHeightOverride";
+static const char pageAgentFontScaleFactorOverride[] = "pageAgentFontScaleFactorOverride";
 static const char showPaintRects[] = "showPaintRects";
 }
 
@@ -310,8 +311,8 @@ InspectorPageAgent::InspectorPageAgent(InstrumentingAgents* instrumentingAgents,
     , m_client(client)
     , m_frontend(0)
     , m_lastScriptIdentifier(0)
-    , m_originalUseFixedLayout(false)
     , m_lastPaintContext(0)
+    , m_didLoadEventFire(false)
 {
 }
 
@@ -336,7 +337,8 @@ void InspectorPageAgent::restore()
         // When restoring the agent, override values are restored into the FrameView.
         int width = static_cast<int>(m_state->getLong(PageAgentState::pageAgentScreenWidthOverride));
         int height = static_cast<int>(m_state->getLong(PageAgentState::pageAgentScreenHeightOverride));
-        updateFrameViewFixedLayout(width, height);
+        double fontScaleFactor = m_state->getDouble(PageAgentState::pageAgentFontScaleFactorOverride);
+        updateViewMetrics(width, height, fontScaleFactor);
     }
 }
 
@@ -354,7 +356,8 @@ void InspectorPageAgent::disable(ErrorString*)
     // When disabling the agent, reset the override values.
     m_state->setLong(PageAgentState::pageAgentScreenWidthOverride, 0);
     m_state->setLong(PageAgentState::pageAgentScreenHeightOverride, 0);
-    updateFrameViewFixedLayout(0, 0);
+    m_state->setDouble(PageAgentState::pageAgentFontScaleFactorOverride, 1);
+    updateViewMetrics(0, 0, 1);
 }
 
 void InspectorPageAgent::addScriptToEvaluateOnLoad(ErrorString*, const String& source, String* identifier)
@@ -636,7 +639,12 @@ void InspectorPageAgent::setDocumentContent(ErrorString* errorString, const Stri
     DOMPatchSupport::patchDocument(document, html);
 }
 
-void InspectorPageAgent::setScreenSizeOverride(ErrorString* errorString, const int width, const int height)
+void InspectorPageAgent::canOverrideDeviceMetrics(ErrorString*, bool* result)
+{
+    *result = m_client->canOverrideDeviceMetrics();
+}
+
+void InspectorPageAgent::setDeviceMetricsOverride(ErrorString* errorString, const int width, const int height, double fontScaleFactor)
 {
     const static long maxDimension = 10000000;
 
@@ -645,17 +653,29 @@ void InspectorPageAgent::setScreenSizeOverride(ErrorString* errorString, const i
         return;
     }
 
+    if (!width ^ !height) {
+        *errorString = "Both width and height must be either zero or non-zero at once";
+        return;
+    }
+
+    if (fontScaleFactor <= 0) {
+        *errorString = "fontScaleFactor must be positive";
+        return;
+    }
+
     // These two always fit an int.
     int currentWidth = static_cast<int>(m_state->getLong(PageAgentState::pageAgentScreenWidthOverride));
     int currentHeight = static_cast<int>(m_state->getLong(PageAgentState::pageAgentScreenHeightOverride));
+    double currentFontScaleFactor = m_state->getDouble(PageAgentState::pageAgentFontScaleFactorOverride);
 
-    if (width == currentWidth && height == currentHeight)
+    if (width == currentWidth && height == currentHeight && fontScaleFactor == currentFontScaleFactor)
         return;
 
     m_state->setLong(PageAgentState::pageAgentScreenWidthOverride, width);
     m_state->setLong(PageAgentState::pageAgentScreenHeightOverride, height);
+    m_state->setDouble(PageAgentState::pageAgentFontScaleFactorOverride, fontScaleFactor);
 
-    updateFrameViewFixedLayout(width, height);
+    updateViewMetrics(width, height, fontScaleFactor);
 }
 
 void InspectorPageAgent::setShowPaintRects(ErrorString*, bool show)
@@ -691,12 +711,13 @@ void InspectorPageAgent::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWor
 
 void InspectorPageAgent::domContentEventFired()
 {
-     m_frontend->domContentEventFired(currentTime());
+    m_didLoadEventFire = true;
+    m_frontend->domContentEventFired(currentTime());
 }
 
 void InspectorPageAgent::loadEventFired()
 {
-     m_frontend->loadEventFired(currentTime());
+    m_frontend->loadEventFired(currentTime());
 }
 
 void InspectorPageAgent::frameNavigated(DocumentLoader* loader)
@@ -820,6 +841,19 @@ void InspectorPageAgent::didPaint()
     m_lastPaintContext = 0;
 }
 
+void InspectorPageAgent::didLayout()
+{
+    if (!m_didLoadEventFire)
+        return;
+
+    m_didLoadEventFire = false;
+    int currentWidth = static_cast<int>(m_state->getLong(PageAgentState::pageAgentScreenWidthOverride));
+    int currentHeight = static_cast<int>(m_state->getLong(PageAgentState::pageAgentScreenHeightOverride));
+
+    if (currentWidth && currentHeight)
+        m_client->autoZoomPageToFitWidth();
+}
+
 PassRefPtr<TypeBuilder::Page::Frame> InspectorPageAgent::buildObjectForFrame(Frame* frame)
 {
     RefPtr<TypeBuilder::Page::Frame> frameObject = TypeBuilder::Page::Frame::create()
@@ -875,40 +909,15 @@ PassRefPtr<TypeBuilder::Page::FrameResourceTree> InspectorPageAgent::buildObject
     return result;
 }
 
-void InspectorPageAgent::updateFrameViewFixedLayout(int width, int height)
+void InspectorPageAgent::updateViewMetrics(int width, int height, double fontScaleFactor)
 {
-    if (!width && !height)
-        clearFrameViewFixedLayout();
-    else
-        setFrameViewFixedLayout(width, height);
+    m_client->overrideDeviceMetrics(width, height, static_cast<float>(fontScaleFactor));
 
     Document* document = mainFrame()->document();
     document->styleSelectorChanged(RecalcStyleImmediately);
     InspectorInstrumentation::mediaQueryResultChanged(document);
 }
 
-void InspectorPageAgent::clearFrameViewFixedLayout()
-{
-    if (m_originalFixedLayoutSize) {
-        // Turning off existing overrides (no-op otherwise) - revert the affected FrameView to the remembered fixed layout settings.
-        mainFrame()->view()->setFixedLayoutSize(*m_originalFixedLayoutSize);
-        m_originalFixedLayoutSize.clear();
-        mainFrame()->view()->setUseFixedLayout(m_originalUseFixedLayout);
-    }
-}
-
-void InspectorPageAgent::setFrameViewFixedLayout(int width, int height)
-{
-    if (!m_originalFixedLayoutSize) {
-        // Turning on the overrides (none currently exist) - remember existing fixed layout for the affected FrameView.
-        m_originalFixedLayoutSize = adoptPtr(new IntSize(mainFrame()->view()->fixedLayoutSize()));
-        m_originalUseFixedLayout = mainFrame()->view()->useFixedLayout();
-    }
-
-    mainFrame()->view()->setFixedLayoutSize(IntSize(width, height));
-    mainFrame()->view()->setUseFixedLayout(true);
-}
-
 } // namespace WebCore
 
 #endif // ENABLE(INSPECTOR)
index 777347a..43e152d 100644 (file)
@@ -102,7 +102,8 @@ public:
     virtual void searchInResource(ErrorString*, const String& frameId, const String& url, const String& query, const bool* optionalCaseSensitive, const bool* optionalIsRegex, RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchMatch> >&);
     virtual void searchInResources(ErrorString*, const String&, const bool* caseSensitive, const bool* isRegex, RefPtr<TypeBuilder::Array<TypeBuilder::Page::SearchResult> >&);
     virtual void setDocumentContent(ErrorString*, const String& frameId, const String& html);
-    virtual void setScreenSizeOverride(ErrorString*, int width, int height);
+    virtual void canOverrideDeviceMetrics(ErrorString*, bool*);
+    virtual void setDeviceMetricsOverride(ErrorString*, int width, int height, double fontScaleFactor);
     virtual void setShowPaintRects(ErrorString*, bool show);
 
     // InspectorInstrumentation API
@@ -116,6 +117,7 @@ public:
     void applyScreenHeightOverride(long*);
     void willPaint(GraphicsContext*, const LayoutRect&);
     void didPaint();
+    void didLayout();
 
     // Inspector Controller API
     virtual void setFrontend(InspectorFrontend*);
@@ -133,9 +135,7 @@ public:
 
 private:
     InspectorPageAgent(InstrumentingAgents*, Page*, InspectorState*, InjectedScriptManager*, InspectorClient*);
-    void updateFrameViewFixedLayout(int, int);
-    void setFrameViewFixedLayout(int, int);
-    void clearFrameViewFixedLayout();
+    void updateViewMetrics(int, int, double);
 
     PassRefPtr<TypeBuilder::Page::Frame> buildObjectForFrame(Frame*);
     PassRefPtr<TypeBuilder::Page::FrameResourceTree> buildObjectForFrameTree(Frame*);
@@ -149,10 +149,9 @@ private:
     HashMap<Frame*, String> m_frameToIdentifier;
     HashMap<String, Frame*> m_identifierToFrame;
     HashMap<DocumentLoader*, String> m_loaderToIdentifier;
-    OwnPtr<IntSize> m_originalFixedLayoutSize;
-    bool m_originalUseFixedLayout;
     GraphicsContext* m_lastPaintContext;
     LayoutRect m_lastPaintRect;
+    bool m_didLoadEventFire;
 };
 
 
index 674f866..7e02eb7 100644 (file)
@@ -102,6 +102,15 @@ long InspectorState::getLong(const String& propertyName)
     return value;
 }
 
+double InspectorState::getDouble(const String& propertyName)
+{
+    InspectorObject::iterator it = m_properties->find(propertyName);
+    double value = 0;
+    if (it != m_properties->end())
+        it->second->asNumber(&value);
+    return value;
+}
+
 PassRefPtr<InspectorObject> InspectorState::getObject(const String& propertyName)
 {
     InspectorObject::iterator it = m_properties->find(propertyName);
index ab12aae..a40e350 100644 (file)
@@ -56,11 +56,13 @@ public:
     bool getBoolean(const String& propertyName);
     String getString(const String& propertyName);
     long getLong(const String& propertyName);
+    double getDouble(const String& propertyName);
     PassRefPtr<InspectorObject> getObject(const String& propertyName);
 
     void setBoolean(const String& propertyName, bool value) { setValue(propertyName, InspectorBasicValue::create(value)); }
     void setString(const String& propertyName, const String& value) { setValue(propertyName, InspectorString::create(value)); }
     void setLong(const String& propertyName, long value) { setValue(propertyName, InspectorBasicValue::create((double)value)); }
+    void setDouble(const String& propertyName, double value) { setValue(propertyName, InspectorBasicValue::create(value)); }
     void setObject(const String& propertyName, PassRefPtr<InspectorObject> value) { setValue(propertyName, value); }
 
 private: