Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow backend to send commands to execute preamble scripts. #1122

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions replay-assets/replay_command_handlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const {
fromJsCollectEventListeners,
fromJsDomPerformSearch,

loadPreambleScript,

// network
getCurrentNetworkRequestEvent,
getCurrentNetworkStreamData,
Expand Down Expand Up @@ -272,6 +274,7 @@ const CommandCallbacks = {
"Target.getCurrentNetworkRequestEvent": Target_getCurrentNetworkRequestEvent,
"Target.getCurrentNetworkStreamData": Target_getCurrentNetworkStreamData,
"Target.topFrameLocation": Target_topFrameLocation,
"Target.loadPreamble": Target_loadPreamble,
"Pause.evaluateInFrame": Pause_evaluateInFrame,
"Pause.evaluateInGlobal": Pause_evaluateInGlobal,
"Pause.getAllFrames": Pause_getAllFrames,
Expand Down Expand Up @@ -451,6 +454,10 @@ function Target_getCurrentNetworkStreamData(params) {
}
}

function Target_loadPreamble(script) {
loadPreambleScript(script);
}

function Target_topFrameLocation() {
try {
const { location } = sendCDPMessage("Debugger.getTopFrameLocation");
Expand Down Expand Up @@ -3043,6 +3050,10 @@ function replayEval(fn) {
}
}

function registerGlobal(name, val) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intended to be called by the preamble script, right?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup--the PR description shows a hypothetical usage.

window.top.__RECORD_REPLAY_GLOBALS__[name] = val;
}

/** ###########################################################################
* Export JS API methods via `__RECORD_REPLAY__`.
* This is officially available for scripts in `eval*` commands to use.
Expand All @@ -3059,6 +3070,7 @@ Object.assign(__RECORD_REPLAY__, {
warning,
getFrameArgumentsArray,
getCurrentEvaluateFrame,
registerGlobal,
replayEval
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "base/record_replay.h"
#include "content/public/renderer/render_thread.h"
#include "content/public/renderer/v8_value_converter.h"
#include "third_party/blink/renderer/bindings/core/v8/local_window_proxy.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_binding_for_core.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_document.h"
#include "third_party/blink/renderer/bindings/core/v8/v8_node.h"
Expand Down Expand Up @@ -823,6 +824,10 @@ const char* gOnNewWindowScript = R""""(
// __RECORD_REPLAY__?
window.__RECORD_REPLAY__ = window.top.__RECORD_REPLAY__;
window.__RECORD_REPLAY_ARGUMENTS__ = window.top.__RECORD_REPLAY_ARGUMENTS__;

for (g of window.top.__RECORD_REPLAY_GLOBALS__) {
Copy link

@Domiii Domiii Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • We also need to pass down __RECORD_REPLAY_GLOBALS__ itself.
    • In fact, I'd vote to only pass down __RECORD_REPLAY_GLOBALS__, and just force any user of this API to go through that, rather than adding random globals into a global context that is shared with arbitrary user JS.
  • linter failure.

window[g] = window.top[g];
}
})()
)"""";

Expand Down Expand Up @@ -2350,6 +2355,46 @@ static void fromJsEndReplayCode(
recordreplay::ExitReplayCode();
}

// Connects replay globals that must be accessible from all contexts; MUST be called
// with AutoMarkReplayCode.
static void ConnectPreambleGlobals(LocalFrame* frame, v8::Isolate *isolate) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you pass in the AutoMarkReplayCode object here to ensure that it is held?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ehh, I waffled on this, due to recursion (it just looks distasteful/re-entrant to me); once we fix our context handling, I think we'll end up with a set of Conexts we can just safely iterate over, rather than this traversal.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe name should be ConnectPreambleGlobalsToCurrentContexts or something like that so it's clear what we are connecting the globals to?

for (Frame* child = frame->Tree().FirstChild(); child != nullptr; child = child->Tree().NextSibling()) {
auto* child_local_frame = DynamicTo<LocalFrame>(child);
if (child_local_frame != nullptr) {
v8::Local<v8::Context> frameContext = child_local_frame->WindowProxy(DOMWrapperWorld::MainWorld())->ContextIfInitialized();
if (!frameContext.IsEmpty()) {
RunScript(isolate, frameContext, gOnNewWindowScript,
"record-replay-OnNewWindow");
}
ConnectPreambleGlobals(child_local_frame, isolate);
}
}
}


// Executes the provided script in the root context, and then propagates all registered globals
// throughout all descendent contexts.
static void loadPreambleScript(
const v8::FunctionCallbackInfo<v8::Value>& args) {
CHECK(args.Length() == 1 && args[0]->IsString() &&
"[RuntimeError] must be called with a string");

v8::String::Utf8Value script(args.GetIsolate(), args[0]);

recordreplay::AutoMarkReplayCode amrc;

LocalFrame* rootFrame = GetLocalFrameRoot(args.GetIsolate());
v8::Local<v8::Context> rootContext = rootFrame->WindowProxy(DOMWrapperWorld::MainWorld())->ContextIfInitialized();

if (!rootContext.IsEmpty()) {
RunScript(args.GetIsolate(), rootContext, *script, "record-replay-preambleScript");

ConnectPreambleGlobals(rootFrame, args.GetIsolate());
} else {
recordreplay::Warning("loadPreambleScript: no root context found. Skipping.");
}
}

/** ###########################################################################
* misc
* ##########################################################################*/
Expand Down Expand Up @@ -2502,6 +2547,10 @@ static void InitializeRecordReplayApiObjects(v8::Isolate* isolate, LocalFrame* l
DefineProperty(isolate, context->Global(), "__RECORD_REPLAY_ARGUMENTS__",
args);

v8::Local<v8::Object> globals = v8::Object::New(isolate);
DefineProperty(isolate, context->Global(), "__RECORD_REPLAY_GLOBALS__",
Copy link

@Domiii Domiii Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will reset the globals on every root-level navigation. We probably want to keep them around or re-run the preamble.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I was also confused about this. do we need to store the scripts somewhere so we can rerun them?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Domi and I have been chatting about our context handling, which is a tad naiive to say the least; we aren't going to worry about it in this PR, but rather address it wholly in another.

globals);

DefineProperty(isolate, args, "REPLAY_CDT_PAUSE_OBJECT_GROUP",
ToV8String(isolate, REPLAY_CDT_PAUSE_OBJECT_GROUP));

Expand Down Expand Up @@ -2570,6 +2619,8 @@ static void InitializeRecordReplayApiObjects(v8::Isolate* isolate, LocalFrame* l
fromJsBeginReplayCode);
SetFunctionProperty(isolate, args, "endReplayCode",
fromJsEndReplayCode);
SetFunctionProperty(isolate, args, "loadPreambleScript",
loadPreambleScript);

// unsorted Replay stuff
SetFunctionProperty(
Expand Down