From c8a82e6e505e38c663d8d2be2754ee855ba17ab5 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 25 Apr 2014 12:52:30 +0800 Subject: [PATCH] Move navigator related APIs to webContents. --- atom/browser/api/atom_api_web_contents.cc | 53 ++++++++++++++++++ atom/browser/api/atom_api_web_contents.h | 10 ++++ atom/browser/api/atom_api_window.cc | 63 +--------------------- atom/browser/api/atom_api_window.h | 12 ----- atom/browser/api/lib/browser-window.coffee | 3 ++ 5 files changed, 67 insertions(+), 74 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 26fd3eee418e..1548cf3e0954 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -62,6 +62,13 @@ bool WebContents::IsAlive() const { return web_contents_ != NULL; } +void WebContents::LoadURL(const GURL& url) { + content::NavigationController::LoadURLParams params(url); + params.transition_type = content::PAGE_TRANSITION_TYPED; + params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE; + web_contents_->GetController().LoadURLWithParams(params); +} + GURL WebContents::GetURL() const { return web_contents_->GetURL(); } @@ -82,6 +89,42 @@ void WebContents::Stop() { web_contents_->Stop(); } +void WebContents::Reload() { + web_contents_->GetController().Reload(false); +} + +void WebContents::ReloadIgnoringCache() { + web_contents_->GetController().ReloadIgnoringCache(false); +} + +bool WebContents::CanGoBack() const { + return web_contents_->GetController().CanGoBack(); +} + +bool WebContents::CanGoForward() const { + return web_contents_->GetController().CanGoForward(); +} + +bool WebContents::CanGoToOffset(int offset) const { + return web_contents_->GetController().CanGoToOffset(offset); +} + +void WebContents::GoBack() { + web_contents_->GetController().GoBack(); +} + +void WebContents::GoForward() { + web_contents_->GetController().GoForward(); +} + +void WebContents::GoToIndex(int index) { + web_contents_->GetController().GoToIndex(index); +} + +void WebContents::GoToOffset(int offset) { + web_contents_->GetController().GoToOffset(offset); +} + int WebContents::GetRoutingID() const { return web_contents_->GetRoutingID(); } @@ -103,11 +146,21 @@ mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( v8::Isolate* isolate) { return mate::ObjectTemplateBuilder(isolate) .SetMethod("isAlive", &WebContents::IsAlive) + .SetMethod("loadUrl", &WebContents::LoadURL) .SetMethod("getUrl", &WebContents::GetURL) .SetMethod("getTitle", &WebContents::GetTitle) .SetMethod("isLoading", &WebContents::IsLoading) .SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse) .SetMethod("stop", &WebContents::Stop) + .SetMethod("reload", &WebContents::Reload) + .SetMethod("reloadIgnoringCache", &WebContents::ReloadIgnoringCache) + .SetMethod("canGoBack", &WebContents::CanGoBack) + .SetMethod("canGoForward", &WebContents::CanGoForward) + .SetMethod("canGoToOffset", &WebContents::CanGoToOffset) + .SetMethod("goBack", &WebContents::GoBack) + .SetMethod("goForward", &WebContents::GoForward) + .SetMethod("goToIndex", &WebContents::GoToIndex) + .SetMethod("goToOffset", &WebContents::GoToOffset) .SetMethod("getRoutingId", &WebContents::GetRoutingID) .SetMethod("getProcessId", &WebContents::GetProcessID) .SetMethod("isCrashed", &WebContents::IsCrashed) diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 5bdf3c0d645c..13e229db123f 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -20,11 +20,21 @@ class WebContents : public mate::EventEmitter, content::WebContents* web_contents); bool IsAlive() const; + void LoadURL(const GURL& url); GURL GetURL() const; string16 GetTitle() const; bool IsLoading() const; bool IsWaitingForResponse() const; void Stop(); + void Reload(); + void ReloadIgnoringCache(); + bool CanGoBack() const; + bool CanGoForward() const; + bool CanGoToOffset(int offset) const; + void GoBack(); + void GoForward(); + void GoToIndex(int index); + void GoToOffset(int offset); int GetRoutingID() const; int GetProcessID() const; bool IsCrashed() const; diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index 482382f230fc..dae2abb93ebf 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -7,13 +7,9 @@ #include "atom/browser/api/atom_api_web_contents.h" #include "atom/browser/native_window.h" #include "atom/common/native_mate_converters/function_converter.h" -#include "atom/common/native_mate_converters/gurl_converter.h" -#include "atom/common/native_mate_converters/string16_converter.h" #include "atom/common/native_mate_converters/value_converter.h" #include "base/bind.h" #include "base/callback.h" -#include "content/public/browser/navigation_entry.h" -#include "content/public/browser/web_contents.h" #include "content/public/browser/render_process_host.h" #include "native_mate/constructor.h" #include "native_mate/dictionary.h" @@ -23,8 +19,6 @@ #include "atom/common/node_includes.h" -using content::NavigationController; - namespace mate { template<> @@ -319,51 +313,6 @@ mate::Handle Window::GetDevToolsWebContents( return WebContents::Create(isolate, window_->GetDevToolsWebContents()); } -void Window::LoadURL(const GURL& url) { - NavigationController& controller = window_->GetWebContents()->GetController(); - - content::NavigationController::LoadURLParams params(url); - params.transition_type = content::PAGE_TRANSITION_TYPED; - params.override_user_agent = content::NavigationController::UA_OVERRIDE_TRUE; - controller.LoadURLWithParams(params); -} - -bool Window::CanGoBack() { - return window_->GetWebContents()->GetController().CanGoBack(); -} - -bool Window::CanGoForward() { - return window_->GetWebContents()->GetController().CanGoForward(); -} - -bool Window::CanGoToOffset(int offset) { - return window_->GetWebContents()->GetController().CanGoToOffset(offset); -} - -void Window::GoBack() { - window_->GetWebContents()->GetController().GoBack(); -} - -void Window::GoForward() { - window_->GetWebContents()->GetController().GoForward(); -} - -void Window::GoToIndex(int index) { - window_->GetWebContents()->GetController().GoToIndex(index); -} - -void Window::GoToOffset(int offset) { - window_->GetWebContents()->GetController().GoToOffset(offset); -} - -void Window::Reload() { - window_->GetWebContents()->GetController().Reload(false); -} - -void Window::ReloadIgnoringCache() { - window_->GetWebContents()->GetController().ReloadIgnoringCache(false); -} - // static void Window::BuildPrototype(v8::Isolate* isolate, v8::Handle prototype) { @@ -409,17 +358,7 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("isWebViewFocused", &Window::IsWebViewFocused) .SetMethod("capturePage", &Window::CapturePage) .SetMethod("_getWebContents", &Window::GetWebContents) - .SetMethod("_getDevToolsWebContents", &Window::GetDevToolsWebContents) - .SetMethod("loadUrl", &Window::LoadURL) - .SetMethod("canGoBack", &Window::CanGoBack) - .SetMethod("canGoForward", &Window::CanGoForward) - .SetMethod("canGoToOffset", &Window::CanGoToOffset) - .SetMethod("goBack", &Window::GoBack) - .SetMethod("goForward", &Window::GoForward) - .SetMethod("goToIndex", &Window::GoToIndex) - .SetMethod("goToOffset", &Window::GoToOffset) - .SetMethod("reload", &Window::Reload) - .SetMethod("reloadIgnoringCache", &Window::ReloadIgnoringCache); + .SetMethod("_getDevToolsWebContents", &Window::GetDevToolsWebContents); } } // namespace api diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index e406e83c0a73..4d762bee90b3 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -103,18 +103,6 @@ class Window : public mate::EventEmitter, mate::Handle GetWebContents(v8::Isolate* isolate) const; mate::Handle GetDevToolsWebContents(v8::Isolate* isolate) const; - // APIs for NavigationController. - void LoadURL(const GURL& url); - bool CanGoBack(); - bool CanGoForward(); - bool CanGoToOffset(int offset); - void GoBack(); - void GoForward(); - void GoToIndex(int index); - void GoToOffset(int offset); - void Reload(); - void ReloadIgnoringCache(); - scoped_ptr window_; DISALLOW_COPY_AND_ASSIGN(Window); diff --git a/atom/browser/api/lib/browser-window.coffee b/atom/browser/api/lib/browser-window.coffee index c357078a9c10..df2c5393d131 100644 --- a/atom/browser/api/lib/browser-window.coffee +++ b/atom/browser/api/lib/browser-window.coffee @@ -88,7 +88,10 @@ BrowserWindow.fromDevTools = (processId, routingId) -> devtools.routingId == routingId # Be compatible with old API. +BrowserWindow::loadUrl = (url) -> @webContents.loadUrl(url) BrowserWindow::getUrl = -> @webContents.getUrl() +BrowserWindow::reload = -> @webContents.reload() +BrowserWindow::reloadIgnoringCache = -> @webContents.reloadIgnoringCache() BrowserWindow::getPageTitle = -> @webContents.getTitle() BrowserWindow::isLoading = -> @webContents.isLoading() BrowserWindow::isWaitingForResponse = -> @webContents.isWaitingForResponse()