Web Inspector: [Extensions API] allow setting extension API per extension security...
authorcaseq@chromium.org <caseq@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 20 Dec 2011 13:42:35 +0000 (13:42 +0000)
committercaseq@chromium.org <caseq@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Tue, 20 Dec 2011 13:42:35 +0000 (13:42 +0000)
https://bugs.webkit.org/show_bug.cgi?id=74868

Reviewed by Pavel Feldman.

This replaces InspectorExtensionAPI string within InspectorFrontendHost with a map by extension
security origin, so that we can have APIs customized by extension (in particular, this is needed
to expose experimental APIs only to certain extensions).

* inspector/InspectorAgent.cpp:
(WebCore::InspectorAgent::didClearWindowObjectInWorld):
(WebCore::InspectorAgent::setInjectedScriptForOrigin):
* inspector/InspectorAgent.h:
* inspector/InspectorController.cpp:
(WebCore::InspectorController::setInjectedScriptForOrigin):
* inspector/InspectorController.h:
* inspector/InspectorFrontendHost.cpp:
(WebCore::InspectorFrontendHost::setInjectedScriptForOrigin):
* inspector/InspectorFrontendHost.h:
* inspector/InspectorFrontendHost.idl:
* inspector/front-end/ExtensionServer.js:
(WebInspector.ExtensionServer.prototype._addExtensions):
(WebInspector.ExtensionServer.prototype._addExtension):

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

14 files changed:
LayoutTests/http/tests/inspector/injected-script-for-origin-expected.txt [new file with mode: 0644]
LayoutTests/http/tests/inspector/injected-script-for-origin.html [new file with mode: 0644]
LayoutTests/http/tests/inspector/resources/injected-script-for-origin-frame.html [new file with mode: 0644]
LayoutTests/inspector/console/console-shadow-dom-access-expected.txt [deleted file]
Source/WebCore/ChangeLog
Source/WebCore/inspector/InspectorAgent.cpp
Source/WebCore/inspector/InspectorAgent.h
Source/WebCore/inspector/InspectorController.cpp
Source/WebCore/inspector/InspectorController.h
Source/WebCore/inspector/InspectorFrontendHost.cpp
Source/WebCore/inspector/InspectorFrontendHost.h
Source/WebCore/inspector/InspectorFrontendHost.idl
Source/WebCore/inspector/front-end/ExtensionServer.js
Source/WebCore/inspector/front-end/InspectorFrontendHostStub.js

diff --git a/LayoutTests/http/tests/inspector/injected-script-for-origin-expected.txt b/LayoutTests/http/tests/inspector/injected-script-for-origin-expected.txt
new file mode 100644 (file)
index 0000000..80665e2
--- /dev/null
@@ -0,0 +1,7 @@
+Tests that different scripts are injected inso subframes of inspector window depending on the origin
+
+{
+    0 : "http://127.0.0.1:8000/inspector/resources/injected-script-for-origin-frame.html, data: 127.0.0.1"
+    1 : "http://localhost:8000/inspector/resources/injected-script-for-origin-frame.html, data: localhost"
+}
+
diff --git a/LayoutTests/http/tests/inspector/injected-script-for-origin.html b/LayoutTests/http/tests/inspector/injected-script-for-origin.html
new file mode 100644 (file)
index 0000000..c8cae5b
--- /dev/null
@@ -0,0 +1,38 @@
+<html>
+<head>
+<script src="inspector-test.js"></script>
+
+<script>
+function test()
+{
+    var messages = [];
+    function onMessage(event)
+    {
+        messages.push(event.data);
+        if (messages.length === 2) {
+            top.removeEventListener("message", onMessage, false);
+            messages.sort();
+            InspectorTest.addObject(messages);
+            InspectorTest.completeTest();
+        }
+    }
+    top.addEventListener("message", onMessage, false);
+    InspectorFrontendHost.setInjectedScriptForOrigin("http://127.0.0.1:8000", "window.foo = '127.0.0.1';");
+    InspectorFrontendHost.setInjectedScriptForOrigin("http://localhost:8000", "window.foo = 'localhost';");
+    function addSubframe(url)
+    {
+        var iframe = document.createElement("iframe");
+        iframe.src = url;
+        document.body.appendChild(iframe);
+    }
+    addSubframe("http://127.0.0.1:8000/inspector/resources/injected-script-for-origin-frame.html");
+    addSubframe("http://localhost:8000/inspector/resources/injected-script-for-origin-frame.html");
+}
+</script>
+</head>
+<body onload="runTest()">
+<p>
+Tests that different scripts are injected inso subframes of inspector window depending on the origin
+</p>
+</body>
+</html>
diff --git a/LayoutTests/http/tests/inspector/resources/injected-script-for-origin-frame.html b/LayoutTests/http/tests/inspector/resources/injected-script-for-origin-frame.html
new file mode 100644 (file)
index 0000000..d04a96e
--- /dev/null
@@ -0,0 +1,7 @@
+<html>
+<head>
+<script type="text/javascript">
+top.postMessage(location.href + ", data: " + window.foo, "*");
+</script>
+</head>
+</html>
diff --git a/LayoutTests/inspector/console/console-shadow-dom-access-expected.txt b/LayoutTests/inspector/console/console-shadow-dom-access-expected.txt
deleted file mode 100644 (file)
index 0dd7a98..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-Tests that $0 may successfully be used to refer to shadow DOM elements.
-
-
-$0.toString() = "[object Node]"
-$0.firstChild.toString() = "[object HTMLDivElement]"
-$0.firstChild.firstChild.style.width = "42%"
-
index 32fc4c2..4984aa5 100644 (file)
@@ -1,3 +1,29 @@
+2011-12-19  Andrey Kosyakov  <caseq@chromium.org>
+
+        Web Inspector: [Extensions API] allow setting extension API per extension security origin
+        https://bugs.webkit.org/show_bug.cgi?id=74868
+
+        Reviewed by Pavel Feldman.
+
+        This replaces InspectorExtensionAPI string within InspectorFrontendHost with a map by extension
+        security origin, so that we can have APIs customized by extension (in particular, this is needed
+        to expose experimental APIs only to certain extensions).
+
+        * inspector/InspectorAgent.cpp:
+        (WebCore::InspectorAgent::didClearWindowObjectInWorld):
+        (WebCore::InspectorAgent::setInjectedScriptForOrigin):
+        * inspector/InspectorAgent.h:
+        * inspector/InspectorController.cpp:
+        (WebCore::InspectorController::setInjectedScriptForOrigin):
+        * inspector/InspectorController.h:
+        * inspector/InspectorFrontendHost.cpp:
+        (WebCore::InspectorFrontendHost::setInjectedScriptForOrigin):
+        * inspector/InspectorFrontendHost.h:
+        * inspector/InspectorFrontendHost.idl:
+        * inspector/front-end/ExtensionServer.js:
+        (WebInspector.ExtensionServer.prototype._addExtensions):
+        (WebInspector.ExtensionServer.prototype._addExtension):
+
 2011-12-20  Peter Rybin  <peter.rybin@gmail.com>
 
         Web Inspector: CodeGenerator should not use pointers for out params of RefPtr type.
index f24cccc..ec4ce1d 100644 (file)
@@ -50,6 +50,7 @@
 #include "ResourceRequest.h"
 #include "ScriptFunctionCall.h"
 #include "ScriptObject.h"
+#include "SecurityOrigin.h"
 #include "Settings.h"
 #include <wtf/PassRefPtr.h>
 #include <wtf/RefPtr.h>
@@ -89,8 +90,13 @@ void InspectorAgent::didClearWindowObjectInWorld(Frame* frame, DOMWrapperWorld*
     if (world != mainThreadNormalWorld())
         return;
 
-    if (!m_inspectorExtensionAPI.isEmpty())
-        m_injectedScriptManager->injectScript(m_inspectorExtensionAPI, mainWorldScriptState(frame));
+    if (m_injectedScriptForOrigin.isEmpty())
+        return;
+
+    String origin = frame->document()->securityOrigin()->toString();
+    String script = m_injectedScriptForOrigin.get(origin);
+    if (!script.isEmpty())
+        m_injectedScriptManager->injectScript(script, mainWorldScriptState(frame));
 }
 
 void InspectorAgent::setFrontend(InspectorFrontend* inspectorFrontend)
@@ -230,9 +236,9 @@ void InspectorAgent::evaluateForTestInFrontend(long callId, const String& script
         m_pendingEvaluateTestCommands.append(pair<long, String>(callId, script));
 }
 
-void InspectorAgent::setInspectorExtensionAPI(const String& source)
+void InspectorAgent::setInjectedScriptForOrigin(const String& origin, const String& source)
 {
-    m_inspectorExtensionAPI = source;
+    m_injectedScriptForOrigin.set(origin, source);
 }
 
 void InspectorAgent::inspect(PassRefPtr<InspectorObject> objectToInspect, PassRefPtr<InspectorObject> hints)
index 5610b69..f7c8e48 100644 (file)
@@ -95,7 +95,7 @@ public:
     // Generic code called from custom implementations.
     void evaluateForTestInFrontend(long testCallId, const String& script);
 
-    void setInspectorExtensionAPI(const String& source);
+    void setInjectedScriptForOrigin(const String& origin, const String& source);
 
     void inspect(PassRefPtr<InspectorObject> objectToInspect, PassRefPtr<InspectorObject> hints);
 
@@ -116,7 +116,8 @@ private:
 
     Vector<pair<long, String> > m_pendingEvaluateTestCommands;
     pair<RefPtr<InspectorObject>, RefPtr<InspectorObject> > m_pendingInspectData;
-    String m_inspectorExtensionAPI;
+    typedef HashMap<String, String> InjectedScriptForOriginMap;
+    InjectedScriptForOriginMap m_injectedScriptForOrigin;
 #if ENABLE(WORKERS)
     typedef HashMap<intptr_t, RefPtr<InspectorWorkerResource> > WorkersMap;
     WorkersMap m_workers;
index f0d8fd5..9619f14 100644 (file)
@@ -300,9 +300,9 @@ Page* InspectorController::inspectedPage() const
     return m_page;
 }
 
-void InspectorController::setInspectorExtensionAPI(const String& source)
+void InspectorController::setInjectedScriptForOrigin(const String& origin, const String& source)
 {
-    m_inspectorAgent->setInspectorExtensionAPI(source);
+    m_inspectorAgent->setInjectedScriptForOrigin(origin, source);
 }
 
 void InspectorController::dispatchMessageFromFrontend(const String& message)
index 049e260..98c87cb 100644 (file)
@@ -76,7 +76,7 @@ public:
     void setInspectorFrontendClient(PassOwnPtr<InspectorFrontendClient>);
     bool hasInspectorFrontendClient() const;
     void didClearWindowObjectInWorld(Frame*, DOMWrapperWorld*);
-    void setInspectorExtensionAPI(const String& source);
+    void setInjectedScriptForOrigin(const String& origin, const String& source);
 
     void dispatchMessageFromFrontend(const String& message);
 
index 71f6a75..ab2a1b2 100644 (file)
@@ -193,10 +193,10 @@ void InspectorFrontendHost::moveWindowBy(float x, float y) const
         m_client->moveWindowBy(x, y);
 }
 
-void InspectorFrontendHost::setExtensionAPI(const String& script)
+void InspectorFrontendHost::setInjectedScriptForOrigin(const String& origin, const String& script)
 {
     ASSERT(m_frontendPage->inspectorController());
-    m_frontendPage->inspectorController()->setInspectorExtensionAPI(script);
+    m_frontendPage->inspectorController()->setInjectedScriptForOrigin(origin, script);
 }
 
 String InspectorFrontendHost::localizedStringsURL()
index f59a69c..9354a05 100644 (file)
@@ -66,7 +66,7 @@ public:
 
     void setAttachedWindowHeight(unsigned height);
     void moveWindowBy(float x, float y) const;
-    void setExtensionAPI(const String& script);
+    void setInjectedScriptForOrigin(const String& origin, const String& script);
 
     String localizedStringsURL();
     String hiddenPanels();
index 6e7824a..9150783 100644 (file)
@@ -43,7 +43,7 @@ module core {
         void requestDetachWindow();
         void setAttachedWindowHeight(in unsigned long height);
         void moveWindowBy(in float x, in float y);
-        void setExtensionAPI(in DOMString script);
+        void setInjectedScriptForOrigin(in DOMString origin, in DOMString script);
 
         DOMString localizedStringsURL();
         DOMString hiddenPanels();
index ed1f03e..d840264 100644 (file)
@@ -603,8 +603,6 @@ WebInspector.ExtensionServer.prototype = {
      */
     _addExtensions: function(extensions)
     {
-        // See ExtensionAPI.js and ExtensionCommon.js for details.
-        InspectorFrontendHost.setExtensionAPI(this._buildExtensionAPIScript());
         for (var i = 0; i < extensions.length; ++i)
             this._addExtension(extensions[i].startPage, extensions[i].name);
     },
@@ -619,7 +617,12 @@ WebInspector.ExtensionServer.prototype = {
                 console.error("Skipping extension with invalid URL: " + startPage);
                 return false;
             }
-            this._registeredExtensions[originMatch[1]] = { name: name };
+            var extensionOrigin = originMatch[1];
+            if (!this._registeredExtensions[extensionOrigin]) {
+                // See ExtensionAPI.js and ExtensionCommon.js for details.
+                InspectorFrontendHost.setInjectedScriptForOrigin(extensionOrigin, this._buildExtensionAPIScript());
+                this._registeredExtensions[extensionOrigin] = { name: name };
+            }
             var iframe = document.createElement("iframe");
             iframe.src = startPage;
             iframe.style.display = "none";
index b3c0769..0f4b165 100644 (file)
@@ -85,7 +85,7 @@ WebInspector.InspectorFrontendHostStub.prototype = {
     {
     },
 
-    setExtensionAPI: function(script)
+    setInjectedScriptForOrigin: function(origin, script)
     {
     },