From e0fe478ae701c09b0124ff39c525f34b92d6446f Mon Sep 17 00:00:00 2001 From: deepak1556 Date: Thu, 31 Mar 2016 06:28:23 +0530 Subject: [PATCH] decide early on render initiated window creations --- atom/browser/api/atom_api_app.cc | 48 +++++++++++++++++++++++ atom/browser/api/atom_api_app.h | 21 ++++++++++ atom/browser/api/atom_api_web_contents.cc | 16 ++------ atom/browser/api/atom_api_web_contents.h | 15 +++---- atom/browser/atom_browser_client.cc | 39 ++++++++++++++++++ atom/browser/atom_browser_client.h | 16 ++++++++ 6 files changed, 132 insertions(+), 23 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index ad5b7214cc..03f2437edd 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -27,6 +27,7 @@ #include "base/path_service.h" #include "brightray/browser/brightray_paths.h" #include "chrome/common/chrome_paths.h" +#include "content/public/browser/browser_thread.h" #include "content/public/browser/client_certificate_delegate.h" #include "content/public/browser/gpu_data_manager.h" #include "content/public/browser/render_frame_host.h" @@ -275,6 +276,53 @@ void App::SelectClientCertificate( cert_request_info->client_certs[0].get()); } +bool App::CanCreateWindow(const GURL& opener_url, + const GURL& opener_top_level_frame_url, + const GURL& source_origin, + WindowContainerType container_type, + const std::string& frame_name, + const GURL& target_url, + const content::Referrer& referrer, + WindowOpenDisposition disposition, + const blink::WebWindowFeatures& features, + bool user_gesture, + bool opener_suppressed, + content::ResourceContext* context, + int render_process_id, + int opener_render_view_id, + int opener_render_frame_id, + bool* no_javascript_access) { + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + + content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE, + base::Bind(&App::OnCreateWindow, + base::Unretained(this), + target_url, + frame_name, + disposition, + render_process_id, + opener_render_frame_id)); + + return false; +} + +void App::OnCreateWindow(const GURL& target_url, + const std::string& frame_name, + WindowOpenDisposition disposition, + int render_process_id, + int render_frame_id) { + v8::Locker locker(isolate()); + v8::HandleScope handle_scope(isolate()); + content::RenderFrameHost* rfh = + content::RenderFrameHost::FromID(render_process_id, render_frame_id); + content::WebContents* web_contents = + content::WebContents::FromRenderFrameHost(rfh); + if (web_contents) { + auto api_web_contents = WebContents::CreateFrom(isolate(), web_contents); + api_web_contents->CreateWindow(target_url, frame_name, disposition); + } +} + void App::OnGpuProcessCrashed(base::TerminationStatus exit_code) { Emit("gpu-process-crashed"); } diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index 5faf8ebb10..3cf442c9e6 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -66,6 +66,22 @@ class App : public AtomBrowserClient::Delegate, content::WebContents* web_contents, net::SSLCertRequestInfo* cert_request_info, scoped_ptr delegate) override; + bool CanCreateWindow(const GURL& opener_url, + const GURL& opener_top_level_frame_url, + const GURL& source_origin, + WindowContainerType container_type, + const std::string& frame_name, + const GURL& target_url, + const content::Referrer& referrer, + WindowOpenDisposition disposition, + const blink::WebWindowFeatures& features, + bool user_gesture, + bool opener_suppressed, + content::ResourceContext* context, + int render_process_id, + int opener_render_view_id, + int opener_render_frame_id, + bool* no_javascript_access) override; // content::GpuDataManagerObserver: void OnGpuProcessCrashed(base::TerminationStatus exit_code) override; @@ -90,6 +106,11 @@ class App : public AtomBrowserClient::Delegate, bool MakeSingleInstance( const ProcessSingleton::NotificationCallback& callback); std::string GetLocale(); + void OnCreateWindow(const GURL& target_url, + const std::string& frame_name, + WindowOpenDisposition disposition, + int render_process_id, + int render_frame_id); #if defined(OS_WIN) bool IsAeroGlassEnabled(); diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index bd48cac015..115926c8a9 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -316,23 +316,13 @@ bool WebContents::AddMessageToConsole(content::WebContents* source, } } -bool WebContents::ShouldCreateWebContents( - content::WebContents* web_contents, - int32_t route_id, - int32_t main_frame_route_id, - int32_t main_frame_widget_route_id, - WindowContainerType window_container_type, - const std::string& frame_name, - const GURL& target_url, - const std::string& partition_id, - content::SessionStorageNamespace* session_storage_namespace) { - auto disposition = (window_container_type == WINDOW_CONTAINER_TYPE_BACKGROUND) - ? "background-tab" : "new-window"; +void WebContents::CreateWindow(const GURL& target_url, + const std::string& frame_name, + WindowOpenDisposition disposition) { if (type_ == BROWSER_WINDOW) Emit("-new-window", target_url, frame_name, disposition); else Emit("new-window", target_url, frame_name, disposition); - return false; } content::WebContents* WebContents::OpenURLFromTab( diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index b734dc304d..d305172c33 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -138,6 +138,11 @@ class WebContents : public mate::TrackableObject, const GURL& origin, bool allowed); + // Create window with the given disposition. + void CreateWindow(const GURL& target_url, + const std::string& frame_name, + WindowOpenDisposition disposition); + // Returns the web preferences of current WebContents. v8::Local GetWebPreferences(v8::Isolate* isolate); @@ -165,16 +170,6 @@ class WebContents : public mate::TrackableObject, const base::string16& message, int32_t line_no, const base::string16& source_id) override; - bool ShouldCreateWebContents( - content::WebContents* web_contents, - int32_t route_id, - int32_t main_frame_route_id, - int32_t main_frame_widget_route_id, - WindowContainerType window_container_type, - const std::string& frame_name, - const GURL& target_url, - const std::string& partition_id, - content::SessionStorageNamespace* session_storage_namespace) override; content::WebContents* OpenURLFromTab( content::WebContents* source, const content::OpenURLParams& params) override; diff --git a/atom/browser/atom_browser_client.cc b/atom/browser/atom_browser_client.cc index 63a1ea46b1..ec58777d9c 100644 --- a/atom/browser/atom_browser_client.cc +++ b/atom/browser/atom_browser_client.cc @@ -264,6 +264,45 @@ void AtomBrowserClient::ResourceDispatcherHostCreated() { resource_dispatcher_host_delegate_.get()); } +bool AtomBrowserClient::CanCreateWindow( + const GURL& opener_url, + const GURL& opener_top_level_frame_url, + const GURL& source_origin, + WindowContainerType container_type, + const std::string& frame_name, + const GURL& target_url, + const content::Referrer& referrer, + WindowOpenDisposition disposition, + const blink::WebWindowFeatures& features, + bool user_gesture, + bool opener_suppressed, + content::ResourceContext* context, + int render_process_id, + int opener_render_view_id, + int opener_render_frame_id, + bool* no_javascript_access) { + if (delegate_) { + return delegate_->CanCreateWindow(opener_url, + opener_top_level_frame_url, + source_origin, + container_type, + frame_name, + target_url, + referrer, + disposition, + features, + user_gesture, + opener_suppressed, + context, + render_process_id, + opener_render_view_id, + opener_render_frame_id, + no_javascript_access); + } else { + return false; + } +} + brightray::BrowserMainParts* AtomBrowserClient::OverrideCreateBrowserMainParts( const content::MainFunctionParams&) { v8::V8::Initialize(); // Init V8 before creating main parts. diff --git a/atom/browser/atom_browser_client.h b/atom/browser/atom_browser_client.h index 8f62887ff6..5354e14cc6 100644 --- a/atom/browser/atom_browser_client.h +++ b/atom/browser/atom_browser_client.h @@ -76,6 +76,22 @@ class AtomBrowserClient : public brightray::BrowserClient, net::SSLCertRequestInfo* cert_request_info, scoped_ptr delegate) override; void ResourceDispatcherHostCreated() override; + bool CanCreateWindow(const GURL& opener_url, + const GURL& opener_top_level_frame_url, + const GURL& source_origin, + WindowContainerType container_type, + const std::string& frame_name, + const GURL& target_url, + const content::Referrer& referrer, + WindowOpenDisposition disposition, + const blink::WebWindowFeatures& features, + bool user_gesture, + bool opener_suppressed, + content::ResourceContext* context, + int render_process_id, + int opener_render_view_id, + int opener_render_frame_id, + bool* no_javascript_access) override; // brightray::BrowserClient: brightray::BrowserMainParts* OverrideCreateBrowserMainParts(