Thanks, that won't work for our purposes though, unfortunately.
I was able to find a workaround for anyone wondering.
Since AlloyContentRendererClient inherits from chromium's ContentRendererClient, we can override DidInitializeWorkerContextOnWorkerThread in our content renderer client and receive the newly created worker context that way.
This works for both dedicated and shared workers, but obviously now we're working with v8::Local<v8::Context> instead of CefRefPtr<CefV8Context>
Basically something like this (in alloy_content_renderer_client.h) to create a JS variable "myVar" and set it to 42 in the new worker's context;
- Code: Select all
virtual void DidInitializeWorkerContextOnWorkerThread(v8::Local<v8::Context> context) override {
LOG(INFO) << "DidInitializeWorkerContextOnWorkerThread";
v8::Isolate* isolate = context->GetIsolate();
v8::MicrotasksScope microtasks_scope(isolate, v8::MicrotasksScope::kDoNotRunMicrotasks);
v8::Local<v8::Object> global = context->Global();
v8::Local<v8::String> key = v8::String::NewFromUtf8Literal(isolate, "myVar");
v8::Local<v8::Value> value = v8::Integer::New(isolate, 42);
global->Set(context, key, value);
}
Without kDoNotRunMicrotasks scope we hit a DCHECK in the call to Set.
Use at your own risk. I just figured this out and haven't tested it extensively, no idea if this is safe.