Move some APIs from Window to WebContents.

This commit is contained in:
Cheng Zhao 2014-04-24 17:00:41 +08:00
parent 26e93e8798
commit 79babe858d
5 changed files with 67 additions and 52 deletions

View file

@ -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

View file

@ -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<WebContents> 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();

View file

@ -336,34 +336,6 @@ mate::Handle<WebContents> 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)

View file

@ -105,13 +105,6 @@ class Window : public mate::EventEmitter,
// APIs for WebContents.
mate::Handle<WebContents> GetWebContents(v8::Isolate* isolate) const;
mate::Handle<WebContents> 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);

View file

@ -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