can_create_window.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 491c8b441dfe84862c8b0caa7941cc1e7d16e331..74b3fb5010895869b606890f1523512cd2ee96af 100644
  10. --- a/content/browser/renderer_host/render_frame_host_impl.cc
  11. +++ b/content/browser/renderer_host/render_frame_host_impl.cc
  12. @@ -5979,6 +5979,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 e3146d7cfdfb1f1856edba2e371a8b7589b627a1..952c073cfb1a2385581759b29683f1b66564d99b 100644
  21. --- a/content/browser/web_contents/web_contents_impl.cc
  22. +++ b/content/browser/web_contents/web_contents_impl.cc
  23. @@ -3774,6 +3774,14 @@ RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow(
  24. }
  25. auto* new_contents_impl = new_contents.get();
  26. + // Call this earlier than Chrome to associate the web preferences with the
  27. + // WebContents before the view gets created.
  28. + if (delegate_) {
  29. + delegate_->WebContentsCreatedWithFullParams(this, render_process_id,
  30. + opener->GetRoutingID(),
  31. + params, new_contents_impl);
  32. + }
  33. +
  34. new_contents_impl->GetController().SetSessionStorageNamespace(
  35. partition_id, session_storage_namespace);
  36. @@ -3816,12 +3824,6 @@ RenderFrameHostDelegate* WebContentsImpl::CreateNewWindow(
  37. AddWebContentsDestructionObserver(new_contents_impl);
  38. }
  39. - if (delegate_) {
  40. - delegate_->WebContentsCreated(this, render_process_id,
  41. - opener->GetRoutingID(), params.frame_name,
  42. - params.target_url, new_contents_impl);
  43. - }
  44. -
  45. observers_.NotifyObservers(&WebContentsObserver::DidOpenRequestedURL,
  46. new_contents_impl, opener, params.target_url,
  47. params.referrer.To<Referrer>(), params.disposition,
  48. diff --git a/content/common/frame.mojom b/content/common/frame.mojom
  49. index 542a9bb6343b16c4dfe3aebae97ec06073021e52..840d562fcc8e0da12393924d56c2a6f319be9006 100644
  50. --- a/content/common/frame.mojom
  51. +++ b/content/common/frame.mojom
  52. @@ -468,6 +468,10 @@ struct CreateNewWindowParams {
  53. // The impression associated with the navigation in the new window, if
  54. // one is specified.
  55. Impression? impression;
  56. +
  57. + // Extra fields added by Electron.
  58. + string raw_features;
  59. + network.mojom.URLRequestBody? body;
  60. };
  61. // Operation result when the renderer asks the browser to create a new window.
  62. diff --git a/content/public/browser/content_browser_client.cc b/content/public/browser/content_browser_client.cc
  63. index 09597633dfd443376784d8645611ab18a903bf23..1fe182db87390b346fa03091dcc177611943d70e 100644
  64. --- a/content/public/browser/content_browser_client.cc
  65. +++ b/content/public/browser/content_browser_client.cc
  66. @@ -566,6 +566,8 @@ bool ContentBrowserClient::CanCreateWindow(
  67. const std::string& frame_name,
  68. WindowOpenDisposition disposition,
  69. const blink::mojom::WindowFeatures& features,
  70. + const std::string& raw_features,
  71. + const scoped_refptr<network::ResourceRequestBody>& body,
  72. bool user_gesture,
  73. bool opener_suppressed,
  74. bool* no_javascript_access) {
  75. diff --git a/content/public/browser/content_browser_client.h b/content/public/browser/content_browser_client.h
  76. index 7dd2b78088759ffdd80c33cd529adbb93215334f..5ec83c0a56fc91d3c13d97dd8c58a286b6358146 100644
  77. --- a/content/public/browser/content_browser_client.h
  78. +++ b/content/public/browser/content_browser_client.h
  79. @@ -160,6 +160,7 @@ class NetworkService;
  80. class TrustedURLLoaderHeaderClient;
  81. } // namespace mojom
  82. struct ResourceRequest;
  83. +class ResourceRequestBody;
  84. } // namespace network
  85. namespace sandbox {
  86. @@ -937,6 +938,8 @@ class CONTENT_EXPORT ContentBrowserClient {
  87. const std::string& frame_name,
  88. WindowOpenDisposition disposition,
  89. const blink::mojom::WindowFeatures& features,
  90. + const std::string& raw_features,
  91. + const scoped_refptr<network::ResourceRequestBody>& body,
  92. bool user_gesture,
  93. bool opener_suppressed,
  94. bool* no_javascript_access);
  95. diff --git a/content/public/browser/web_contents_delegate.cc b/content/public/browser/web_contents_delegate.cc
  96. index 8ba5d82ebb39b495a0c175c771be8c4f071b5052..cf2974dad890e61e4090d3328b23de203838bd7c 100644
  97. --- a/content/public/browser/web_contents_delegate.cc
  98. +++ b/content/public/browser/web_contents_delegate.cc
  99. @@ -28,6 +28,17 @@ namespace content {
  100. WebContentsDelegate::WebContentsDelegate() = default;
  101. +void WebContentsDelegate::WebContentsCreatedWithFullParams(
  102. + WebContents* source_contents,
  103. + int opener_render_process_id,
  104. + int opener_render_frame_id,
  105. + const mojom::CreateNewWindowParams& params,
  106. + WebContents* new_contents) {
  107. + WebContentsCreated(source_contents, opener_render_process_id,
  108. + opener_render_frame_id, params.frame_name,
  109. + params.target_url, new_contents);
  110. +}
  111. +
  112. WebContents* WebContentsDelegate::OpenURLFromTab(WebContents* source,
  113. const OpenURLParams& params) {
  114. return nullptr;
  115. diff --git a/content/public/browser/web_contents_delegate.h b/content/public/browser/web_contents_delegate.h
  116. index 8a3c4c24a55ae7c520514ded527eb6214a5708a9..631c9b3aab0f2dfe8e6d8437b3f31676e8e158b5 100644
  117. --- a/content/public/browser/web_contents_delegate.h
  118. +++ b/content/public/browser/web_contents_delegate.h
  119. @@ -16,6 +16,7 @@
  120. #include "base/memory/scoped_refptr.h"
  121. #include "build/build_config.h"
  122. #include "content/common/content_export.h"
  123. +#include "content/common/frame.mojom.h"
  124. #include "content/public/browser/eye_dropper.h"
  125. #include "content/public/browser/invalidate_type.h"
  126. #include "content/public/browser/media_stream_request.h"
  127. @@ -340,6 +341,13 @@ class CONTENT_EXPORT WebContentsDelegate {
  128. const StoragePartitionId& partition_id,
  129. SessionStorageNamespace* session_storage_namespace);
  130. + virtual void WebContentsCreatedWithFullParams(
  131. + WebContents* source_contents,
  132. + int opener_render_process_id,
  133. + int opener_render_frame_id,
  134. + const mojom::CreateNewWindowParams& params,
  135. + WebContents* new_contents);
  136. +
  137. // Notifies the delegate about the creation of a new WebContents. This
  138. // typically happens when popups are created.
  139. virtual void WebContentsCreated(WebContents* source_contents,
  140. diff --git a/content/renderer/render_view_impl.cc b/content/renderer/render_view_impl.cc
  141. index a229509d7254f87af5128ffdaad4cd9c74c3e980..766adb31cf654ff964fbca3acc941c092e382448 100644
  142. --- a/content/renderer/render_view_impl.cc
  143. +++ b/content/renderer/render_view_impl.cc
  144. @@ -26,6 +26,7 @@
  145. #include "third_party/blink/public/platform/impression_conversions.h"
  146. #include "third_party/blink/public/platform/modules/video_capture/web_video_capture_impl_manager.h"
  147. #include "third_party/blink/public/platform/url_conversion.h"
  148. +#include "third_party/blink/public/platform/web_url_request_util.h"
  149. #include "third_party/blink/public/web/modules/mediastream/web_media_stream_device_observer.h"
  150. #include "third_party/blink/public/web/web_frame_widget.h"
  151. #include "third_party/blink/public/web/web_local_frame.h"
  152. @@ -284,6 +285,10 @@ WebView* RenderViewImpl::CreateView(
  153. params->impression = blink::ConvertWebImpressionToImpression(*impression);
  154. }
  155. + params->raw_features = features.raw_features.Utf8(
  156. + WTF::UTF8ConversionMode::kStrictUTF8ConversionReplacingUnpairedSurrogatesWithFFFD);
  157. + params->body = GetRequestBodyForWebURLRequest(request);
  158. +
  159. // We preserve this information before sending the message since |params| is
  160. // moved on send.
  161. bool is_background_tab =
  162. diff --git a/content/web_test/browser/web_test_content_browser_client.cc b/content/web_test/browser/web_test_content_browser_client.cc
  163. index e3dd040164fec1a70c9bd2dac7cc3d93478ec39f..f8894682e181a99b9e6cf3d0e4b46413076eb5ef 100644
  164. --- a/content/web_test/browser/web_test_content_browser_client.cc
  165. +++ b/content/web_test/browser/web_test_content_browser_client.cc
  166. @@ -450,6 +450,8 @@ bool WebTestContentBrowserClient::CanCreateWindow(
  167. const std::string& frame_name,
  168. WindowOpenDisposition disposition,
  169. const blink::mojom::WindowFeatures& features,
  170. + const std::string& raw_features,
  171. + const scoped_refptr<network::ResourceRequestBody>& body,
  172. bool user_gesture,
  173. bool opener_suppressed,
  174. bool* no_javascript_access) {
  175. diff --git a/content/web_test/browser/web_test_content_browser_client.h b/content/web_test/browser/web_test_content_browser_client.h
  176. index c65d30c9187dd275488ed74bcc3a4eb918d2cbce..e4c6c828150e91f555b1b42e1988a1013ab1a1f0 100644
  177. --- a/content/web_test/browser/web_test_content_browser_client.h
  178. +++ b/content/web_test/browser/web_test_content_browser_client.h
  179. @@ -83,6 +83,8 @@ class WebTestContentBrowserClient : public ShellContentBrowserClient {
  180. const std::string& frame_name,
  181. WindowOpenDisposition disposition,
  182. const blink::mojom::WindowFeatures& features,
  183. + const std::string& raw_features,
  184. + const scoped_refptr<network::ResourceRequestBody>& body,
  185. bool user_gesture,
  186. bool opener_suppressed,
  187. bool* no_javascript_access) override;
  188. diff --git a/third_party/blink/public/web/web_window_features.h b/third_party/blink/public/web/web_window_features.h
  189. index 84d32491a56528a84b4395fba1d54cdbb38d522b..09998a83c449ef8cd9f360fbcdcf7edc0bbfa4a9 100644
  190. --- a/third_party/blink/public/web/web_window_features.h
  191. +++ b/third_party/blink/public/web/web_window_features.h
  192. @@ -34,6 +34,7 @@
  193. #include "third_party/abseil-cpp/absl/types/optional.h"
  194. #include "third_party/blink/public/platform/web_impression.h"
  195. +#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
  196. namespace blink {
  197. @@ -68,6 +69,8 @@ struct WebWindowFeatures {
  198. // Represents the attribution source declared by Attribution Reporting related
  199. // window features, if any.
  200. absl::optional<WebImpression> impression;
  201. +
  202. + String raw_features;
  203. };
  204. } // namespace blink
  205. diff --git a/third_party/blink/renderer/core/frame/local_dom_window.cc b/third_party/blink/renderer/core/frame/local_dom_window.cc
  206. index 06983193aa144c526b606c6b1291b2271a4570b3..cf2bc64ee509727570cc55a08dcdb39d3f18cce9 100644
  207. --- a/third_party/blink/renderer/core/frame/local_dom_window.cc
  208. +++ b/third_party/blink/renderer/core/frame/local_dom_window.cc
  209. @@ -1989,6 +1989,7 @@ DOMWindow* LocalDOMWindow::open(v8::Isolate* isolate,
  210. WebWindowFeatures window_features =
  211. GetWindowFeaturesFromString(features, incumbent_window);
  212. + window_features.raw_features = features;
  213. FrameLoadRequest frame_request(incumbent_window,
  214. ResourceRequest(completed_url));