From: Cheng Zhao Date: Tue, 22 Apr 2014 15:07:21 +0000 (+0800) Subject: Discard all our custom V8 helpers, use native-mate instead. X-Git-Tag: v0.12.0~18^2~14 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=a040a966529adc400db23fcccb99069e1e2f806c;p=electron.git Discard all our custom V8 helpers, use native-mate instead. --- diff --git a/atom.gyp b/atom.gyp index 444c3e6e..a56ae613 100644 --- a/atom.gyp +++ b/atom.gyp @@ -50,8 +50,6 @@ 'atom/browser/api/atom_api_auto_updater.h', 'atom/browser/api/atom_api_browser_ipc.cc', 'atom/browser/api/atom_api_dialog.cc', - 'atom/browser/api/atom_api_event.cc', - 'atom/browser/api/atom_api_event.h', 'atom/browser/api/atom_api_menu.cc', 'atom/browser/api/atom_api_menu.h', 'atom/browser/api/atom_api_menu_gtk.cc', @@ -146,8 +144,6 @@ 'atom/common/api/api_messages.h', 'atom/common/api/atom_api_clipboard.cc', 'atom/common/api/atom_api_crash_reporter.cc', - 'atom/common/api/atom_api_event_emitter.cc', - 'atom/common/api/atom_api_event_emitter.h', 'atom/common/api/atom_api_id_weak_map.cc', 'atom/common/api/atom_api_id_weak_map.h', 'atom/common/api/atom_api_screen.cc', @@ -181,6 +177,7 @@ 'atom/common/linux/application_info.cc', 'atom/common/native_mate_converters/file_path_converter.h', 'atom/common/native_mate_converters/function_converter.h', + 'atom/common/native_mate_converters/gurl_converter.h', 'atom/common/native_mate_converters/string16_converter.h', 'atom/common/native_mate_converters/v8_value_converter.cc', 'atom/common/native_mate_converters/v8_value_converter.h', @@ -201,9 +198,6 @@ 'atom/common/platform_util_linux.cc', 'atom/common/platform_util_mac.mm', 'atom/common/platform_util_win.cc', - 'atom/common/swap_or_assign.h', - 'atom/common/v8/scoped_persistent.h', - 'atom/common/v8/native_type_conversions.h', 'atom/renderer/api/atom_api_renderer_ipc.cc', 'atom/renderer/api/atom_renderer_bindings.cc', 'atom/renderer/api/atom_renderer_bindings.h', diff --git a/atom/browser/api/atom_api_dialog.cc b/atom/browser/api/atom_api_dialog.cc index 8fe5a131..e1aaec11 100644 --- a/atom/browser/api/atom_api_dialog.cc +++ b/atom/browser/api/atom_api_dialog.cc @@ -16,30 +16,6 @@ #include "atom/common/node_includes.h" -namespace mate { - -template<> -struct Converter { - static bool FromV8(v8::Isolate* isolate, - v8::Handle val, - atom::NativeWindow** out) { - using atom::api::Window; - if (val->IsNull()) { - *out = NULL; - return true; // NULL is a valid value for NativeWindow*. - } else if (val->IsObject()) { - Window* window = Window::Unwrap(val->ToObject()); - *out = window->window(); - return true; - } else { - return false; - } - } -}; - -} // namespace mate - - namespace { void ShowMessageBox(int type, @@ -47,18 +23,18 @@ void ShowMessageBox(int type, const std::string& title, const std::string& message, const std::string& detail, - atom::NativeWindow* window, + atom::api::Window* window, mate::Arguments* args) { v8::Handle peek = args->PeekNext(); atom::MessageBoxCallback callback; if (mate::Converter::FromV8(node_isolate, peek, &callback)) { - atom::ShowMessageBox(window, (atom::MessageBoxType)type, buttons, title, - message, detail, callback); + atom::ShowMessageBox(window->window(), (atom::MessageBoxType)type, buttons, + title, message, detail, callback); } else { int chosen = atom::ShowMessageBox( - window, + window->window(), (atom::MessageBoxType)type, buttons, title, @@ -71,18 +47,18 @@ void ShowMessageBox(int type, void ShowOpenDialog(const std::string& title, const base::FilePath& default_path, int properties, - atom::NativeWindow* window, + atom::api::Window* window, mate::Arguments* args) { v8::Handle peek = args->PeekNext(); file_dialog::OpenDialogCallback callback; if (mate::Converter::FromV8(node_isolate, peek, &callback)) { - file_dialog::ShowOpenDialog(window, title, default_path, properties, - callback); + file_dialog::ShowOpenDialog(window->window(), title, default_path, + properties, callback); } else { std::vector paths; - if (file_dialog::ShowOpenDialog(window, + if (file_dialog::ShowOpenDialog(window->window(), title, default_path, properties, @@ -93,19 +69,18 @@ void ShowOpenDialog(const std::string& title, void ShowSaveDialog(const std::string& title, const base::FilePath& default_path, - atom::NativeWindow* window, + atom::api::Window* window, mate::Arguments* args) { v8::Handle peek = args->PeekNext(); file_dialog::SaveDialogCallback callback; if (mate::Converter::FromV8(node_isolate, peek, &callback)) { - file_dialog::ShowSaveDialog(window, title, default_path, callback); + file_dialog::ShowSaveDialog(window->window(), title, default_path, + callback); } else { base::FilePath path; - if (file_dialog::ShowSaveDialog(window, - title, - default_path, + if (file_dialog::ShowSaveDialog(window->window(), title, default_path, &path)) args->Return(path); } diff --git a/atom/browser/api/atom_api_event.cc b/atom/browser/api/atom_api_event.cc deleted file mode 100644 index f1d305d0..00000000 --- a/atom/browser/api/atom_api_event.cc +++ /dev/null @@ -1,99 +0,0 @@ -// Copyright (c) 2013 GitHub, Inc. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "atom/browser/api/atom_api_event.h" - -#include "atom/common/api/api_messages.h" -#include "atom/common/node_includes.h" -#include "atom/common/v8/native_type_conversions.h" -#include "content/public/browser/web_contents.h" - -namespace atom { - -namespace api { - -ScopedPersistent Event::constructor_template_; - -Event::Event() - : sender_(NULL), - message_(NULL), - prevent_default_(false) { -} - -Event::~Event() { -} - -// static -v8::Handle Event::CreateV8Object() { - if (constructor_template_.IsEmpty()) { - v8::Local t = v8::FunctionTemplate::New(New); - t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(v8::String::NewSymbol("Event")); - - NODE_SET_PROTOTYPE_METHOD(t, "preventDefault", PreventDefault); - NODE_SET_PROTOTYPE_METHOD(t, "sendReply", SendReply); - NODE_SET_PROTOTYPE_METHOD(t, "destroy", Destroy); - - constructor_template_.reset(t->GetFunction()); - } - - v8::Handle t = constructor_template_.NewHandle(node_isolate); - return t->NewInstance(0, NULL); -} - -void Event::SetSenderAndMessage(content::WebContents* sender, - IPC::Message* message) { - DCHECK(!sender_); - DCHECK(!message_); - sender_ = sender; - message_ = message; - - Observe(sender); -} - -void Event::WebContentsDestroyed(content::WebContents* web_contents) { - sender_ = NULL; - message_ = NULL; -} - -// static -void Event::New(const v8::FunctionCallbackInfo& args) { - Event* event = new Event; - event->Wrap(args.This()); -} - -// static -void Event::PreventDefault(const v8::FunctionCallbackInfo& args) { - Event* event = Unwrap(args.This()); - if (event == NULL) - return node::ThrowError("Event is already destroyed"); - - event->prevent_default_ = true; -} - -// static -void Event::SendReply(const v8::FunctionCallbackInfo& args) { - Event* event = Unwrap(args.This()); - if (event == NULL) - return node::ThrowError("Event is already destroyed"); - - if (event->message_ == NULL || event->sender_ == NULL) - return node::ThrowError("Can only send reply to synchronous events"); - - string16 json = FromV8Value(args[0]); - - AtomViewHostMsg_Message_Sync::WriteReplyParams(event->message_, json); - event->sender_->Send(event->message_); - - delete event; -} - -// static -void Event::Destroy(const v8::FunctionCallbackInfo& args) { - delete Unwrap(args.This()); -} - -} // namespace api - -} // namespace atom diff --git a/atom/browser/api/atom_api_event.h b/atom/browser/api/atom_api_event.h deleted file mode 100644 index 1bbcb4a2..00000000 --- a/atom/browser/api/atom_api_event.h +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2013 GitHub, Inc. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef ATOM_BROWSER_API_ATOM_API_EVENT_H_ -#define ATOM_BROWSER_API_ATOM_API_EVENT_H_ - -#include "atom/common/v8/scoped_persistent.h" -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "base/strings/string16.h" -#include "content/public/browser/web_contents_observer.h" -#include "vendor/node/src/node_object_wrap.h" - -namespace IPC { -class Message; -} - -namespace atom { - -namespace api { - -class Event : public node::ObjectWrap, - public content::WebContentsObserver { - public: - virtual ~Event(); - - // Create a V8 Event object. - static v8::Handle CreateV8Object(); - - // Pass the sender and message to be replied. - void SetSenderAndMessage(content::WebContents* sender, IPC::Message* message); - - // Whether event.preventDefault() is called. - bool prevent_default() const { return prevent_default_; } - - protected: - Event(); - - // content::WebContentsObserver implementations: - virtual void WebContentsDestroyed(content::WebContents*) OVERRIDE; - - private: - static void New(const v8::FunctionCallbackInfo& args); - - static void PreventDefault(const v8::FunctionCallbackInfo& args); - static void SendReply(const v8::FunctionCallbackInfo& args); - static void Destroy(const v8::FunctionCallbackInfo& args); - - static ScopedPersistent constructor_template_; - - // Replyer for the synchronous messages. - content::WebContents* sender_; - IPC::Message* message_; - - bool prevent_default_; - - DISALLOW_COPY_AND_ASSIGN(Event); -}; - -} // namespace api - -} // namespace atom - -#endif // ATOM_BROWSER_API_ATOM_API_EVENT_H_ diff --git a/atom/browser/api/atom_api_menu.cc b/atom/browser/api/atom_api_menu.cc index dc0ac656..77c77933 100644 --- a/atom/browser/api/atom_api_menu.cc +++ b/atom/browser/api/atom_api_menu.cc @@ -13,30 +13,6 @@ #include "atom/common/node_includes.h" -namespace mate { - -template<> -struct Converter { - static bool FromV8(v8::Isolate* isolate, - v8::Handle val, - atom::NativeWindow** out) { - using atom::api::Window; - if (val->IsNull()) { - *out = NULL; - return true; // NULL is a valid value for NativeWindow*. - } else if (val->IsObject()) { - Window* window = Window::Unwrap(val->ToObject()); - *out = window->window(); - return true; - } else { - return false; - } - } -}; - -} // namespace mate - - namespace atom { namespace api { diff --git a/atom/browser/api/atom_api_menu.h b/atom/browser/api/atom_api_menu.h index 2f00c736..779f1433 100644 --- a/atom/browser/api/atom_api_menu.h +++ b/atom/browser/api/atom_api_menu.h @@ -7,14 +7,13 @@ #include +#include "atom/browser/api/atom_api_window.h" #include "base/memory/scoped_ptr.h" #include "ui/base/models/simple_menu_model.h" #include "native_mate/wrappable.h" namespace atom { -class NativeWindow; - namespace api { class MenuMac; @@ -51,7 +50,7 @@ class Menu : public mate::Wrappable, virtual string16 GetSublabelForCommandId(int command_id) const OVERRIDE; virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; - virtual void Popup(NativeWindow* window) = 0; + virtual void Popup(Window* window) = 0; scoped_ptr model_; @@ -81,7 +80,7 @@ class Menu : public mate::Wrappable, bool IsVisibleAt(int index) const; #if defined(OS_WIN) || defined(TOOLKIT_GTK) - void AttachToWindow(NativeWindow* window); + void AttachToWindow(Window* window); #endif DISALLOW_COPY_AND_ASSIGN(Menu); diff --git a/atom/browser/api/atom_api_menu_gtk.cc b/atom/browser/api/atom_api_menu_gtk.cc index d70d439c..dff0b032 100644 --- a/atom/browser/api/atom_api_menu_gtk.cc +++ b/atom/browser/api/atom_api_menu_gtk.cc @@ -18,10 +18,11 @@ namespace api { MenuGtk::MenuGtk() { } -void MenuGtk::Popup(NativeWindow* native_window) { +void MenuGtk::Popup(Window* window) { uint32_t triggering_event_time; gfx::Point point; + BrowserWindow* native_window = window->window(); GdkEventButton* event = native_window->GetWebContents()-> GetRenderWidgetHostView()->GetLastMouseDown(); if (event) { @@ -36,11 +37,8 @@ void MenuGtk::Popup(NativeWindow* native_window) { menu_gtk_->PopupAsContext(point, triggering_event_time); } -void Menu::AttachToWindow(NativeWindow* window) { - if (window == NULL) - return node::ThrowTypeError("Window is dead"); - - static_cast(native_window)->SetMenu(model_.get()); +void Menu::AttachToWindow(Window* window) { + static_cast(window->window())->SetMenu(model_.get()); } // static diff --git a/atom/browser/api/atom_api_menu_gtk.h b/atom/browser/api/atom_api_menu_gtk.h index 5e53bdc1..1a2bbdd9 100644 --- a/atom/browser/api/atom_api_menu_gtk.h +++ b/atom/browser/api/atom_api_menu_gtk.h @@ -15,7 +15,7 @@ namespace api { class MenuGtk : public Menu, public ::MenuGtk::Delegate { protected: - virtual void Popup(NativeWindow* window) OVERRIDE; + virtual void Popup(Window* window) OVERRIDE; private: MenuGtk(); diff --git a/atom/browser/api/atom_api_menu_mac.h b/atom/browser/api/atom_api_menu_mac.h index f4995966..3855683e 100644 --- a/atom/browser/api/atom_api_menu_mac.h +++ b/atom/browser/api/atom_api_menu_mac.h @@ -19,7 +19,7 @@ class MenuMac : public Menu { protected: MenuMac(); - virtual void Popup(NativeWindow* window) OVERRIDE; + virtual void Popup(Window* window) OVERRIDE; base::scoped_nsobject menu_controller_; diff --git a/atom/browser/api/atom_api_menu_mac.mm b/atom/browser/api/atom_api_menu_mac.mm index 0fc3e491..922dfb91 100644 --- a/atom/browser/api/atom_api_menu_mac.mm +++ b/atom/browser/api/atom_api_menu_mac.mm @@ -19,22 +19,23 @@ namespace api { MenuMac::MenuMac() { } -void MenuMac::Popup(NativeWindow* native_window) { +void MenuMac::Popup(Window* window) { base::scoped_nsobject menu_controller( [[AtomMenuController alloc] initWithModel:model_.get()]); - NSWindow* window = native_window->GetNativeWindow(); + NativeWindow* native_window = window->window(); + NSWindow* nswindow = native_window->GetNativeWindow(); content::WebContents* web_contents = native_window->GetWebContents(); // Fake out a context menu event. NSEvent* currentEvent = [NSApp currentEvent]; - NSPoint position = [window mouseLocationOutsideOfEventStream]; + NSPoint position = [nswindow mouseLocationOutsideOfEventStream]; NSTimeInterval eventTime = [currentEvent timestamp]; NSEvent* clickEvent = [NSEvent mouseEventWithType:NSRightMouseDown location:position modifierFlags:NSRightMouseDownMask timestamp:eventTime - windowNumber:[window windowNumber] + windowNumber:[nswindow windowNumber] context:nil eventNumber:0 clickCount:1 diff --git a/atom/browser/api/atom_api_menu_win.cc b/atom/browser/api/atom_api_menu_win.cc index 48c6fe10..5aff3c46 100644 --- a/atom/browser/api/atom_api_menu_win.cc +++ b/atom/browser/api/atom_api_menu_win.cc @@ -18,17 +18,14 @@ namespace api { MenuWin::MenuWin() { } -void MenuWin::Popup(NativeWindow* native_window) { +void MenuWin::Popup(Window* window) { gfx::Point cursor = gfx::Screen::GetNativeScreen()->GetCursorScreenPoint(); menu_.reset(new atom::Menu2(model_.get())); menu_->RunContextMenuAt(cursor); } -void Menu::AttachToWindow(NativeWindow* window) { - if (window == NULL) - return node::ThrowTypeError("Window is dead"); - - static_cast(native_window)->SetMenu(model_.get()); +void Menu::AttachToWindow(Window* window) { + static_cast(window->window())->SetMenu(model_.get()); } // static diff --git a/atom/browser/api/atom_api_menu_win.h b/atom/browser/api/atom_api_menu_win.h index 732e305b..eed70035 100644 --- a/atom/browser/api/atom_api_menu_win.h +++ b/atom/browser/api/atom_api_menu_win.h @@ -15,7 +15,7 @@ namespace api { class MenuWin : public Menu { protected: - virtual void Popup(NativeWindow* window) OVERRIDE; + virtual void Popup(Window* window) OVERRIDE; private: MenuWin(); diff --git a/atom/browser/api/atom_api_window.cc b/atom/browser/api/atom_api_window.cc index ecbb6062..989f153c 100644 --- a/atom/browser/api/atom_api_window.cc +++ b/atom/browser/api/atom_api_window.cc @@ -4,36 +4,71 @@ #include "atom/browser/api/atom_api_window.h" -#include - +#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 "base/process/kill.h" -#include "atom/browser/native_window.h" -#include "atom/common/v8/native_type_conversions.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" #include "ui/gfx/point.h" +#include "ui/gfx/rect.h" #include "ui/gfx/size.h" #include "atom/common/node_includes.h" -#include "vendor/node/src/node_buffer.h" using content::NavigationController; -using node::ObjectWrap; -#define UNWRAP_WINDOW_AND_CHECK \ - Window* self = ObjectWrap::Unwrap(args.This()); \ - if (self == NULL) \ - return node::ThrowError("Window is already destroyed") +namespace mate { + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Handle val, + gfx::Rect* out) { + if (!val->IsObject()) + return false; + mate::Dictionary dict(isolate, val->ToObject()); + int x, y, width, height; + if (!dict.Get("x", &x) || !dict.Get("y", &y) || + !dict.Get("width", &width) || !dict.Get("height", &height)) + return false; + *out = gfx::Rect(x, y, width, height); + return true; + } +}; + +} // namespace mate namespace atom { namespace api { -Window::Window(v8::Handle wrapper, base::DictionaryValue* options) - : EventEmitter(wrapper), - window_(NativeWindow::Create(options)) { +namespace { + +void OnCapturePageDone( + const base::Callback)>& callback, + const std::vector& data) { + v8::Locker locker(node_isolate); + v8::HandleScope handle_scope(node_isolate); + + v8::Local buffer = node::Buffer::New( + reinterpret_cast(data.data()), + data.size()); + callback.Run(buffer); +} + +} // namespace + + +Window::Window(base::DictionaryValue* options) + : window_(NativeWindow::Create(options)) { window_->InitFromOptions(options); window_->AddObserver(this); } @@ -48,13 +83,13 @@ void Window::OnPageTitleUpdated(bool* prevent_default, const std::string& title) { base::ListValue args; args.AppendString(title); - *prevent_default = Emit("page-title-updated", &args); + *prevent_default = Emit("page-title-updated", args); } void Window::OnLoadingStateChanged(bool is_loading) { base::ListValue args; args.AppendBoolean(is_loading); - Emit("loading-state-changed", &args); + Emit("loading-state-changed", args); } void Window::WillCloseWindow(bool* prevent_default) { @@ -63,10 +98,6 @@ void Window::WillCloseWindow(bool* prevent_default) { void Window::OnWindowClosed() { Emit("closed"); - - // Free memory when native window is closed, the delete is delayed so other - // observers would not get a invalid pointer of NativeWindow. - base::MessageLoop::current()->DeleteSoon(FROM_HERE, this); } void Window::OnWindowBlur() { @@ -85,459 +116,257 @@ void Window::OnRenderViewDeleted(int process_id, int routing_id) { base::ListValue args; args.AppendInteger(process_id); args.AppendInteger(routing_id); - Emit("render-view-deleted", &args); + Emit("render-view-deleted", args); } void Window::OnRendererCrashed() { Emit("crashed"); } -void Window::OnCapturePageDone(const RefCountedV8Function& callback, - const std::vector& data) { - v8::Locker locker(node_isolate); - v8::HandleScope handle_scope(node_isolate); - - v8::Local buffer = node::Buffer::New( - reinterpret_cast(data.data()), - data.size()); - callback->NewHandle(node_isolate)->Call( - v8::Context::GetCurrent()->Global(), 1, &buffer); -} - // static -void Window::New(const v8::FunctionCallbackInfo& args) { - if (!args.IsConstructCall()) - return node::ThrowError("Require constructor call"); - - scoped_ptr options; - if (!FromV8Arguments(args, &options)) - return node::ThrowTypeError("Bad argument"); - - if (!options || !options->IsType(base::Value::TYPE_DICTIONARY)) - return node::ThrowTypeError("Options must be dictionary"); - - new Window(args.This(), static_cast(options.get())); +mate::Wrappable* Window::New(mate::Arguments* args, + const base::DictionaryValue& options) { + scoped_ptr copied_options(options.DeepCopy()); + Window* window = new Window(copied_options.get()); + window->Wrap(args->isolate(), args->GetThis()); // Give js code a chance to do initialization. - node::MakeCallback(args.This(), "_init", 0, NULL); -} - -// static -void Window::Destroy(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; + node::MakeCallback(args->GetThis(), "_init", 0, NULL); - base::ProcessHandle handle = self->window_->GetRenderProcessHandle(); - delete self; - - // Make sure the renderer process is terminated, it could happen that the - // renderer process became a zombie. - base::MessageLoop::current()->PostDelayedTask( - FROM_HERE, - base::Bind(base::IgnoreResult(base::KillProcess), handle, 0, false), - base::TimeDelta::FromMilliseconds(5000)); + return window; } -// static -void Window::Close(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->Close(); +void Window::Destroy() { + base::KillProcess(window_->GetRenderProcessHandle(), 0, false); + window_->CloseImmediately(); } -// static -void Window::Focus(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->Focus(args[0]->IsBoolean() ? args[0]->BooleanValue(): true); +void Window::Close() { + window_->Close(); } -// static -void Window::IsFocused(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->IsFocused()); +void Window::Focus() { + window_->Focus(true); } -// static -void Window::Show(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->Show(); +bool Window::IsFocused() { + return window_->IsFocused(); } -// static -void Window::Hide(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->Hide(); +void Window::Show() { + window_->Show(); } -// static -void Window::IsVisible(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - return args.GetReturnValue().Set(self->window_->IsVisible()); +void Window::Hide() { + window_->Hide(); } -// static -void Window::Maximize(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->Maximize(); +bool Window::IsVisible() { + return window_->IsVisible(); } -// static -void Window::Unmaximize(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->Unmaximize(); +void Window::Maximize() { + window_->Maximize(); } -// static -void Window::Minimize(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->Minimize(); +void Window::Unmaximize() { + window_->Unmaximize(); } -// static -void Window::Restore(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->Restore(); +void Window::Minimize() { + window_->Minimize(); } -// static -void Window::SetFullscreen(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - bool fs; - if (!FromV8Arguments(args, &fs)) - return node::ThrowTypeError("Bad argument"); - - self->window_->SetFullscreen(fs); +void Window::Restore() { + window_->Restore(); } -// static -void Window::IsFullscreen(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->IsFullscreen()); +void Window::SetFullscreen(bool fullscreen) { + window_->SetFullscreen(fullscreen); } -// static -void Window::SetSize(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - int width, height; - if (!FromV8Arguments(args, &width, &height)) - return node::ThrowTypeError("Bad argument"); - - self->window_->SetSize(gfx::Size(width, height)); +bool Window::IsFullscreen() { + return window_->IsFullscreen(); } -// static -void Window::GetSize(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - gfx::Size size = self->window_->GetSize(); - v8::Handle ret = v8::Array::New(2); - ret->Set(0, ToV8Value(size.width())); - ret->Set(1, ToV8Value(size.height())); - - args.GetReturnValue().Set(ret); +void Window::SetSize(int width, int height) { + window_->SetSize(gfx::Size(width, height)); } -// static -void Window::SetMinimumSize(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - int width, height; - if (!FromV8Arguments(args, &width, &height)) - return node::ThrowTypeError("Bad argument"); - - self->window_->SetMinimumSize(gfx::Size(width, height)); +std::vector Window::GetSize() { + std::vector result(2); + gfx::Size size = window_->GetSize(); + result[0] = size.width(); + result[1] = size.height(); + return result; } -// static -void Window::GetMinimumSize(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - gfx::Size size = self->window_->GetMinimumSize(); - v8::Handle ret = v8::Array::New(2); - ret->Set(0, ToV8Value(size.width())); - ret->Set(1, ToV8Value(size.height())); - - args.GetReturnValue().Set(ret); +void Window::SetMinimumSize(int width, int height) { + window_->SetMinimumSize(gfx::Size(width, height)); } -// static -void Window::SetMaximumSize(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - int width, height; - if (!FromV8Arguments(args, &width, &height)) - return node::ThrowTypeError("Bad argument"); - - self->window_->SetMaximumSize(gfx::Size(width, height)); +std::vector Window::GetMinimumSize() { + std::vector result(2); + gfx::Size size = window_->GetMinimumSize(); + result[0] = size.width(); + result[1] = size.height(); + return result; } -// static -void Window::GetMaximumSize(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - gfx::Size size = self->window_->GetMaximumSize(); - v8::Handle ret = v8::Array::New(2); - ret->Set(0, ToV8Value(size.width())); - ret->Set(1, ToV8Value(size.height())); - - args.GetReturnValue().Set(ret); +void Window::SetMaximumSize(int width, int height) { + window_->SetMaximumSize(gfx::Size(width, height)); } -// static -void Window::SetResizable(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - bool resizable; - if (!FromV8Arguments(args, &resizable)) - return node::ThrowTypeError("Bad argument"); - - self->window_->SetResizable(resizable); +std::vector Window::GetMaximumSize() { + std::vector result(2); + gfx::Size size = window_->GetMaximumSize(); + result[0] = size.width(); + result[1] = size.height(); + return result; } -// static -void Window::IsResizable(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->IsResizable()); +void Window::SetResizable(bool resizable) { + window_->SetResizable(resizable); } -// static -void Window::SetAlwaysOnTop(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - bool top; - if (!FromV8Arguments(args, &top)) - return node::ThrowTypeError("Bad argument"); - - self->window_->SetAlwaysOnTop(top); +bool Window::IsResizable() { + return window_->IsResizable(); } -// static -void Window::IsAlwaysOnTop(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->IsAlwaysOnTop()); +void Window::SetAlwaysOnTop(bool top) { + window_->SetAlwaysOnTop(top); } -// static -void Window::Center(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->Center(); +bool Window::IsAlwaysOnTop() { + return window_->IsAlwaysOnTop(); } -// static -void Window::SetPosition(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - int x, y; - if (!FromV8Arguments(args, &x, &y)) - return node::ThrowTypeError("Bad argument"); - - self->window_->SetPosition(gfx::Point(x, y)); +void Window::Center() { + window_->Center(); } -// static -void Window::GetPosition(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - gfx::Point pos = self->window_->GetPosition(); - v8::Handle ret = v8::Array::New(2); - ret->Set(0, ToV8Value(pos.x())); - ret->Set(1, ToV8Value(pos.y())); - - args.GetReturnValue().Set(ret); +void Window::SetPosition(int x, int y) { + window_->SetPosition(gfx::Point(x, y)); } -// static -void Window::SetTitle(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - std::string title; - if (!FromV8Arguments(args, &title)) - return node::ThrowTypeError("Bad argument"); - - self->window_->SetTitle(title); +std::vector Window::GetPosition() { + std::vector result(2); + gfx::Point pos = window_->GetPosition(); + result[0] = pos.x(); + result[1] = pos.y(); + return result; } -// static -void Window::GetTitle(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(ToV8Value(self->window_->GetTitle())); +void Window::SetTitle(const std::string& title) { + window_->SetTitle(title); } -// static -void Window::FlashFrame(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->FlashFrame( - args[0]->IsBoolean() ? args[0]->BooleanValue(): true); +std::string Window::GetTitle() { + return window_->GetTitle(); } -// static -void Window::SetKiosk(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - bool kiosk; - if (!FromV8Arguments(args, &kiosk)) - return node::ThrowTypeError("Bad argument"); - - self->window_->SetKiosk(kiosk); +void Window::FlashFrame(bool flash) { + window_->FlashFrame(flash); } -// static -void Window::IsKiosk(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->IsKiosk()); +void Window::SetKiosk(bool kiosk) { + window_->SetKiosk(kiosk); } -// static -void Window::OpenDevTools(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->OpenDevTools(); +bool Window::IsKiosk() { + return window_->IsKiosk(); } -// static -void Window::CloseDevTools(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->CloseDevTools(); +void Window::OpenDevTools() { + window_->OpenDevTools(); } -// static -void Window::IsDevToolsOpened(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->IsDevToolsOpened()); +void Window::CloseDevTools() { + window_->CloseDevTools(); } -// static -void Window::InspectElement(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - int x, y; - if (!FromV8Arguments(args, &x, &y)) - return node::ThrowTypeError("Bad argument"); - - self->window_->InspectElement(x, y); +bool Window::IsDevToolsOpened() { + return window_->IsDevToolsOpened(); } -// static -void Window::DebugDevTools(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - if (self->window_->IsDevToolsOpened()) - NativeWindow::Debug(self->window_->GetDevToolsWebContents()); +void Window::InspectElement(int x, int y) { + window_->InspectElement(x, y); } -// static -void Window::FocusOnWebView(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->FocusOnWebView(); +void Window::DebugDevTools() { + if (window_->IsDevToolsOpened()) + NativeWindow::Debug(window_->GetDevToolsWebContents()); } -// static -void Window::BlurWebView(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->BlurWebView(); +void Window::FocusOnWebView() { + window_->FocusOnWebView(); } -// static -void Window::IsWebViewFocused(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->IsWebViewFocused()); +void Window::BlurWebView() { + window_->BlurWebView(); } -// static -void Window::CapturePage(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; +bool Window::IsWebViewFocused() { + return window_->IsWebViewFocused(); +} +void Window::CapturePage(mate::Arguments* args) { gfx::Rect rect; - RefCountedV8Function callback; - if (!FromV8Arguments(args, &rect, &callback) && - !FromV8Arguments(args, &callback)) - return node::ThrowTypeError("Bad argument"); + base::Callback)> callback; - self->window_->CapturePage(rect, base::Bind(&Window::OnCapturePageDone, - base::Unretained(self), - callback)); -} + if ((args->Length() == 1 && !args->GetNext(&callback)) || + (args->Length() == 2 && !args->GetNext(&rect) + && !args->GetNext(&callback)) || + (args->Length() == 0)) { + args->ThrowError(); + return; + } -// static -void Window::GetPageTitle(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(ToV8Value( - self->window_->GetWebContents()->GetTitle())); + window_->CapturePage(rect, base::Bind(&OnCapturePageDone, callback)); } -// static -void Window::IsLoading(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->GetWebContents()->IsLoading()); +string16 Window::GetPageTitle() { + return window_->GetWebContents()->GetTitle(); } -// static -void Window::IsWaitingForResponse( - const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set( - self->window_->GetWebContents()->IsWaitingForResponse()); +bool Window::IsLoading() { + return window_->GetWebContents()->IsLoading(); } -// static -void Window::Stop(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - self->window_->GetWebContents()->Stop(); +bool Window::IsWaitingForResponse() { + return window_->GetWebContents()->IsWaitingForResponse(); } -// static -void Window::GetRoutingID(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->GetWebContents()->GetRoutingID()); +void Window::Stop() { + window_->GetWebContents()->Stop(); } -// static -void Window::GetProcessID(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - args.GetReturnValue().Set( - self->window_->GetWebContents()->GetRenderProcessHost()->GetID()); +int Window::GetRoutingID() { + return window_->GetWebContents()->GetRoutingID(); } -// static -void Window::IsCrashed(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - args.GetReturnValue().Set(self->window_->GetWebContents()->IsCrashed()); +int Window::GetProcessID() { + return window_->GetWebContents()->GetRenderProcessHost()->GetID(); } -// static -void Window::GetDevTools(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - content::WebContents* web_contents = self->window_->GetDevToolsWebContents(); - v8::Local devtools = v8::Object::New(); - devtools->Set(ToV8Value("processId"), - ToV8Value(web_contents->GetRenderProcessHost()->GetID())); - devtools->Set(ToV8Value("routingId"), - ToV8Value(web_contents->GetRoutingID())); - args.GetReturnValue().Set(devtools); +bool Window::IsCrashed() { + return window_->GetWebContents()->IsCrashed(); } -// static -void Window::ExecuteJavaScriptInDevTools( - const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - std::string code; - if (!FromV8Arguments(args, &code)) - return node::ThrowTypeError("Bad argument"); - - self->window_->ExecuteJavaScriptInDevTools(code); +mate::Dictionary Window::GetDevTools(v8::Isolate* isolate) { + content::WebContents* web_contents = window_->GetDevToolsWebContents(); + mate::Dictionary dict(isolate); + dict.Set("processId", web_contents->GetRenderProcessHost()->GetID()); + dict.Set("routingId", web_contents->GetRoutingID()); + return dict; } -// static -void Window::LoadURL(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - GURL url; - if (!FromV8Arguments(args, &url)) - return node::ThrowTypeError("Bad argument"); +void Window::ExecuteJavaScriptInDevTools(const std::string& code) { + window_->ExecuteJavaScriptInDevTools(code); +} - NavigationController& controller = - self->window_->GetWebContents()->GetController(); +void Window::LoadURL(const GURL& url) { + NavigationController& controller = window_->GetWebContents()->GetController(); content::NavigationController::LoadURLParams params(url); params.transition_type = content::PAGE_TRANSITION_TYPED; @@ -545,193 +374,131 @@ void Window::LoadURL(const v8::FunctionCallbackInfo& args) { controller.LoadURLWithParams(params); } -// static -void Window::GetURL(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - GURL url; - if (controller.GetActiveEntry()) - url = controller.GetActiveEntry()->GetVirtualURL(); - - args.GetReturnValue().Set(ToV8Value(url)); +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(); +} + +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) { + mate::ObjectTemplateBuilder(isolate, prototype) + .SetMethod("destroy", &Window::Destroy) + .SetMethod("close", &Window::Close) + .SetMethod("focus", &Window::Focus) + .SetMethod("isFocused", &Window::IsFocused) + .SetMethod("show", &Window::Show) + .SetMethod("hide", &Window::Hide) + .SetMethod("isVisible", &Window::IsVisible) + .SetMethod("maximize", &Window::Maximize) + .SetMethod("unmaximize", &Window::Unmaximize) + .SetMethod("minimize", &Window::Minimize) + .SetMethod("restore", &Window::Restore) + .SetMethod("setFullScreen", &Window::SetFullscreen) + .SetMethod("isFullScreen", &Window::IsFullscreen) + .SetMethod("getSize", &Window::GetSize) + .SetMethod("setSize", &Window::SetSize) + .SetMethod("setMinimumSize", &Window::SetMinimumSize) + .SetMethod("getMinimumSize", &Window::GetMinimumSize) + .SetMethod("setMaximumSize", &Window::SetMaximumSize) + .SetMethod("getMaximumSize", &Window::GetMaximumSize) + .SetMethod("setResizable", &Window::SetResizable) + .SetMethod("isResizable", &Window::IsResizable) + .SetMethod("setAlwaysOnTop", &Window::SetAlwaysOnTop) + .SetMethod("isAlwaysOnTop", &Window::IsAlwaysOnTop) + .SetMethod("center", &Window::Center) + .SetMethod("setPosition", &Window::SetPosition) + .SetMethod("getPosition", &Window::GetPosition) + .SetMethod("setTitle", &Window::SetTitle) + .SetMethod("getTitle", &Window::GetTitle) + .SetMethod("flashFrame", &Window::FlashFrame) + .SetMethod("setKiosk", &Window::SetKiosk) + .SetMethod("isKiosk", &Window::IsKiosk) + .SetMethod("openDevTools", &Window::OpenDevTools) + .SetMethod("closeDevTools", &Window::CloseDevTools) + .SetMethod("isDevToolsOpened", &Window::IsDevToolsOpened) + .SetMethod("inspectElement", &Window::InspectElement) + .SetMethod("debugDevTools", &Window::DebugDevTools) + .SetMethod("focusOnWebView", &Window::FocusOnWebView) + .SetMethod("blurWebView", &Window::BlurWebView) + .SetMethod("isWebViewFocused", &Window::IsWebViewFocused) + .SetMethod("capturePage", &Window::CapturePage) + .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) + .SetMethod("goBack", &Window::GoBack) + .SetMethod("goForward", &Window::GoForward) + .SetMethod("goToIndex", &Window::GoToIndex) + .SetMethod("goToOffset", &Window::GoToOffset) + .SetMethod("reload", &Window::Reload) + .SetMethod("reloadIgnoringCache", &Window::ReloadIgnoringCache); } -// static -void Window::CanGoBack(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - - args.GetReturnValue().Set(controller.CanGoBack()); -} - -// static -void Window::CanGoForward(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - - args.GetReturnValue().Set(controller.CanGoForward()); -} - -// static -void Window::CanGoToOffset(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - int offset; - if (!FromV8Arguments(args, &offset)) - return node::ThrowTypeError("Bad argument"); - - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - - args.GetReturnValue().Set(controller.CanGoToOffset(offset)); -} - -// static -void Window::GoBack(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - controller.GoBack(); -} - -// static -void Window::GoForward(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - controller.GoForward(); -} - -// static -void Window::GoToIndex(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - int index; - if (!FromV8Arguments(args, &index)) - return node::ThrowTypeError("Bad argument"); - - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - controller.GoToIndex(index); -} - -// static -void Window::GoToOffset(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - - int offset; - if (!FromV8Arguments(args, &offset)) - return node::ThrowTypeError("Bad argument"); - - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - controller.GoToOffset(offset); -} - -// static -void Window::Reload(const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; +} // namespace api - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - controller.Reload(args[0]->IsBoolean() ? args[0]->BooleanValue(): false); -} +} // namespace atom -// static -void Window::ReloadIgnoringCache( - const v8::FunctionCallbackInfo& args) { - UNWRAP_WINDOW_AND_CHECK; - NavigationController& controller = - self->window_->GetWebContents()->GetController(); - controller.ReloadIgnoringCache( - args[0]->IsBoolean() ? args[0]->BooleanValue(): false); -} +namespace { -// static -void Window::Initialize(v8::Handle target) { - v8::Local t = v8::FunctionTemplate::New(Window::New); - t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(v8::String::NewSymbol("BrowserWindow")); - - 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, "isFocused", IsFocused); - NODE_SET_PROTOTYPE_METHOD(t, "show", Show); - NODE_SET_PROTOTYPE_METHOD(t, "hide", Hide); - NODE_SET_PROTOTYPE_METHOD(t, "isVisible", IsVisible); - 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, "getMinimumSize", GetMinimumSize); - NODE_SET_PROTOTYPE_METHOD(t, "setMaximumSize", SetMaximumSize); - NODE_SET_PROTOTYPE_METHOD(t, "getMaximumSize", GetMaximumSize); - NODE_SET_PROTOTYPE_METHOD(t, "setResizable", SetResizable); - NODE_SET_PROTOTYPE_METHOD(t, "isResizable", IsResizable); - NODE_SET_PROTOTYPE_METHOD(t, "setAlwaysOnTop", SetAlwaysOnTop); - NODE_SET_PROTOTYPE_METHOD(t, "isAlwaysOnTop", IsAlwaysOnTop); - NODE_SET_PROTOTYPE_METHOD(t, "center", Center); - 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, "openDevTools", OpenDevTools); - NODE_SET_PROTOTYPE_METHOD(t, "closeDevTools", CloseDevTools); - NODE_SET_PROTOTYPE_METHOD(t, "isDevToolsOpened", IsDevToolsOpened); - NODE_SET_PROTOTYPE_METHOD(t, "inspectElement", InspectElement); - NODE_SET_PROTOTYPE_METHOD(t, "debugDevTools", DebugDevTools); - NODE_SET_PROTOTYPE_METHOD(t, "focusOnWebView", FocusOnWebView); - NODE_SET_PROTOTYPE_METHOD(t, "blurWebView", BlurWebView); - NODE_SET_PROTOTYPE_METHOD(t, "isWebViewFocused", IsWebViewFocused); - NODE_SET_PROTOTYPE_METHOD(t, "capturePage", CapturePage); - - 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, "getRoutingId", GetRoutingID); - NODE_SET_PROTOTYPE_METHOD(t, "getProcessId", GetProcessID); - NODE_SET_PROTOTYPE_METHOD(t, "isCrashed", IsCrashed); - - NODE_SET_PROTOTYPE_METHOD(t, "getDevTools", GetDevTools); - NODE_SET_PROTOTYPE_METHOD( - t, "executeJavaScriptInDevTools", ExecuteJavaScriptInDevTools); - - NODE_SET_PROTOTYPE_METHOD(t, "loadUrl", LoadURL); - NODE_SET_PROTOTYPE_METHOD(t, "getUrl", GetURL); - 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("BrowserWindow"), t->GetFunction()); +void Initialize(v8::Handle exports) { + using atom::api::Window; + v8::Local constructor = mate::CreateConstructor( + node_isolate, "BrowserWindow", base::Bind(&Window::New)); + mate::Dictionary dict(v8::Isolate::GetCurrent(), exports); + dict.Set("BrowserWindow", static_cast>(constructor)); } -} // namespace api - -} // namespace atom +} // namespace -NODE_MODULE(atom_browser_window, atom::api::Window::Initialize) +NODE_MODULE(atom_browser_window, Initialize) diff --git a/atom/browser/api/atom_api_window.h b/atom/browser/api/atom_api_window.h index f12bce1d..1f7a697b 100644 --- a/atom/browser/api/atom_api_window.h +++ b/atom/browser/api/atom_api_window.h @@ -10,33 +10,41 @@ #include "base/memory/scoped_ptr.h" #include "atom/browser/native_window_observer.h" -#include "atom/common/api/atom_api_event_emitter.h" -#include "atom/common/v8/scoped_persistent.h" +#include "atom/browser/api/event_emitter.h" + +class GURL; namespace base { class DictionaryValue; } +namespace mate { +class Arguments; +class Dictionary; +} + namespace atom { class NativeWindow; namespace api { -class Window : public EventEmitter, +class Window : public mate::EventEmitter, public NativeWindowObserver { public: - virtual ~Window(); + static mate::Wrappable* New(mate::Arguments* args, + const base::DictionaryValue& options); - static void Initialize(v8::Handle target); + static void BuildPrototype(v8::Isolate* isolate, + v8::Handle prototype); - NativeWindow* window() { return window_.get(); } + NativeWindow* window() const { return window_.get(); } protected: - explicit Window(v8::Handle wrapper, - base::DictionaryValue* options); + explicit Window(base::DictionaryValue* options); + virtual ~Window(); - // Implementations of NativeWindowObserver. + // Implementations of NativeWindowObserver: virtual void OnPageTitleUpdated(bool* prevent_default, const std::string& title) OVERRIDE; virtual void OnLoadingStateChanged(bool is_loading) OVERRIDE; @@ -49,82 +57,73 @@ class Window : public EventEmitter, virtual void OnRendererCrashed() OVERRIDE; private: - static void New(const v8::FunctionCallbackInfo& args); - static void Destroy(const v8::FunctionCallbackInfo& args); - // APIs for NativeWindow. - static void Close(const v8::FunctionCallbackInfo& args); - static void Focus(const v8::FunctionCallbackInfo& args); - static void IsFocused(const v8::FunctionCallbackInfo& args); - static void Show(const v8::FunctionCallbackInfo& args); - static void Hide(const v8::FunctionCallbackInfo& args); - static void IsVisible(const v8::FunctionCallbackInfo& args); - static void Maximize(const v8::FunctionCallbackInfo& args); - static void Unmaximize(const v8::FunctionCallbackInfo& args); - static void Minimize(const v8::FunctionCallbackInfo& args); - static void Restore(const v8::FunctionCallbackInfo& args); - static void SetFullscreen(const v8::FunctionCallbackInfo& args); - static void IsFullscreen(const v8::FunctionCallbackInfo& args); - static void SetSize(const v8::FunctionCallbackInfo& args); - static void GetSize(const v8::FunctionCallbackInfo& args); - static void SetMinimumSize(const v8::FunctionCallbackInfo& args); - static void GetMinimumSize(const v8::FunctionCallbackInfo& args); - static void SetMaximumSize(const v8::FunctionCallbackInfo& args); - static void GetMaximumSize(const v8::FunctionCallbackInfo& args); - static void SetResizable(const v8::FunctionCallbackInfo& args); - static void IsResizable(const v8::FunctionCallbackInfo& args); - static void SetAlwaysOnTop(const v8::FunctionCallbackInfo& args); - static void IsAlwaysOnTop(const v8::FunctionCallbackInfo& args); - static void Center(const v8::FunctionCallbackInfo& args); - static void SetPosition(const v8::FunctionCallbackInfo& args); - static void GetPosition(const v8::FunctionCallbackInfo& args); - static void SetTitle(const v8::FunctionCallbackInfo& args); - static void GetTitle(const v8::FunctionCallbackInfo& args); - static void FlashFrame(const v8::FunctionCallbackInfo& args); - static void SetKiosk(const v8::FunctionCallbackInfo& args); - static void IsKiosk(const v8::FunctionCallbackInfo& args); - static void OpenDevTools(const v8::FunctionCallbackInfo& args); - static void CloseDevTools(const v8::FunctionCallbackInfo& args); - static void IsDevToolsOpened(const v8::FunctionCallbackInfo& args); - static void InspectElement(const v8::FunctionCallbackInfo& args); - static void DebugDevTools(const v8::FunctionCallbackInfo& args); - static void FocusOnWebView(const v8::FunctionCallbackInfo& args); - static void BlurWebView(const v8::FunctionCallbackInfo& args); - static void IsWebViewFocused(const v8::FunctionCallbackInfo& args); - static void CapturePage(const v8::FunctionCallbackInfo& args); + void Destroy(); + void Close(); + void Focus(); + bool IsFocused(); + void Show(); + void Hide(); + bool IsVisible(); + void Maximize(); + void Unmaximize(); + void Minimize(); + void Restore(); + void SetFullscreen(bool fullscreen); + bool IsFullscreen(); + void SetSize(int width, int height); + std::vector GetSize(); + void SetMinimumSize(int width, int height); + std::vector GetMinimumSize(); + void SetMaximumSize(int width, int height); + std::vector GetMaximumSize(); + void SetResizable(bool resizable); + bool IsResizable(); + void SetAlwaysOnTop(bool top); + bool IsAlwaysOnTop(); + void Center(); + void SetPosition(int x, int y); + std::vector GetPosition(); + void SetTitle(const std::string& title); + std::string GetTitle(); + void FlashFrame(bool flash); + void SetKiosk(bool kiosk); + bool IsKiosk(); + void OpenDevTools(); + void CloseDevTools(); + bool IsDevToolsOpened(); + void InspectElement(int x, int y); + void DebugDevTools(); + void FocusOnWebView(); + void BlurWebView(); + bool IsWebViewFocused(); + void CapturePage(mate::Arguments* args); // APIs for WebContents. - static void GetPageTitle(const v8::FunctionCallbackInfo& args); - static void IsLoading(const v8::FunctionCallbackInfo& args); - static void IsWaitingForResponse( - const v8::FunctionCallbackInfo& args); - static void Stop(const v8::FunctionCallbackInfo& args); - static void GetRoutingID(const v8::FunctionCallbackInfo& args); - static void GetProcessID(const v8::FunctionCallbackInfo& args); - static void IsCrashed(const v8::FunctionCallbackInfo& args); + string16 GetPageTitle(); + bool IsLoading(); + bool IsWaitingForResponse(); + void Stop(); + int GetRoutingID(); + int GetProcessID(); + bool IsCrashed(); // APIs for devtools. - static void GetDevTools(const v8::FunctionCallbackInfo& args); - static void ExecuteJavaScriptInDevTools( - const v8::FunctionCallbackInfo& args); + mate::Dictionary GetDevTools(v8::Isolate* isolate); + void ExecuteJavaScriptInDevTools(const std::string& code); // APIs for NavigationController. - static void LoadURL(const v8::FunctionCallbackInfo& args); - static void GetURL(const v8::FunctionCallbackInfo& args); - static void CanGoBack(const v8::FunctionCallbackInfo& args); - static void CanGoForward(const v8::FunctionCallbackInfo& args); - static void CanGoToOffset(const v8::FunctionCallbackInfo& args); - static void GoBack(const v8::FunctionCallbackInfo& args); - static void GoForward(const v8::FunctionCallbackInfo& args); - static void GoToIndex(const v8::FunctionCallbackInfo& args); - static void GoToOffset(const v8::FunctionCallbackInfo& args); - static void Reload(const v8::FunctionCallbackInfo& args); - static void ReloadIgnoringCache( - const v8::FunctionCallbackInfo& args); - - // Called when capturePage is done. - void OnCapturePageDone(const RefCountedV8Function& callback, - const std::vector& data); + void LoadURL(const GURL& url); + GURL GetURL(); + 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_; diff --git a/atom/browser/native_window.cc b/atom/browser/native_window.cc index 088f1427..fa39ac56 100644 --- a/atom/browser/native_window.cc +++ b/atom/browser/native_window.cc @@ -297,6 +297,10 @@ void NativeWindow::CapturePage(const gfx::Rect& rect, callback)); } +void NativeWindow::DestroyWebContents() { + inspectable_web_contents_.reset(); +} + void NativeWindow::CloseWebContents() { bool prevent_default = false; FOR_EACH_OBSERVER(NativeWindowObserver, @@ -365,10 +369,6 @@ void NativeWindow::NotifyWindowBlur() { FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowBlur()); } -void NativeWindow::DestroyWebContents() { - inspectable_web_contents_.reset(); -} - // In atom-shell all reloads and navigations started by renderer process would // be redirected to this method, so we can have precise control of how we // would open the url (in our case, is to restart the renderer process). See diff --git a/atom/browser/native_window.h b/atom/browser/native_window.h index 47ce314f..405fe527 100644 --- a/atom/browser/native_window.h +++ b/atom/browser/native_window.h @@ -158,6 +158,9 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, // Should be called by platform code when user want to close the window. virtual void CloseWebContents(); + // Destroy the WebContents immediately. + virtual void DestroyWebContents(); + base::WeakPtr GetWeakPtr() { return weak_factory_.GetWeakPtr(); } @@ -196,9 +199,6 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate, void NotifyWindowClosed(); void NotifyWindowBlur(); - // Destroy the inspectable_web_contents. - void DestroyWebContents(); - // Called when the window needs to update its draggable region. virtual void UpdateDraggableRegions( const std::vector& regions) = 0; diff --git a/atom/common/api/atom_api_event_emitter.cc b/atom/common/api/atom_api_event_emitter.cc deleted file mode 100644 index a358fedc..00000000 --- a/atom/common/api/atom_api_event_emitter.cc +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright (c) 2013 GitHub, Inc. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#include "atom/common/api/atom_api_event_emitter.h" - -#include - -#include "atom/browser/api/atom_api_event.h" -#include "atom/common/browser_v8_locker.h" -#include "atom/common/v8/native_type_conversions.h" -#include "base/logging.h" - -#include "atom/common/node_includes.h" - -namespace atom { - -namespace api { - -EventEmitter::EventEmitter(v8::Handle wrapper) { - Wrap(wrapper); -} - -EventEmitter::~EventEmitter() { - // Clear the aligned pointer, it should have been done by ObjectWrap but - // somehow node v0.11.x changed this behaviour. - BrowserV8Locker locker(node_isolate); - v8::HandleScope handle_scope(node_isolate); - handle()->SetAlignedPointerInInternalField(0, NULL); -} - -bool EventEmitter::Emit(const std::string& name) { - base::ListValue args; - return Emit(name, &args); -} - -bool EventEmitter::Emit(const std::string& name, base::ListValue* args) { - BrowserV8Locker locker(node_isolate); - v8::HandleScope handle_scope(node_isolate); - - v8::Handle context = v8::Context::GetCurrent(); - scoped_ptr converter(new V8ValueConverter); - - v8::Handle v8_event = Event::CreateV8Object(); - Event* event = Event::Unwrap(v8_event); - - // Generate arguments for calling handle.emit. - std::vector> v8_args; - v8_args.reserve(args->GetSize() + 2); - v8_args.push_back(ToV8Value(name)); - v8_args.push_back(v8_event); - for (size_t i = 0; i < args->GetSize(); i++) { - base::Value* value = NULL; - if (args->Get(i, &value)) { - DCHECK(value); - v8_args.push_back(converter->ToV8Value(value, context)); - } else { - NOTREACHED() << "Wrong offset " << i << " for " << *args; - } - } - - node::MakeCallback(handle(), "emit", v8_args.size(), &v8_args[0]); - - bool prevent_default = event->prevent_default(); - - // Don't wait for V8 GC, delete it immediately. - delete event; - - return prevent_default; -} - -} // namespace api - -} // namespace atom diff --git a/atom/common/api/atom_api_event_emitter.h b/atom/common/api/atom_api_event_emitter.h deleted file mode 100644 index 81689d23..00000000 --- a/atom/common/api/atom_api_event_emitter.h +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2013 GitHub, Inc. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_ -#define ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_ - -#include - -#include "base/basictypes.h" -#include "vendor/node/src/node_object_wrap.h" - -namespace base { -class ListValue; -} - -namespace atom { - -namespace api { - -// Class interiting EventEmitter should assume it's a javascript object which -// interits require('events').EventEmitter, this class provides many helper -// methods to do event processing in C++. -class EventEmitter : public node::ObjectWrap { - public: - virtual ~EventEmitter(); - - // Emit an event and returns whether the handler has called preventDefault(). - bool Emit(const std::string& name); - bool Emit(const std::string& name, base::ListValue* args); - - protected: - explicit EventEmitter(v8::Handle wrapper); - - private: - DISALLOW_COPY_AND_ASSIGN(EventEmitter); -}; - -} // namespace api - -} // namespace atom - -#endif // ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_ diff --git a/atom/common/api/atom_api_shell.cc b/atom/common/api/atom_api_shell.cc index 9109b9c3..74571348 100644 --- a/atom/common/api/atom_api_shell.cc +++ b/atom/common/api/atom_api_shell.cc @@ -6,30 +6,11 @@ #include "atom/common/platform_util.h" #include "atom/common/native_mate_converters/file_path_converter.h" +#include "atom/common/native_mate_converters/gurl_converter.h" #include "native_mate/dictionary.h" -#include "url/gurl.h" #include "atom/common/node_includes.h" -namespace mate { - -template<> -struct Converter { - static bool FromV8(v8::Isolate* isolate, - v8::Handle val, - GURL* out) { - std::string url; - if (Converter::FromV8(isolate, val, &url)) { - *out = GURL(url); - return true; - } else { - return false; - } - } -}; - -} // namespace mate - namespace { void Initialize(v8::Handle exports) { diff --git a/atom/common/api/object_life_monitor.h b/atom/common/api/object_life_monitor.h index 1fc28529..495c225d 100644 --- a/atom/common/api/object_life_monitor.h +++ b/atom/common/api/object_life_monitor.h @@ -1,13 +1,12 @@ // Copyright (c) 2013 GitHub, Inc. All rights reserved. -// Copyright (c) 2012 Intel Corp. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_ #define ATOM_COMMON_API_OBJECT_LIFE_MONITOR_H_ -#include "atom/common/v8/scoped_persistent.h" #include "base/basictypes.h" +#include "native_mate/scoped_persistent.h" namespace atom { @@ -23,7 +22,7 @@ class ObjectLifeMonitor { v8::Persistent* value, ObjectLifeMonitor* self); - ScopedPersistent handle_; + mate::ScopedPersistent handle_; DISALLOW_COPY_AND_ASSIGN(ObjectLifeMonitor); }; diff --git a/atom/common/native_mate_converters/gurl_converter.h b/atom/common/native_mate_converters/gurl_converter.h new file mode 100644 index 00000000..4f6cf44e --- /dev/null +++ b/atom/common/native_mate_converters/gurl_converter.h @@ -0,0 +1,34 @@ +// Copyright (c) 2014 GitHub, Inc. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_GURL_CONVERTER_H_ +#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_GURL_CONVERTER_H_ + +#include "native_mate/converter.h" +#include "url/gurl.h" + +namespace mate { + +template<> +struct Converter { + static v8::Handle ToV8(v8::Isolate* isolate, + const GURL& val) { + return Converter::ToV8(isolate, val.spec()); + } + static bool FromV8(v8::Isolate* isolate, + v8::Handle val, + GURL* out) { + std::string url; + if (Converter::FromV8(isolate, val, &url)) { + *out = GURL(url); + return true; + } else { + return false; + } + } +}; + +} // namespace mate + +#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_GURL_CONVERTER_H_ diff --git a/atom/common/native_mate_converters/value_converter.cc b/atom/common/native_mate_converters/value_converter.cc index 4833630a..aa40ea63 100644 --- a/atom/common/native_mate_converters/value_converter.cc +++ b/atom/common/native_mate_converters/value_converter.cc @@ -9,6 +9,20 @@ namespace mate { +bool Converter::FromV8(v8::Isolate* isolate, + v8::Handle val, + base::DictionaryValue* out) { + scoped_ptr converter(new atom::V8ValueConverter); + scoped_ptr value(converter->FromV8Value( + val, v8::Context::GetCurrent())); + if (value->IsType(base::Value::TYPE_DICTIONARY)) { + out->Swap(static_cast(value.get())); + return true; + } else { + return false; + } +} + bool Converter::FromV8(v8::Isolate* isolate, v8::Handle val, base::ListValue* out) { diff --git a/atom/common/native_mate_converters/value_converter.h b/atom/common/native_mate_converters/value_converter.h index 55a2d767..3735ae8a 100644 --- a/atom/common/native_mate_converters/value_converter.h +++ b/atom/common/native_mate_converters/value_converter.h @@ -8,11 +8,19 @@ #include "native_mate/converter.h" namespace base { +class DictionaryValue; class ListValue; } namespace mate { +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Handle val, + base::DictionaryValue* out); +}; + template<> struct Converter { static bool FromV8(v8::Isolate* isolate, diff --git a/atom/common/node_includes.h b/atom/common/node_includes.h index 8dc39570..d025c9ff 100644 --- a/atom/common/node_includes.h +++ b/atom/common/node_includes.h @@ -14,6 +14,7 @@ #include "vendor/node/src/env.h" #include "vendor/node/src/env-inl.h" #include "vendor/node/src/node.h" +#include "vendor/node/src/node_buffer.h" #include "vendor/node/src/node_internals.h" using node::node_isolate; diff --git a/atom/common/swap_or_assign.h b/atom/common/swap_or_assign.h deleted file mode 100644 index a3749ca8..00000000 --- a/atom/common/swap_or_assign.h +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (c) 2013 GitHub, Inc. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef ATOM_COMMON_SWAP_OR_ASSIGN_H_ -#define ATOM_COMMON_SWAP_OR_ASSIGN_H_ - -#include "base/compiler_specific.h" - -namespace internal { - -#if defined(OS_WIN) -template inline -void SwapOrAssign(T& v1, const T& v2) { - __if_exists(T::swap) { - v1.swap(const_cast(v2)); - } - - __if_not_exists(T::swap) { - v1 = v2; - } -} - -template inline -void SwapOrAssign(T*& v1, T* v2) { - v1 = v2; -} - -inline -void SwapOrAssign(int& v1, int v2) { - v1 = v2; -} - -inline -void SwapOrAssign(bool& v1, bool v2) { - v1 = v2; -} -#else // defined(OS_WIN) -// Helper to detect whether value has specified method. -template -class HasSwapMethod { - typedef char one; - typedef long two; - template static one test(char[sizeof(&C::swap)]) ; - template static two test(...); - public: - enum { value = sizeof(test(0)) == sizeof(char) }; -}; - -template -struct enable_if {}; - -template -struct enable_if { typedef T type; }; - -template inline -typename enable_if::value>::type SwapOrAssign( - T& v1, const T& v2) { - v1.swap(const_cast(v2)); -} - -template inline -typename enable_if::value>::type SwapOrAssign( - T& v1, const T& v2) { - v1 = v2; -} -#endif // !defined(OS_WIN) - -} // namespace internal - -#endif // ATOM_COMMON_SWAP_OR_ASSIGN_H_ diff --git a/atom/common/v8/native_type_conversions.h b/atom/common/v8/native_type_conversions.h deleted file mode 100644 index e7c4c73a..00000000 --- a/atom/common/v8/native_type_conversions.h +++ /dev/null @@ -1,344 +0,0 @@ -// Copyright (c) 2013 GitHub, Inc. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef ATOM_COMMON_V8_NATIVE_TYPE_CONVERSIONS_H_ -#define ATOM_COMMON_V8_NATIVE_TYPE_CONVERSIONS_H_ - -#include -#include -#include - -#include "atom/browser/api/atom_api_window.h" -#include "atom/common/swap_or_assign.h" -#include "atom/common/v8/scoped_persistent.h" -#include "atom/common/native_mate_converters/v8_value_converter.h" -#include "base/files/file_path.h" -#include "base/strings/string16.h" -#include "base/template_util.h" -#include "base/values.h" -#include "ui/gfx/point.h" -#include "ui/gfx/rect.h" -#include "ui/gfx/size.h" -#include "url/gurl.h" - -// Convert V8 value to arbitrary supported types. -struct FromV8Value { - explicit FromV8Value(v8::Handle value) : value_(value) {} - - operator int() { - return value_->IntegerValue(); - } - - operator bool() { - return value_->BooleanValue(); - } - - operator std::string() { - return *v8::String::Utf8Value(value_); - } - - operator string16() { - v8::String::Value s(value_); - return string16(reinterpret_cast(*s), s.length()); - } - - operator GURL() { - std::string str = FromV8Value(value_); - return GURL(str); - } - - operator base::FilePath() { - return base::FilePath::FromUTF8Unsafe(FromV8Value(value_)); - } - - operator gfx::Rect() { - v8::Handle rect = value_->ToObject(); - v8::Handle x = rect->Get(v8::String::New("x")); - v8::Handle y = rect->Get(v8::String::New("y")); - v8::Handle width = rect->Get(v8::String::New("width")); - v8::Handle height = rect->Get(v8::String::New("height")); - if (!x->IsNumber() || !y->IsNumber() || - !width->IsNumber() || !height->IsNumber()) - return gfx::Rect(); - else - return gfx::Rect(x->IntegerValue(), y->IntegerValue(), - width->IntegerValue(), height->IntegerValue()); - } - - operator scoped_ptr() { - scoped_ptr converter(new atom::V8ValueConverter); - return scoped_ptr( - converter->FromV8Value(value_, v8::Context::GetCurrent())); - } - - template - operator std::vector() { - std::vector array; - v8::Handle v8_array = v8::Handle::Cast(value_); - for (uint32_t i = 0; i < v8_array->Length(); ++i) - array.push_back(FromV8Value(v8_array->Get(i))); - - return array; - } - - template - operator std::map() { - std::map dict; - v8::Handle v8_dict = value_->ToObject(); - v8::Handle v8_keys = v8_dict->GetOwnPropertyNames(); - for (uint32_t i = 0; i < v8_keys->Length(); ++i) { - v8::Handle v8_key = v8_keys->Get(i); - K key = FromV8Value(v8_key); - dict[key] = V(FromV8Value(v8_dict->Get(v8_key))); - } - - return dict; - } - - operator atom::NativeWindow*() { - using atom::api::Window; - if (value_->IsObject()) { - Window* window = Window::Unwrap(value_->ToObject()); - if (window && window->window()) - return window->window(); - } - return NULL; - } - - operator atom::RefCountedV8Function() { - v8::Handle func = v8::Handle::Cast(value_); - return atom::RefCountedV8Function( - new atom::RefCountedPersistent(func)); - } - - v8::Handle value_; -}; - -// Convert arbitrary supported native type to V8 value. -inline v8::Handle ToV8Value(int i) { - return v8::Integer::New(i); -} - -inline v8::Handle ToV8Value(bool b) { - return v8::Boolean::New(b); -} - -inline v8::Handle ToV8Value(float f) { - return v8::Number::New(f); -} - -inline v8::Handle ToV8Value(double f) { - return v8::Number::New(f); -} - -inline v8::Handle ToV8Value(const char* s) { - return v8::String::New(s); -} - -inline v8::Handle ToV8Value(const std::string& s) { - return v8::String::New(s.data(), s.size()); -} - -inline v8::Handle ToV8Value(const string16& s) { - return v8::String::New(reinterpret_cast(s.data()), s.size()); -} - -inline v8::Handle ToV8Value(const GURL& url) { - return ToV8Value(url.spec()); -} - -inline v8::Handle ToV8Value(const base::FilePath& path) { - std::string path_string(path.AsUTF8Unsafe()); - return v8::String::New(path_string.data(), path_string.size()); -} - -inline v8::Handle ToV8Value(void* whatever) { - return v8::Undefined(); -} - -template inline -v8::Handle ToV8Value(const std::vector& arr) { - v8::Handle result = v8::Array::New(arr.size()); - for (size_t i = 0; i < arr.size(); ++i) - result->Set(i, ToV8Value(arr[i])); - return result; -} - -inline v8::Handle ToV8Value(const gfx::Point& point) { - v8::Handle obj = v8::Object::New(); - obj->Set(ToV8Value("x"), ToV8Value(point.x())); - obj->Set(ToV8Value("y"), ToV8Value(point.y())); - return obj; -} - -inline v8::Handle ToV8Value(const gfx::Rect& rect) { - v8::Handle obj = v8::Object::New(); - obj->Set(ToV8Value("x"), ToV8Value(rect.x())); - obj->Set(ToV8Value("y"), ToV8Value(rect.y())); - obj->Set(ToV8Value("width"), ToV8Value(rect.width())); - obj->Set(ToV8Value("height"), ToV8Value(rect.height())); - return obj; -} - -inline v8::Handle ToV8Value(const gfx::Size& size) { - v8::Handle obj = v8::Object::New(); - obj->Set(ToV8Value("width"), ToV8Value(size.width())); - obj->Set(ToV8Value("height"), ToV8Value(size.height())); - return obj; -} - -// Check if a V8 Value is of specified type. -template inline -bool V8ValueCanBeConvertedTo(v8::Handle value) { - return false; -} - -template<> inline -bool V8ValueCanBeConvertedTo(v8::Handle value) { - return value->IsNumber(); -} - -template<> inline -bool V8ValueCanBeConvertedTo(v8::Handle value) { - return value->IsBoolean(); -} - -template<> inline -bool V8ValueCanBeConvertedTo(v8::Handle value) { - return value->IsString(); -} - -template<> inline -bool V8ValueCanBeConvertedTo(v8::Handle value) { - return V8ValueCanBeConvertedTo(value); -} - -template<> inline -bool V8ValueCanBeConvertedTo(v8::Handle value) { - return V8ValueCanBeConvertedTo(value); -} - -template<> inline -bool V8ValueCanBeConvertedTo(v8::Handle value) { - return V8ValueCanBeConvertedTo(value); -} - -template<> inline -bool V8ValueCanBeConvertedTo(v8::Handle value) { - return value->IsObject(); -} - -template<> inline -bool V8ValueCanBeConvertedTo>( - v8::Handle value) { - return value->IsObject(); -} - -template<> inline -bool V8ValueCanBeConvertedTo>( - v8::Handle value) { - return value->IsArray(); -} - -template<> inline -bool V8ValueCanBeConvertedTo>( - v8::Handle value) { - return value->IsObject(); -} - -template<> inline -bool V8ValueCanBeConvertedTo(v8::Handle value) { - using atom::api::Window; - if (value->IsObject()) { - Window* window = Window::Unwrap(value->ToObject()); - if (window && window->window()) - return true; - } - return false; -} - -template<> inline -bool V8ValueCanBeConvertedTo( - v8::Handle value) { - return value->IsFunction(); -} - -// Check and convert V8's Arguments to native types. -template inline -bool FromV8Arguments(const v8::FunctionCallbackInfo& args, - T1* value, - int index = 0) { - if (!V8ValueCanBeConvertedTo(args[index])) - return false; - internal::SwapOrAssign(*value, - FromV8Value(args[index]).operator T1()); - return true; -} - -template inline -bool FromV8Arguments(const v8::FunctionCallbackInfo& args, - T1* a1, - T2* a2) { - return FromV8Arguments(args, a1) && FromV8Arguments(args, a2, 1); -} - -template inline -bool FromV8Arguments(const v8::FunctionCallbackInfo& args, - T1* a1, - T2* a2, - T3* a3) { - return FromV8Arguments(args, a1, a2) && - FromV8Arguments(args, a3, 2); -} - -template inline -bool FromV8Arguments(const v8::FunctionCallbackInfo& args, - T1* a1, - T2* a2, - T3* a3, - T4* a4) { - return FromV8Arguments(args, a1, a2, a3) && - FromV8Arguments(args, a4, 3); -} - -template inline -bool FromV8Arguments(const v8::FunctionCallbackInfo& args, - T1* a1, - T2* a2, - T3* a3, - T4* a4, - T5* a5) { - return FromV8Arguments(args, a1, a2, a3, a4) && - FromV8Arguments(args, a5, 4); -} - -template inline -bool FromV8Arguments(const v8::FunctionCallbackInfo& args, - T1* a1, - T2* a2, - T3* a3, - T4* a4, - T5* a5, - T6* a6) { - return FromV8Arguments(args, a1, a2, a3, a4, a5) && - FromV8Arguments(args, a6, 5); -} - -template inline -bool FromV8Arguments(const v8::FunctionCallbackInfo& args, - T1* a1, - T2* a2, - T3* a3, - T4* a4, - T5* a5, - T6* a6, - T7* a7) { - return - FromV8Arguments(args, a1, a2, a3, a4, a5, a6) && - FromV8Arguments(args, a7, 6); -} - -#endif // ATOM_COMMON_V8_NATIVE_TYPE_CONVERSIONS_H_ diff --git a/atom/common/v8/scoped_persistent.h b/atom/common/v8/scoped_persistent.h deleted file mode 100644 index 7d2fe47a..00000000 --- a/atom/common/v8/scoped_persistent.h +++ /dev/null @@ -1,115 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef ATOM_COMMON_V8_SCOPED_PERSISTENT_H_ -#define ATOM_COMMON_V8_SCOPED_PERSISTENT_H_ - -#include "base/logging.h" -#include "base/memory/ref_counted.h" -#include "v8/include/v8.h" - -namespace atom { - -// A v8::Persistent handle to a V8 value which destroys and clears the -// underlying handle on destruction. -template -class ScopedPersistent { - public: - ScopedPersistent() { - } - - explicit ScopedPersistent(v8::Handle handle) { - reset(handle); - } - - ~ScopedPersistent() { - reset(); - } - - void reset(v8::Handle handle) { - if (!handle.IsEmpty()) - handle_.Reset(GetIsolate(handle), handle); - else - reset(); - } - - void reset() { - handle_.Reset(); - } - - bool IsEmpty() const { - return handle_.IsEmpty(); - } - - v8::Handle NewHandle() const { - if (handle_.IsEmpty()) - return v8::Local(); - return v8::Local::New(GetIsolate(handle_), handle_); - } - - v8::Handle NewHandle(v8::Isolate* isolate) const { - if (handle_.IsEmpty()) - return v8::Local(); - return v8::Local::New(isolate, handle_); - } - - template - void MakeWeak(P* parameters, - typename v8::WeakReferenceCallbacks::Revivable callback) { - handle_.MakeWeak(parameters, callback); - } - - private: - template - static v8::Isolate* GetIsolate(v8::Handle object_handle) { - // Only works for v8::Object and its subclasses. Add specialisations for - // anything else. - if (!object_handle.IsEmpty()) - return GetIsolate(object_handle->CreationContext()); - return v8::Isolate::GetCurrent(); - } - static v8::Isolate* GetIsolate(v8::Handle context_handle) { - if (!context_handle.IsEmpty()) - return context_handle->GetIsolate(); - return v8::Isolate::GetCurrent(); - } - static v8::Isolate* GetIsolate( - v8::Handle template_handle) { - return v8::Isolate::GetCurrent(); - } - template - static v8::Isolate* GetIsolate(const U& any_handle) { - return v8::Isolate::GetCurrent(); - } - - v8::Persistent handle_; - - DISALLOW_COPY_AND_ASSIGN(ScopedPersistent); -}; - -template -class RefCountedPersistent : public ScopedPersistent, - public base::RefCounted> { - public: - RefCountedPersistent() {} - - explicit RefCountedPersistent(v8::Handle handle) - : ScopedPersistent(handle) { - } - - protected: - friend class base::RefCounted>; - - ~RefCountedPersistent() {} - - private: - DISALLOW_COPY_AND_ASSIGN(RefCountedPersistent); -}; - -typedef scoped_refptr> RefCountedV8Function; -typedef scoped_refptr> RefCountedV8Object; - -} // namespace atom - -#endif // ATOM_COMMON_V8_SCOPED_PERSISTENT_H_ diff --git a/vendor/native_mate b/vendor/native_mate index 9cc90ac7..3d219b4b 160000 --- a/vendor/native_mate +++ b/vendor/native_mate @@ -1 +1 @@ -Subproject commit 9cc90ac7d52e04d909ffdceda130e12d8f79e0e8 +Subproject commit 3d219b4be6f37fe99ca2e738eb0ea9d1b152af34