can_create_window.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
  2. From: Cheng Zhao <[email protected]>
  3. Date: Thu, 20 Sep 2018 17:45:32 -0700
  4. Subject: can_create_window.patch
  5. This adds a hook to the window creation flow so that Electron can intercede and
  6. potentially prevent a window from being created.
  7. TODO(loc): this patch is currently broken.
  8. diff --git a/content/browser/renderer_host/render_frame_host_impl.cc b/content/browser/renderer_host/render_frame_host_impl.cc
  9. index 8accd729b4f39c738b722d36bc0bbe769e598cba..2937f35372bafd9158be712f04d2731bb4100f9a 100644
  10. --- a/content/browser/renderer_host/render_frame_host_impl.cc
  11. +++ b/content/browser/renderer_host/render_frame_host_impl.cc
  12. @@ -7711,6 +7711,7 @@ void RenderFrameHostImpl::CreateNewWindow(
  13. last_committed_origin_, params->window_container_type,
  14. params->target_url, params->referrer.To<Referrer>(),
  15. params->frame_name, params->disposition, *params->features,
  16. + params->raw_features, params->body,
  17. effective_transient_activation_state, params->opener_suppressed,
  18. &no_javascript_access);
  19. diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc
  20. index bd4a6817ae9b60338219dcffd6c0c7d2fbd4bf97..bc9e24f2346a0d3e494502fedaaa0b24e82fc494 100644
  21. --- a/content/browser/web_contents/web_contents_impl.cc
  22. +++ b/content/browser/web_contents/web_contents_impl.cc
  23. @@ -4160,6 +4160,12 @@ FrameTree* WebContentsImpl::CreateNewWindow(
  24. auto* new_contents_impl = new_contents.get();
  25. + if (delegate_) {
  26. + delegate_->WebContentsCreatedWithFullParams(this, render_process_id,
  27. + opener->GetRoutingID(),
  28. + params, new_contents_impl);
  29. + }
  30. +
  31. // If the new frame has a name, make sure any SiteInstances that can find
  32. // this named frame have proxies for it. Must be called after
  33. // SetSessionStorageNamespace, since this calls CreateRenderView, which uses
  34. @@ -4201,12 +4207,6 @@ FrameTree* WebContentsImpl::CreateNewWindow(
  35. AddWebContentsDestructionObserver(new_contents_impl);
  36. }
  37. - if (delegate_) {
  38. - delegate_->WebContentsCreated(this, render_process_id,
  39. - opener->GetRoutingID(), params.frame_name,
  40. - params.target_url, new_contents_impl);
  41. - }
  42. -
  43. observers_.NotifyObservers(&WebContentsObserver::DidOpenRequestedURL,
  44. new_contents_impl, opener, params.target_url,
  45. params.referrer.To<Referrer>(), params.disposition,
  46. diff --git a/content/common/frame.mojom b/content/common/frame.mojom
  47. index 12f4a2066a2a31e9852216c0cb3344095c7b0e39..588ca46227c58f9596317d6d4d05d0b3c76cbc06 100644
  48. --- a/content/common/frame.mojom
  49. +++ b/content/common/frame.mojom
  50. @@ -593,6 +593,10 @@ struct CreateNewWindowParams {
  51. // The navigation initiator's user activation and ad status.
  52. blink.mojom.NavigationInitiatorActivationAndAdStatus
  53. initiator_activation_and_ad_status;
  54. +
  55. + // Extra fields added by Electron.
  56. + string raw_features;
  57. + network.mojom.URLRequestBody? body;
  58. };
  59. // Operation result when the renderer asks the browser to create a new window.
  60. diff --git a/content/public/browser/content_browser_client.cc b/content/public/browser/content_browser_client.cc
  61. index abc759cc548276accc0869b67c55d1078cf4c097..36e70e69615a4463afdb7b069b60f20d010dfb27 100644
  62. --- a/content/public/browser/content_browser_client.cc
  63. +++ b/content/public/browser/content_browser_client.cc
  64. @@ -631,6 +631,8 @@ bool ContentBrowserClient::CanCreateWindow(
  65. const std::string& frame_name,
  66. WindowOpenDisposition disposition,
  67. const blink::mojom::WindowFeatures& features,
  68. + const std::string& raw_features,
  69. + const scoped_refptr<network::ResourceRequestBody>& body,
  70. bool user_gesture,
  71. bool opener_suppressed,
  72. bool* no_javascript_access) {
  73. diff --git a/content/public/browser/content_browser_client.h b/content/public/browser/content_browser_client.h
  74. index 6fc121fb5f3a418763df5042619f89cae0551a00..d89b97d141c66696caa5535b03f4da994fd28210 100644
  75. --- a/content/public/browser/content_browser_client.h
  76. +++ b/content/public/browser/content_browser_client.h
  77. @@ -165,6 +165,7 @@ class NetworkService;
  78. class TrustedURLLoaderHeaderClient;
  79. } // namespace mojom
  80. struct ResourceRequest;
  81. +class ResourceRequestBody;
  82. } // namespace network
  83. namespace sandbox {
  84. @@ -1032,6 +1033,8 @@ class CONTENT_EXPORT ContentBrowserClient {
  85. const std::string& frame_name,
  86. WindowOpenDisposition disposition,
  87. const blink::mojom::WindowFeatures& features,
  88. + const std::string& raw_features,
  89. + const scoped_refptr<network::ResourceRequestBody>& body,
  90. bool user_gesture,
  91. bool opener_suppressed,
  92. bool* no_javascript_access);
  93. diff --git a/content/public/browser/web_contents_delegate.cc b/content/public/browser/web_contents_delegate.cc
  94. index 885899b151520e0173a5ca68c2613b1333cadddf..5b53ee691c0a07ed1655139ca2d90341b5fa55a3 100644
  95. --- a/content/public/browser/web_contents_delegate.cc
  96. +++ b/content/public/browser/web_contents_delegate.cc
  97. @@ -28,6 +28,17 @@ namespace content {
  98. WebContentsDelegate::WebContentsDelegate() = default;
  99. +void WebContentsDelegate::WebContentsCreatedWithFullParams(
  100. + WebContents* source_contents,
  101. + int opener_render_process_id,
  102. + int opener_render_frame_id,
  103. + const mojom::CreateNewWindowParams& params,
  104. + WebContents* new_contents) {
  105. + WebContentsCreated(source_contents, opener_render_process_id,
  106. + opener_render_frame_id, params.frame_name,
  107. + params.target_url, new_contents);
  108. +}
  109. +
  110. WebContents* WebContentsDelegate::OpenURLFromTab(WebContents* source,
  111. const OpenURLParams& params) {
  112. return nullptr;
  113. diff --git a/content/public/browser/web_contents_delegate.h b/content/public/browser/web_contents_delegate.h
  114. index 5da6f93293bc5ddae88c17ac2dd8d7037ba8e8f3..76d699790fb7d92587293b14332f696dc5460322 100644
  115. --- a/content/public/browser/web_contents_delegate.h
  116. +++ b/content/public/browser/web_contents_delegate.h
  117. @@ -16,6 +16,7 @@
  118. #include "base/memory/scoped_refptr.h"
  119. #include "build/build_config.h"
  120. #include "content/common/content_export.h"
  121. +#include "content/common/frame.mojom.h"
  122. #include "content/public/browser/eye_dropper.h"
  123. #include "content/public/browser/fullscreen_types.h"
  124. #include "content/public/browser/invalidate_type.h"
  125. @@ -343,6 +344,13 @@ class CONTENT_EXPORT WebContentsDelegate {
  126. const StoragePartitionConfig& partition_config,
  127. SessionStorageNamespace* session_storage_namespace);
  128. + virtual void WebContentsCreatedWithFullParams(
  129. + WebContents* source_contents,
  130. + int opener_render_process_id,
  131. + int opener_render_frame_id,
  132. + const mojom::CreateNewWindowParams& params,
  133. + WebContents* new_contents);
  134. +
  135. // Notifies the delegate about the creation of a new WebContents. This
  136. // typically happens when popups are created.
  137. virtual void WebContentsCreated(WebContents* source_contents,
  138. diff --git a/content/renderer/render_frame_impl.cc b/content/renderer/render_frame_impl.cc
  139. index 3257ffeafbc86eb728e58498a2c2283eabf305f3..456304cef9e3a8063b112b3f4cfabc25d9d479d4 100644
  140. --- a/content/renderer/render_frame_impl.cc
  141. +++ b/content/renderer/render_frame_impl.cc
  142. @@ -6317,6 +6317,10 @@ WebView* RenderFrameImpl::CreateNewWindow(
  143. blink::GetNavigationInitiatorActivationAndAdStatus(
  144. request.HasUserGesture(), GetWebFrame()->IsAdScriptInStack());
  145. + params->raw_features = features.raw_features.Utf8(
  146. + WTF::UTF8ConversionMode::kStrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD);
  147. + params->body = GetRequestBodyForWebURLRequest(request);
  148. +
  149. // We preserve this information before sending the message since |params| is
  150. // moved on send.
  151. bool is_background_tab =
  152. diff --git a/content/web_test/browser/web_test_content_browser_client.cc b/content/web_test/browser/web_test_content_browser_client.cc
  153. index 48a07780e61463b7e70e2a511bc6f81bd6fbdbf3..465cfb29380df203de7bcf2d27617b604097118a 100644
  154. --- a/content/web_test/browser/web_test_content_browser_client.cc
  155. +++ b/content/web_test/browser/web_test_content_browser_client.cc
  156. @@ -499,6 +499,8 @@ bool WebTestContentBrowserClient::CanCreateWindow(
  157. const std::string& frame_name,
  158. WindowOpenDisposition disposition,
  159. const blink::mojom::WindowFeatures& features,
  160. + const std::string& raw_features,
  161. + const scoped_refptr<network::ResourceRequestBody>& body,
  162. bool user_gesture,
  163. bool opener_suppressed,
  164. bool* no_javascript_access) {
  165. diff --git a/content/web_test/browser/web_test_content_browser_client.h b/content/web_test/browser/web_test_content_browser_client.h
  166. index 22254206063abe36739e1c0e7c065223ab6807d2..7f5d89f8dc8b46ac1338e73b0394872569d803b8 100644
  167. --- a/content/web_test/browser/web_test_content_browser_client.h
  168. +++ b/content/web_test/browser/web_test_content_browser_client.h
  169. @@ -84,6 +84,8 @@ class WebTestContentBrowserClient : public ShellContentBrowserClient {
  170. const std::string& frame_name,
  171. WindowOpenDisposition disposition,
  172. const blink::mojom::WindowFeatures& features,
  173. + const std::string& raw_features,
  174. + const scoped_refptr<network::ResourceRequestBody>& body,
  175. bool user_gesture,
  176. bool opener_suppressed,
  177. bool* no_javascript_access) override;
  178. diff --git a/third_party/blink/public/web/web_window_features.h b/third_party/blink/public/web/web_window_features.h
  179. index 4156256596276015ca3205b0f49f69d1ab47208e..271bc153afe3e3bfa98a7baa6ae4f92285d41929 100644
  180. --- a/third_party/blink/public/web/web_window_features.h
  181. +++ b/third_party/blink/public/web/web_window_features.h
  182. @@ -32,6 +32,7 @@
  183. #define THIRD_PARTY_BLINK_PUBLIC_WEB_WEB_WINDOW_FEATURES_H_
  184. #include "third_party/blink/public/platform/web_string.h"
  185. +#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
  186. namespace blink {
  187. @@ -68,6 +69,8 @@ struct WebWindowFeatures {
  188. // request should be made. Otherwise, an impression should be set and a
  189. // background request should be made to the contained relative URL.
  190. WebString attribution_src;
  191. +
  192. + String raw_features;
  193. };
  194. } // namespace blink
  195. diff --git a/third_party/blink/renderer/core/frame/local_dom_window.cc b/third_party/blink/renderer/core/frame/local_dom_window.cc
  196. index 1b718368110b62bd3581985a70a441a2d2674056..d38dc86c8368a923a347889b35a00539ae42c50f 100644
  197. --- a/third_party/blink/renderer/core/frame/local_dom_window.cc
  198. +++ b/third_party/blink/renderer/core/frame/local_dom_window.cc
  199. @@ -2219,6 +2219,8 @@ DOMWindow* LocalDOMWindow::open(v8::Isolate* isolate,
  200. WebWindowFeatures window_features =
  201. GetWindowFeaturesFromString(features, entered_window);
  202. + window_features.raw_features = features;
  203. +
  204. // In fenced frames, we should always use `noopener`.
  205. if (GetFrame()->IsInFencedFrameTree()) {
  206. window_features.noopener = true;