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
Page enabled
Main Frame obtained
PASS - isolated world created.
PASS - execution context id match.
......@@ -6,20 +6,34 @@
function test()
{
var mainFrameId = "";
var executionContextId;
var createIsolatedWorldReturnValue;
InspectorTest.eventHandler["Runtime.executionContextCreated"] =
function(message) {
if (message.params.context.auxData.frameId !== mainFrameId)
return;
if (message.params.context.auxData.isDefault === false &&
message.params.context.name === "Test world") {
InspectorTest.log("PASS - isolated world created.");
executionContextId = message.params.context.id;
maybeFinish();
} else {
InspectorTest.log("fail - main world created.");
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);
function runtimeAgentEnabled() {
......@@ -42,7 +56,13 @@ function test()
function onMainFrameId(mainFrameId) {
InspectorTest.log("Main Frame obtained");
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(
}
}
int ScriptController::CreateNewDInspectorIsolatedWorld(
PassRefPtr<DOMWrapperWorld> ScriptController::CreateNewInspectorIsolatedWorld(
const String& world_name) {
RefPtr<DOMWrapperWorld> world = DOMWrapperWorld::Create(
GetIsolate(), DOMWrapperWorld::WorldType::kInspectorIsolated);
// Bail out if we could not create an isolated world.
if (!world)
return DOMWrapperWorld::kInvalidWorldId;
return nullptr;
if (!world_name.IsEmpty()) {
DOMWrapperWorld::SetIsolatedWorldHumanReadableName(world->GetWorldId(),
world_name);
}
// Make sure the execution context exists.
WindowProxy(*world);
return world->GetWorldId();
return world;
}
} // namespace blink
......@@ -112,9 +112,9 @@ class CORE_EXPORT ScriptController final
bool ExecuteScriptIfJavaScriptURL(const KURL&, Element*);
// Creates a new isolated world for DevTools with the given human readable
// |world_name| and returns it's id or DOMWrapperWorld::kInvalidWorldId on
// failure.
int CreateNewDInspectorIsolatedWorld(const String& world_name);
// |world_name| and returns it id or nullptr on failure.
PassRefPtr<DOMWrapperWorld> CreateNewInspectorIsolatedWorld(
const String& world_name);
// 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
......
......@@ -71,6 +71,7 @@
#include "platform/wtf/Vector.h"
#include "platform/wtf/text/Base64.h"
#include "platform/wtf/text/TextEncoding.h"
#include "v8/include/v8-inspector.h"
namespace blink {
......@@ -924,23 +925,32 @@ Response InspectorPageAgent::getLayoutMetrics(
protocol::Response InspectorPageAgent::createIsolatedWorld(
const String& frame_id,
Maybe<String> world_name,
Maybe<bool> grant_universal_access) {
Maybe<bool> grant_universal_access,
int* execution_context_id) {
LocalFrame* frame =
IdentifiersFactory::FrameById(inspected_frames_, frame_id);
if (!frame)
return Response::Error("No frame for given id found");
int world_id = frame->GetScriptController().CreateNewDInspectorIsolatedWorld(
world_name.fromMaybe(""));
if (world_id == DOMWrapperWorld::kInvalidWorldId)
RefPtr<DOMWrapperWorld> world =
frame->GetScriptController().CreateNewInspectorIsolatedWorld(
world_name.fromMaybe(""));
if (!world)
return Response::Error("Could not create isolated world");
if (grant_universal_access.fromMaybe(false)) {
RefPtr<SecurityOrigin> security_origin =
frame->GetSecurityContext()->GetSecurityOrigin()->IsolatedCopy();
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();
}
......
......@@ -143,10 +143,10 @@ class CORE_EXPORT InspectorPageAgent final
std::unique_ptr<protocol::Page::LayoutViewport>*,
std::unique_ptr<protocol::Page::VisualViewport>*,
std::unique_ptr<protocol::DOM::Rect>*) override;
protocol::Response createIsolatedWorld(
const String& frame_id,
Maybe<String> world_name,
Maybe<bool> grant_universal_access) override;
protocol::Response createIsolatedWorld(const String& frame_id,
Maybe<String> world_name,
Maybe<bool> grant_universal_access,
int* execution_context_id) override;
// InspectorInstrumentation API
void DidClearDocumentOfWindowObject(LocalFrame*);
......
......@@ -539,6 +539,9 @@
{ "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": "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