Title: [98328] trunk
Revision
98328
Author
ca...@chromium.org
Date
2011-10-25 02:25:37 -0700 (Tue, 25 Oct 2011)

Log Message

Web Inspector: [Extensions API] allow extensions to specify script to be injected on reload
https://bugs.webkit.org/show_bug.cgi?id=70600

Reviewed by Pavel Feldman.

- change webInspector.inspectedPage.reload() to accept multiple options in an object
- add an ability to inject a script upon a page reload
- return script id in addScriptToEvaluateOnLoad()
- provide removeScriptToEvaluateOnLoad()
- store scripts to evaluate on load in inspector state cookie

Source/WebCore:

Test: inspector/extensions/extensions-reload.html

* inspector/Inspector.json:
* inspector/InspectorPageAgent.cpp:
(WebCore::InspectorPageAgent::InspectorPageAgent):
(WebCore::InspectorPageAgent::restore):
(WebCore::InspectorPageAgent::addScriptToEvaluateOnLoad):
(WebCore::InspectorPageAgent::removeScriptToEvaluateOnLoad):
(WebCore::InspectorPageAgent::removeAllScriptsToEvaluateOnLoad):
(WebCore::InspectorPageAgent::didClearWindowObjectInWorld):
(WebCore::InspectorPageAgent::frameNavigated):
* inspector/InspectorPageAgent.h:
* inspector/front-end/ExtensionAPI.js:
(injectedExtensionAPI.InspectedWindow.prototype.reload):
* inspector/front-end/ExtensionServer.js:
(WebInspector.ExtensionServer.prototype._onReload):
* inspector/front-end/WorkersSidebarPane.js:
(WebInspector.WorkersSidebarPane.prototype.setInstrumentation.callback):
(WebInspector.WorkersSidebarPane.prototype.setInstrumentation):

LayoutTests:

* inspector/extensions/extensions-reload-expected.txt: Added.
* inspector/extensions/extensions-reload.html: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (98327 => 98328)


--- trunk/LayoutTests/ChangeLog	2011-10-25 08:37:35 UTC (rev 98327)
+++ trunk/LayoutTests/ChangeLog	2011-10-25 09:25:37 UTC (rev 98328)
@@ -1,3 +1,13 @@
+2011-10-21  Andrey Kosyakov  <ca...@chromium.org>
+
+        Web Inspector: [Extensions API] allow extensions to specify script to be injected on reload
+        https://bugs.webkit.org/show_bug.cgi?id=70600
+
+        Reviewed by Pavel Feldman.
+
+        * inspector/extensions/extensions-reload-expected.txt: Added.
+        * inspector/extensions/extensions-reload.html: Added.
+
 2011-10-25  Sheriff Bot  <webkit.review....@gmail.com>
 
         Unreviewed, rolling out r98309.

Added: trunk/LayoutTests/inspector/extensions/extensions-reload-expected.txt (0 => 98328)


--- trunk/LayoutTests/inspector/extensions/extensions-reload-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/inspector/extensions/extensions-reload-expected.txt	2011-10-25 09:25:37 UTC (rev 98328)
@@ -0,0 +1,11 @@
+Tests that webInspector.inspectedWindow.reload() successfully injects user's code upon reload
+
+Started extension.
+Running tests...
+RUNNING TEST: extension_testReloadInjectsCode
+Page reloaded.
+Page reloaded.
+With injected code: foo = 42
+Without injected code: foo = undefined
+All tests done.
+
Property changes on: trunk/LayoutTests/inspector/extensions/extensions-reload-expected.txt
___________________________________________________________________

Added: svn:eol-style

Added: trunk/LayoutTests/inspector/extensions/extensions-reload.html (0 => 98328)


--- trunk/LayoutTests/inspector/extensions/extensions-reload.html	                        (rev 0)
+++ trunk/LayoutTests/inspector/extensions/extensions-reload.html	2011-10-25 09:25:37 UTC (rev 98328)
@@ -0,0 +1,40 @@
+<html>
+<head>
+<script src=""
+<script src=""
+<script type="text/_javascript_">
+
+window.bar = "foo = " + window.foo;
+
+function extension_testReloadInjectsCode(nextTest)
+{
+    var valueWithInjectedCode;
+
+    function onPageWithInjectedCodeLoaded()
+    {
+        webInspector.inspectedWindow.eval("window.bar", function(value) {
+            valueWithInjectedCode = value;
+            evaluateOnFrontend("InspectorTest.runWhenPageLoads(reply)", onPageWithoutInjectedCodeLoaded);
+            webInspector.inspectedWindow.reload();
+        });
+    }
+    function onPageWithoutInjectedCodeLoaded()
+    {
+        webInspector.inspectedWindow.eval("window.bar", function(value) {
+            output("With injected code: " + valueWithInjectedCode);
+            output("Without injected code: " + value);
+            nextTest();
+        });
+    }
+    evaluateOnFrontend("InspectorTest.runWhenPageLoads(reply)", onPageWithInjectedCodeLoaded);
+    webInspector.inspectedWindow.reload({
+        injectedScript: "window.foo = 42;"
+    });
+}
+
+</script>
+</head>
+<body _onload_="runTest()">
+<p>Tests that webInspector.inspectedWindow.reload() successfully injects user's code upon reload</p>
+</body>
+</html>
Property changes on: trunk/LayoutTests/inspector/extensions/extensions-reload.html
___________________________________________________________________

Added: svn:eol-style

Modified: trunk/Source/WebCore/ChangeLog (98327 => 98328)


--- trunk/Source/WebCore/ChangeLog	2011-10-25 08:37:35 UTC (rev 98327)
+++ trunk/Source/WebCore/ChangeLog	2011-10-25 09:25:37 UTC (rev 98328)
@@ -1,3 +1,36 @@
+2011-10-21  Andrey Kosyakov  <ca...@chromium.org>
+
+        Web Inspector: [Extensions API] allow extensions to specify script to be injected on reload
+        https://bugs.webkit.org/show_bug.cgi?id=70600
+
+        Reviewed by Pavel Feldman.
+
+        - change webInspector.inspectedPage.reload() to accept multiple options in an object
+        - add an ability to inject a script upon a page reload
+        - return script id in addScriptToEvaluateOnLoad()
+        - provide removeScriptToEvaluateOnLoad()
+        - store scripts to evaluate on load in inspector state cookie
+
+        Test: inspector/extensions/extensions-reload.html
+
+        * inspector/Inspector.json:
+        * inspector/InspectorPageAgent.cpp:
+        (WebCore::InspectorPageAgent::InspectorPageAgent):
+        (WebCore::InspectorPageAgent::restore):
+        (WebCore::InspectorPageAgent::addScriptToEvaluateOnLoad):
+        (WebCore::InspectorPageAgent::removeScriptToEvaluateOnLoad):
+        (WebCore::InspectorPageAgent::removeAllScriptsToEvaluateOnLoad):
+        (WebCore::InspectorPageAgent::didClearWindowObjectInWorld):
+        (WebCore::InspectorPageAgent::frameNavigated):
+        * inspector/InspectorPageAgent.h:
+        * inspector/front-end/ExtensionAPI.js:
+        (injectedExtensionAPI.InspectedWindow.prototype.reload):
+        * inspector/front-end/ExtensionServer.js:
+        (WebInspector.ExtensionServer.prototype._onReload):
+        * inspector/front-end/WorkersSidebarPane.js:
+        (WebInspector.WorkersSidebarPane.prototype.setInstrumentation.callback):
+        (WebInspector.WorkersSidebarPane.prototype.setInstrumentation):
+
 2011-10-24  Pavel Feldman  <pfeld...@google.com>
 
         Web Inspector: print expired message count before remaining messages.

Modified: trunk/Source/WebCore/inspector/Inspector.json (98327 => 98328)


--- trunk/Source/WebCore/inspector/Inspector.json	2011-10-25 08:37:35 UTC (rev 98327)
+++ trunk/Source/WebCore/inspector/Inspector.json	2011-10-25 09:25:37 UTC (rev 98328)
@@ -140,6 +140,12 @@
                     { "name": "session", "type": "boolean", "description": "True in case of session cookie." }
                 ],
                 "hidden": true
+            },
+            {
+                "id": "ScriptIdentifier",
+                "type": "string",
+                "description": "Unique script identifier.",
+                "hidden": true
             }
         ],
         "commands": [
@@ -156,16 +162,23 @@
                 "parameters": [
                     { "name": "scriptSource", "type": "string" }
                 ],
+                "returns": [
+                    { "name": "identifier", "$ref": "ScriptIdentifier", "description": "Identifier of the added script." }
+                ],
                 "hidden": true
             },
             {
-                "name": "removeAllScriptsToEvaluateOnLoad",
+                "name": "removeScriptToEvaluateOnLoad",
+                "parameters": [
+                    { "name": "identifier", "$ref": "ScriptIdentifier" }
+                ],
                 "hidden": true
             },
             {
                 "name": "reload",
                 "parameters": [
-                    { "name": "ignoreCache", "type": "boolean", "optional": true, "description": "If true, browser cache is ignored (as if the user pressed Shift+refresh)." }
+                    { "name": "ignoreCache", "type": "boolean", "optional": true, "description": "If true, browser cache is ignored (as if the user pressed Shift+refresh)." },
+                    { "name": "scriptToEvaluateOnLoad", "type": "string", "optional": true, "description": "If set, the script will be injected into all frames of the inspected page after reload." }
                 ],
                 "description": "Reloads given page optionally ignoring the cache."
             },

Modified: trunk/Source/WebCore/inspector/InspectorPageAgent.cpp (98327 => 98328)


--- trunk/Source/WebCore/inspector/InspectorPageAgent.cpp	2011-10-25 08:37:35 UTC (rev 98327)
+++ trunk/Source/WebCore/inspector/InspectorPageAgent.cpp	2011-10-25 09:25:37 UTC (rev 98328)
@@ -73,6 +73,7 @@
 
 namespace PageAgentState {
 static const char pageAgentEnabled[] = "resourceAgentEnabled";
+static const char pageAgentScriptsToEvaluateOnLoad[] = "pageAgentScriptsToEvaluateOnLoad";
 }
 
 static bool decodeSharedBuffer(PassRefPtr<SharedBuffer> buffer, const String& textEncodingName, String* result)
@@ -275,6 +276,7 @@
     , m_injectedScriptManager(injectedScriptManager)
     , m_state(state)
     , m_frontend(0)
+    , m_lastScriptIdentifier(0)
 {
 }
 
@@ -310,18 +312,34 @@
     m_instrumentingAgents->setInspectorPageAgent(0);
 }
 
-void InspectorPageAgent::addScriptToEvaluateOnLoad(ErrorString*, const String& source)
+void InspectorPageAgent::addScriptToEvaluateOnLoad(ErrorString*, const String& source, String* identifier)
 {
-    m_scriptsToEvaluateOnLoad.append(source);
+    RefPtr<InspectorObject> scripts = m_state->getObject(PageAgentState::pageAgentScriptsToEvaluateOnLoad);
+    if (!scripts) {
+        scripts = InspectorObject::create();
+        m_state->setObject(PageAgentState::pageAgentScriptsToEvaluateOnLoad, scripts);
+    }
+    // Assure we don't override existing ids -- m_lastScriptIdentifier could get out of sync WRT actual
+    // scripts once we restored the scripts from the cookie during navigation.
+    do {
+        *identifier = String::number(++m_lastScriptIdentifier);
+    } while (scripts->find(*identifier) != scripts->end());
+    scripts->setString(*identifier, source);
 }
 
-void InspectorPageAgent::removeAllScriptsToEvaluateOnLoad(ErrorString*)
+void InspectorPageAgent::removeScriptToEvaluateOnLoad(ErrorString* error, const String& identifier)
 {
-    m_scriptsToEvaluateOnLoad.clear();
+    RefPtr<InspectorObject> scripts = m_state->getObject(PageAgentState::pageAgentScriptsToEvaluateOnLoad);
+    if (!scripts || scripts->find(identifier) == scripts->end()) {
+        *error = "Script not found";
+        return;
+    }
+    scripts->remove(identifier);
 }
 
-void InspectorPageAgent::reload(ErrorString*, const bool* const optionalIgnoreCache)
+void InspectorPageAgent::reload(ErrorString*, const bool* const optionalIgnoreCache, const String* optionalScriptToEvaluateOnLoad)
 {
+    m_pendingScriptToEvaluateOnLoadOnce = optionalScriptToEvaluateOnLoad ? *optionalScriptToEvaluateOnLoad : "";
     m_page->mainFrame()->loader()->reload(optionalIgnoreCache ? *optionalIgnoreCache : false);
 }
 
@@ -563,13 +581,20 @@
     if (frame == m_page->mainFrame())
         m_injectedScriptManager->discardInjectedScripts();
 
-    if (m_scriptsToEvaluateOnLoad.size()) {
-        ScriptState* scriptState = mainWorldScriptState(frame);
-        for (Vector<String>::iterator it = m_scriptsToEvaluateOnLoad.begin();
-             it != m_scriptsToEvaluateOnLoad.end(); ++it) {
-            m_injectedScriptManager->injectScript(*it, scriptState);
+    if (!m_frontend)
+        return;
+
+    RefPtr<InspectorObject> scripts = m_state->getObject(PageAgentState::pageAgentScriptsToEvaluateOnLoad);
+    if (scripts) {
+        InspectorObject::const_iterator end = scripts->end();
+        for (InspectorObject::const_iterator it = scripts->begin(); it != end; ++it) {
+            String scriptText;
+            if (it->second->asString(&scriptText))
+                m_injectedScriptManager->injectScript(scriptText, mainWorldScriptState(frame));
         }
     }
+    if (!m_scriptToEvaluateOnLoadOnce.isEmpty())
+        m_injectedScriptManager->injectScript(m_scriptToEvaluateOnLoadOnce, mainWorldScriptState(frame));
 }
 
 void InspectorPageAgent::domContentEventFired()
@@ -584,6 +609,10 @@
 
 void InspectorPageAgent::frameNavigated(DocumentLoader* loader)
 {
+    if (loader->frame() == m_page->mainFrame()) {
+        m_scriptToEvaluateOnLoadOnce = m_pendingScriptToEvaluateOnLoadOnce;
+        m_pendingScriptToEvaluateOnLoadOnce = String();
+    }
     m_frontend->frameNavigated(buildObjectForFrame(loader->frame()), loaderId(loader));
 }
 

Modified: trunk/Source/WebCore/inspector/InspectorPageAgent.h (98327 => 98328)


--- trunk/Source/WebCore/inspector/InspectorPageAgent.h	2011-10-25 08:37:35 UTC (rev 98327)
+++ trunk/Source/WebCore/inspector/InspectorPageAgent.h	2011-10-25 09:25:37 UTC (rev 98328)
@@ -62,7 +62,6 @@
 class InspectorPageAgent {
     WTF_MAKE_NONCOPYABLE(InspectorPageAgent);
 public:
-
     enum ResourceType {
         DocumentResource,
         StylesheetResource,
@@ -89,9 +88,9 @@
     // Page API for InspectorFrontend
     void enable(ErrorString*);
     void disable(ErrorString*);
-    void addScriptToEvaluateOnLoad(ErrorString*, const String& source);
-    void removeAllScriptsToEvaluateOnLoad(ErrorString*);
-    void reload(ErrorString*, const bool* const optionalIgnoreCache);
+    void addScriptToEvaluateOnLoad(ErrorString*, const String& source, String* result);
+    void removeScriptToEvaluateOnLoad(ErrorString*, const String& identifier);
+    void reload(ErrorString*, const bool* const optionalIgnoreCache, const String* optionalScriptToEvaluateOnLoad);
     void open(ErrorString*, const String& url, const bool* const inNewWindow);
     void getCookies(ErrorString*, RefPtr<InspectorArray>* cookies, WTF::String* cookiesString);
     void deleteCookie(ErrorString*, const String& cookieName, const String& domain);
@@ -131,7 +130,9 @@
     InjectedScriptManager* m_injectedScriptManager;
     InspectorState* m_state;
     InspectorFrontend::Page* m_frontend;
-    Vector<String> m_scriptsToEvaluateOnLoad;
+    long m_lastScriptIdentifier;
+    String m_pendingScriptToEvaluateOnLoadOnce;
+    String m_scriptToEvaluateOnLoadOnce;
     HashMap<Frame*, String> m_frameToIdentifier;
     HashMap<String, Frame*> m_identifierToFrame;
     HashMap<DocumentLoader*, String> m_loaderToIdentifier;

Modified: trunk/Source/WebCore/inspector/front-end/ExtensionAPI.js (98327 => 98328)


--- trunk/Source/WebCore/inspector/front-end/ExtensionAPI.js	2011-10-25 08:37:35 UTC (rev 98327)
+++ trunk/Source/WebCore/inspector/front-end/ExtensionAPI.js	2011-10-25 09:25:37 UTC (rev 98328)
@@ -489,9 +489,17 @@
 }
 
 InspectedWindow.prototype = {
-    reload: function(userAgent)
+    reload: function(optionsOrUserAgent)
     {
-        return extensionServer.sendRequest({ command: "reload", userAgent: userAgent });
+        var options = null;
+        if (typeof optionsOrUserAgent === "object")
+            options = optionsOrUserAgent;
+        else if (typeof optionsOrUserAgent === "string") {
+            options = { userAgent: optionsOrUserAgent };
+            console.warn("Passing userAgent as string parameter to inspectedWindow.reload() is deprecated. " +
+                         "Use inspectedWindow.reload({ userAgent: value}) instead.");
+        }
+        return extensionServer.sendRequest({ command: "reload", options: options });
     },
 
     eval: function(_expression_, callback)

Modified: trunk/Source/WebCore/inspector/front-end/ExtensionServer.js (98327 => 98328)


--- trunk/Source/WebCore/inspector/front-end/ExtensionServer.js	2011-10-25 08:37:35 UTC (rev 98327)
+++ trunk/Source/WebCore/inspector/front-end/ExtensionServer.js	2011-10-25 09:25:37 UTC (rev 98328)
@@ -282,10 +282,15 @@
 
     _onReload: function(message)
     {
-        if (typeof message.userAgent === "string")
-            NetworkAgent.setUserAgentOverride(message.userAgent);
-
-        PageAgent.reload(false);
+        var options = message.options || {};
+        NetworkAgent.setUserAgentOverride(typeof options.userAgent === "string" ? options.userAgent : "");
+        var injectedScript;
+        if (options.injectedScript) {
+            // Wrap client script into anonymous function, return another anonymous function that
+            // returns empty object for compatibility with InjectedScriptManager on the backend.
+            injectedScript = "((function(){" + options.injectedScript + "})(),function(){return {}})";
+        }
+        PageAgent.reload(false, injectedScript);
         return this._status.OK();
     },
 

Modified: trunk/Source/WebCore/inspector/front-end/WorkersSidebarPane.js (98327 => 98328)


--- trunk/Source/WebCore/inspector/front-end/WorkersSidebarPane.js	2011-10-25 08:37:35 UTC (rev 98327)
+++ trunk/Source/WebCore/inspector/front-end/WorkersSidebarPane.js	2011-10-25 09:25:37 UTC (rev 98328)
@@ -76,23 +76,35 @@
         }
     },
 
-    setInstrumentation: function(enabled)
+    _setInstrumentation: function(enabled)
     {
-        PageAgent.removeAllScriptsToEvaluateOnLoad();
-        if (enabled)
-            PageAgent.addScriptToEvaluateOnLoad("(" + InjectedFakeWorker + ")");
+        if (!enabled === !this._fakeWorkersScriptIdentifier)
+            return;
+
+        if (enabled) {
+            this._enableWorkersCheckbox.disabled = true;
+            function callback(error, identifier)
+            {
+                this._fakeWorkersScriptIdentifier = identifier;
+                this._enableWorkersCheckbox.disabled = false;
+            }
+            PageAgent.addScriptToEvaluateOnLoad("(" + InjectedFakeWorker + ")", callback.bind(this));
+        } else {
+            PageAgent.removeScriptToEvaluateOnLoad(this._fakeWorkersScriptIdentifier);
+            this._fakeWorkersScriptIdentifier = null;
+        }
     },
 
     reset: function()
     {
-        this.setInstrumentation(this._enableWorkersCheckbox.checked);
+        this._setInstrumentation(this._enableWorkersCheckbox.checked);
         this._treeOutline.removeChildren();
         this._workers = {};
     },
 
     _onTriggerInstrument: function(event)
     {
-        this.setInstrumentation(this._enableWorkersCheckbox.checked);
+        this._setInstrumentation(this._enableWorkersCheckbox.checked);
     }
 };
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to