Add partial APIs for WebContents and NavigationController.

This commit is contained in:
Cheng Zhao 2013-04-18 15:09:53 +08:00
parent 257e439efb
commit d06472b092
4 changed files with 263 additions and 61 deletions

View file

@ -14,21 +14,26 @@
#include "ui/gfx/size.h"
using content::V8ValueConverter;
using content::NavigationController;
using node::ObjectWrap;
namespace atom {
namespace api {
namespace {
// Converts string16 to V8 String.
v8::Handle<v8::String> UTF16ToV8String(const string16& s) {
return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size());
}
}
Window::Window(v8::Handle<v8::Object> wrapper, base::DictionaryValue* options)
: EventEmitter(wrapper),
window_(NativeWindow::Create(AtomBrowserContext::Get(), options)) {
window_->InitFromOptions(options);
window_->GetWebContents()->GetController().LoadURL(
GURL("https://github.com"),
content::Referrer(),
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
}
Window::~Window() {
@ -55,7 +60,7 @@ v8::Handle<v8::Value> Window::New(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::Destroy(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
delete self;
@ -64,7 +69,7 @@ v8::Handle<v8::Value> Window::Destroy(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::Close(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Close();
@ -73,7 +78,7 @@ v8::Handle<v8::Value> Window::Close(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::Focus(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Focus(args[0]->IsBoolean() ? args[0]->BooleanValue(): true);
@ -82,7 +87,7 @@ v8::Handle<v8::Value> Window::Focus(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::Show(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Show();
@ -91,7 +96,7 @@ v8::Handle<v8::Value> Window::Show(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::Hide(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Hide();
@ -100,7 +105,7 @@ v8::Handle<v8::Value> Window::Hide(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::Maximize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Maximize();
@ -109,7 +114,7 @@ v8::Handle<v8::Value> Window::Maximize(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::Unmaximize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Unmaximize();
@ -118,7 +123,7 @@ v8::Handle<v8::Value> Window::Unmaximize(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::Minimize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Minimize();
@ -127,7 +132,7 @@ v8::Handle<v8::Value> Window::Minimize(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::Restore(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->Restore();
@ -136,7 +141,7 @@ v8::Handle<v8::Value> Window::Restore(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::SetFullscreen(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 1 || !args[0]->IsBoolean())
return node::ThrowTypeError("Bad argument");
@ -147,14 +152,14 @@ v8::Handle<v8::Value> Window::SetFullscreen(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::IsFullscreen(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
return v8::Boolean::New(self->window_->IsFullscreen());
}
// static
v8::Handle<v8::Value> Window::SetSize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 2)
return node::ThrowTypeError("Bad argument");
@ -166,7 +171,7 @@ v8::Handle<v8::Value> Window::SetSize(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::GetSize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
gfx::Size size = self->window_->GetSize();
v8::Handle<v8::Array> ret = v8::Array::New(2);
@ -178,7 +183,7 @@ v8::Handle<v8::Value> Window::GetSize(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::SetMinimumSize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 2)
return node::ThrowTypeError("Bad argument");
@ -190,7 +195,7 @@ v8::Handle<v8::Value> Window::SetMinimumSize(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::SetMaximumSize(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 2)
return node::ThrowTypeError("Bad argument");
@ -202,7 +207,7 @@ v8::Handle<v8::Value> Window::SetMaximumSize(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::SetResizable(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 1 || !args[0]->IsBoolean())
return node::ThrowTypeError("Bad argument");
@ -213,7 +218,7 @@ v8::Handle<v8::Value> Window::SetResizable(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::SetAlwaysOnTop(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 1 || !args[0]->IsBoolean())
return node::ThrowTypeError("Bad argument");
@ -224,7 +229,7 @@ v8::Handle<v8::Value> Window::SetAlwaysOnTop(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::SetPosition(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 2)
return node::ThrowTypeError("Bad argument");
@ -236,7 +241,7 @@ v8::Handle<v8::Value> Window::SetPosition(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::GetPosition(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
gfx::Point pos = self->window_->GetPosition();
v8::Handle<v8::Array> ret = v8::Array::New(2);
@ -248,7 +253,7 @@ v8::Handle<v8::Value> Window::GetPosition(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::SetTitle(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 1 || !args[0]->IsString())
return node::ThrowTypeError("Bad argument");
@ -259,7 +264,7 @@ v8::Handle<v8::Value> Window::SetTitle(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::GetTitle(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
std::string title = self->window_->GetTitle();
@ -268,7 +273,7 @@ v8::Handle<v8::Value> Window::GetTitle(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::FlashFrame(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->FlashFrame(
args[0]->IsBoolean() ? args[0]->BooleanValue(): true);
@ -278,7 +283,7 @@ v8::Handle<v8::Value> Window::FlashFrame(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::SetKiosk(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 1 || !args[0]->IsBoolean())
return node::ThrowTypeError("Bad argument");
@ -289,14 +294,14 @@ v8::Handle<v8::Value> Window::SetKiosk(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::IsKiosk(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
return v8::Boolean::New(self->window_->IsKiosk());
}
// static
v8::Handle<v8::Value> Window::ShowDevTools(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->ShowDevTools();
@ -305,13 +310,170 @@ v8::Handle<v8::Value> Window::ShowDevTools(const v8::Arguments &args) {
// static
v8::Handle<v8::Value> Window::CloseDevTools(const v8::Arguments &args) {
Window *self = ObjectWrap::Unwrap<Window>(args.This());
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->CloseDevTools();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::GetPageTitle(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
string16 title = self->window_->GetWebContents()->GetTitle();
return UTF16ToV8String(title);
}
// static
v8::Handle<v8::Value> Window::IsLoading(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
return v8::Boolean::New(self->window_->GetWebContents()->IsLoading());
}
// static
v8::Handle<v8::Value> Window::IsWaitingForResponse(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
return v8::Boolean::New(
self->window_->GetWebContents()->IsWaitingForResponse());
}
// static
v8::Handle<v8::Value> Window::Stop(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
self->window_->GetWebContents()->Stop();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::LoadURL(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 1 || !args[0]->IsString())
return node::ThrowTypeError("Bad argument");
NavigationController& controller =
self->window_->GetWebContents()->GetController();
controller.LoadURL(GURL(*v8::String::Utf8Value(args[0])),
content::Referrer(),
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::CanGoBack(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
NavigationController& controller =
self->window_->GetWebContents()->GetController();
return v8::Boolean::New(controller.CanGoBack());
}
// static
v8::Handle<v8::Value> Window::CanGoForward(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
NavigationController& controller =
self->window_->GetWebContents()->GetController();
return v8::Boolean::New(controller.CanGoForward());
}
// static
v8::Handle<v8::Value> Window::CanGoToOffset(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 1)
return node::ThrowTypeError("Bad argument");
NavigationController& controller =
self->window_->GetWebContents()->GetController();
int offset = args[0]->IntegerValue();
return v8::Boolean::New(controller.CanGoToOffset(offset));
}
// static
v8::Handle<v8::Value> Window::GoBack(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
NavigationController& controller =
self->window_->GetWebContents()->GetController();
controller.GoBack();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::GoForward(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
NavigationController& controller =
self->window_->GetWebContents()->GetController();
controller.GoForward();
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::GoToIndex(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 1)
return node::ThrowTypeError("Bad argument");
NavigationController& controller =
self->window_->GetWebContents()->GetController();
controller.GoToIndex(args[0]->IntegerValue());
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::GoToOffset(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
if (args.Length() < 1)
return node::ThrowTypeError("Bad argument");
NavigationController& controller =
self->window_->GetWebContents()->GetController();
controller.GoToOffset(args[0]->IntegerValue());
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::Reload(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
NavigationController& controller =
self->window_->GetWebContents()->GetController();
controller.Reload(args[0]->IsBoolean() ? args[0]->BooleanValue(): false);
return v8::Undefined();
}
// static
v8::Handle<v8::Value> Window::ReloadIgnoringCache(const v8::Arguments &args) {
Window* self = ObjectWrap::Unwrap<Window>(args.This());
NavigationController& controller =
self->window_->GetWebContents()->GetController();
controller.ReloadIgnoringCache(
args[0]->IsBoolean() ? args[0]->BooleanValue(): false);
return v8::Undefined();
}
// static
void Window::Initialize(v8::Handle<v8::Object> target) {
v8::HandleScope scope;
@ -320,32 +482,48 @@ void Window::Initialize(v8::Handle<v8::Object> target) {
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(v8::String::NewSymbol("Window"));
NODE_SET_PROTOTYPE_METHOD(t, "destroy" , Destroy);
NODE_SET_PROTOTYPE_METHOD(t, "close" , Close);
NODE_SET_PROTOTYPE_METHOD(t, "focus" , Focus);
NODE_SET_PROTOTYPE_METHOD(t, "show" , Show);
NODE_SET_PROTOTYPE_METHOD(t, "hide" , Hide);
NODE_SET_PROTOTYPE_METHOD(t, "maximize" , Maximize);
NODE_SET_PROTOTYPE_METHOD(t, "unmaximize" , Unmaximize);
NODE_SET_PROTOTYPE_METHOD(t, "minimize" , Minimize);
NODE_SET_PROTOTYPE_METHOD(t, "restore" , Restore);
NODE_SET_PROTOTYPE_METHOD(t, "setFullscreen" , SetFullscreen);
NODE_SET_PROTOTYPE_METHOD(t, "isFullscreen" , IsFullscreen);
NODE_SET_PROTOTYPE_METHOD(t, "setSize" , SetSize);
NODE_SET_PROTOTYPE_METHOD(t, "getSize" , GetSize);
NODE_SET_PROTOTYPE_METHOD(t, "setMinimumSize" , SetMinimumSize);
NODE_SET_PROTOTYPE_METHOD(t, "setMaximumSize" , SetMaximumSize);
NODE_SET_PROTOTYPE_METHOD(t, "setResizable" , SetResizable);
NODE_SET_PROTOTYPE_METHOD(t, "setAlwaysOnTop" , SetAlwaysOnTop);
NODE_SET_PROTOTYPE_METHOD(t, "setPosition" , SetPosition);
NODE_SET_PROTOTYPE_METHOD(t, "getPosition" , GetPosition);
NODE_SET_PROTOTYPE_METHOD(t, "setTitle" , SetTitle);
NODE_SET_PROTOTYPE_METHOD(t, "getTitle" , GetTitle);
NODE_SET_PROTOTYPE_METHOD(t, "flashFrame" , FlashFrame);
NODE_SET_PROTOTYPE_METHOD(t, "setKiosk" , SetKiosk);
NODE_SET_PROTOTYPE_METHOD(t, "isKiosk" , IsKiosk);
NODE_SET_PROTOTYPE_METHOD(t, "showDevTools" , ShowDevTools);
NODE_SET_PROTOTYPE_METHOD(t, "closeDevTools" , CloseDevTools);
NODE_SET_PROTOTYPE_METHOD(t, "destroy", Destroy);
NODE_SET_PROTOTYPE_METHOD(t, "close", Close);
NODE_SET_PROTOTYPE_METHOD(t, "focus", Focus);
NODE_SET_PROTOTYPE_METHOD(t, "show", Show);
NODE_SET_PROTOTYPE_METHOD(t, "hide", Hide);
NODE_SET_PROTOTYPE_METHOD(t, "maximize", Maximize);
NODE_SET_PROTOTYPE_METHOD(t, "unmaximize", Unmaximize);
NODE_SET_PROTOTYPE_METHOD(t, "minimize", Minimize);
NODE_SET_PROTOTYPE_METHOD(t, "restore", Restore);
NODE_SET_PROTOTYPE_METHOD(t, "setFullscreen", SetFullscreen);
NODE_SET_PROTOTYPE_METHOD(t, "isFullscreen", IsFullscreen);
NODE_SET_PROTOTYPE_METHOD(t, "setSize", SetSize);
NODE_SET_PROTOTYPE_METHOD(t, "getSize", GetSize);
NODE_SET_PROTOTYPE_METHOD(t, "setMinimumSize", SetMinimumSize);
NODE_SET_PROTOTYPE_METHOD(t, "setMaximumSize", SetMaximumSize);
NODE_SET_PROTOTYPE_METHOD(t, "setResizable", SetResizable);
NODE_SET_PROTOTYPE_METHOD(t, "setAlwaysOnTop", SetAlwaysOnTop);
NODE_SET_PROTOTYPE_METHOD(t, "setPosition", SetPosition);
NODE_SET_PROTOTYPE_METHOD(t, "getPosition", GetPosition);
NODE_SET_PROTOTYPE_METHOD(t, "setTitle", SetTitle);
NODE_SET_PROTOTYPE_METHOD(t, "getTitle", GetTitle);
NODE_SET_PROTOTYPE_METHOD(t, "flashFrame", FlashFrame);
NODE_SET_PROTOTYPE_METHOD(t, "setKiosk", SetKiosk);
NODE_SET_PROTOTYPE_METHOD(t, "isKiosk", IsKiosk);
NODE_SET_PROTOTYPE_METHOD(t, "showDevTools", ShowDevTools);
NODE_SET_PROTOTYPE_METHOD(t, "closeDevTools", CloseDevTools);
NODE_SET_PROTOTYPE_METHOD(t, "getPageTitle", GetPageTitle);
NODE_SET_PROTOTYPE_METHOD(t, "isLoading", IsLoading);
NODE_SET_PROTOTYPE_METHOD(t, "isWaitingForResponse", IsWaitingForResponse);
NODE_SET_PROTOTYPE_METHOD(t, "stop", Stop);
NODE_SET_PROTOTYPE_METHOD(t, "loadURL", LoadURL);
NODE_SET_PROTOTYPE_METHOD(t, "canGoBack", CanGoBack);
NODE_SET_PROTOTYPE_METHOD(t, "canGoForward", CanGoForward);
NODE_SET_PROTOTYPE_METHOD(t, "canGoToOffset", CanGoToOffset);
NODE_SET_PROTOTYPE_METHOD(t, "goBack", GoBack);
NODE_SET_PROTOTYPE_METHOD(t, "goForward", GoForward);
NODE_SET_PROTOTYPE_METHOD(t, "goToIndex", GoToIndex);
NODE_SET_PROTOTYPE_METHOD(t, "goToOffset", GoToOffset);
NODE_SET_PROTOTYPE_METHOD(t, "reload", Reload);
NODE_SET_PROTOTYPE_METHOD(t, "reloadIgnoringCache", ReloadIgnoringCache);
target->Set(v8::String::NewSymbol("Window"), t->GetFunction());
}

View file

@ -60,6 +60,24 @@ class Window : public EventEmitter {
static v8::Handle<v8::Value> ShowDevTools(const v8::Arguments &args);
static v8::Handle<v8::Value> CloseDevTools(const v8::Arguments &args);
// APIs for WebContents.
static v8::Handle<v8::Value> GetPageTitle(const v8::Arguments &args);
static v8::Handle<v8::Value> IsLoading(const v8::Arguments &args);
static v8::Handle<v8::Value> IsWaitingForResponse(const v8::Arguments &args);
static v8::Handle<v8::Value> Stop(const v8::Arguments &args);
// APIs for NavigationController.
static v8::Handle<v8::Value> LoadURL(const v8::Arguments &args);
static v8::Handle<v8::Value> CanGoBack(const v8::Arguments &args);
static v8::Handle<v8::Value> CanGoForward(const v8::Arguments &args);
static v8::Handle<v8::Value> CanGoToOffset(const v8::Arguments &args);
static v8::Handle<v8::Value> GoBack(const v8::Arguments &args);
static v8::Handle<v8::Value> GoForward(const v8::Arguments &args);
static v8::Handle<v8::Value> GoToIndex(const v8::Arguments &args);
static v8::Handle<v8::Value> GoToOffset(const v8::Arguments &args);
static v8::Handle<v8::Value> Reload(const v8::Arguments &args);
static v8::Handle<v8::Value> ReloadIgnoringCache(const v8::Arguments &args);
scoped_ptr<NativeWindow> window_;
DISALLOW_COPY_AND_ASSIGN(Window);

View file

@ -0,0 +1,9 @@
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>Title</title>
</head>
<body>
Content
</body>
</html>

View file

@ -5,8 +5,5 @@ var mainWindow = null;
atom.browserMainParts.preMainMessageLoopRun = function() {
mainWindow = new Window({ width: 800, height: 600 });
setTimeout(function() {
mainWindow.destroy();
}, 10000);
mainWindow.loadURL('file://' + __dirname + '/index.html');
}