From 79babe858def14add3bf0687a6729be180fab362 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 24 Apr 2014 17:00:41 +0800 Subject: [PATCH] Move some APIs from Window to WebContents. --- atom/browser/api/atom_api_web_contents.cc | 45 +++++++++++++++++++++- atom/browser/api/atom_api_web_contents.h | 11 ++++++ atom/browser/api/atom_api_window.cc | 43 --------------------- atom/browser/api/atom_api_window.h | 8 ---- atom/browser/api/lib/browser-window.coffee | 12 ++++++ 5 files changed, 67 insertions(+), 52 deletions(-) diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 47925db1f131..37b81f4e4115 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -4,6 +4,9 @@ #include "atom/browser/api/atom_api_web_contents.h" +#include "atom/common/native_mate_converters/gurl_converter.h" +#include "atom/common/native_mate_converters/string16_converter.h" +#include "content/public/browser/render_process_host.h" #include "content/public/browser/web_contents.h" #include "native_mate/object_template_builder.h" @@ -18,9 +21,49 @@ WebContents::WebContents(content::WebContents* web_contents) WebContents::~WebContents() { } +GURL WebContents::GetURL() const { + return web_contents_->GetURL(); +} + +string16 WebContents::GetTitle() const { + return web_contents_->GetTitle(); +} + +bool WebContents::IsLoading() const { + return web_contents_->IsLoading(); +} + +bool WebContents::IsWaitingForResponse() const { + return web_contents_->IsWaitingForResponse(); +} + +void WebContents::Stop() { + web_contents_->Stop(); +} + +int WebContents::GetRoutingID() const { + return web_contents_->GetRoutingID(); +} + +int WebContents::GetProcessID() const { + return web_contents_->GetRenderProcessHost()->GetID(); +} + +bool WebContents::IsCrashed() const { + return web_contents_->IsCrashed(); +} + mate::ObjectTemplateBuilder WebContents::GetObjectTemplateBuilder( v8::Isolate* isolate) { - return mate::ObjectTemplateBuilder(isolate); + return mate::ObjectTemplateBuilder(isolate) + .SetMethod("getUrl", &WebContents::GetURL) + .SetMethod("getTitle", &WebContents::GetTitle) + .SetMethod("isLoading", &WebContents::IsLoading) + .SetMethod("isWaitingForResponse", &WebContents::IsWaitingForResponse) + .SetMethod("stop", &WebContents::Stop) + .SetMethod("getRoutingId", &WebContents::GetRoutingID) + .SetMethod("getProcessId", &WebContents::GetProcessID) + .SetMethod("isCrashed", &WebContents::IsCrashed); } // static diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 5cad17da7772..8b8a5266d3d2 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -8,6 +8,8 @@ #include "atom/browser/api/event_emitter.h" #include "native_mate/handle.h" +class GURL; + namespace content { class WebContents; } @@ -21,6 +23,15 @@ class WebContents : public mate::EventEmitter { static mate::Handle Create(v8::Isolate* isolate, content::WebContents* web_contents); + GURL GetURL() const; + string16 GetTitle() const; + bool IsLoading() const; + bool IsWaitingForResponse() const; + void Stop(); + int GetRoutingID() const; + int GetProcessID() const; + bool IsCrashed() const; + protected: explicit WebContents(content::WebContents* web_contents); virtual ~WebContents(); diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index e17e3b3e7b22..ceb300b448ba 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -336,34 +336,6 @@ mate::Handle Window::GetDevToolsWebContents( return WebContents::Create(isolate, window_->GetDevToolsWebContents()); } -string16 Window::GetPageTitle() { - return window_->GetWebContents()->GetTitle(); -} - -bool Window::IsLoading() { - return window_->GetWebContents()->IsLoading(); -} - -bool Window::IsWaitingForResponse() { - return window_->GetWebContents()->IsWaitingForResponse(); -} - -void Window::Stop() { - window_->GetWebContents()->Stop(); -} - -int Window::GetRoutingID() { - return window_->GetWebContents()->GetRoutingID(); -} - -int Window::GetProcessID() { - return window_->GetWebContents()->GetRenderProcessHost()->GetID(); -} - -bool Window::IsCrashed() { - return window_->GetWebContents()->IsCrashed(); -} - mate::Dictionary Window::GetDevTools(v8::Isolate* isolate) { mate::Dictionary dict(mate::Dictionary::CreateEmpty(isolate)); content::WebContents* web_contents = window_->GetDevToolsWebContents(); @@ -385,13 +357,6 @@ void Window::LoadURL(const GURL& url) { controller.LoadURLWithParams(params); } -GURL Window::GetURL() { - NavigationController& controller = window_->GetWebContents()->GetController(); - if (!controller.GetActiveEntry()) - return GURL(); - return controller.GetActiveEntry()->GetVirtualURL(); -} - bool Window::CanGoBack() { return window_->GetWebContents()->GetController().CanGoBack(); } @@ -474,18 +439,10 @@ void Window::BuildPrototype(v8::Isolate* isolate, .SetMethod("capturePage", &Window::CapturePage) .SetMethod("getWebContents", &Window::GetWebContents) .SetMethod("getDevToolsWebContents", &Window::GetDevToolsWebContents) - .SetMethod("getPageTitle", &Window::GetPageTitle) - .SetMethod("isLoading", &Window::IsLoading) - .SetMethod("isWaitingForResponse", &Window::IsWaitingForResponse) - .SetMethod("stop", &Window::Stop) - .SetMethod("getRoutingId", &Window::GetRoutingID) - .SetMethod("getProcessId", &Window::GetProcessID) - .SetMethod("isCrashed", &Window::IsCrashed) .SetMethod("getDevTools", &Window::GetDevTools) .SetMethod("executeJavaScriptInDevTools", &Window::ExecuteJavaScriptInDevTools) .SetMethod("loadUrl", &Window::LoadURL) - .SetMethod("getUrl", &Window::GetURL) .SetMethod("canGoBack", &Window::CanGoBack) .SetMethod("canGoForward", &Window::CanGoForward) .SetMethod("canGoToOffset", &Window::CanGoToOffset) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index cd3b13e86a36..5c3927926bfe 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -105,13 +105,6 @@ class Window : public mate::EventEmitter, // APIs for WebContents. mate::Handle GetWebContents(v8::Isolate* isolate) const; mate::Handle GetDevToolsWebContents(v8::Isolate* isolate) const; - string16 GetPageTitle(); - bool IsLoading(); - bool IsWaitingForResponse(); - void Stop(); - int GetRoutingID(); - int GetProcessID(); - bool IsCrashed(); // APIs for devtools. mate::Dictionary GetDevTools(v8::Isolate* isolate); @@ -119,7 +112,6 @@ class Window : public mate::EventEmitter, // APIs for NavigationController. void LoadURL(const GURL& url); - GURL GetURL(); bool CanGoBack(); bool CanGoForward(); bool CanGoToOffset(int offset); diff --git a/atom/browser/api/lib/browser-window.coffee b/atom/browser/api/lib/browser-window.coffee index 18dacbf2e6e7..eb3d01e73eda 100644 --- a/atom/browser/api/lib/browser-window.coffee +++ b/atom/browser/api/lib/browser-window.coffee @@ -14,6 +14,8 @@ BrowserWindow::_init = -> menu = app.getApplicationMenu() @setMenu menu if menu? + @webContents = @getWebContents() + # Remember the window. id = BrowserWindow.windows.add this @@ -62,4 +64,14 @@ BrowserWindow.fromDevTools = (processId, routingId) -> return window if devtools.processId == processId and devtools.routingId == routingId +# Be compatible with old API. +BrowserWindow::getUrl = -> @webContents.getUrl() +BrowserWindow::getPageTitle = -> @webContents.getTitle() +BrowserWindow::isLoading = -> @webContents.isLoading() +BrowserWindow::isWaitingForResponse = -> @webContents.isWaitingForResponse() +BrowserWindow::stop = -> @webContents.stop() +BrowserWindow::getRoutingId = -> @webContents.getRoutingId() +BrowserWindow::getProcessId = -> @webContents.getProcessId() +BrowserWindow::isCrashed = -> @webContents.isCrashed() + module.exports = BrowserWindow