From fd596d4a6531cb7d92c8d20748a4305047bbc208 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Wed, 17 Dec 2014 14:40:19 -0800 Subject: [PATCH] Add "will-navigate" event for BrowserWindow --- atom/browser/api/atom_api_window.cc | 6 ++++++ atom/browser/api/atom_api_window.h | 1 + atom/browser/native_window.cc | 16 ++++++++++++---- atom/browser/native_window_observer.h | 3 +++ 4 files changed, 22 insertions(+), 4 deletions(-) diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 17255fd238ec..4f663bc63d6b 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -92,6 +92,12 @@ void Window::WillCreatePopupWindow(const base::string16& frame_name, Emit("-new-window", args); } +void Window::WillNavigate(bool* prevent_default, const GURL& url) { + base::ListValue args; + args.AppendString(url.spec()); + *prevent_default = Emit("will-navigate", args); +} + void Window::WillCloseWindow(bool* prevent_default) { *prevent_default = Emit("close"); } diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index c5a7e73320b6..3b123de7160d 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -50,6 +50,7 @@ class Window : public mate::EventEmitter, const GURL& target_url, const std::string& partition_id, WindowOpenDisposition disposition) override; + void WillNavigate(bool* prevent_default, const GURL& url) override; void WillCloseWindow(bool* prevent_default) override; void OnWindowClosed() override; void OnWindowBlur() override; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index d807c1ba7cd5..8e1a1e237ae4 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -169,7 +169,7 @@ NativeWindow* NativeWindow::FromRenderView(int process_id, int routing_id) { return window; } - return NULL; + return nullptr; } void NativeWindow::InitFromOptions(const mate::Dictionary& options) { @@ -370,13 +370,13 @@ void NativeWindow::CloseWebContents() { content::WebContents* NativeWindow::GetWebContents() const { if (!inspectable_web_contents_) - return NULL; + return nullptr; return inspectable_web_contents()->GetWebContents(); } content::WebContents* NativeWindow::GetDevToolsWebContents() const { if (!inspectable_web_contents_) - return NULL; + return nullptr; return inspectable_web_contents()->devtools_web_contents(); } @@ -524,9 +524,17 @@ content::WebContents* NativeWindow::OpenURLFromTab( params.url, "", params.disposition)); - return NULL; + return nullptr; } + // Give user a chance to prevent navigation. + bool prevent_default = false; + FOR_EACH_OBSERVER(NativeWindowObserver, + observers_, + WillNavigate(&prevent_default, params.url)); + if (prevent_default) + return nullptr; + content::NavigationController::LoadURLParams load_url_params(params.url); load_url_params.referrer = params.referrer; load_url_params.transition_type = params.transition; diff --git a/atom/browser/native_window_observer.h b/atom/browser/native_window_observer.h index e83018087524..38d19600af57 100644 --- a/atom/browser/native_window_observer.h +++ b/atom/browser/native_window_observer.h @@ -27,6 +27,9 @@ class NativeWindowObserver { const std::string& partition_id, WindowOpenDisposition disposition) {} + // Called when user is starting an navigation in web page. + virtual void WillNavigate(bool* prevent_default, const GURL& url) {} + // Called when the window is gonna closed. virtual void WillCloseWindow(bool* prevent_default) {}