Commit f66a2d5e authored by Alex Clarke's avatar Alex Clarke Committed by Commit Bot

Page.createIsolatedWorld to return execution context id

This makes it easier to evaluate script in the newly created isolated 
world.

Bug: 
Change-Id: I4ff82f79333f2e4ef69790c1cbb1da280ecc78cb
Reviewed-on: https://chromium-review.googlesource.com/541336Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Commit-Queue: Alex Clarke <alexclarke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#481900}
parent 241f77e5
Runtime enabled Runtime enabled
Page enabled Page enabled
Main Frame obtained Main Frame obtained
PASS - isolated world created. PASS - execution context id match.
...@@ -6,20 +6,34 @@ ...@@ -6,20 +6,34 @@
function test() function test()
{ {
var mainFrameId = ""; var mainFrameId = "";
var executionContextId;
var createIsolatedWorldReturnValue;
InspectorTest.eventHandler["Runtime.executionContextCreated"] = InspectorTest.eventHandler["Runtime.executionContextCreated"] =
function(message) { function(message) {
if (message.params.context.auxData.frameId !== mainFrameId) if (message.params.context.auxData.frameId !== mainFrameId)
return; return;
if (message.params.context.auxData.isDefault === false && if (message.params.context.auxData.isDefault === false &&
message.params.context.name === "Test world") { message.params.context.name === "Test world") {
InspectorTest.log("PASS - isolated world created."); executionContextId = message.params.context.id;
maybeFinish();
} else { } else {
InspectorTest.log("fail - main world created."); InspectorTest.log("fail - main world created.");
InspectorTest.log(JSON.stringify(message.params)); InspectorTest.log(JSON.stringify(message.params));
InspectorTest.completeTest();
} }
InspectorTest.completeTest();
}; };
function maybeFinish() {
if (!executionContextId || !createIsolatedWorldReturnValue)
return;
if (executionContextId === createIsolatedWorldReturnValue) {
InspectorTest.log("PASS - execution context id match.");
} else {
InspectorTest.log("fail - execution context id differ.");
}
InspectorTest.completeTest();
}
InspectorTest.sendCommandOrDie("Runtime.enable", {}, runtimeAgentEnabled); InspectorTest.sendCommandOrDie("Runtime.enable", {}, runtimeAgentEnabled);
function runtimeAgentEnabled() { function runtimeAgentEnabled() {
...@@ -42,7 +56,13 @@ function test() ...@@ -42,7 +56,13 @@ function test()
function onMainFrameId(mainFrameId) { function onMainFrameId(mainFrameId) {
InspectorTest.log("Main Frame obtained"); InspectorTest.log("Main Frame obtained");
InspectorTest.sendCommandOrDie("Page.createIsolatedWorld", { InspectorTest.sendCommandOrDie("Page.createIsolatedWorld", {
frameId: mainFrameId, worldName: "Test world"}); frameId: mainFrameId, worldName: "Test world"},
isolatedWorldCreated);
}
function isolatedWorldCreated(payload) {
createIsolatedWorldReturnValue = payload.executionContextId;
maybeFinish();
} }
} }
......
...@@ -359,20 +359,20 @@ void ScriptController::ExecuteScriptInIsolatedWorld( ...@@ -359,20 +359,20 @@ void ScriptController::ExecuteScriptInIsolatedWorld(
} }
} }
int ScriptController::CreateNewDInspectorIsolatedWorld( PassRefPtr<DOMWrapperWorld> ScriptController::CreateNewInspectorIsolatedWorld(
const String& world_name) { const String& world_name) {
RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::Create( RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::Create(
GetIsolate(), DOMWrapperWorld::WorldType::kInspectorIsolated); GetIsolate(), DOMWrapperWorld::WorldType::kInspectorIsolated);
// Bail out if we could not create an isolated world. // Bail out if we could not create an isolated world.
if (!world) if (!world)
return DOMWrapperWorld::kInvalidWorldId; return nullptr;
if (!world_name.IsEmpty()) { if (!world_name.IsEmpty()) {
DOMWrapperWorld::SetIsolatedWorldHumanReadableName(world->GetWorldId(), DOMWrapperWorld::SetIsolatedWorldHumanReadableName(world->GetWorldId(),
world_name); world_name);
} }
// Make sure the execution context exists. // Make sure the execution context exists.
WindowProxy(*world); WindowProxy(*world);
return world->GetWorldId(); return world;
} }
} // namespace blink } // namespace blink
...@@ -112,9 +112,9 @@ class CORE_EXPORT ScriptController final ...@@ -112,9 +112,9 @@ class CORE_EXPORT ScriptController final
bool ExecuteScriptIfJavaScriptURL(const KURL&, Element*); bool ExecuteScriptIfJavaScriptURL(const KURL&, Element*);
// Creates a new isolated world for DevTools with the given human readable // Creates a new isolated world for DevTools with the given human readable
// |world_name| and returns it's id or DOMWrapperWorld::kInvalidWorldId on // |world_name| and returns it id or nullptr on failure.
// failure. PassRefPtr<DOMWrapperWorld> CreateNewInspectorIsolatedWorld(
int CreateNewDInspectorIsolatedWorld(const String& world_name); const String& world_name);
// Returns true if the current world is isolated, and has its own Content // Returns true if the current world is isolated, and has its own Content
// Security Policy. In this case, the policy of the main world should be // Security Policy. In this case, the policy of the main world should be
......
...@@ -71,6 +71,7 @@ ...@@ -71,6 +71,7 @@
#include "platform/wtf/Vector.h" #include "platform/wtf/Vector.h"
#include "platform/wtf/text/Base64.h" #include "platform/wtf/text/Base64.h"
#include "platform/wtf/text/TextEncoding.h" #include "platform/wtf/text/TextEncoding.h"
#include "v8/include/v8-inspector.h"
namespace blink { namespace blink {
...@@ -924,23 +925,32 @@ Response InspectorPageAgent::getLayoutMetrics( ...@@ -924,23 +925,32 @@ Response InspectorPageAgent::getLayoutMetrics(
protocol::Response InspectorPageAgent::createIsolatedWorld( protocol::Response InspectorPageAgent::createIsolatedWorld(
const String& frame_id, const String& frame_id,
Maybe<String> world_name, Maybe<String> world_name,
Maybe<bool> grant_universal_access) { Maybe<bool> grant_universal_access,
int* execution_context_id) {
LocalFrame* frame = LocalFrame* frame =
IdentifiersFactory::FrameById(inspected_frames_, frame_id); IdentifiersFactory::FrameById(inspected_frames_, frame_id);
if (!frame) if (!frame)
return Response::Error("No frame for given id found"); return Response::Error("No frame for given id found");
int world_id = frame->GetScriptController().CreateNewDInspectorIsolatedWorld( RefPtr<DOMWrapperWorld> world =
world_name.fromMaybe("")); frame->GetScriptController().CreateNewInspectorIsolatedWorld(
if (world_id == DOMWrapperWorld::kInvalidWorldId) world_name.fromMaybe(""));
if (!world)
return Response::Error("Could not create isolated world"); return Response::Error("Could not create isolated world");
if (grant_universal_access.fromMaybe(false)) { if (grant_universal_access.fromMaybe(false)) {
RefPtr<SecurityOrigin> security_origin = RefPtr<SecurityOrigin> security_origin =
frame->GetSecurityContext()->GetSecurityOrigin()->IsolatedCopy(); frame->GetSecurityContext()->GetSecurityOrigin()->IsolatedCopy();
security_origin->GrantUniversalAccess(); security_origin->GrantUniversalAccess();
DOMWrapperWorld::SetIsolatedWorldSecurityOrigin(world_id, security_origin); DOMWrapperWorld::SetIsolatedWorldSecurityOrigin(world->GetWorldId(),
security_origin);
} }
LocalWindowProxy* isolated_world_window_proxy =
frame->GetScriptController().WindowProxy(*world);
v8::HandleScope handle_scope(V8PerIsolateData::MainThreadIsolate());
*execution_context_id = v8_inspector::V8ContextInfo::executionContextId(
isolated_world_window_proxy->ContextIfInitialized());
return Response::OK(); return Response::OK();
} }
......
...@@ -143,10 +143,10 @@ class CORE_EXPORT InspectorPageAgent final ...@@ -143,10 +143,10 @@ class CORE_EXPORT InspectorPageAgent final
std::unique_ptr<protocol::Page::LayoutViewport>*, std::unique_ptr<protocol::Page::LayoutViewport>*,
std::unique_ptr<protocol::Page::VisualViewport>*, std::unique_ptr<protocol::Page::VisualViewport>*,
std::unique_ptr<protocol::DOM::Rect>*) override; std::unique_ptr<protocol::DOM::Rect>*) override;
protocol::Response createIsolatedWorld( protocol::Response createIsolatedWorld(const String& frame_id,
const String& frame_id, Maybe<String> world_name,
Maybe<String> world_name, Maybe<bool> grant_universal_access,
Maybe<bool> grant_universal_access) override; int* execution_context_id) override;
// InspectorInstrumentation API // InspectorInstrumentation API
void DidClearDocumentOfWindowObject(LocalFrame*); void DidClearDocumentOfWindowObject(LocalFrame*);
......
...@@ -539,6 +539,9 @@ ...@@ -539,6 +539,9 @@
{ "name": "frameId", "$ref": "FrameId", "description": "Id of the frame in which the isolated world should be created." }, { "name": "frameId", "$ref": "FrameId", "description": "Id of the frame in which the isolated world should be created." },
{ "name": "worldName", "type": "string", "optional": true, "description": "An optional name which is reported in the Execution Context." }, { "name": "worldName", "type": "string", "optional": true, "description": "An optional name which is reported in the Execution Context." },
{ "name": "grantUniveralAccess", "type": "boolean", "optional": true, "description": "Whether or not universal access should be granted to the isolated world. This is a powerful option, use with caution." } { "name": "grantUniveralAccess", "type": "boolean", "optional": true, "description": "Whether or not universal access should be granted to the isolated world. This is a powerful option, use with caution." }
],
"returns": [
{ "name": "executionContextId", "$ref": "Runtime.ExecutionContextId", "description": "Execution context of the isolated world." }
] ]
} }
], ],
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment