Commit 3321d814 authored by Pavel Feldman's avatar Pavel Feldman

DevTools: allow addScriptToEvaluateOnNewDocument accept optional worldName parameter.

Change-Id: Ic22c1bf38aaf8e5d3e7caabf3c686c8c45b985b0
Reviewed-on: https://chromium-review.googlesource.com/1244460Reviewed-by: default avatarDmitry Gozman <dgozman@chromium.org>
Commit-Queue: Pavel Feldman <pfeldman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#594161}
parent 42e84a42
Tests that Page.addScriptToEvaluateOnNewDocument is executed in the given world
Adding scripts
world#0
message from 0
world#1
message from 1
world#2
message from 2
world#3
message from 3
world#4
message from 4
Removing scripts
(async function(testRunner) {
const {page, session, dp} = await testRunner.startBlank(
'Tests that Page.addScriptToEvaluateOnNewDocument is executed in the given world');
dp.Runtime.enable();
dp.Page.enable();
const scriptIds = [];
dp.Runtime.onConsoleAPICalled(msg => testRunner.log(msg.params.args[0].value));
dp.Runtime.onExecutionContextCreated(msg => {
if (msg.params.context.name.includes('world'))
testRunner.log(msg.params.context.name);
});
testRunner.log('Adding scripts');
for (let i = 0; i < 5; ++i) {
const result = await dp.Page.addScriptToEvaluateOnNewDocument({source: `
console.log('message from ${i}');`, worldName: `world#${i}`});
scriptIds.push(result.result.identifier);
}
await session.navigate('../resources/blank.html');
testRunner.log('Removing scripts');
for (let identifier of scriptIds) {
const response = await dp.Page.removeScriptToEvaluateOnNewDocument({identifier});
if (!response.result)
testRunner.log('Failed script removal');
}
await session.navigate('../resources/blank.html');
testRunner.completeTest();
})
Tests that Page.addScriptToEvaluateOnLoad is executed in the order of addition
Adding scripts
message from 0
message from 1
message from 2
message from 3
message from 4
message from 5
message from 6
message from 7
message from 8
message from 9
message from 10
message from 11
message from 12
message from 13
message from 14
Removing scripts
......@@ -4,13 +4,27 @@
dp.Runtime.enable();
dp.Page.enable();
const scriptIds = [];
dp.Runtime.onConsoleAPICalled(msg => testRunner.log(msg.params.args[0].value));
for (let i = 0; i < 15; ++i) {
await dp.Page.addScriptToEvaluateOnNewDocument({source: `
testRunner.log('Adding scripts');
for (let i = 0; i < 5; ++i) {
const result = await dp.Page.addScriptToEvaluateOnNewDocument({source: `
console.log('message from ${i}');
`});
scriptIds.push(result.result.identifier);
}
await session.navigate('../resources/blank.html');
testRunner.log('Removing scripts');
for (let identifier of scriptIds) {
const response = await dp.Page.removeScriptToEvaluateOnNewDocument({identifier});
if (!response.result)
testRunner.log('Failed script removal');
}
await session.navigate('../resources/blank.html');
testRunner.completeTest();
})
......@@ -4870,6 +4870,10 @@ domain Page
command addScriptToEvaluateOnNewDocument
parameters
string source
# If specified, creates an isolated world with the given name and evaluates given script in it.
# This world name will be used as the ExecutionContextDescription::name when the corresponding
# event is emitted.
experimental optional string worldName
returns
# Identifier of the added script.
ScriptIdentifier identifier
......
......@@ -70,6 +70,8 @@
#include "third_party/blink/renderer/core/probe/core_probes.h"
#include "third_party/blink/renderer/core/style/computed_style.h"
#include "third_party/blink/renderer/platform/bindings/dom_wrapper_world.h"
#include "third_party/blink/renderer/platform/json/json_parser.h"
#include "third_party/blink/renderer/platform/json/json_values.h"
#include "third_party/blink/renderer/platform/loader/fetch/memory_cache.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource_fetcher.h"
......@@ -548,23 +550,29 @@ Response InspectorPageAgent::disable() {
return Response::OK();
}
Response InspectorPageAgent::addScriptToEvaluateOnLoad(const String& source,
String* identifier) {
Response InspectorPageAgent::addScriptToEvaluateOnNewDocument(
const String& source,
Maybe<String> world_name,
String* identifier) {
std::vector<WTF::String> keys = scripts_to_evaluate_on_load_.Keys();
auto result = std::max_element(
keys.begin(), keys.end(), [](const WTF::String& a, const WTF::String& b) {
return Decimal::FromString(a) < Decimal::FromString(b);
});
if (result == keys.end()) {
scripts_to_evaluate_on_load_.Set(String::Number(1), source);
*identifier = String::Number(1);
} else {
scripts_to_evaluate_on_load_.Set(
String::Number(Decimal::FromString(*result).ToDouble() + 1), source);
*identifier = String::Number(Decimal::FromString(*result).ToDouble() + 1);
}
std::unique_ptr<JSONObject> script = JSONObject::Create();
script->SetString("source", source);
script->SetString("world_name", world_name.fromMaybe(""));
scripts_to_evaluate_on_load_.Set(*identifier, script->ToJSONString());
return Response::OK();
}
Response InspectorPageAgent::removeScriptToEvaluateOnLoad(
Response InspectorPageAgent::removeScriptToEvaluateOnNewDocument(
const String& identifier) {
if (scripts_to_evaluate_on_load_.Get(identifier).IsNull())
return Response::Error("Script not found");
......@@ -572,15 +580,15 @@ Response InspectorPageAgent::removeScriptToEvaluateOnLoad(
return Response::OK();
}
Response InspectorPageAgent::addScriptToEvaluateOnNewDocument(
const String& source,
String* identifier) {
return addScriptToEvaluateOnLoad(source, identifier);
Response InspectorPageAgent::addScriptToEvaluateOnLoad(const String& source,
String* identifier) {
return addScriptToEvaluateOnNewDocument(source, Maybe<String>(""),
identifier);
}
Response InspectorPageAgent::removeScriptToEvaluateOnNewDocument(
Response InspectorPageAgent::removeScriptToEvaluateOnLoad(
const String& identifier) {
return removeScriptToEvaluateOnLoad(identifier);
return removeScriptToEvaluateOnNewDocument(identifier);
}
Response InspectorPageAgent::setLifecycleEventsEnabled(bool enabled) {
......@@ -843,10 +851,45 @@ void InspectorPageAgent::DidClearDocumentOfWindowObject(LocalFrame* frame) {
return Decimal::FromString(a) < Decimal::FromString(b);
});
HashMap<String, int> world_id_by_name;
for (const WTF::String& key : keys) {
const WTF::String& script = scripts_to_evaluate_on_load_.Get(key);
frame->GetScriptController().ExecuteScriptInMainWorld(script);
const String script = scripts_to_evaluate_on_load_.Get(key);
std::unique_ptr<JSONObject> object = JSONObject::From(ParseJSON(script));
String source;
DCHECK(object->GetString("source", &source));
String world_name;
DCHECK(object->GetString("world_name", &world_name));
if (world_name.IsEmpty()) {
frame->GetScriptController().ExecuteScriptInMainWorld(source);
continue;
}
auto it = world_id_by_name.find(world_name);
int world_id = 0;
if (it != world_id_by_name.end()) {
world_id = it->value;
} else {
scoped_refptr<DOMWrapperWorld> world =
frame->GetScriptController().CreateNewInspectorIsolatedWorld(
world_name);
if (!world)
continue;
world_id = world->GetWorldId();
world_id_by_name.Set(world_name, world_id);
scoped_refptr<SecurityOrigin> security_origin =
frame->GetSecurityContext()->GetSecurityOrigin()->IsolatedCopy();
security_origin->GrantUniversalAccess();
DOMWrapperWorld::SetIsolatedWorldSecurityOrigin(world_id,
security_origin);
}
v8::HandleScope handle_scope(V8PerIsolateData::MainThreadIsolate());
frame->GetScriptController().ExecuteScriptInIsolatedWorld(
world_id, source, KURL(), kNotSharableCrossOrigin);
}
if (!script_to_evaluate_on_load_once_.IsEmpty()) {
frame->GetScriptController().ExecuteScriptInMainWorld(
script_to_evaluate_on_load_once_);
......
......@@ -115,6 +115,7 @@ class CORE_EXPORT InspectorPageAgent final
const String& identifier) override;
protocol::Response addScriptToEvaluateOnNewDocument(
const String& source,
Maybe<String> world_name,
String* identifier) override;
protocol::Response removeScriptToEvaluateOnNewDocument(
const String& identifier) override;
......
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