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 831a3574055881f31a3e7dbc98c0525b47af5dcb..9626f181ee471fec68af7ddfc968f2d852698b74 100644 --- a/content/public/renderer/render_frame_observer.h +++ b/content/public/renderer/render_frame_observer.h @@ -114,6 +114,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 c01f9074c4896d7d67609efa86d5a0e995fa974c..95cec369e38dada721cbf26fe244461171d748f8 100644 --- a/content/renderer/render_frame_impl.cc +++ b/content/renderer/render_frame_impl.cc @@ -4807,6 +4807,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 2d34029a192ad9680d445ae6c5394f1fbfc7e894..9f21385919593cfb1314d9fb659e4b5a3326bf44 100644 --- a/content/renderer/render_frame_impl.h +++ b/content/renderer/render_frame_impl.h @@ -751,6 +751,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 5ab90fc0fb34c9bc5920b7d7efcb8819fed358c0..0a86dc4fc70e6c16e4cb27d6512ae5f589cd1125 100644 --- a/third_party/blink/public/web/web_local_frame_client.h +++ b/third_party/blink/public/web/web_local_frame_client.h @@ -548,6 +548,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 1841d4bea144e2fe0fb69839d52ff2ba4aa6b551..64c5eb7d0da7684531b044de9d6d114fc897786e 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 @@ -201,6 +201,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 8639eecc68c75c311af8ae00c8e028b63624a986..e66039442fbd4d3a456a724afe5eb28314459dc5 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 @@ -348,6 +348,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 2dd3d3d0d036cce2ad49be52915e00c6872eca43..b7c0f955a3388f2c7758d2058d3077e2ba4e8496 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 @@ -78,6 +78,8 @@ class 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 b9a8d9bce7a02a6f46b91697efd4a32d62638f57..99d6bbdf43748adabf2084310abe458383a25f77 100644 --- a/third_party/blink/renderer/core/frame/local_frame_client.h +++ b/third_party/blink/renderer/core/frame/local_frame_client.h @@ -297,6 +297,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 7f060dbe946a6df2d4e4f376512679390edadc07..435882fafb3e08b94424e812ee825e7a042dfef3 100644 --- a/third_party/blink/renderer/core/loader/empty_clients.h +++ b/third_party/blink/renderer/core/loader/empty_clients.h @@ -334,6 +334,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; }