From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 20 Sep 2018 17:45:32 -0700 Subject: can_create_window.patch This adds a hook to the window creation flow so that Electron can intercede and potentially prevent a window from being created. TODO(loc): this patch is currently broken. diff --git a/content/browser/renderer_host/render_frame_host_impl.cc b/content/browser/renderer_host/render_frame_host_impl.cc index bd4f5fefc10ea5366846baca5e936440e861c3d4..d4fbb821e35335fd69820e429447690b01a7fe59 100644 --- a/content/browser/renderer_host/render_frame_host_impl.cc +++ b/content/browser/renderer_host/render_frame_host_impl.cc @@ -5116,6 +5116,7 @@ void RenderFrameHostImpl::CreateNewWindow( last_committed_origin_, params->window_container_type, params->target_url, params->referrer.To(), params->frame_name, params->disposition, *params->features, + params->raw_features, params->body, effective_transient_activation_state, params->opener_suppressed, &no_javascript_access); diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc index 16d2127e706bf4839f596c975e88a0bf4337b3c4..382e7d01444f55650971a0cade2211fab2a37754 100644 --- a/content/browser/web_contents/web_contents_impl.cc +++ b/content/browser/web_contents/web_contents_impl.cc @@ -3598,6 +3598,14 @@ RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow( } auto* new_contents_impl = new_contents.get(); + // Call this earlier than Chrome to associate the web preferences with the + // WebContents before the view gets created. + if (delegate_) { + delegate_->WebContentsCreatedWithFullParams(this, render_process_id, + opener->GetRoutingID(), + params, new_contents_impl); + } + new_contents_impl->GetController().SetSessionStorageNamespace( partition_id, session_storage_namespace); @@ -3639,12 +3647,6 @@ RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow( AddDestructionObserver(new_contents_impl); } - if (delegate_) { - delegate_->WebContentsCreated(this, render_process_id, - opener->GetRoutingID(), params.frame_name, - params.target_url, new_contents_impl); - } - observers_.ForEachObserver([&](WebContentsObserver* observer) { observer->DidOpenRequestedURL(new_contents_impl, opener, params.target_url, params.referrer.To(), diff --git a/content/common/frame.mojom b/content/common/frame.mojom index e959809bb8d20294e3946a73631667e71fca8ec7..68923a8f48ccec81cbc24cff70958cc19f2c00b1 100644 --- a/content/common/frame.mojom +++ b/content/common/frame.mojom @@ -275,6 +275,10 @@ struct CreateNewWindowParams { // The window features to use for the new window. blink.mojom.WindowFeatures features; + + // Extra fields added by Electron. + string raw_features; + network.mojom.URLRequestBody? body; }; // Operation result when the renderer asks the browser to create a new window. diff --git a/content/public/browser/content_browser_client.cc b/content/public/browser/content_browser_client.cc index e1c6c1f7ca02fc5b8d379e435be6d52572e16266..2b0dd7cd7bdfcdc0c5a2e947e7f5faee86546330 100644 --- a/content/public/browser/content_browser_client.cc +++ b/content/public/browser/content_browser_client.cc @@ -544,6 +544,8 @@ bool ContentBrowserClient::CanCreateWindow( const std::string& frame_name, WindowOpenDisposition disposition, const blink::mojom::WindowFeatures& features, + const std::string& raw_features, + const scoped_refptr& body, bool user_gesture, bool opener_suppressed, bool* no_javascript_access) { diff --git a/content/public/browser/content_browser_client.h b/content/public/browser/content_browser_client.h index 2a341fb5d2f9636c827b9f162d7bf0fcd66fe2e1..79d88cab12a1bce6e386fc470622d670bfaa6730 100644 --- a/content/public/browser/content_browser_client.h +++ b/content/public/browser/content_browser_client.h @@ -150,6 +150,7 @@ class NetworkService; class TrustedURLLoaderHeaderClient; } // namespace mojom struct ResourceRequest; +class ResourceRequestBody; } // namespace network namespace sandbox { @@ -874,6 +875,8 @@ class CONTENT_EXPORT ContentBrowserClient { const std::string& frame_name, WindowOpenDisposition disposition, const blink::mojom::WindowFeatures& features, + const std::string& raw_features, + const scoped_refptr& body, bool user_gesture, bool opener_suppressed, bool* no_javascript_access); diff --git a/content/public/browser/web_contents_delegate.cc b/content/public/browser/web_contents_delegate.cc index 88e89a64cd0333de5fd38aaf2e29f39c8c6a2a20..3abbd89cf26a5e9d53352ad8c9f70d9d0bc52399 100644 --- a/content/public/browser/web_contents_delegate.cc +++ b/content/public/browser/web_contents_delegate.cc @@ -27,6 +27,17 @@ namespace content { WebContentsDelegate::WebContentsDelegate() = default; +void WebContentsDelegate::WebContentsCreatedWithFullParams( + WebContents* source_contents, + int opener_render_process_id, + int opener_render_frame_id, + const mojom::CreateNewWindowParams& params, + WebContents* new_contents) { + WebContentsCreated(source_contents, opener_render_process_id, + opener_render_frame_id, params.frame_name, + params.target_url, new_contents); +} + WebContents* WebContentsDelegate::OpenURLFromTab(WebContents* source, const OpenURLParams& params) { return nullptr; diff --git a/content/public/browser/web_contents_delegate.h b/content/public/browser/web_contents_delegate.h index 49771a2f3db4d0d17822da81cd0cdc46453c1fd2..91fa6ef49923afdd39a5bb9848c88ee9979e2770 100644 --- a/content/public/browser/web_contents_delegate.h +++ b/content/public/browser/web_contents_delegate.h @@ -17,6 +17,7 @@ #include "base/strings/string16.h" #include "build/build_config.h" #include "content/common/content_export.h" +#include "content/common/frame.mojom.h" #include "content/public/browser/eye_dropper.h" #include "content/public/browser/invalidate_type.h" #include "content/public/browser/media_stream_request.h" @@ -337,6 +338,13 @@ class CONTENT_EXPORT WebContentsDelegate { const std::string& partition_id, SessionStorageNamespace* session_storage_namespace); + virtual void WebContentsCreatedWithFullParams( + WebContents* source_contents, + int opener_render_process_id, + int opener_render_frame_id, + const mojom::CreateNewWindowParams& params, + WebContents* new_contents); + // Notifies the delegate about the creation of a new WebContents. This // typically happens when popups are created. virtual void WebContentsCreated(WebContents* source_contents, diff --git a/content/renderer/render_view_impl.cc b/content/renderer/render_view_impl.cc index 2024bbc8dd91defdda32ad058c7450facf2c3382..bc49833e7788c312bfe38db56f4444f2e294ae13 100644 --- a/content/renderer/render_view_impl.cc +++ b/content/renderer/render_view_impl.cc @@ -27,6 +27,7 @@ #include "third_party/blink/public/common/features.h" #include "third_party/blink/public/platform/modules/video_capture/web_video_capture_impl_manager.h" #include "third_party/blink/public/platform/url_conversion.h" +#include "third_party/blink/public/platform/web_url_request_util.h" #include "third_party/blink/public/web/modules/mediastream/web_media_stream_device_observer.h" #include "third_party/blink/public/web/web_frame_widget.h" #include "third_party/blink/public/web/web_local_frame.h" @@ -381,6 +382,10 @@ WebView* RenderViewImpl::CreateView( } params->features = ConvertWebWindowFeaturesToMojoWindowFeatures(features); + params->raw_features = features.raw_features.Utf8( + WTF::UTF8ConversionMode::kStrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD); + params->body = GetRequestBodyForWebURLRequest(request); + // We preserve this information before sending the message since |params| is // moved on send. bool is_background_tab = diff --git a/content/web_test/browser/web_test_content_browser_client.cc b/content/web_test/browser/web_test_content_browser_client.cc index 6f5ae5bf1938133933f8055824e554a57470bbb9..7391fac052218c660cb122181e16ef84dc1a2e74 100644 --- a/content/web_test/browser/web_test_content_browser_client.cc +++ b/content/web_test/browser/web_test_content_browser_client.cc @@ -378,6 +378,8 @@ bool WebTestContentBrowserClient::CanCreateWindow( const std::string& frame_name, WindowOpenDisposition disposition, const blink::mojom::WindowFeatures& features, + const std::string& raw_features, + const scoped_refptr& body, bool user_gesture, bool opener_suppressed, bool* no_javascript_access) { diff --git a/content/web_test/browser/web_test_content_browser_client.h b/content/web_test/browser/web_test_content_browser_client.h index 15262ff3e3a7be037d5eedda3279c8897628e4d8..4e285a97787d9f487b5424b2dfc2b0fff2df53a2 100644 --- a/content/web_test/browser/web_test_content_browser_client.h +++ b/content/web_test/browser/web_test_content_browser_client.h @@ -83,6 +83,8 @@ class WebTestContentBrowserClient : public ShellContentBrowserClient { const std::string& frame_name, WindowOpenDisposition disposition, const blink::mojom::WindowFeatures& features, + const std::string& raw_features, + const scoped_refptr& body, bool user_gesture, bool opener_suppressed, bool* no_javascript_access) override; diff --git a/third_party/blink/public/web/web_window_features.h b/third_party/blink/public/web/web_window_features.h index 4f735ad0d97eaac9a57dad137e479f8a7ec33a36..0a3c5821962c85609b64b3625fa6b8d658cd9ab2 100644 --- a/third_party/blink/public/web/web_window_features.h +++ b/third_party/blink/public/web/web_window_features.h @@ -31,6 +31,8 @@ #ifndef THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_WINDOW_FEATURES_H_ #define THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_WINDOW_FEATURES_H_ +#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" + namespace blink { struct WebWindowFeatures { @@ -60,6 +62,8 @@ struct WebWindowFeatures { bool noreferrer = false; bool background = false; bool persistent = false; + + String raw_features; }; } // namespace blink diff --git a/third_party/blink/renderer/core/frame/local_dom_window.cc b/third_party/blink/renderer/core/frame/local_dom_window.cc index fa07443f71c1a0ab3780290157cbe6db04dae8f7..271da1447c0efe17b245f556d5ce3f9b3fb22ae2 100644 --- a/third_party/blink/renderer/core/frame/local_dom_window.cc +++ b/third_party/blink/renderer/core/frame/local_dom_window.cc @@ -1952,6 +1952,7 @@ DOMWindow* LocalDOMWindow::open(v8::Isolate* isolate, } WebWindowFeatures window_features = GetWindowFeaturesFromString(features); + window_features.raw_features = features; FrameLoadRequest frame_request(incumbent_window, ResourceRequest(completed_url));