Commit 8dd71fb5 authored by Andrey Lushnikov's avatar Andrey Lushnikov Committed by Commit Bot

DevTools: execute scripts in addScriptToEvaluateOnLoad in order

Scripts added by addScriptToEvaluateOnLoad used to be evaluated
in the order of their addition.

This was regressed recently with https://crrev.com/578514.

R=caseq, pfeldman

Change-Id: Ieabd67aca497fab8eaff749fea0877d93c54deae
Reviewed-on: https://chromium-review.googlesource.com/1180585Reviewed-by: default avatarAndrey Kosyakov <caseq@chromium.org>
Commit-Queue: Andrey Lushnikov <lushnikov@chromium.org>
Cr-Commit-Position: refs/heads/master@{#584293}
parent 1e10f438
Tests that Page.addScriptToEvaluateOnLoad is executed in the order of addition
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
(async function(testRunner) {
const {page, session, dp} = await testRunner.startBlank(
'Tests that Page.addScriptToEvaluateOnLoad is executed in the order of addition');
dp.Runtime.enable();
dp.Page.enable();
dp.Runtime.onConsoleAPICalled(msg => testRunner.log(msg.params.args[0].value));
for (let i = 0; i < 15; ++i) {
await dp.Page.addScriptToEvaluateOnNewDocument({source: `
console.log('message from ${i}');
`});
}
await session.navigate('../resources/blank.html');
testRunner.completeTest();
})
......@@ -444,7 +444,6 @@ InspectorPageAgent::InspectorPageAgent(
: inspected_frames_(inspected_frames),
v8_session_(v8_session),
client_(client),
last_script_identifier_(0),
reloading_(false),
inspector_resource_content_loader_(resource_content_loader),
resource_content_loader_client_id_(
......@@ -542,13 +541,17 @@ Response InspectorPageAgent::disable() {
Response InspectorPageAgent::addScriptToEvaluateOnLoad(const String& source,
String* identifier) {
// Assure we don't override existing ids -- m_lastScriptIdentifier could get
// out of sync WRT actual scripts once we restored the scripts from the cookie
// during navigation.
do {
*identifier = String::Number(++last_script_identifier_);
} while (!scripts_to_evaluate_on_load_.Get(*identifier).IsNull());
scripts_to_evaluate_on_load_.Set(*identifier, source);
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);
} else {
scripts_to_evaluate_on_load_.Set(
String::Number(Decimal::FromString(*result).ToDouble() + 1), source);
}
return Response::OK();
}
......@@ -825,7 +828,13 @@ void InspectorPageAgent::DidNavigateWithinDocument(LocalFrame* frame) {
void InspectorPageAgent::DidClearDocumentOfWindowObject(LocalFrame* frame) {
if (!GetFrontend())
return;
for (const WTF::String& key : scripts_to_evaluate_on_load_.Keys()) {
std::vector<WTF::String> keys = scripts_to_evaluate_on_load_.Keys();
std::sort(keys.begin(), keys.end(),
[](const WTF::String& a, const WTF::String& b) {
return Decimal::FromString(a) < Decimal::FromString(b);
});
for (const WTF::String& key : keys) {
const WTF::String& script = scripts_to_evaluate_on_load_.Get(key);
frame->GetScriptController().ExecuteScriptInMainWorld(script);
}
......
......@@ -237,7 +237,6 @@ class CORE_EXPORT InspectorPageAgent final
HashMap<String, Vector<char>> compilation_cache_;
v8_inspector::V8InspectorSession* v8_session_;
Client* client_;
long last_script_identifier_;
String pending_script_to_evaluate_on_load_once_;
String script_to_evaluate_on_load_once_;
bool reloading_;
......
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