Skip to content

Instantly share code, notes, and snippets.

@jeremyroman
Created November 19, 2019 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremyroman/accc3d439ace3198b2140cfe3a7a4a23 to your computer and use it in GitHub Desktop.
Save jeremyroman/accc3d439ace3198b2140cfe3a7a4a23 to your computer and use it in GitHub Desktop.
diff --git a/third_party/blink/renderer/modules/modules_initializer.cc b/third_party/blink/renderer/modules/modules_initializer.cc
index 4eaa4575ca63..50e56db64b5b 100644
--- a/third_party/blink/renderer/modules/modules_initializer.cc
+++ b/third_party/blink/renderer/modules/modules_initializer.cc
@@ -170,6 +170,8 @@ void ModulesInitializer::InitLocalFrame(LocalFrame& frame) const {
&AppBannerController::BindMojoRequest, WrapWeakPersistent(&frame)));
frame.GetInterfaceRegistry()->AddInterface(WTF::BindRepeating(
&TextSuggestionBackendImpl::Create, WrapWeakPersistent(&frame)));
+ frame.GetInterfaceRegistry()->AddInterface(WTF::BindRepeating(
+ &RemoteObjectGatewayImpl::BindMojoReceiver, WrapWeakPersistent(&frame)));
}
void ModulesInitializer::InstallSupplements(LocalFrame& frame) const {
@@ -193,7 +195,6 @@ void ModulesInitializer::InstallSupplements(LocalFrame& frame) const {
ImageDownloaderImpl::ProvideTo(frame);
}
MediaInspectorContextImpl::ProvideToLocalFrame(frame);
- RemoteObjectGatewayImpl::ProvideTo(frame);
}
void ModulesInitializer::ProvideLocalFileSystemToWorker(
@@ -251,6 +252,11 @@ void ModulesInitializer::OnClearWindowObjectInMainWorld(
// presentation can offer a connection to the presentation receiver.
PresentationReceiver::From(document);
}
+
+ LocalFrame* frame = document.GetFrame();
+ DCHECK(frame);
+ if (auto* gateway = RemoteObjectGatewayImpl::From(*frame))
+ gateway->OnClearWindowObjectInMainWorld();
}
std::unique_ptr<WebMediaPlayer> ModulesInitializer::CreateWebMediaPlayer(
diff --git a/third_party/blink/renderer/modules/remote_objects/remote_object_gateway_impl.cc b/third_party/blink/renderer/modules/remote_objects/remote_object_gateway_impl.cc
index b6abb7a9f722..9f5163cabf1b 100644
--- a/third_party/blink/renderer/modules/remote_objects/remote_object_gateway_impl.cc
+++ b/third_party/blink/renderer/modules/remote_objects/remote_object_gateway_impl.cc
@@ -51,14 +51,14 @@ void RemoteObjectGatewayImpl::ProvideTo(LocalFrame& frame) {
frame, MakeGarbageCollected<RemoteObjectGatewayImpl>(frame));
}
-void RemoteObjectGatewayImpl::InjectNamed(blink::WebLocalFrame* frame,
- const WTF::String& object_name,
+void RemoteObjectGatewayImpl::InjectNamed(const WTF::String& object_name,
int32_t object_id) {
LOG(ERROR) << "RemoteObjectGatewayImpl::InjectNamed this = " << this
<< ", name = " << object_name;
- v8::Isolate* isolate = V8PerIsolateData::MainThreadIsolate();
- v8::HandleScope handle_scope(isolate);
- v8::Local<v8::Context> context = frame->MainWorldScriptContext();
+ ScriptState* script_state = ToScriptStateForMainWorld(GetSupplementable());
+ ScriptState::Scope scope(script_state);
+ v8::Isolate* isolate = script_state->GetIsolate();
+ v8::Local<v8::Context> context = script_state->GetContext();
if (context.IsEmpty())
return;
@@ -77,21 +77,30 @@ void RemoteObjectGatewayImpl::InjectNamed(blink::WebLocalFrame* frame,
.Check();
}
+void RemoteObjectGatewayImpl::OnClearWindowObjectInMainWorld() {
+ for (const auto& pair : named_objects_)
+ InjectNamed(pair.first, pair.second);
+}
+
RemoteObjectGatewayImpl::RemoteObjectGatewayImpl(LocalFrame& frame)
: Supplement<LocalFrame>(frame) {
LOG(ERROR) << "RemoteObjectGatewayImpl this = " << this;
- frame.GetInterfaceRegistry()->AddInterface(WTF::BindRepeating(
- &RemoteObjectGatewayImpl::BindReceiver, WrapWeakPersistent(this)));
}
RemoteObjectGatewayImpl::~RemoteObjectGatewayImpl() {
LOG(ERROR) << "~RemoteObjectGatewayImpl this = " << this;
}
-void RemoteObjectGatewayImpl::BindReceiver(
+// static
+void RemoteObjectGatewayImpl::BindMojoReceiver(
+ LocalFrame* frame,
mojo::PendingReceiver<mojom::blink::RemoteObjectGateway> receiver) {
+ if (!frame)
+ return;
LOG(ERROR) << "RemoteObjectGatewayImpl BindReceiver";
- receiver_.Bind(std::move(receiver));
+ ProvideTo(*frame);
+ auto* self = From(*frame);
+ self->receiver_.Bind(std::move(receiver));
}
void RemoteObjectGatewayImpl::SetHost(
@@ -101,7 +110,10 @@ void RemoteObjectGatewayImpl::SetHost(
void RemoteObjectGatewayImpl::AddNamedObject(const WTF::String& name,
int32_t id) {
- InjectNamed(GetSupplementable()->Client()->GetWebFrame(), name, id);
+ // TODO(jbroman, oksamyt): There might be more subtlety about whether we want
+ // the exact same wrapper object across window object clears or not . . .
+ named_objects_.emplace_back(name, id);
+ InjectNamed(name, id);
}
void RemoteObjectGatewayImpl::RemoveNamedObject(const WTF::String& name) {}
diff --git a/third_party/blink/renderer/modules/remote_objects/remote_object_gateway_impl.h b/third_party/blink/renderer/modules/remote_objects/remote_object_gateway_impl.h
index ee2114b0c708..6dacaca1bb7a 100644
--- a/third_party/blink/renderer/modules/remote_objects/remote_object_gateway_impl.h
+++ b/third_party/blink/renderer/modules/remote_objects/remote_object_gateway_impl.h
@@ -54,12 +54,17 @@ class MODULES_EXPORT RemoteObjectGatewayImpl final
~RemoteObjectGatewayImpl() override;
+ static void BindMojoReceiver(
+ LocalFrame*, mojo::PendingReceiver<mojom::blink::RemoteObjectGateway>);
+
static RemoteObjectGatewayImpl* From(LocalFrame&);
static void ProvideTo(LocalFrame&);
- void InjectNamed(blink::WebLocalFrame* frame,
- const WTF::String& object_name,
+ void InjectNamed(const WTF::String& object_name,
int32_t object_id);
+
+ void OnClearWindowObjectInMainWorld();
+
void Trace(Visitor* visitor) override {
Supplement<LocalFrame>::Trace(visitor);
}
@@ -71,9 +76,10 @@ class MODULES_EXPORT RemoteObjectGatewayImpl final
void AddNamedObject(const WTF::String& name, int32_t id) override;
void RemoveNamedObject(const WTF::String& name) override;
- void BindReceiver(
- mojo::PendingReceiver<mojom::blink::RemoteObjectGateway> receiver);
+ Vector<std::pair<String, int32_t>> named_objects_;
+ // TODO(jbroman, oksamyt): Can this only ever be requested once? We might want
+ // a ReceiverSet instead. (I'm also a little worried about finalization here.)
mojo::Receiver<mojom::blink::RemoteObjectGateway> receiver_{this};
mojo::Remote<mojom::blink::RemoteObjectHost> object_host_;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment