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 8aa5ad8401d047197694b0aa908cd48abb479c25..0d2f09549829a55099b60cfc8915a28fbcb9b3d5 100644
  10. --- a/content/browser/renderer_host/render_frame_host_impl.cc
  11. +++ b/content/browser/renderer_host/render_frame_host_impl.cc
  12. @@ -8270,6 +8270,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 d57a64e5b15512bdd9c7c99c5d5ee66cf833640f..161d0c99eb1da6af69a5dcf6bbb6f753e3397b5b 100644
  21. --- a/content/browser/web_contents/web_contents_impl.cc
  22. +++ b/content/browser/web_contents/web_contents_impl.cc
  23. @@ -4492,6 +4492,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. @@ -4533,12 +4539,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 40c14e1757dee4fda9aa79f3a52532f8ab737a97..2c833d683b737bd6b24d2ec10d97b46d2c9fcdb0 100644
  48. --- a/content/common/frame.mojom
  49. +++ b/content/common/frame.mojom
  50. @@ -598,6 +598,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 39b5ff0122f950990ab1cdd7b686fb4429e97a8a..f602a3fac375bcd18a6db25d163efb40de485cd0 100644
  62. --- a/content/public/browser/content_browser_client.cc
  63. +++ b/content/public/browser/content_browser_client.cc
  64. @@ -716,6 +716,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 e479adacb16343891c2782973fa9ba8c1925b275..9bef261091b60fcf1b79b224612180f307f8f0c6 100644
  75. --- a/content/public/browser/content_browser_client.h
  76. +++ b/content/public/browser/content_browser_client.h
  77. @@ -177,6 +177,7 @@ class NetworkService;
  78. class TrustedURLLoaderHeaderClient;
  79. } // namespace mojom
  80. struct ResourceRequest;
  81. +class ResourceRequestBody;
  82. } // namespace network
  83. namespace sandbox {
  84. @@ -1153,6 +1154,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 9f38850f8efe3d9a29a7be6528129b48cf707a80..35c378f2ecedec3fc799189ecb70139ea090a5c2 100644
  95. --- a/content/public/browser/web_contents_delegate.cc
  96. +++ b/content/public/browser/web_contents_delegate.cc
  97. @@ -30,6 +30,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 9fc7ad92817cc7fea982492b08da6e91ec4b8274..2885b140bb3a7e544d6ab7431090252e29e20ea5 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. @@ -345,6 +346,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 d7c6222c977058f230d5e6153b1df13e127a9939..47562a65e99b1bdbeca57f1ba69822df768c0291 100644
  140. --- a/content/renderer/render_frame_impl.cc
  141. +++ b/content/renderer/render_frame_impl.cc
  142. @@ -6507,6 +6507,10 @@ WebView* RenderFrameImpl::CreateNewWindow(
  143. request.HasUserGesture(), GetWebFrame()->IsAdFrame(),
  144. 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 51de81df6cb5126a750dab43322f8fc1fc484d80..9cb924a1969eef8d3940cc261e286e10c6d84d5c 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. @@ -512,6 +512,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 4869a7502ed31ad6101bd89f899325ffef427c0e..954037dc4fdc21b922debb7a123f1e93e788a908 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. @@ -86,6 +86,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 bef5a989bac50c177f15f52fe87ac3790d553e85..65dcd2e3b51929400c8bfb6a98a4fb59bb6a3d6b 100644
  180. --- a/third_party/blink/public/web/web_window_features.h
  181. +++ b/third_party/blink/public/web/web_window_features.h
  182. @@ -34,6 +34,7 @@
  183. #include "third_party/abseil-cpp/absl/types/optional.h"
  184. #include "third_party/blink/public/platform/web_string.h"
  185. #include "third_party/blink/public/platform/web_vector.h"
  186. +#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
  187. namespace blink {
  188. @@ -73,6 +74,8 @@ struct WebWindowFeatures {
  189. // TODO(apaseltiner): Investigate moving this field to a non-public struct
  190. // since it is only needed within //third_party/blink.
  191. absl::optional<WebVector<WebString>> attribution_srcs;
  192. +
  193. + String raw_features;
  194. };
  195. } // namespace blink
  196. diff --git a/third_party/blink/renderer/core/frame/local_dom_window.cc b/third_party/blink/renderer/core/frame/local_dom_window.cc
  197. index 7405d410af1859db405f3cc7a2169f3aab597631..bb27d4350590bc7712a271567e1457e6551c7f7d 100644
  198. --- a/third_party/blink/renderer/core/frame/local_dom_window.cc
  199. +++ b/third_party/blink/renderer/core/frame/local_dom_window.cc
  200. @@ -2175,6 +2175,8 @@ DOMWindow* LocalDOMWindow::open(v8::Isolate* isolate,
  201. WebWindowFeatures window_features =
  202. GetWindowFeaturesFromString(features, entered_window);
  203. + window_features.raw_features = features;
  204. +
  205. // In fenced frames, we should always use `noopener`.
  206. if (GetFrame()->IsInFencedFrameTree()) {
  207. window_features.noopener = true;