Title: [116957] trunk
Revision
116957
Author
[email protected]
Date
2012-05-14 09:34:15 -0700 (Mon, 14 May 2012)

Log Message

Web Inspector: [Extensions API] allow extensions to evaluate in the context of their content scripts
https://bugs.webkit.org/show_bug.cgi?id=86361

Reviewed by Yury Semikhatsky.

Source/WebCore:

Test: inspector/extensions/extensions-eval-content-script.html

* inspector/front-end/ExtensionAPI.js:
(injectedExtensionAPI.ExtensionSidebarPaneImpl.prototype.setExpression): Added evaluateOptions optional parameter.
(injectedExtensionAPI.InspectedWindow.prototype.eval):
(injectedExtensionAPI.extractCallbackArgument): A helper to extract callback function from last argument.
* inspector/front-end/ExtensionPanel.js:
(WebInspector.ExtensionSidebarPane.prototype.setExpression): Added evaluateOptions optional parameter.
* inspector/front-end/ExtensionServer.js:
(WebInspector.ExtensionServer.prototype._onSetSidebarContent): Use ExtensionServer.evaluate() wrapper.
(WebInspector.ExtensionServer.prototype._onEvaluateOnInspectedPage): Ditto.
(WebInspector.ExtensionServer.prototype._normalizePath):
(WebInspector.ExtensionServer.prototype.evaluate): A wrapper for PageAgent.evaluate() that handles evaluateOptions.
* inspector/front-end/_javascript_ContextManager.js:
(WebInspector._javascript_ContextManager.prototype.contextByFrameAndSecurityOrigin): Expose interface to locate context id by frame and content script security origin.
(WebInspector.FrameEvaluationContext.prototype._addExecutionContext): Maintain contexts sorted by name.
(WebInspector.FrameEvaluationContext.prototype.isolatedContexts): ditto (skip sorting when returning contexts)
(WebInspector.FrameEvaluationContext.prototype.contextBySecurityOrigin):
* inspector/front-end/inspector.js: Hold/expose _javascript_ContextManager.

LayoutTests:

* inspector/extensions/extensions-eval-content-script-expected.txt: Added.
* inspector/extensions/extensions-eval-content-script.html: Added.
* platform/gtk/test_expectations.txt:
* platform/mac/Skipped:
* platform/qt/Skipped:
* platform/win/Skipped:
* platform/wk2/Skipped:

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (116956 => 116957)


--- trunk/LayoutTests/ChangeLog	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/LayoutTests/ChangeLog	2012-05-14 16:34:15 UTC (rev 116957)
@@ -1,3 +1,18 @@
+2012-05-14  Andrey Kosyakov  <[email protected]>
+
+        Web Inspector: [Extensions API] allow extensions to evaluate in the context of their content scripts
+        https://bugs.webkit.org/show_bug.cgi?id=86361
+
+        Reviewed by Yury Semikhatsky.
+
+        * inspector/extensions/extensions-eval-content-script-expected.txt: Added.
+        * inspector/extensions/extensions-eval-content-script.html: Added.
+        * platform/gtk/test_expectations.txt:
+        * platform/mac/Skipped:
+        * platform/qt/Skipped:
+        * platform/win/Skipped:
+        * platform/wk2/Skipped:
+
 2012-05-14  Mihnea Ovidenie  <[email protected]>
 
         [CSSRegions]Convert render-region-custom-style-mark to ref test

Added: trunk/LayoutTests/inspector/extensions/extensions-eval-content-script-expected.txt (0 => 116957)


--- trunk/LayoutTests/inspector/extensions/extensions-eval-content-script-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/inspector/extensions/extensions-eval-content-script-expected.txt	2012-05-14 16:34:15 UTC (rev 116957)
@@ -0,0 +1,12 @@
+Tests WebInspector extension API
+
+Started extension.
+Running tests...
+RUNNING TEST: extension_testEvalInContentScriptContext
+Evaluate: "brave new world" (exception: undefined)
+RUNNING TEST: extension_testEvalInMainWorldExplicit
+Evaluate: "main world" (exception: undefined)
+RUNNING TEST: extension_testEvalInMainWorldImplicit
+Evaluate: "main world" (exception: undefined)
+All tests done.
+
Property changes on: trunk/LayoutTests/inspector/extensions/extensions-eval-content-script-expected.txt
___________________________________________________________________

Added: svn:eol-style

Added: trunk/LayoutTests/inspector/extensions/extensions-eval-content-script.html (0 => 116957)


--- trunk/LayoutTests/inspector/extensions/extensions-eval-content-script.html	                        (rev 0)
+++ trunk/LayoutTests/inspector/extensions/extensions-eval-content-script.html	2012-05-14 16:34:15 UTC (rev 116957)
@@ -0,0 +1,37 @@
+<html>
+<head>
+<script src=""
+<script src=""
+<script type="text/_javascript_">
+
+window.whereAmI = "main world";
+
+layoutTestController.setIsolatedWorldSecurityOrigin(632, "file:///");
+layoutTestController.evaluateScriptInIsolatedWorld(632, "window.whereAmI = 'brave new world'");
+
+function extension_testEvalInMainWorldImplicit(nextTest)
+{
+    webInspector.inspectedWindow.eval("whereAmI", callbackAndNextTest(extension_onEval, nextTest));
+}
+
+function extension_testEvalInMainWorldExplicit(nextTest)
+{
+    webInspector.inspectedWindow.eval("whereAmI", { useContentScriptContext: false }, callbackAndNextTest(extension_onEval, nextTest));
+}
+
+function extension_testEvalInContentScriptContext(nextTest)
+{
+    webInspector.inspectedWindow.eval("whereAmI", { useContentScriptContext: true }, callbackAndNextTest(extension_onEval, nextTest));
+}
+
+function extension_onEval(value, isException)
+{
+    output("Evaluate: " + JSON.stringify(value) + " (exception: " + isException + ")");
+}
+
+</script>
+</head>
+<body _onload_="runTest()">
+<p>Tests WebInspector extension API</p>
+</body>
+</html>
Property changes on: trunk/LayoutTests/inspector/extensions/extensions-eval-content-script.html
___________________________________________________________________

Added: svn:eol-style

Modified: trunk/LayoutTests/platform/gtk/test_expectations.txt (116956 => 116957)


--- trunk/LayoutTests/platform/gtk/test_expectations.txt	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/LayoutTests/platform/gtk/test_expectations.txt	2012-05-14 16:34:15 UTC (rev 116957)
@@ -783,6 +783,7 @@
 BUGWK40300 SKIP : inspector/debugger/live-edit.html = FAIL
 BUGWK50868 SKIP : inspector/debugger/debugger-step-out.html = FAIL
 BUGWK50868 SKIP : inspector/styles/styles-source-lines-inline.html = FAIL
+BUGWK85709 SKIP : inspector/extensions/extensions-eval-content-script.html = FAIL
 
 // These inspector tests fell out of the radar after the transition to NRWT
 // Some time out and some are flaky or present different baselines.

Modified: trunk/LayoutTests/platform/mac/Skipped (116956 => 116957)


--- trunk/LayoutTests/platform/mac/Skipped	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/LayoutTests/platform/mac/Skipped	2012-05-14 16:34:15 UTC (rev 116957)
@@ -839,3 +839,6 @@
 
 # Hits assert https://bugs.webkit.org/show_bug.cgi?id=86208
 ietestcenter/css3/valuesandunits/units-000.htm
+
+# Inspector only supports evaluation in content script world with v8, see https://bugs.webkit.org/show_bug.cgi?id=85709
+inspector/extensions/extensions-eval-content-script.html

Modified: trunk/LayoutTests/platform/qt/Skipped (116956 => 116957)


--- trunk/LayoutTests/platform/qt/Skipped	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/LayoutTests/platform/qt/Skipped	2012-05-14 16:34:15 UTC (rev 116957)
@@ -2561,3 +2561,5 @@
 # https://bugs.webkit.org/show_bug.cgi?id=86182
 fast/frames/seamless/seamless-inherited-document-style.html
 
+# Inspector only supports evaluation in content script world with v8, see https://bugs.webkit.org/show_bug.cgi?id=85709
+inspector/extensions/extensions-eval-content-script.html 

Modified: trunk/LayoutTests/platform/win/Skipped (116956 => 116957)


--- trunk/LayoutTests/platform/win/Skipped	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/LayoutTests/platform/win/Skipped	2012-05-14 16:34:15 UTC (rev 116957)
@@ -1744,3 +1744,7 @@
 
 # https://bugs.webkit.org/show_bug.cgi?id=86000
 http/tests/security/referrer-policy-redirect-link.html
+
+# Inspector only supports evaluation in content script world with v8, see https://bugs.webkit.org/show_bug.cgi?id=85709
+inspector/extensions/extensions-eval-content-script.html 
+

Modified: trunk/LayoutTests/platform/wk2/Skipped (116956 => 116957)


--- trunk/LayoutTests/platform/wk2/Skipped	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/LayoutTests/platform/wk2/Skipped	2012-05-14 16:34:15 UTC (rev 116957)
@@ -71,6 +71,9 @@
 http/tests/inspector/extensions-headers.html
 http/tests/inspector/extensions-network-redirect.html
 
+# Inspector only supports evaluation in content script world with v8, see https://bugs.webkit.org/show_bug.cgi?id=85709
+inspector/extensions/extensions-eval-content-script.html 
+
 # A bunch of inspector tests time out on Lion Intel Debug WebKit2 testers.
 # https://bugs.webkit.org/show_bug.cgi?id=81601
 http/tests/inspector/console-cross-origin-iframe-logging.html

Modified: trunk/Source/WebCore/ChangeLog (116956 => 116957)


--- trunk/Source/WebCore/ChangeLog	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/Source/WebCore/ChangeLog	2012-05-14 16:34:15 UTC (rev 116957)
@@ -1,3 +1,30 @@
+2012-05-14  Andrey Kosyakov  <[email protected]>
+
+        Web Inspector: [Extensions API] allow extensions to evaluate in the context of their content scripts
+        https://bugs.webkit.org/show_bug.cgi?id=86361
+
+        Reviewed by Yury Semikhatsky.
+
+        Test: inspector/extensions/extensions-eval-content-script.html
+
+        * inspector/front-end/ExtensionAPI.js: 
+        (injectedExtensionAPI.ExtensionSidebarPaneImpl.prototype.setExpression): Added evaluateOptions optional parameter.
+        (injectedExtensionAPI.InspectedWindow.prototype.eval):
+        (injectedExtensionAPI.extractCallbackArgument): A helper to extract callback function from last argument.
+        * inspector/front-end/ExtensionPanel.js:
+        (WebInspector.ExtensionSidebarPane.prototype.setExpression): Added evaluateOptions optional parameter.
+        * inspector/front-end/ExtensionServer.js:
+        (WebInspector.ExtensionServer.prototype._onSetSidebarContent): Use ExtensionServer.evaluate() wrapper.
+        (WebInspector.ExtensionServer.prototype._onEvaluateOnInspectedPage): Ditto.
+        (WebInspector.ExtensionServer.prototype._normalizePath):
+        (WebInspector.ExtensionServer.prototype.evaluate): A wrapper for PageAgent.evaluate() that handles evaluateOptions.
+        * inspector/front-end/_javascript_ContextManager.js:
+        (WebInspector._javascript_ContextManager.prototype.contextByFrameAndSecurityOrigin): Expose interface to locate context id by frame and content script security origin.
+        (WebInspector.FrameEvaluationContext.prototype._addExecutionContext): Maintain contexts sorted by name.
+        (WebInspector.FrameEvaluationContext.prototype.isolatedContexts): ditto (skip sorting when returning contexts)
+        (WebInspector.FrameEvaluationContext.prototype.contextBySecurityOrigin):
+        * inspector/front-end/inspector.js: Hold/expose _javascript_ContextManager.
+
 2012-05-14  Pavel Feldman  <[email protected]>
 
         Web Inspector: preserve tab index while widening / shrinking tabbed pane area.

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


--- trunk/Source/WebCore/inspector/front-end/ExtensionAPI.js	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/Source/WebCore/inspector/front-end/ExtensionAPI.js	2012-05-14 16:34:15 UTC (rev 116957)
@@ -414,9 +414,19 @@
         extensionServer.sendRequest({ command: commands.SetSidebarHeight, id: this._id, height: height });
     },
 
-    setExpression: function(_expression_, rootTitle, callback)
+    setExpression: function(_expression_, rootTitle, evaluateOptions)
     {
-        extensionServer.sendRequest({ command: commands.SetSidebarContent, id: this._id, _expression_: _expression_, rootTitle: rootTitle, evaluateOnPage: true }, callback);
+        var callback = extractCallbackArgument(arguments);
+        var request = {
+            command: commands.SetSidebarContent,
+            id: this._id,
+            _expression_: _expression_,
+            rootTitle: rootTitle,
+            evaluateOnPage: true,
+        };
+        if (typeof evaluateOptions === "object")
+            request.evaluateOptions = evaluateOptions;
+        extensionServer.sendRequest(request, callback);
     },
 
     setObject: function(jsonObject, rootTitle, callback)
@@ -602,13 +612,20 @@
         return extensionServer.sendRequest({ command: commands.Reload, options: options });
     },
 
-    eval: function(_expression_, callback)
+    eval: function(_expression_, evaluateOptions)
     {
+        var callback = extractCallbackArgument(arguments);
         function callbackWrapper(result)
         {
             callback(result.value, result.isException);
         }
-        return extensionServer.sendRequest({ command: commands.EvaluateOnInspectedPage, _expression_: _expression_ }, callback && callbackWrapper);
+        var request = {
+            command: commands.EvaluateOnInspectedPage,
+            _expression_: _expression_
+        };
+        if (typeof evaluateOptions === "object")
+            request.evaluateOptions = evaluateOptions;
+        return extensionServer.sendRequest(request, callback && callbackWrapper);
     },
 
     getResources: function(callback)
@@ -786,6 +803,12 @@
     object.__defineGetter__(oldName, getter);
 }
 
+function extractCallbackArgument(args)
+{
+    var lastArgument = args[args.length - 1];
+    return typeof lastArgument === "function" ? lastArgument : undefined;
+}
+
 var AuditCategory = declareInterfaceClass(AuditCategoryImpl);
 var AuditResult = declareInterfaceClass(AuditResultImpl);
 var Button = declareInterfaceClass(ButtonImpl);

Modified: trunk/Source/WebCore/inspector/front-end/ExtensionPanel.js (116956 => 116957)


--- trunk/Source/WebCore/inspector/front-end/ExtensionPanel.js	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/Source/WebCore/inspector/front-end/ExtensionPanel.js	2012-05-14 16:34:15 UTC (rev 116957)
@@ -245,10 +245,10 @@
      * @param {string} title
      * @param {function(?string=)} callback
      */
-    setExpression: function(_expression_, title, callback)
+    setExpression: function(_expression_, title, evaluateOptions, securityOrigin, callback)
     {
         this._createObjectPropertiesView();
-        RuntimeAgent.evaluate(_expression_, "extension-watch", true, undefined, undefined, undefined, this._onEvaluate.bind(this, title, callback));
+        return WebInspector.extensionServer.evaluate(_expression_, true, false, evaluateOptions, securityOrigin, this._onEvaluate.bind(this, title, callback));
     },
 
     /**

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


--- trunk/Source/WebCore/inspector/front-end/ExtensionServer.js	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/Source/WebCore/inspector/front-end/ExtensionServer.js	2012-05-14 16:34:15 UTC (rev 116957)
@@ -260,9 +260,8 @@
             this._dispatchCallback(message.requestId, port, result);
         }
         if (message.evaluateOnPage)
-            sidebar.setExpression(message._expression_, message.rootTitle, callback.bind(this));
-        else
-            sidebar.setObject(message._expression_, message.rootTitle, callback.bind(this));
+            return sidebar.setExpression(message._expression_, message.rootTitle, message.evaluateOptions, port._extensionOrigin, callback.bind(this));
+        sidebar.setObject(message._expression_, message.rootTitle, callback.bind(this));
     },
 
     _onSetSidebarPage: function(message, port)
@@ -338,7 +337,7 @@
       
             this._dispatchCallback(message.requestId, port, result);
         }
-        RuntimeAgent.evaluate(message._expression_, "", true, undefined, undefined, true, callback.bind(this));
+        return this.evaluate(message._expression_, true, true, message.evaluateOptions, port._extensionOrigin, callback.bind(this));
     },
 
     _onGetConsoleMessages: function()
@@ -723,6 +722,29 @@
                 result.push(source[i]);
         }
         return "/" + result.join("/");
+    },
+
+    /**
+     * @param {string} _expression_
+     * @param {boolean} exposeCommandLineAPI
+     * @param {boolean} returnByValue
+     * @param {Object} options
+     * @param {string} securityOrigin
+     * @param {function(?string, ?RuntimeAgent.RemoteObject, boolean=)} callback
+     */
+    evaluate: function(_expression_, exposeCommandLineAPI, returnByValue, options, securityOrigin, callback) 
+    {
+        var contextId;
+        if (typeof options === "object" && options["useContentScriptContext"]) {
+            var mainFrame = WebInspector.resourceTreeModel.mainFrame;
+            if (!mainFrame)
+                return this._status.E_FAILED("main frame not available yet");
+            var context = WebInspector._javascript_ContextManager.contextByFrameAndSecurityOrigin(mainFrame, securityOrigin);
+            if (!context)
+                return this._status.E_NOTFOUND(securityOrigin);
+            contextId = context.id;
+        }
+        RuntimeAgent.evaluate(_expression_, "extension", exposeCommandLineAPI, true, contextId, returnByValue, callback);
     }
 }
 

Modified: trunk/Source/WebCore/inspector/front-end/_javascript_ContextManager.js (116956 => 116957)


--- trunk/Source/WebCore/inspector/front-end/_javascript_ContextManager.js	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/Source/WebCore/inspector/front-end/_javascript_ContextManager.js	2012-05-14 16:34:15 UTC (rev 116957)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2011 Google Inc. All rights reserved.
+ * Copyright (C) 2012 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions are
@@ -43,6 +43,16 @@
 }
 
 WebInspector._javascript_ContextManager.prototype = {
+    /**
+     * @param {WebInspector.ResourceTreeFrame} frame
+     * @param {string} securityOrigin
+     */
+    contextByFrameAndSecurityOrigin: function(frame, securityOrigin)
+    {
+        var frameContext = this._frameIdToContext[frame.id];
+        return frameContext && frameContext.contextBySecurityOrigin(securityOrigin);
+    },
+
     _frameAdded: function(event)
     {
         var frame = event.data;
@@ -87,6 +97,10 @@
 
 WebInspector._javascript_ContextManager.prototype.__proto__ = WebInspector.Object.prototype;
 
+/**
+ * @type {WebInspector._javascript_ContextManager}
+ */
+WebInspector._javascript_ContextManager = null;
 
 /**
  * @constructor
@@ -117,8 +131,9 @@
 }
 
 /**
- * @param {WebInspector.ExecutionContext} a
- * @param {WebInspector.ExecutionContext} b
+ * @param {*} a
+ * @param {*} b
+ * @return {number}
  */
 WebInspector.ExecutionContext.comparator = function(a, b)
 {
@@ -163,8 +178,10 @@
     {
         if (context.isMainWorldContext)
             this._mainWorldContext = context;
-        else
-            this._isolatedContexts.push(context);
+        else {
+            var insertAt = insertionIndexForObjectInListSortedByFunction(context, this._isolatedContexts, WebInspector.ExecutionContext.comparator);
+            this._isolatedContexts.splice(insertAt, 0, context);
+        }
         this.dispatchEventToListeners(WebInspector.FrameEvaluationContext.EventTypes.AddedExecutionContext, this);
     },
 
@@ -175,11 +192,21 @@
 
     isolatedContexts: function()
     {
-        if (this._isolatedContexts.length)
-            this._isolatedContexts.sort(WebInspector.ExecutionContext.comparator);
         return this._isolatedContexts;
     },
 
+    /**
+     * @param {string} securityOrigin
+     */
+    contextBySecurityOrigin: function(securityOrigin)
+    {
+        for (var i = 0; i < this._isolatedContexts.length; ++i) {
+            var context = this._isolatedContexts[i];
+            if (!context.isMainWorldContext && context.name === securityOrigin)
+                return context; 
+        }
+    },
+
     get frameId()
     {
         return this._frame.id;

Modified: trunk/Source/WebCore/inspector/front-end/inspector.js (116956 => 116957)


--- trunk/Source/WebCore/inspector/front-end/inspector.js	2012-05-14 16:10:01 UTC (rev 116956)
+++ trunk/Source/WebCore/inspector/front-end/inspector.js	2012-05-14 16:34:15 UTC (rev 116957)
@@ -392,7 +392,7 @@
     this.resourceTreeModel = new WebInspector.ResourceTreeModel(this.networkManager);
     this.networkLog = new WebInspector.NetworkLog();
     this.domAgent = new WebInspector.DOMAgent();
-    new WebInspector._javascript_ContextManager(this.resourceTreeModel, this.consoleView);
+    this._javascript_ContextManager = new WebInspector._javascript_ContextManager(this.resourceTreeModel, this.consoleView);
 
     InspectorBackend.registerInspectorDispatcher(this);
 
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to