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 ad0092ef2e13853e4bb8b923481559a043b00ab7..1c2dfd23f18733e21312992877ae1499634d3849 100644 --- a/content/public/renderer/render_frame_observer.h +++ b/content/public/renderer/render_frame_observer.h @@ -150,6 +150,8 @@ class CONTENT_EXPORT RenderFrameObserver 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 1fd800fe2d56c510aed0d532dc3d370538da348f..85257faa0446475f21eb762d53038ce33528d569 100644 --- a/content/renderer/render_frame_impl.cc +++ b/content/renderer/render_frame_impl.cc @@ -4773,6 +4773,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 12a8d900d76119fa3a3372d55cb545d581659b51..5d3cfd296e9296941cf756f58e227558f5b4fdce 100644 --- a/content/renderer/render_frame_impl.h +++ b/content/renderer/render_frame_impl.h @@ -648,6 +648,8 @@ class CONTENT_EXPORT RenderFrameImpl void DidObserveLayoutShift(double score, bool after_input_or_scroll) 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 5031b829fa08c8cae8c8b9890b0fa0b3bfb4f9f4..5f3d93352f5c8388fcfe495fb66c5cbd05024852 100644 --- a/third_party/blink/public/web/web_local_frame_client.h +++ b/third_party/blink/public/web/web_local_frame_client.h @@ -661,6 +661,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 f7e0144c74f879e9b29871d7c372b99e127966bb..c3cd7b77ed282f212a56d151dc3fbec36d183701 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 @@ -217,6 +217,7 @@ void LocalWindowProxy::Initialize() { } InstallConditionalFeatures(); + GetFrame()->Client()->DidInstallConditionalFeatures(context, world_->GetWorldId()); if (World().IsMainWorld()) { probe::DidCreateMainWorldContext(GetFrame()); 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 2cffa645b83caf8ce2e0beffd085abbc980a7db3..34c8c20723cdf1d27296cd8ef608c493ffbaba8a 100644 --- a/third_party/blink/renderer/core/frame/local_frame_client.h +++ b/third_party/blink/renderer/core/frame/local_frame_client.h @@ -301,6 +301,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/frame/local_frame_client_impl.cc b/third_party/blink/renderer/core/frame/local_frame_client_impl.cc index bff19ab30f8e399d16069c05364d4180ec464864..8f557753ea2fe370f7964e3f41019ebf5d6d6df6 100644 --- a/third_party/blink/renderer/core/frame/local_frame_client_impl.cc +++ b/third_party/blink/renderer/core/frame/local_frame_client_impl.cc @@ -294,6 +294,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/frame/local_frame_client_impl.h b/third_party/blink/renderer/core/frame/local_frame_client_impl.h index b9f3c7cb372bd0ba2b8131594e175c3bcda28771..9d1cd076a97474a451504a866c7a02bd16a70d1c 100644 --- a/third_party/blink/renderer/core/frame/local_frame_client_impl.h +++ b/third_party/blink/renderer/core/frame/local_frame_client_impl.h @@ -83,6 +83,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/loader/empty_clients.h b/third_party/blink/renderer/core/loader/empty_clients.h index 821447d9510b7bffce71bb9cb797c95e526fe890..b1f4f8abdd2b4d1a669274f55588d6d936f0c691 100644 --- a/third_party/blink/renderer/core/loader/empty_clients.h +++ b/third_party/blink/renderer/core/loader/empty_clients.h @@ -416,6 +416,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; }