From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jeremy Apthorp Date: Wed, 15 Jan 2020 16:35:18 -0800 Subject: add DidInstallConditionalFeatures This adds a hook on script context creation _after conditional features have been installed_. Electron uses this to run preload scripts and various other initialization. This is necessary because at the time DidCreateScriptContext is called, not all JS APIs are available in the context, which can cause some preload scripts to trip. diff --git a/content/public/renderer/render_frame_observer.h b/content/public/renderer/render_frame_observer.h index 8ab07f180b0e5d7baaa314cf1121c7182de7cce9..feb76f5278ba11f82c65e298d9772fc8742739a9 100644 --- a/content/public/renderer/render_frame_observer.h +++ b/content/public/renderer/render_frame_observer.h @@ -115,6 +115,8 @@ class CONTENT_EXPORT RenderFrameObserver : public IPC::Listener, virtual void DidHandleOnloadEvents() {} virtual void DidCreateScriptContext(v8::Local context, int32_t world_id) {} + virtual void DidInstallConditionalFeatures(v8::Local context, + int32_t world_id) {} virtual void WillReleaseScriptContext(v8::Local context, int32_t world_id) {} virtual void DidClearWindowObject() {} diff --git a/content/renderer/render_frame_impl.cc b/content/renderer/render_frame_impl.cc index bcac6e71666b3f3a820a4311ab04e3d26d06618a..f9e97c36bca9c70af0c585b81902165c1ac6ba5d 100644 --- a/content/renderer/render_frame_impl.cc +++ b/content/renderer/render_frame_impl.cc @@ -4996,6 +4996,12 @@ void RenderFrameImpl::DidCreateScriptContext(v8::Local context, observer.DidCreateScriptContext(context, world_id); } +void RenderFrameImpl::DidInstallConditionalFeatures( + v8::Local context, int world_id) { + for (auto& observer : observers_) + observer.DidInstallConditionalFeatures(context, world_id); +} + void RenderFrameImpl::WillReleaseScriptContext(v8::Local context, int world_id) { for (auto& observer : observers_) diff --git a/content/renderer/render_frame_impl.h b/content/renderer/render_frame_impl.h index 905845607558ac3af57ba33a1537ebb4ff0e8e7a..8e95993cc9ae88d57b373ff194e97976154fb91d 100644 --- a/content/renderer/render_frame_impl.h +++ b/content/renderer/render_frame_impl.h @@ -717,6 +717,8 @@ class CONTENT_EXPORT RenderFrameImpl bool ShouldTrackUseCounter(const blink::WebURL& url) override; void DidCreateScriptContext(v8::Local context, int world_id) override; + void DidInstallConditionalFeatures(v8::Local context, + int world_id) override; void WillReleaseScriptContext(v8::Local context, int world_id) override; void DidChangeScrollOffset() override; diff --git a/third_party/blink/public/web/web_local_frame_client.h b/third_party/blink/public/web/web_local_frame_client.h index eecf846eabce6429ef61f9ef333572644dcbe129..e490f7d5977f946e36a3a824ad60acc0d5629780 100644 --- a/third_party/blink/public/web/web_local_frame_client.h +++ b/third_party/blink/public/web/web_local_frame_client.h @@ -535,6 +535,9 @@ class BLINK_EXPORT WebLocalFrameClient { virtual void DidCreateScriptContext(v8::Local, int32_t world_id) {} + virtual void DidInstallConditionalFeatures(v8::Local, + int32_t world_id) {} + // WebKit is about to release its reference to a v8 context for a frame. virtual void WillReleaseScriptContext(v8::Local, int32_t world_id) {} diff --git a/third_party/blink/renderer/bindings/core/v8/local_window_proxy.cc b/third_party/blink/renderer/bindings/core/v8/local_window_proxy.cc index 4f4efaadcc60ed5d60fca74b9938b472eb67295d..6ec9d80ba9c4e167c5ec474e8de6ea655e928481 100644 --- a/third_party/blink/renderer/bindings/core/v8/local_window_proxy.cc +++ b/third_party/blink/renderer/bindings/core/v8/local_window_proxy.cc @@ -203,6 +203,7 @@ void LocalWindowProxy::Initialize() { } InstallConditionalFeatures(); + GetFrame()->Client()->DidInstallConditionalFeatures(context, world_->GetWorldId()); if (World().IsMainWorld()) { GetFrame()->Loader().DispatchDidClearWindowObjectInMainWorld(); diff --git a/third_party/blink/renderer/core/exported/local_frame_client_impl.cc b/third_party/blink/renderer/core/exported/local_frame_client_impl.cc index b99784880e15e5b8f8dbe2ae1616d71ffd7a16d1..d6bbf992272e13e2f4f2478af66c6e4ce19e3d24 100644 --- a/third_party/blink/renderer/core/exported/local_frame_client_impl.cc +++ b/third_party/blink/renderer/core/exported/local_frame_client_impl.cc @@ -392,6 +392,13 @@ void LocalFrameClientImpl::DidCreateScriptContext( web_frame_->Client()->DidCreateScriptContext(context, world_id); } +void LocalFrameClientImpl::DidInstallConditionalFeatures( + v8::Local context, + int32_t world_id) { + if (web_frame_->Client()) + web_frame_->Client()->DidInstallConditionalFeatures(context, world_id); +} + void LocalFrameClientImpl::WillReleaseScriptContext( v8::Local context, int32_t world_id) { diff --git a/third_party/blink/renderer/core/exported/local_frame_client_impl.h b/third_party/blink/renderer/core/exported/local_frame_client_impl.h index 1a9e59e20ebfcdaac5fc3a3b4098e4ec6f9192f4..7da3cdcf3ca099e81b8ac94e87c697ad2d841e61 100644 --- a/third_party/blink/renderer/core/exported/local_frame_client_impl.h +++ b/third_party/blink/renderer/core/exported/local_frame_client_impl.h @@ -77,6 +77,8 @@ class CORE_EXPORT LocalFrameClientImpl final : public LocalFrameClient { void DidCreateScriptContext(v8::Local, int32_t world_id) override; + void DidInstallConditionalFeatures(v8::Local, + int32_t world_id) override; void WillReleaseScriptContext(v8::Local, int32_t world_id) override; diff --git a/third_party/blink/renderer/core/frame/local_frame_client.h b/third_party/blink/renderer/core/frame/local_frame_client.h index 831ce5ccc32676790307d49416a3a40e7e3652bc..8898a6162a759a3a4d313d8956051b6208acd4af 100644 --- a/third_party/blink/renderer/core/frame/local_frame_client.h +++ b/third_party/blink/renderer/core/frame/local_frame_client.h @@ -289,6 +289,8 @@ class CORE_EXPORT LocalFrameClient : public FrameClient { virtual void DidCreateScriptContext(v8::Local, int32_t world_id) = 0; + virtual void DidInstallConditionalFeatures(v8::Local, + int32_t world_id) = 0; virtual void WillReleaseScriptContext(v8::Local, int32_t world_id) = 0; virtual bool AllowScriptExtensions() = 0; diff --git a/third_party/blink/renderer/core/loader/empty_clients.h b/third_party/blink/renderer/core/loader/empty_clients.h index 8080a1b3c4efe3854bd12dc4d714838a4e135689..d16d2572a37b46028dfa8d7554f97289918bf792 100644 --- a/third_party/blink/renderer/core/loader/empty_clients.h +++ b/third_party/blink/renderer/core/loader/empty_clients.h @@ -325,6 +325,8 @@ class CORE_EXPORT EmptyLocalFrameClient : public LocalFrameClient { void DidCreateScriptContext(v8::Local, int32_t world_id) override {} + void DidInstallConditionalFeatures(v8::Local, + int32_t world_id) override {} void WillReleaseScriptContext(v8::Local, int32_t world_id) override {} bool AllowScriptExtensions() override { return false; }