Merge pull request #231 from atom/native-mate
Reimplement JS API with native-mate
This commit is contained in:
Коммит
a365e0b032
|
@ -13,3 +13,6 @@
|
|||
[submodule "vendor/breakpad"]
|
||||
path = vendor/breakpad
|
||||
url = https://github.com/atom/chromium-breakpad.git
|
||||
[submodule "vendor/native_mate"]
|
||||
path = vendor/native_mate
|
||||
url = https://github.com/zcbenz/native-mate.git
|
||||
|
|
36
atom.gyp
36
atom.gyp
|
@ -1,5 +1,8 @@
|
|||
{
|
||||
'variables': {
|
||||
'includes': [
|
||||
'vendor/native_mate/native_mate_files.gypi',
|
||||
],
|
||||
'project_name': 'atom',
|
||||
'product_name': 'Atom',
|
||||
'framework_name': 'Atom Framework',
|
||||
|
@ -46,11 +49,7 @@
|
|||
'atom/browser/api/atom_api_auto_updater.cc',
|
||||
'atom/browser/api/atom_api_auto_updater.h',
|
||||
'atom/browser/api/atom_api_browser_ipc.cc',
|
||||
'atom/browser/api/atom_api_browser_ipc.h',
|
||||
'atom/browser/api/atom_api_dialog.cc',
|
||||
'atom/browser/api/atom_api_dialog.h',
|
||||
'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',
|
||||
|
@ -67,6 +66,10 @@
|
|||
'atom/browser/api/atom_api_window.h',
|
||||
'atom/browser/api/atom_browser_bindings.cc',
|
||||
'atom/browser/api/atom_browser_bindings.h',
|
||||
'atom/browser/api/event.cc',
|
||||
'atom/browser/api/event.h',
|
||||
'atom/browser/api/event_emitter.cc',
|
||||
'atom/browser/api/event_emitter.h',
|
||||
'atom/browser/auto_updater.cc',
|
||||
'atom/browser/auto_updater.h',
|
||||
'atom/browser/auto_updater_delegate.h',
|
||||
|
@ -140,17 +143,12 @@
|
|||
'atom/common/api/api_messages.cc',
|
||||
'atom/common/api/api_messages.h',
|
||||
'atom/common/api/atom_api_clipboard.cc',
|
||||
'atom/common/api/atom_api_clipboard.h',
|
||||
'atom/common/api/atom_api_crash_reporter.cc',
|
||||
'atom/common/api/atom_api_crash_reporter.h',
|
||||
'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',
|
||||
'atom/common/api/atom_api_screen.h',
|
||||
'atom/common/api/atom_api_shell.cc',
|
||||
'atom/common/api/atom_api_shell.h',
|
||||
'atom/common/api/atom_api_v8_util.cc',
|
||||
'atom/common/api/atom_bindings.cc',
|
||||
'atom/common/api/atom_bindings.h',
|
||||
|
@ -158,6 +156,8 @@
|
|||
'atom/common/api/atom_extensions.h',
|
||||
'atom/common/api/object_life_monitor.cc',
|
||||
'atom/common/api/object_life_monitor.h',
|
||||
'atom/common/browser_v8_locker.cc',
|
||||
'atom/common/browser_v8_locker.h',
|
||||
'atom/common/crash_reporter/crash_reporter.cc',
|
||||
'atom/common/crash_reporter/crash_reporter.h',
|
||||
'atom/common/crash_reporter/crash_reporter_linux.cc',
|
||||
|
@ -175,6 +175,14 @@
|
|||
'atom/common/draggable_region.cc',
|
||||
'atom/common/draggable_region.h',
|
||||
'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',
|
||||
'atom/common/native_mate_converters/value_converter.cc',
|
||||
'atom/common/native_mate_converters/value_converter.h',
|
||||
'atom/common/node_bindings.cc',
|
||||
'atom/common/node_bindings.h',
|
||||
'atom/common/node_bindings_linux.cc',
|
||||
|
@ -183,20 +191,14 @@
|
|||
'atom/common/node_bindings_mac.h',
|
||||
'atom/common/node_bindings_win.cc',
|
||||
'atom/common/node_bindings_win.h',
|
||||
'atom/common/node_includes.h',
|
||||
'atom/common/options_switches.cc',
|
||||
'atom/common/options_switches.h',
|
||||
'atom/common/platform_util.h',
|
||||
'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/node_common.h',
|
||||
'atom/common/v8/scoped_persistent.h',
|
||||
'atom/common/v8/native_type_conversions.h',
|
||||
'atom/common/v8/v8_value_converter.cc',
|
||||
'atom/common/v8/v8_value_converter.h',
|
||||
'atom/renderer/api/atom_api_renderer_ipc.cc',
|
||||
'atom/renderer/api/atom_api_renderer_ipc.h',
|
||||
'atom/renderer/api/atom_renderer_bindings.cc',
|
||||
'atom/renderer/api/atom_renderer_bindings.h',
|
||||
'atom/renderer/atom_render_view_observer.cc',
|
||||
|
@ -215,6 +217,7 @@
|
|||
'chrome/browser/ui/gtk/gtk_window_util.h',
|
||||
'chrome/browser/ui/gtk/menu_gtk.cc',
|
||||
'chrome/browser/ui/gtk/menu_gtk.h',
|
||||
'<@(native_mate_files)',
|
||||
],
|
||||
'framework_sources': [
|
||||
'atom/app/atom_library_main.cc',
|
||||
|
@ -371,6 +374,7 @@
|
|||
'include_dirs': [
|
||||
'.',
|
||||
'vendor/brightray',
|
||||
'vendor/native_mate',
|
||||
# Include directories for uv and node.
|
||||
'vendor/node/src',
|
||||
'vendor/node/deps/http_parser',
|
||||
|
|
|
@ -9,16 +9,18 @@
|
|||
#include "base/values.h"
|
||||
#include "base/command_line.h"
|
||||
#include "atom/browser/browser.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
using atom::Browser;
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
App::App(v8::Handle<v8::Object> wrapper)
|
||||
: EventEmitter(wrapper) {
|
||||
App::App() {
|
||||
Browser::Get()->AddObserver(this);
|
||||
}
|
||||
|
||||
|
@ -37,13 +39,13 @@ void App::OnWindowAllClosed() {
|
|||
void App::OnOpenFile(bool* prevent_default, const std::string& file_path) {
|
||||
base::ListValue args;
|
||||
args.AppendString(file_path);
|
||||
*prevent_default = Emit("open-file", &args);
|
||||
*prevent_default = Emit("open-file", args);
|
||||
}
|
||||
|
||||
void App::OnOpenURL(const std::string& url) {
|
||||
base::ListValue args;
|
||||
args.AppendString(url);
|
||||
Emit("open-url", &args);
|
||||
Emit("open-url", args);
|
||||
}
|
||||
|
||||
void App::OnActivateWithNoOpenWindows() {
|
||||
|
@ -58,144 +60,80 @@ void App::OnFinishLaunching() {
|
|||
Emit("ready");
|
||||
}
|
||||
|
||||
// static
|
||||
void App::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
v8::HandleScope scope(args.GetIsolate());
|
||||
|
||||
if (!args.IsConstructCall())
|
||||
return node::ThrowError("Require constructor call");
|
||||
|
||||
new App(args.This());
|
||||
mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) {
|
||||
Browser* browser = Browser::Get();
|
||||
return mate::ObjectTemplateBuilder(isolate)
|
||||
.SetMethod("quit", base::Bind(&Browser::Quit,
|
||||
base::Unretained(browser)))
|
||||
.SetMethod("focus", base::Bind(&Browser::Focus,
|
||||
base::Unretained(browser)))
|
||||
.SetMethod("getVersion", base::Bind(&Browser::GetVersion,
|
||||
base::Unretained(browser)))
|
||||
.SetMethod("setVersion", base::Bind(&Browser::SetVersion,
|
||||
base::Unretained(browser)))
|
||||
.SetMethod("getName", base::Bind(&Browser::GetName,
|
||||
base::Unretained(browser)))
|
||||
.SetMethod("setName", base::Bind(&Browser::SetName,
|
||||
base::Unretained(browser)));
|
||||
}
|
||||
|
||||
// static
|
||||
void App::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
Browser::Get()->Quit();
|
||||
}
|
||||
|
||||
// static
|
||||
void App::Focus(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
Browser::Get()->Focus();
|
||||
}
|
||||
|
||||
// static
|
||||
void App::GetVersion(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
args.GetReturnValue().Set(ToV8Value(Browser::Get()->GetVersion()));
|
||||
}
|
||||
|
||||
// static
|
||||
void App::SetVersion(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string version;
|
||||
if (!FromV8Arguments(args, &version))
|
||||
return node::ThrowError("Bad argument");
|
||||
|
||||
Browser::Get()->SetVersion(version);
|
||||
}
|
||||
|
||||
// static
|
||||
void App::GetName(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
return args.GetReturnValue().Set(ToV8Value(Browser::Get()->GetName()));
|
||||
}
|
||||
|
||||
// static
|
||||
void App::SetName(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string name;
|
||||
if (!FromV8Arguments(args, &name))
|
||||
return node::ThrowError("Bad argument");
|
||||
|
||||
Browser::Get()->SetName(name);
|
||||
}
|
||||
|
||||
// static
|
||||
void App::AppendSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string switch_string;
|
||||
if (!FromV8Arguments(args, &switch_string))
|
||||
return node::ThrowError("Bad argument");
|
||||
|
||||
if (args.Length() == 1) {
|
||||
CommandLine::ForCurrentProcess()->AppendSwitch(switch_string);
|
||||
} else {
|
||||
std::string value = FromV8Value(args[1]);
|
||||
CommandLine::ForCurrentProcess()->AppendSwitchASCII(
|
||||
switch_string, value);
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
void App::AppendArgument(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string value;
|
||||
if (!FromV8Arguments(args, &value))
|
||||
return node::ThrowError("Bad argument");
|
||||
|
||||
CommandLine::ForCurrentProcess()->AppendArg(value);
|
||||
}
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
|
||||
// static
|
||||
void App::DockBounce(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string type;
|
||||
if (!FromV8Arguments(args, &type))
|
||||
return node::ThrowError("Bad argument");
|
||||
|
||||
int request_id = -1;
|
||||
|
||||
if (type == "critical")
|
||||
request_id = Browser::Get()->DockBounce(Browser::BOUNCE_CRITICAL);
|
||||
else if (type == "informational")
|
||||
request_id = Browser::Get()->DockBounce(Browser::BOUNCE_INFORMATIONAL);
|
||||
else
|
||||
return node::ThrowTypeError("Invalid bounce type");
|
||||
|
||||
args.GetReturnValue().Set(request_id);
|
||||
}
|
||||
|
||||
// static
|
||||
void App::DockCancelBounce(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
Browser::Get()->DockCancelBounce(FromV8Value(args[0]));
|
||||
}
|
||||
|
||||
// static
|
||||
void App::DockSetBadgeText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
Browser::Get()->DockSetBadgeText(FromV8Value(args[0]));
|
||||
}
|
||||
|
||||
// static
|
||||
void App::DockGetBadgeText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string text(Browser::Get()->DockGetBadgeText());
|
||||
args.GetReturnValue().Set(ToV8Value(text));
|
||||
}
|
||||
|
||||
#endif // defined(OS_MACOSX)
|
||||
|
||||
// static
|
||||
void App::Initialize(v8::Handle<v8::Object> target) {
|
||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
t->SetClassName(v8::String::NewSymbol("Application"));
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "quit", Quit);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "focus", Focus);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "getVersion", GetVersion);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "setVersion", SetVersion);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "getName", GetName);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "setName", SetName);
|
||||
|
||||
target->Set(v8::String::NewSymbol("Application"), t->GetFunction());
|
||||
|
||||
NODE_SET_METHOD(target, "appendSwitch", AppendSwitch);
|
||||
NODE_SET_METHOD(target, "appendArgument", AppendArgument);
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
NODE_SET_METHOD(target, "dockBounce", DockBounce);
|
||||
NODE_SET_METHOD(target, "dockCancelBounce", DockCancelBounce);
|
||||
NODE_SET_METHOD(target, "dockSetBadgeText", DockSetBadgeText);
|
||||
NODE_SET_METHOD(target, "dockGetBadgeText", DockGetBadgeText);
|
||||
#endif // defined(OS_MACOSX)
|
||||
mate::Handle<App> App::Create(v8::Isolate* isolate) {
|
||||
return CreateHandle(isolate, new App);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_browser_app, atom::api::App::Initialize)
|
||||
|
||||
namespace {
|
||||
|
||||
void AppendSwitch(const std::string& switch_string, mate::Arguments* args) {
|
||||
std::string value;
|
||||
if (args->GetNext(&value))
|
||||
CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_string, value);
|
||||
else
|
||||
CommandLine::ForCurrentProcess()->AppendSwitch(switch_string);
|
||||
}
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
int DockBounce(const std::string& type) {
|
||||
int request_id = -1;
|
||||
if (type == "critical")
|
||||
request_id = Browser::Get()->DockBounce(Browser::BOUNCE_CRITICAL);
|
||||
else if (type == "informational")
|
||||
request_id = Browser::Get()->DockBounce(Browser::BOUNCE_INFORMATIONAL);
|
||||
return request_id;
|
||||
}
|
||||
#endif
|
||||
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
Browser* browser = Browser::Get();
|
||||
CommandLine* command_line = CommandLine::ForCurrentProcess();
|
||||
|
||||
mate::Dictionary dict(isolate, exports);
|
||||
dict.Set("app", atom::api::App::Create(isolate));
|
||||
dict.SetMethod("appendSwitch", &AppendSwitch);
|
||||
dict.SetMethod("appendArgument",
|
||||
base::Bind(&CommandLine::AppendArg,
|
||||
base::Unretained(command_line)));
|
||||
#if defined(OS_MACOSX)
|
||||
dict.SetMethod("dockBounce", &DockBounce);
|
||||
dict.SetMethod("dockCancelBounce",
|
||||
base::Bind(&Browser::DockCancelBounce,
|
||||
base::Unretained(browser)));
|
||||
dict.SetMethod("dockSetBadgeText",
|
||||
base::Bind(&Browser::DockSetBadgeText,
|
||||
base::Unretained(browser)));
|
||||
dict.SetMethod("dockGetBadgeText",
|
||||
base::Bind(&Browser::DockGetBadgeText,
|
||||
base::Unretained(browser)));
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NODE_MODULE(atom_browser_app, Initialize)
|
||||
|
|
|
@ -8,22 +8,22 @@
|
|||
#include <string>
|
||||
|
||||
#include "base/compiler_specific.h"
|
||||
#include "atom/browser/api/event_emitter.h"
|
||||
#include "atom/browser/browser_observer.h"
|
||||
#include "atom/common/api/atom_api_event_emitter.h"
|
||||
#include "native_mate/handle.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class App : public EventEmitter,
|
||||
class App : public mate::EventEmitter,
|
||||
public BrowserObserver {
|
||||
public:
|
||||
virtual ~App();
|
||||
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
static mate::Handle<App> Create(v8::Isolate* isolate);
|
||||
|
||||
protected:
|
||||
explicit App(v8::Handle<v8::Object> wrapper);
|
||||
App();
|
||||
virtual ~App();
|
||||
|
||||
// BrowserObserver implementations:
|
||||
virtual void OnWillQuit(bool* prevent_default) OVERRIDE;
|
||||
|
@ -35,25 +35,11 @@ class App : public EventEmitter,
|
|||
virtual void OnWillFinishLaunching() OVERRIDE;
|
||||
virtual void OnFinishLaunching() OVERRIDE;
|
||||
|
||||
// mate::Wrappable implementations:
|
||||
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate);
|
||||
|
||||
private:
|
||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static void Quit(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Focus(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetVersion(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetVersion(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetName(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetName(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void AppendSwitch(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void AppendArgument(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
static void DockBounce(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void DockCancelBounce(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void DockSetBadgeText(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void DockGetBadgeText(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
#endif // defined(OS_MACOSX)
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(App);
|
||||
};
|
||||
|
||||
|
|
|
@ -7,16 +7,16 @@
|
|||
#include "base/time/time.h"
|
||||
#include "base/values.h"
|
||||
#include "atom/browser/auto_updater.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
AutoUpdater::AutoUpdater(v8::Handle<v8::Object> wrapper)
|
||||
: EventEmitter(wrapper) {
|
||||
AutoUpdater::AutoUpdater() {
|
||||
auto_updater::AutoUpdater::SetDelegate(this);
|
||||
}
|
||||
|
||||
|
@ -27,7 +27,7 @@ AutoUpdater::~AutoUpdater() {
|
|||
void AutoUpdater::OnError(const std::string& error) {
|
||||
base::ListValue args;
|
||||
args.AppendString(error);
|
||||
Emit("error", &args);
|
||||
Emit("error", args);
|
||||
}
|
||||
|
||||
void AutoUpdater::OnCheckingForUpdate() {
|
||||
|
@ -54,53 +54,40 @@ void AutoUpdater::OnUpdateDownloaded(const std::string& release_notes,
|
|||
args.AppendString(release_name);
|
||||
args.AppendDouble(release_date.ToJsTime());
|
||||
args.AppendString(update_url);
|
||||
Emit("update-downloaded-raw", &args);
|
||||
Emit("update-downloaded-raw", args);
|
||||
}
|
||||
|
||||
mate::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) {
|
||||
return mate::ObjectTemplateBuilder(isolate)
|
||||
.SetMethod("setFeedUrl", &auto_updater::AutoUpdater::SetFeedURL)
|
||||
.SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
|
||||
.SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall);
|
||||
}
|
||||
|
||||
void AutoUpdater::QuitAndInstall() {
|
||||
if (!quit_and_install_.is_null())
|
||||
quit_and_install_.Run();
|
||||
}
|
||||
|
||||
// static
|
||||
void AutoUpdater::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
if (!args.IsConstructCall())
|
||||
return node::ThrowError("Require constructor call");
|
||||
|
||||
new AutoUpdater(args.This());
|
||||
}
|
||||
|
||||
// static
|
||||
void AutoUpdater::SetFeedURL(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto_updater::AutoUpdater::SetFeedURL(FromV8Value(args[0]));
|
||||
}
|
||||
|
||||
// static
|
||||
void AutoUpdater::CheckForUpdates(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
auto_updater::AutoUpdater::CheckForUpdates();
|
||||
}
|
||||
|
||||
// static
|
||||
void AutoUpdater::QuitAndInstall(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
AutoUpdater* self = AutoUpdater::Unwrap<AutoUpdater>(args.This());
|
||||
|
||||
if (!self->quit_and_install_.is_null())
|
||||
self->quit_and_install_.Run();
|
||||
}
|
||||
|
||||
// static
|
||||
void AutoUpdater::Initialize(v8::Handle<v8::Object> target) {
|
||||
v8::Local<v8::FunctionTemplate> t(
|
||||
v8::FunctionTemplate::New(AutoUpdater::New));
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
t->SetClassName(v8::String::NewSymbol("AutoUpdater"));
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "setFeedUrl", SetFeedURL);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "checkForUpdates", CheckForUpdates);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "quitAndInstall", QuitAndInstall);
|
||||
|
||||
target->Set(v8::String::NewSymbol("AutoUpdater"), t->GetFunction());
|
||||
mate::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
|
||||
return CreateHandle(isolate, new AutoUpdater);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_browser_auto_updater, atom::api::AutoUpdater::Initialize)
|
||||
|
||||
namespace {
|
||||
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
mate::Dictionary dict(isolate, exports);
|
||||
dict.Set("autoUpdater", atom::api::AutoUpdater::Create(isolate));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NODE_MODULE(atom_browser_auto_updater, Initialize)
|
||||
|
|
|
@ -8,23 +8,22 @@
|
|||
#include <string>
|
||||
|
||||
#include "base/callback.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "atom/browser/api/event_emitter.h"
|
||||
#include "atom/browser/auto_updater_delegate.h"
|
||||
#include "atom/common/api/atom_api_event_emitter.h"
|
||||
#include "native_mate/handle.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class AutoUpdater : public EventEmitter,
|
||||
class AutoUpdater : public mate::EventEmitter,
|
||||
public auto_updater::AutoUpdaterDelegate {
|
||||
public:
|
||||
virtual ~AutoUpdater();
|
||||
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
static mate::Handle<AutoUpdater> Create(v8::Isolate* isolate);
|
||||
|
||||
protected:
|
||||
explicit AutoUpdater(v8::Handle<v8::Object> wrapper);
|
||||
AutoUpdater();
|
||||
virtual ~AutoUpdater();
|
||||
|
||||
// AutoUpdaterDelegate implementations.
|
||||
virtual void OnError(const std::string& error) OVERRIDE;
|
||||
|
@ -38,14 +37,12 @@ class AutoUpdater : public EventEmitter,
|
|||
const std::string& update_url,
|
||||
const base::Closure& quit_and_install) OVERRIDE;
|
||||
|
||||
// mate::Wrappable implementations:
|
||||
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate);
|
||||
|
||||
private:
|
||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static void SetFeedURL(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void CheckForUpdates(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static void ContinueUpdate(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void QuitAndInstall(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
void QuitAndInstall();
|
||||
|
||||
base::Closure quit_and_install_;
|
||||
|
||||
|
|
|
@ -2,47 +2,36 @@
|
|||
// 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_browser_ipc.h"
|
||||
|
||||
#include "atom/common/api/api_messages.h"
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||
#include "atom/common/native_mate_converters/value_converter.h"
|
||||
#include "content/public/browser/render_view_host.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
using content::RenderViewHost;
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
// static
|
||||
void BrowserIPC::Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
string16 channel;
|
||||
int process_id, routing_id;
|
||||
scoped_ptr<base::Value> arguments;
|
||||
if (!FromV8Arguments(args, &channel, &process_id, &routing_id, &arguments))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
DCHECK(arguments && arguments->IsType(base::Value::TYPE_LIST));
|
||||
namespace {
|
||||
|
||||
bool Send(const string16& channel, int process_id, int routing_id,
|
||||
const base::ListValue& arguments) {
|
||||
RenderViewHost* render_view_host(RenderViewHost::FromID(
|
||||
process_id, routing_id));
|
||||
if (!render_view_host)
|
||||
return node::ThrowError("Invalid render view host");
|
||||
if (!render_view_host) {
|
||||
node::ThrowError("Invalid render view host");
|
||||
return false;
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(render_view_host->Send(new AtomViewMsg_Message(
|
||||
routing_id,
|
||||
channel,
|
||||
*static_cast<base::ListValue*>(arguments.get()))));
|
||||
return render_view_host->Send(new AtomViewMsg_Message(routing_id, channel,
|
||||
arguments));
|
||||
}
|
||||
|
||||
// static
|
||||
void BrowserIPC::Initialize(v8::Handle<v8::Object> target) {
|
||||
NODE_SET_METHOD(target, "send", Send);
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||
dict.SetMethod("send", &Send);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
} // namespace
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_browser_ipc, atom::api::BrowserIPC::Initialize)
|
||||
NODE_MODULE(atom_browser_ipc, Initialize)
|
||||
|
|
|
@ -1,29 +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_BROWSER_IPC_H_
|
||||
#define ATOM_BROWSER_API_ATOM_API_BROWSER_IPC_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class BrowserIPC {
|
||||
public:
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
|
||||
private:
|
||||
static void Send(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(BrowserIPC);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_API_ATOM_API_BROWSER_IPC_H_
|
|
@ -2,143 +2,97 @@
|
|||
// 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_dialog.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/bind.h"
|
||||
#include "atom/browser/api/atom_api_window.h"
|
||||
#include "atom/browser/native_window.h"
|
||||
#include "atom/browser/ui/file_dialog.h"
|
||||
#include "atom/browser/ui/message_box.h"
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "atom/common/native_mate_converters/file_path_converter.h"
|
||||
#include "atom/common/native_mate_converters/function_converter.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace {
|
||||
|
||||
template<typename T>
|
||||
void CallV8Function(const RefCountedV8Function& callback, T arg) {
|
||||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
|
||||
v8::Handle<v8::Value> value = ToV8Value(arg);
|
||||
callback->NewHandle(node_isolate)->Call(
|
||||
v8::Context::GetCurrent()->Global(), 1, &value);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
void CallV8Function2(const RefCountedV8Function& callback, bool result, T arg) {
|
||||
if (result)
|
||||
return CallV8Function<T>(callback, arg);
|
||||
else
|
||||
return CallV8Function<void*>(callback, NULL);
|
||||
}
|
||||
|
||||
void Initialize(v8::Handle<v8::Object> target) {
|
||||
NODE_SET_METHOD(target, "showMessageBox", ShowMessageBox);
|
||||
NODE_SET_METHOD(target, "showOpenDialog", ShowOpenDialog);
|
||||
NODE_SET_METHOD(target, "showSaveDialog", ShowSaveDialog);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void ShowMessageBox(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
int type;
|
||||
std::vector<std::string> buttons;
|
||||
std::string title, message, detail;
|
||||
if (!FromV8Arguments(args, &type, &buttons, &title, &message, &detail))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
NativeWindow* native_window = FromV8Value(args[5]);
|
||||
|
||||
if (!args[6]->IsFunction()) {
|
||||
void ShowMessageBox(int type,
|
||||
const std::vector<std::string>& buttons,
|
||||
const std::string& title,
|
||||
const std::string& message,
|
||||
const std::string& detail,
|
||||
atom::api::Window* window,
|
||||
mate::Arguments* args) {
|
||||
v8::Handle<v8::Value> peek = args->PeekNext();
|
||||
atom::MessageBoxCallback callback;
|
||||
if (mate::Converter<atom::MessageBoxCallback>::FromV8(node_isolate,
|
||||
peek,
|
||||
&callback)) {
|
||||
atom::ShowMessageBox(window->window(), (atom::MessageBoxType)type, buttons,
|
||||
title, message, detail, callback);
|
||||
} else {
|
||||
int chosen = atom::ShowMessageBox(
|
||||
native_window,
|
||||
(MessageBoxType)type,
|
||||
window->window(),
|
||||
(atom::MessageBoxType)type,
|
||||
buttons,
|
||||
title,
|
||||
message,
|
||||
detail);
|
||||
args.GetReturnValue().Set(chosen);
|
||||
} else {
|
||||
RefCountedV8Function callback = FromV8Value(args[6]);
|
||||
atom::ShowMessageBox(
|
||||
native_window,
|
||||
(MessageBoxType)type,
|
||||
buttons,
|
||||
title,
|
||||
message,
|
||||
detail,
|
||||
base::Bind(&CallV8Function<int>, callback));
|
||||
args->Return(chosen);
|
||||
}
|
||||
}
|
||||
|
||||
void ShowOpenDialog(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string title;
|
||||
base::FilePath default_path;
|
||||
int properties;
|
||||
if (!FromV8Arguments(args, &title, &default_path, &properties))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
NativeWindow* native_window = FromV8Value(args[3]);
|
||||
|
||||
if (!args[4]->IsFunction()) {
|
||||
void ShowOpenDialog(const std::string& title,
|
||||
const base::FilePath& default_path,
|
||||
int properties,
|
||||
atom::api::Window* window,
|
||||
mate::Arguments* args) {
|
||||
v8::Handle<v8::Value> peek = args->PeekNext();
|
||||
file_dialog::OpenDialogCallback callback;
|
||||
if (mate::Converter<file_dialog::OpenDialogCallback>::FromV8(node_isolate,
|
||||
peek,
|
||||
&callback)) {
|
||||
file_dialog::ShowOpenDialog(window->window(), title, default_path,
|
||||
properties, callback);
|
||||
} else {
|
||||
std::vector<base::FilePath> paths;
|
||||
if (!file_dialog::ShowOpenDialog(native_window,
|
||||
title,
|
||||
default_path,
|
||||
properties,
|
||||
&paths))
|
||||
return;
|
||||
|
||||
v8::Handle<v8::Array> result = v8::Array::New(paths.size());
|
||||
for (size_t i = 0; i < paths.size(); ++i)
|
||||
result->Set(i, ToV8Value(paths[i]));
|
||||
|
||||
args.GetReturnValue().Set(result);
|
||||
} else {
|
||||
RefCountedV8Function callback = FromV8Value(args[4]);
|
||||
file_dialog::ShowOpenDialog(
|
||||
native_window,
|
||||
title,
|
||||
default_path,
|
||||
properties,
|
||||
base::Bind(&CallV8Function2<const std::vector<base::FilePath>&>,
|
||||
callback));
|
||||
}
|
||||
}
|
||||
|
||||
void ShowSaveDialog(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string title;
|
||||
base::FilePath default_path;
|
||||
if (!FromV8Arguments(args, &title, &default_path))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
NativeWindow* native_window = FromV8Value(args[2]);
|
||||
|
||||
if (!args[3]->IsFunction()) {
|
||||
base::FilePath path;
|
||||
if (file_dialog::ShowSaveDialog(native_window,
|
||||
if (file_dialog::ShowOpenDialog(window->window(),
|
||||
title,
|
||||
default_path,
|
||||
&path))
|
||||
args.GetReturnValue().Set(ToV8Value(path));
|
||||
} else {
|
||||
RefCountedV8Function callback = FromV8Value(args[3]);
|
||||
file_dialog::ShowSaveDialog(
|
||||
native_window,
|
||||
title,
|
||||
default_path,
|
||||
base::Bind(&CallV8Function2<const base::FilePath&>, callback));
|
||||
properties,
|
||||
&paths))
|
||||
args->Return(paths);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
void ShowSaveDialog(const std::string& title,
|
||||
const base::FilePath& default_path,
|
||||
atom::api::Window* window,
|
||||
mate::Arguments* args) {
|
||||
v8::Handle<v8::Value> peek = args->PeekNext();
|
||||
file_dialog::SaveDialogCallback callback;
|
||||
if (mate::Converter<file_dialog::SaveDialogCallback>::FromV8(node_isolate,
|
||||
peek,
|
||||
&callback)) {
|
||||
file_dialog::ShowSaveDialog(window->window(), title, default_path,
|
||||
callback);
|
||||
} else {
|
||||
base::FilePath path;
|
||||
if (file_dialog::ShowSaveDialog(window->window(), title, default_path,
|
||||
&path))
|
||||
args->Return(path);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace atom
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||
dict.SetMethod("showMessageBox", &ShowMessageBox);
|
||||
dict.SetMethod("showOpenDialog", &ShowOpenDialog);
|
||||
dict.SetMethod("showSaveDialog", &ShowSaveDialog);
|
||||
}
|
||||
|
||||
NODE_MODULE(atom_browser_dialog, atom::api::Initialize)
|
||||
} // namespace
|
||||
|
||||
NODE_MODULE(atom_browser_dialog, Initialize)
|
||||
|
|
|
@ -1,22 +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_DIALOG_H_
|
||||
#define ATOM_BROWSER_API_ATOM_API_DIALOG_H_
|
||||
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
void ShowMessageBox(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
void ShowOpenDialog(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
void ShowSaveDialog(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_BROWSER_API_ATOM_API_DIALOG_H_
|
|
@ -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/v8/node_common.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "content/public/browser/web_contents.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
ScopedPersistent<v8::Function> Event::constructor_template_;
|
||||
|
||||
Event::Event()
|
||||
: sender_(NULL),
|
||||
message_(NULL),
|
||||
prevent_default_(false) {
|
||||
}
|
||||
|
||||
Event::~Event() {
|
||||
}
|
||||
|
||||
// static
|
||||
v8::Handle<v8::Object> Event::CreateV8Object() {
|
||||
if (constructor_template_.IsEmpty()) {
|
||||
v8::Local<v8::FunctionTemplate> 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<v8::Function> 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<v8::Value>& args) {
|
||||
Event* event = new Event;
|
||||
event->Wrap(args.This());
|
||||
}
|
||||
|
||||
// static
|
||||
void Event::PreventDefault(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
Event* event = Unwrap<Event>(args.This());
|
||||
if (event == NULL)
|
||||
return node::ThrowError("Event is already destroyed");
|
||||
|
||||
event->prevent_default_ = true;
|
||||
}
|
||||
|
||||
// static
|
||||
void Event::SendReply(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
Event* event = Unwrap<Event>(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<v8::Value>& args) {
|
||||
delete Unwrap<Event>(args.This());
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
|
@ -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<v8::Object> 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<v8::Value>& args);
|
||||
|
||||
static void PreventDefault(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SendReply(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Destroy(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static ScopedPersistent<v8::Function> 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_
|
|
@ -4,16 +4,14 @@
|
|||
|
||||
#include "atom/browser/api/atom_api_menu.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "atom/browser/api/atom_api_window.h"
|
||||
#include "atom/browser/ui/accelerator_util.h"
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||
#include "native_mate/constructor.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
|
||||
#define UNWRAP_MEMNU_AND_CHECK \
|
||||
Menu* self = ObjectWrap::Unwrap<Menu>(args.This()); \
|
||||
if (self == NULL) \
|
||||
return node::ThrowError("Menu is already destroyed")
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
|
@ -46,9 +44,7 @@ v8::Handle<v8::Value> CallDelegate(v8::Handle<v8::Value> default_value,
|
|||
|
||||
} // namespace
|
||||
|
||||
Menu::Menu(v8::Handle<v8::Object> wrapper)
|
||||
: EventEmitter(wrapper),
|
||||
model_(new ui::SimpleMenuModel(this)) {
|
||||
Menu::Menu() : model_(new ui::SimpleMenuModel(this)) {
|
||||
}
|
||||
|
||||
Menu::~Menu() {
|
||||
|
@ -58,7 +54,7 @@ bool Menu::IsCommandIdChecked(int command_id) const {
|
|||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
return CallDelegate(v8::False(),
|
||||
const_cast<Menu*>(this)->handle(),
|
||||
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
||||
"isCommandIdChecked",
|
||||
command_id)->BooleanValue();
|
||||
}
|
||||
|
@ -67,7 +63,7 @@ bool Menu::IsCommandIdEnabled(int command_id) const {
|
|||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
return CallDelegate(v8::True(),
|
||||
const_cast<Menu*>(this)->handle(),
|
||||
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
||||
"isCommandIdEnabled",
|
||||
command_id)->BooleanValue();
|
||||
}
|
||||
|
@ -76,7 +72,7 @@ bool Menu::IsCommandIdVisible(int command_id) const {
|
|||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
return CallDelegate(v8::True(),
|
||||
const_cast<Menu*>(this)->handle(),
|
||||
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
||||
"isCommandIdVisible",
|
||||
command_id)->BooleanValue();
|
||||
}
|
||||
|
@ -86,11 +82,11 @@ bool Menu::GetAcceleratorForCommandId(int command_id,
|
|||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
v8::Handle<v8::Value> shortcut = CallDelegate(v8::Undefined(),
|
||||
handle(),
|
||||
GetWrapper(node_isolate),
|
||||
"getAcceleratorForCommandId",
|
||||
command_id);
|
||||
if (shortcut->IsString()) {
|
||||
std::string shortcut_str = FromV8Value(shortcut);
|
||||
std::string shortcut_str = mate::V8ToString(shortcut);
|
||||
return accelerator_util::StringToAccelerator(shortcut_str, accelerator);
|
||||
}
|
||||
|
||||
|
@ -101,7 +97,7 @@ bool Menu::IsItemForCommandIdDynamic(int command_id) const {
|
|||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
return CallDelegate(v8::False(),
|
||||
const_cast<Menu*>(this)->handle(),
|
||||
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
||||
"isItemForCommandIdDynamic",
|
||||
command_id)->BooleanValue();
|
||||
}
|
||||
|
@ -109,254 +105,150 @@ bool Menu::IsItemForCommandIdDynamic(int command_id) const {
|
|||
string16 Menu::GetLabelForCommandId(int command_id) const {
|
||||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
return FromV8Value(CallDelegate(v8::False(),
|
||||
const_cast<Menu*>(this)->handle(),
|
||||
"getLabelForCommandId",
|
||||
command_id));
|
||||
v8::Handle<v8::Value> result = CallDelegate(
|
||||
v8::False(),
|
||||
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
||||
"getLabelForCommandId",
|
||||
command_id);
|
||||
string16 label;
|
||||
mate::ConvertFromV8(node_isolate, result, &label);
|
||||
return label;
|
||||
}
|
||||
|
||||
string16 Menu::GetSublabelForCommandId(int command_id) const {
|
||||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
return FromV8Value(CallDelegate(v8::False(),
|
||||
const_cast<Menu*>(this)->handle(),
|
||||
"getSubLabelForCommandId",
|
||||
command_id));
|
||||
v8::Handle<v8::Value> result = CallDelegate(
|
||||
v8::False(),
|
||||
const_cast<Menu*>(this)->GetWrapper(node_isolate),
|
||||
"getSubLabelForCommandId",
|
||||
command_id);
|
||||
string16 label;
|
||||
mate::ConvertFromV8(node_isolate, result, &label);
|
||||
return label;
|
||||
}
|
||||
|
||||
void Menu::ExecuteCommand(int command_id, int event_flags) {
|
||||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
CallDelegate(v8::False(), handle(), "executeCommand", command_id);
|
||||
CallDelegate(v8::False(), GetWrapper(node_isolate), "executeCommand",
|
||||
command_id);
|
||||
}
|
||||
|
||||
void Menu::InsertItemAt(
|
||||
int index, int command_id, const base::string16& label) {
|
||||
model_->InsertItemAt(index, command_id, label);
|
||||
}
|
||||
|
||||
void Menu::InsertSeparatorAt(int index) {
|
||||
model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
|
||||
}
|
||||
|
||||
void Menu::InsertCheckItemAt(int index,
|
||||
int command_id,
|
||||
const base::string16& label) {
|
||||
model_->InsertCheckItemAt(index, command_id, label);
|
||||
}
|
||||
|
||||
void Menu::InsertRadioItemAt(int index,
|
||||
int command_id,
|
||||
const base::string16& label,
|
||||
int group_id) {
|
||||
model_->InsertRadioItemAt(index, command_id, label, group_id);
|
||||
}
|
||||
|
||||
void Menu::InsertSubMenuAt(int index,
|
||||
int command_id,
|
||||
const base::string16& label,
|
||||
Menu* menu) {
|
||||
model_->InsertSubMenuAt(index, command_id, label, menu->model_.get());
|
||||
}
|
||||
|
||||
void Menu::SetSublabel(int index, const base::string16& sublabel) {
|
||||
model_->SetSublabel(index, sublabel);
|
||||
}
|
||||
|
||||
void Menu::Clear() {
|
||||
model_->Clear();
|
||||
}
|
||||
|
||||
int Menu::GetIndexOfCommandId(int command_id) {
|
||||
return model_->GetIndexOfCommandId(command_id);
|
||||
}
|
||||
|
||||
int Menu::GetItemCount() const {
|
||||
return model_->GetItemCount();
|
||||
}
|
||||
|
||||
int Menu::GetCommandIdAt(int index) const {
|
||||
return model_->GetCommandIdAt(index);
|
||||
}
|
||||
|
||||
base::string16 Menu::GetLabelAt(int index) const {
|
||||
return model_->GetLabelAt(index);
|
||||
}
|
||||
|
||||
base::string16 Menu::GetSublabelAt(int index) const {
|
||||
return model_->GetSublabelAt(index);
|
||||
}
|
||||
|
||||
bool Menu::IsItemCheckedAt(int index) const {
|
||||
return model_->IsItemCheckedAt(index);
|
||||
}
|
||||
|
||||
bool Menu::IsEnabledAt(int index) const {
|
||||
return model_->IsEnabledAt(index);
|
||||
}
|
||||
|
||||
bool Menu::IsVisibleAt(int index) const {
|
||||
return model_->IsVisibleAt(index);
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
if (!args.IsConstructCall())
|
||||
return node::ThrowError("Require constructor call");
|
||||
|
||||
Menu::Create(args.This());
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::InsertItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
|
||||
int index, command_id;
|
||||
string16 label;
|
||||
if (!FromV8Arguments(args, &index, &command_id, &label))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
if (index < 0)
|
||||
self->model_->AddItem(command_id, label);
|
||||
else
|
||||
self->model_->InsertItemAt(index, command_id, label);
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::InsertCheckItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
|
||||
int index, command_id;
|
||||
string16 label;
|
||||
if (!FromV8Arguments(args, &index, &command_id, &label))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
if (index < 0)
|
||||
self->model_->AddCheckItem(command_id, label);
|
||||
else
|
||||
self->model_->InsertCheckItemAt(index, command_id, label);
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::InsertRadioItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
|
||||
int index, command_id, group_id;
|
||||
string16 label;
|
||||
if (!FromV8Arguments(args, &index, &command_id, &label, &group_id))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
if (index < 0)
|
||||
self->model_->AddRadioItem(command_id, label, group_id);
|
||||
else
|
||||
self->model_->InsertRadioItemAt(index, command_id, label, group_id);
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::InsertSeparator(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
|
||||
int index;
|
||||
if (!FromV8Arguments(args, &index))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
if (index < 0)
|
||||
self->model_->AddSeparator(ui::NORMAL_SEPARATOR);
|
||||
else
|
||||
self->model_->InsertSeparatorAt(index, ui::NORMAL_SEPARATOR);
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::InsertSubMenu(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
|
||||
int index, command_id;
|
||||
string16 label;
|
||||
if (!FromV8Arguments(args, &index, &command_id, &label))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
Menu* submenu = ObjectWrap::Unwrap<Menu>(args[3]->ToObject());
|
||||
if (!submenu)
|
||||
return node::ThrowTypeError("The submenu is already destroyed");
|
||||
|
||||
if (index < 0)
|
||||
self->model_->AddSubMenu(command_id, label, submenu->model_.get());
|
||||
else
|
||||
self->model_->InsertSubMenuAt(
|
||||
index, command_id, label, submenu->model_.get());
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::SetIcon(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
|
||||
int index;
|
||||
base::FilePath path;
|
||||
if (!FromV8Arguments(args, &index, &path))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
// FIXME use webkit_glue's image decoder here.
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::SetSublabel(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
|
||||
int index;
|
||||
string16 label;
|
||||
if (!FromV8Arguments(args, &index, &label))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
self->model_->SetSublabel(index, label);
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::Clear(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
self->model_->Clear();
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::GetIndexOfCommandId(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
int index = FromV8Value(args[0]);
|
||||
args.GetReturnValue().Set(self->model_->GetIndexOfCommandId(index));
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::GetItemCount(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
args.GetReturnValue().Set(self->model_->GetItemCount());
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::GetCommandIdAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
int index = FromV8Value(args[0]);
|
||||
args.GetReturnValue().Set(self->model_->GetCommandIdAt(index));
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::GetLabelAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
int index = FromV8Value(args[0]);
|
||||
args.GetReturnValue().Set(ToV8Value(self->model_->GetLabelAt(index)));
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::GetSublabelAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
int index = FromV8Value(args[0]);
|
||||
args.GetReturnValue().Set(ToV8Value(self->model_->GetSublabelAt(index)));
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::IsItemCheckedAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
int index = FromV8Value(args[0]);
|
||||
args.GetReturnValue().Set(self->model_->IsItemCheckedAt(index));
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::IsEnabledAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
int index = FromV8Value(args[0]);
|
||||
args.GetReturnValue().Set(self->model_->IsEnabledAt(index));
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::IsVisibleAt(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
int index = FromV8Value(args[0]);
|
||||
args.GetReturnValue().Set(self->model_->IsVisibleAt(index));
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::Popup(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_MEMNU_AND_CHECK;
|
||||
|
||||
atom::NativeWindow* window;
|
||||
if (!FromV8Arguments(args, &window))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
self->Popup(window);
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::Initialize(v8::Handle<v8::Object> target) {
|
||||
v8::Local<v8::FunctionTemplate> t(v8::FunctionTemplate::New(Menu::New));
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
t->SetClassName(v8::String::NewSymbol("Menu"));
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "insertItem", InsertItem);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "insertCheckItem", InsertCheckItem);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "insertRadioItem", InsertRadioItem);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "insertSeparator", InsertSeparator);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "insertSubMenu", InsertSubMenu);
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "setIcon", SetIcon);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "setSublabel", SetSublabel);
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "clear", Clear);
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "getIndexOfCommandId", GetIndexOfCommandId);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "getItemCount", GetItemCount);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "getCommandIdAt", GetCommandIdAt);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "getLabelAt", GetLabelAt);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "getSublabelAt", GetSublabelAt);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "isItemCheckedAt", IsItemCheckedAt);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "isEnabledAt", IsEnabledAt);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "isVisibleAt", IsVisibleAt);
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "popup", Popup);
|
||||
|
||||
void Menu::BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Handle<v8::ObjectTemplate> prototype) {
|
||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||
.SetMethod("insertItem", &Menu::InsertItemAt)
|
||||
.SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
|
||||
.SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)
|
||||
.SetMethod("insertSeparator", &Menu::InsertSeparatorAt)
|
||||
.SetMethod("insertSubMenu", &Menu::InsertSubMenuAt)
|
||||
.SetMethod("setSublabel", &Menu::SetSublabel)
|
||||
.SetMethod("clear", &Menu::Clear)
|
||||
.SetMethod("getIndexOfCommandId", &Menu::GetIndexOfCommandId)
|
||||
.SetMethod("getItemCount", &Menu::GetItemCount)
|
||||
.SetMethod("getCommandIdAt", &Menu::GetCommandIdAt)
|
||||
.SetMethod("getLabelAt", &Menu::GetLabelAt)
|
||||
.SetMethod("getSublabelAt", &Menu::GetSublabelAt)
|
||||
.SetMethod("isItemCheckedAt", &Menu::IsItemCheckedAt)
|
||||
.SetMethod("isEnabledAt", &Menu::IsEnabledAt)
|
||||
.SetMethod("isVisibleAt", &Menu::IsVisibleAt)
|
||||
#if defined(OS_WIN) || defined(TOOLKIT_GTK)
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "attachToWindow", AttachToWindow);
|
||||
#endif
|
||||
|
||||
target->Set(v8::String::NewSymbol("Menu"), t->GetFunction());
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
NODE_SET_METHOD(target, "setApplicationMenu", SetApplicationMenu);
|
||||
NODE_SET_METHOD(
|
||||
target, "sendActionToFirstResponder", SendActionToFirstResponder);
|
||||
.SetMethod("attachToWindow", &Menu::AttachToWindow)
|
||||
#endif
|
||||
.SetMethod("popup", &Menu::Popup);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_browser_menu, atom::api::Menu::Initialize)
|
||||
|
||||
namespace {
|
||||
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
using atom::api::Menu;
|
||||
v8::Local<v8::Function> constructor = mate::CreateConstructor<Menu>(
|
||||
node_isolate, "Menu", base::Bind(&Menu::Create));
|
||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||
dict.Set("Menu", static_cast<v8::Handle<v8::Value>>(constructor));
|
||||
#if defined(OS_MACOSX)
|
||||
dict.SetMethod("setApplicationMenu", &Menu::SetApplicationMenu);
|
||||
dict.SetMethod("sendActionToFirstResponder",
|
||||
&Menu::SendActionToFirstResponder);
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NODE_MODULE(atom_browser_menu, Initialize)
|
||||
|
|
|
@ -5,27 +5,38 @@
|
|||
#ifndef ATOM_BROWSER_API_ATOM_API_MENU_H_
|
||||
#define ATOM_BROWSER_API_ATOM_API_MENU_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "atom/browser/api/atom_api_window.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "atom/common/api/atom_api_event_emitter.h"
|
||||
#include "ui/base/models/simple_menu_model.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
class NativeWindow;
|
||||
|
||||
namespace api {
|
||||
|
||||
class Menu : public EventEmitter,
|
||||
class MenuMac;
|
||||
|
||||
class Menu : public mate::Wrappable,
|
||||
public ui::SimpleMenuModel::Delegate {
|
||||
public:
|
||||
virtual ~Menu();
|
||||
static mate::Wrappable* Create();
|
||||
|
||||
static Menu* Create(v8::Handle<v8::Object> wrapper);
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Handle<v8::ObjectTemplate> prototype);
|
||||
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
#if defined(OS_MACOSX)
|
||||
// Set the global menubar.
|
||||
static void SetApplicationMenu(Menu* menu);
|
||||
|
||||
// Fake sending an action from the application menu.
|
||||
static void SendActionToFirstResponder(const std::string& action);
|
||||
#endif
|
||||
|
||||
protected:
|
||||
explicit Menu(v8::Handle<v8::Object> wrapper);
|
||||
Menu();
|
||||
virtual ~Menu();
|
||||
|
||||
// ui::SimpleMenuModel::Delegate implementations:
|
||||
virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
|
||||
|
@ -39,43 +50,37 @@ class Menu : public EventEmitter,
|
|||
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<ui::SimpleMenuModel> model_;
|
||||
|
||||
private:
|
||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static void InsertItem(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void InsertCheckItem(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void InsertRadioItem(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void InsertSeparator(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void InsertSubMenu(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static void SetIcon(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetSublabel(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static void Clear(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static void GetIndexOfCommandId(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetItemCount(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetCommandIdAt(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetLabelAt(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetSublabelAt(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsItemCheckedAt(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsEnabledAt(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsVisibleAt(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static void Popup(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
void InsertItemAt(int index, int command_id, const base::string16& label);
|
||||
void InsertSeparatorAt(int index);
|
||||
void InsertCheckItemAt(int index,
|
||||
int command_id,
|
||||
const base::string16& label);
|
||||
void InsertRadioItemAt(int index,
|
||||
int command_id,
|
||||
const base::string16& label,
|
||||
int group_id);
|
||||
void InsertSubMenuAt(int index,
|
||||
int command_id,
|
||||
const base::string16& label,
|
||||
Menu* menu);
|
||||
void SetSublabel(int index, const base::string16& sublabel);
|
||||
void Clear();
|
||||
int GetIndexOfCommandId(int command_id);
|
||||
int GetItemCount() const;
|
||||
int GetCommandIdAt(int index) const;
|
||||
base::string16 GetLabelAt(int index) const;
|
||||
base::string16 GetSublabelAt(int index) const;
|
||||
bool IsItemCheckedAt(int index) const;
|
||||
bool IsEnabledAt(int index) const;
|
||||
bool IsVisibleAt(int index) const;
|
||||
|
||||
#if defined(OS_WIN) || defined(TOOLKIT_GTK)
|
||||
static void AttachToWindow(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
#elif defined(OS_MACOSX)
|
||||
static void SetApplicationMenu(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SendActionToFirstResponder(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
void AttachToWindow(Window* window);
|
||||
#endif
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Menu);
|
||||
|
|
|
@ -5,28 +5,24 @@
|
|||
#include "atom/browser/api/atom_api_menu_gtk.h"
|
||||
|
||||
#include "atom/browser/native_window_gtk.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "content/public/browser/render_widget_host_view.h"
|
||||
#include "ui/gfx/point.h"
|
||||
#include "ui/gfx/screen.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
MenuGtk::MenuGtk(v8::Handle<v8::Object> wrapper)
|
||||
: Menu(wrapper) {
|
||||
MenuGtk::MenuGtk() {
|
||||
}
|
||||
|
||||
MenuGtk::~MenuGtk() {
|
||||
}
|
||||
|
||||
void MenuGtk::Popup(NativeWindow* native_window) {
|
||||
void MenuGtk::Popup(Window* window) {
|
||||
uint32_t triggering_event_time;
|
||||
gfx::Point point;
|
||||
|
||||
NativeWindow* native_window = window->window();
|
||||
GdkEventButton* event = native_window->GetWebContents()->
|
||||
GetRenderWidgetHostView()->GetLastMouseDown();
|
||||
if (event) {
|
||||
|
@ -41,22 +37,13 @@ void MenuGtk::Popup(NativeWindow* native_window) {
|
|||
menu_gtk_->PopupAsContext(point, triggering_event_time);
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::AttachToWindow(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
Menu* self = ObjectWrap::Unwrap<Menu>(args.This());
|
||||
if (self == NULL)
|
||||
return node::ThrowError("Menu is already destroyed");
|
||||
|
||||
NativeWindow* native_window;
|
||||
if (!FromV8Arguments(args, &native_window))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
static_cast<NativeWindowGtk*>(native_window)->SetMenu(self->model_.get());
|
||||
void Menu::AttachToWindow(Window* window) {
|
||||
static_cast<NativeWindowGtk*>(window->window())->SetMenu(model_.get());
|
||||
}
|
||||
|
||||
// static
|
||||
Menu* Menu::Create(v8::Handle<v8::Object> wrapper) {
|
||||
return new MenuGtk(wrapper);
|
||||
mate::Wrappable* Menu::Create() {
|
||||
return new MenuGtk();
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
|
|
@ -15,11 +15,10 @@ namespace api {
|
|||
class MenuGtk : public Menu,
|
||||
public ::MenuGtk::Delegate {
|
||||
public:
|
||||
explicit MenuGtk(v8::Handle<v8::Object> wrapper);
|
||||
virtual ~MenuGtk();
|
||||
MenuGtk();
|
||||
|
||||
protected:
|
||||
virtual void Popup(NativeWindow* window) OVERRIDE;
|
||||
virtual void Popup(Window* window) OVERRIDE;
|
||||
|
||||
private:
|
||||
scoped_ptr<::MenuGtk> menu_gtk_;
|
||||
|
|
|
@ -16,19 +16,16 @@ namespace atom {
|
|||
namespace api {
|
||||
|
||||
class MenuMac : public Menu {
|
||||
public:
|
||||
explicit MenuMac(v8::Handle<v8::Object> wrapper);
|
||||
virtual ~MenuMac();
|
||||
|
||||
protected:
|
||||
virtual void Popup(NativeWindow* window) OVERRIDE;
|
||||
MenuMac();
|
||||
|
||||
virtual void Popup(Window* window) OVERRIDE;
|
||||
|
||||
base::scoped_nsobject<AtomMenuController> menu_controller_;
|
||||
|
||||
private:
|
||||
friend class Menu;
|
||||
|
||||
// Fake sending an action from the application menu.
|
||||
static void SendActionToFirstResponder(const std::string& action);
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(MenuMac);
|
||||
|
|
|
@ -4,41 +4,38 @@
|
|||
|
||||
#import "atom/browser/api/atom_api_menu_mac.h"
|
||||
|
||||
#include "atom/browser/native_window.h"
|
||||
#include "base/message_loop/message_loop.h"
|
||||
#include "base/strings/sys_string_conversions.h"
|
||||
#include "atom/browser/native_window.h"
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "content/public/browser/web_contents.h"
|
||||
#include "content/public/browser/web_contents_view.h"
|
||||
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
MenuMac::MenuMac(v8::Handle<v8::Object> wrapper)
|
||||
: Menu(wrapper) {
|
||||
MenuMac::MenuMac() {
|
||||
}
|
||||
|
||||
MenuMac::~MenuMac() {
|
||||
}
|
||||
|
||||
void MenuMac::Popup(NativeWindow* native_window) {
|
||||
void MenuMac::Popup(Window* window) {
|
||||
base::scoped_nsobject<AtomMenuController> 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
|
||||
|
@ -51,22 +48,8 @@ void MenuMac::Popup(NativeWindow* native_window) {
|
|||
}
|
||||
|
||||
// static
|
||||
void MenuMac::SendActionToFirstResponder(const std::string& action) {
|
||||
SEL selector = NSSelectorFromString(base::SysUTF8ToNSString(action));
|
||||
[[NSApplication sharedApplication] sendAction:selector
|
||||
to:nil
|
||||
from:[NSApp mainMenu]];
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::SetApplicationMenu(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
if (!args[0]->IsObject())
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
MenuMac* menu = ObjectWrap::Unwrap<MenuMac>(args[0]->ToObject());
|
||||
if (!menu)
|
||||
return node::ThrowError("Menu is destroyed");
|
||||
|
||||
void Menu::SetApplicationMenu(Menu* base_menu) {
|
||||
MenuMac* menu = static_cast<MenuMac*>(base_menu);
|
||||
base::scoped_nsobject<AtomMenuController> menu_controller(
|
||||
[[AtomMenuController alloc] initWithModel:menu->model_.get()]);
|
||||
[NSApp setMainMenu:[menu_controller menu]];
|
||||
|
@ -76,18 +59,14 @@ void Menu::SetApplicationMenu(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|||
}
|
||||
|
||||
// static
|
||||
void Menu::SendActionToFirstResponder(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string action;
|
||||
if (!FromV8Arguments(args, &action))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
MenuMac::SendActionToFirstResponder(action);
|
||||
void Menu::SendActionToFirstResponder(const std::string& action) {
|
||||
SEL selector = NSSelectorFromString(base::SysUTF8ToNSString(action));
|
||||
[NSApp sendAction:selector to:nil from:[NSApp mainMenu]];
|
||||
}
|
||||
|
||||
// static
|
||||
Menu* Menu::Create(v8::Handle<v8::Object> wrapper) {
|
||||
return new MenuMac(wrapper);
|
||||
mate::Wrappable* Menu::Create() {
|
||||
return new MenuMac();
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
|
|
@ -6,45 +6,31 @@
|
|||
|
||||
#include "atom/browser/native_window_win.h"
|
||||
#include "atom/browser/ui/win/menu_2.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "ui/gfx/point.h"
|
||||
#include "ui/gfx/screen.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
MenuWin::MenuWin(v8::Handle<v8::Object> wrapper)
|
||||
: Menu(wrapper) {
|
||||
MenuWin::MenuWin() {
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// static
|
||||
void Menu::AttachToWindow(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
Menu* self = ObjectWrap::Unwrap<Menu>(args.This());
|
||||
if (self == NULL)
|
||||
return node::ThrowError("Menu is already destroyed");
|
||||
|
||||
NativeWindow* native_window;
|
||||
if (!FromV8Arguments(args, &native_window))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
static_cast<NativeWindowWin*>(native_window)->SetMenu(self->model_.get());
|
||||
void Menu::AttachToWindow(Window* window) {
|
||||
static_cast<NativeWindowWin*>(window->window())->SetMenu(model_.get());
|
||||
}
|
||||
|
||||
// static
|
||||
Menu* Menu::Create(v8::Handle<v8::Object> wrapper) {
|
||||
return new MenuWin(wrapper);
|
||||
mate::Wrappable* Menu::Create() {
|
||||
return new MenuWin();
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
|
|
@ -15,11 +15,10 @@ namespace api {
|
|||
|
||||
class MenuWin : public Menu {
|
||||
public:
|
||||
explicit MenuWin(v8::Handle<v8::Object> wrapper);
|
||||
virtual ~MenuWin();
|
||||
MenuWin();
|
||||
|
||||
protected:
|
||||
virtual void Popup(NativeWindow* window) OVERRIDE;
|
||||
virtual void Popup(Window* window) OVERRIDE;
|
||||
|
||||
private:
|
||||
scoped_ptr<atom::Menu2> menu_;
|
||||
|
|
|
@ -6,15 +6,15 @@
|
|||
|
||||
#include "base/power_monitor/power_monitor.h"
|
||||
#include "base/power_monitor/power_monitor_device_source.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
PowerMonitor::PowerMonitor(v8::Handle<v8::Object> wrapper)
|
||||
: EventEmitter(wrapper) {
|
||||
PowerMonitor::PowerMonitor() {
|
||||
base::PowerMonitor::Get()->AddObserver(this);
|
||||
}
|
||||
|
||||
|
@ -38,30 +38,29 @@ void PowerMonitor::OnResume() {
|
|||
}
|
||||
|
||||
// static
|
||||
void PowerMonitor::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
if (!args.IsConstructCall())
|
||||
return node::ThrowError("Require constructor call");
|
||||
|
||||
new PowerMonitor(args.This());
|
||||
mate::Handle<PowerMonitor> PowerMonitor::Create(v8::Isolate* isolate) {
|
||||
return CreateHandle(isolate, new PowerMonitor);
|
||||
}
|
||||
|
||||
// static
|
||||
void PowerMonitor::Initialize(v8::Handle<v8::Object> target) {
|
||||
#if defined(OS_MACOSX)
|
||||
base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
|
||||
#endif
|
||||
|
||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(
|
||||
PowerMonitor::New);
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
t->SetClassName(v8::String::NewSymbol("PowerMonitor"));
|
||||
|
||||
target->Set(v8::String::NewSymbol("PowerMonitor"), t->GetFunction());
|
||||
}
|
||||
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_browser_power_monitor, atom::api::PowerMonitor::Initialize)
|
||||
|
||||
namespace {
|
||||
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
#if defined(OS_MACOSX)
|
||||
base::PowerMonitorDeviceSource::AllocateSystemIOPorts();
|
||||
#endif
|
||||
|
||||
using atom::api::PowerMonitor;
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
mate::Handle<PowerMonitor> power_monitor = PowerMonitor::Create(isolate);
|
||||
mate::Dictionary dict(isolate, exports);
|
||||
dict.Set("powerMonitor", power_monitor);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NODE_MODULE(atom_browser_power_monitor, Initialize)
|
||||
|
|
|
@ -5,31 +5,30 @@
|
|||
#ifndef ATOM_BROWSER_API_ATOM_API_POWER_MONITOR_H_
|
||||
#define ATOM_BROWSER_API_ATOM_API_POWER_MONITOR_H_
|
||||
|
||||
#include "atom/browser/api/event_emitter.h"
|
||||
#include "base/compiler_specific.h"
|
||||
#include "base/power_monitor/power_observer.h"
|
||||
#include "atom/common/api/atom_api_event_emitter.h"
|
||||
#include "native_mate/handle.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class PowerMonitor : public EventEmitter,
|
||||
class PowerMonitor : public mate::EventEmitter,
|
||||
public base::PowerObserver {
|
||||
public:
|
||||
virtual ~PowerMonitor();
|
||||
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
static mate::Handle<PowerMonitor> Create(v8::Isolate* isolate);
|
||||
|
||||
protected:
|
||||
explicit PowerMonitor(v8::Handle<v8::Object> wrapper);
|
||||
PowerMonitor();
|
||||
virtual ~PowerMonitor();
|
||||
|
||||
// base::PowerObserver implementations:
|
||||
virtual void OnPowerStateChange(bool on_battery_power) OVERRIDE;
|
||||
virtual void OnSuspend() OVERRIDE;
|
||||
virtual void OnResume() OVERRIDE;
|
||||
|
||||
private:
|
||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(PowerMonitor);
|
||||
};
|
||||
|
||||
|
|
|
@ -9,142 +9,111 @@
|
|||
#include "atom/browser/net/adapter_request_job.h"
|
||||
#include "atom/browser/net/atom_url_request_context_getter.h"
|
||||
#include "atom/browser/net/atom_url_request_job_factory.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "atom/common/native_mate_converters/file_path_converter.h"
|
||||
#include "atom/common/native_mate_converters/function_converter.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "net/url_request/url_request_context.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
namespace mate {
|
||||
|
||||
template<>
|
||||
struct Converter<const net::URLRequest*> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const net::URLRequest* val) {
|
||||
return mate::ObjectTemplateBuilder(isolate)
|
||||
.SetValue("method", val->method())
|
||||
.SetValue("url", val->url().spec())
|
||||
.SetValue("referrer", val->referrer())
|
||||
.Build()->NewInstance();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
typedef net::URLRequestJobFactory::ProtocolHandler ProtocolHandler;
|
||||
|
||||
namespace {
|
||||
|
||||
// The protocol module object.
|
||||
ScopedPersistent<v8::Object> g_protocol_object;
|
||||
|
||||
// Registered protocol handlers.
|
||||
typedef std::map<std::string, RefCountedV8Function> HandlersMap;
|
||||
static HandlersMap g_handlers;
|
||||
|
||||
static const char* kEarlyUseProtocolError = "This method can only be used"
|
||||
"after the application has finished launching.";
|
||||
|
||||
// Emit an event for the protocol module.
|
||||
void EmitEventInUI(const std::string& event, const std::string& parameter) {
|
||||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
|
||||
v8::Handle<v8::Value> argv[] = {
|
||||
ToV8Value(event),
|
||||
ToV8Value(parameter),
|
||||
};
|
||||
node::MakeCallback(g_protocol_object.NewHandle(node_isolate),
|
||||
"emit", 2, argv);
|
||||
}
|
||||
|
||||
// Convert the URLRequest object to V8 object.
|
||||
v8::Handle<v8::Object> ConvertURLRequestToV8Object(
|
||||
const net::URLRequest* request) {
|
||||
v8::Local<v8::Object> obj = v8::Object::New();
|
||||
obj->Set(ToV8Value("method"), ToV8Value(request->method()));
|
||||
obj->Set(ToV8Value("url"), ToV8Value(request->url().spec()));
|
||||
obj->Set(ToV8Value("referrer"), ToV8Value(request->referrer()));
|
||||
return obj;
|
||||
}
|
||||
|
||||
// Get the job factory.
|
||||
AtomURLRequestJobFactory* GetRequestJobFactory() {
|
||||
return AtomBrowserContext::Get()->url_request_context_getter()->job_factory();
|
||||
}
|
||||
typedef net::URLRequestJobFactory::ProtocolHandler ProtocolHandler;
|
||||
|
||||
class CustomProtocolRequestJob : public AdapterRequestJob {
|
||||
public:
|
||||
CustomProtocolRequestJob(ProtocolHandler* protocol_handler,
|
||||
CustomProtocolRequestJob(Protocol* registry,
|
||||
ProtocolHandler* protocol_handler,
|
||||
net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate)
|
||||
: AdapterRequestJob(protocol_handler, request, network_delegate) {
|
||||
: AdapterRequestJob(protocol_handler, request, network_delegate),
|
||||
registry_(registry) {
|
||||
}
|
||||
|
||||
// AdapterRequestJob:
|
||||
virtual void GetJobTypeInUI() OVERRIDE {
|
||||
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
|
||||
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
|
||||
|
||||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
|
||||
// Call the JS handler.
|
||||
v8::Handle<v8::Value> argv[] = {
|
||||
ConvertURLRequestToV8Object(request()),
|
||||
};
|
||||
RefCountedV8Function callback = g_handlers[request()->url().scheme()];
|
||||
v8::Handle<v8::Value> result = callback->NewHandle(node_isolate)->Call(
|
||||
v8::Context::GetCurrent()->Global(), 1, argv);
|
||||
Protocol::JsProtocolHandler callback =
|
||||
registry_->GetProtocolHandler(request()->url().scheme());
|
||||
v8::Handle<v8::Value> result = callback.Run(request());
|
||||
|
||||
// Determine the type of the job we are going to create.
|
||||
if (result->IsString()) {
|
||||
std::string data = FromV8Value(result);
|
||||
content::BrowserThread::PostTask(
|
||||
content::BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
std::string data = mate::V8ToString(result);
|
||||
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&AdapterRequestJob::CreateStringJobAndStart,
|
||||
GetWeakPtr(),
|
||||
"text/plain",
|
||||
"UTF-8",
|
||||
data));
|
||||
GetWeakPtr(), "text/plain", "UTF-8", data));
|
||||
return;
|
||||
} else if (result->IsObject()) {
|
||||
v8::Handle<v8::Object> obj = result->ToObject();
|
||||
std::string name = FromV8Value(obj->GetConstructorName());
|
||||
mate::Dictionary dict(node_isolate, obj);
|
||||
std::string name = mate::V8ToString(obj->GetConstructorName());
|
||||
if (name == "RequestStringJob") {
|
||||
std::string mime_type = FromV8Value(obj->Get(
|
||||
v8::String::New("mimeType")));
|
||||
std::string charset = FromV8Value(obj->Get(v8::String::New("charset")));
|
||||
std::string data = FromV8Value(obj->Get(v8::String::New("data")));
|
||||
std::string mime_type, charset, data;
|
||||
dict.Get("mimeType", &mime_type);
|
||||
dict.Get("charset", &charset);
|
||||
dict.Get("data", &data);
|
||||
|
||||
content::BrowserThread::PostTask(
|
||||
content::BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&AdapterRequestJob::CreateStringJobAndStart,
|
||||
GetWeakPtr(),
|
||||
mime_type,
|
||||
charset,
|
||||
data));
|
||||
GetWeakPtr(), mime_type, charset, data));
|
||||
return;
|
||||
} else if (name == "RequestFileJob") {
|
||||
base::FilePath path = FromV8Value(obj->Get(v8::String::New("path")));
|
||||
base::FilePath path;
|
||||
dict.Get("path", &path);
|
||||
|
||||
content::BrowserThread::PostTask(
|
||||
content::BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&AdapterRequestJob::CreateFileJobAndStart,
|
||||
GetWeakPtr(),
|
||||
path));
|
||||
GetWeakPtr(), path));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Try the default protocol handler if we have.
|
||||
if (default_protocol_handler()) {
|
||||
content::BrowserThread::PostTask(
|
||||
content::BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&AdapterRequestJob::CreateJobFromProtocolHandlerAndStart,
|
||||
GetWeakPtr()));
|
||||
return;
|
||||
}
|
||||
|
||||
// Fallback to the not implemented error.
|
||||
content::BrowserThread::PostTask(
|
||||
content::BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
|
||||
base::Bind(&AdapterRequestJob::CreateErrorJobAndStart,
|
||||
GetWeakPtr(),
|
||||
net::ERR_NOT_IMPLEMENTED));
|
||||
GetWeakPtr(), net::ERR_NOT_IMPLEMENTED));
|
||||
}
|
||||
|
||||
private:
|
||||
Protocol* registry_; // Weak, the Protocol class is expected to live forever.
|
||||
};
|
||||
|
||||
// Always return the same CustomProtocolRequestJob for all requests, because
|
||||
|
@ -155,16 +124,16 @@ class CustomProtocolRequestJob : public AdapterRequestJob {
|
|||
// registered handler doesn't want to deal with the request.
|
||||
class CustomProtocolHandler : public ProtocolHandler {
|
||||
public:
|
||||
explicit CustomProtocolHandler(ProtocolHandler* protocol_handler = NULL)
|
||||
: protocol_handler_(protocol_handler) {
|
||||
CustomProtocolHandler(api::Protocol* registry,
|
||||
ProtocolHandler* protocol_handler = NULL)
|
||||
: registry_(registry), protocol_handler_(protocol_handler) {
|
||||
}
|
||||
|
||||
virtual net::URLRequestJob* MaybeCreateJob(
|
||||
net::URLRequest* request,
|
||||
net::NetworkDelegate* network_delegate) const OVERRIDE {
|
||||
return new CustomProtocolRequestJob(protocol_handler_.get(),
|
||||
request,
|
||||
network_delegate);
|
||||
return new CustomProtocolRequestJob(registry_, protocol_handler_.get(),
|
||||
request, network_delegate);
|
||||
}
|
||||
|
||||
ProtocolHandler* ReleaseDefaultProtocolHandler() {
|
||||
|
@ -174,6 +143,7 @@ class CustomProtocolHandler : public ProtocolHandler {
|
|||
ProtocolHandler* original_handler() { return protocol_handler_.get(); }
|
||||
|
||||
private:
|
||||
Protocol* registry_; // Weak, the Protocol class is expected to live forever.
|
||||
scoped_ptr<ProtocolHandler> protocol_handler_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(CustomProtocolHandler);
|
||||
|
@ -181,206 +151,182 @@ class CustomProtocolHandler : public ProtocolHandler {
|
|||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
void Protocol::RegisterProtocol(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string scheme;
|
||||
RefCountedV8Function callback;
|
||||
if (!FromV8Arguments(args, &scheme, &callback))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
Protocol::Protocol() : job_factory_(
|
||||
AtomBrowserContext::Get()->url_request_context_getter()->job_factory()) {
|
||||
}
|
||||
|
||||
if (g_handlers.find(scheme) != g_handlers.end() ||
|
||||
GetRequestJobFactory()->IsHandledProtocol(scheme))
|
||||
Protocol::JsProtocolHandler Protocol::GetProtocolHandler(
|
||||
const std::string& scheme) {
|
||||
return protocol_handlers_[scheme];
|
||||
}
|
||||
|
||||
mate::ObjectTemplateBuilder Protocol::GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate) {
|
||||
return mate::ObjectTemplateBuilder(isolate)
|
||||
.SetMethod("registerProtocol",
|
||||
base::Bind(&Protocol::RegisterProtocol,
|
||||
base::Unretained(this)))
|
||||
.SetMethod("unregisterProtocol",
|
||||
base::Bind(&Protocol::UnregisterProtocol,
|
||||
base::Unretained(this)))
|
||||
.SetMethod("isHandledProtocol",
|
||||
base::Bind(&Protocol::IsHandledProtocol,
|
||||
base::Unretained(this)))
|
||||
.SetMethod("interceptProtocol",
|
||||
base::Bind(&Protocol::InterceptProtocol,
|
||||
base::Unretained(this)))
|
||||
.SetMethod("uninterceptProtocol",
|
||||
base::Bind(&Protocol::UninterceptProtocol,
|
||||
base::Unretained(this)));
|
||||
}
|
||||
|
||||
void Protocol::RegisterProtocol(const std::string& scheme,
|
||||
const JsProtocolHandler& callback) {
|
||||
if (ContainsKey(protocol_handlers_, scheme) ||
|
||||
job_factory_->IsHandledProtocol(scheme))
|
||||
return node::ThrowError("The scheme is already registered");
|
||||
|
||||
if (AtomBrowserContext::Get()->url_request_context_getter() == NULL)
|
||||
return node::ThrowError(kEarlyUseProtocolError);
|
||||
|
||||
// Store the handler in a map.
|
||||
g_handlers[scheme] = callback;
|
||||
|
||||
content::BrowserThread::PostTask(content::BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
base::Bind(&RegisterProtocolInIO, scheme));
|
||||
protocol_handlers_[scheme] = callback;
|
||||
BrowserThread::PostTask(BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
base::Bind(&Protocol::RegisterProtocolInIO,
|
||||
base::Unretained(this), scheme));
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::UnregisterProtocol(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string scheme;
|
||||
if (!FromV8Arguments(args, &scheme))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
if (AtomBrowserContext::Get()->url_request_context_getter() == NULL)
|
||||
return node::ThrowError(kEarlyUseProtocolError);
|
||||
|
||||
// Erase the handler from map.
|
||||
HandlersMap::iterator it(g_handlers.find(scheme));
|
||||
if (it == g_handlers.end())
|
||||
void Protocol::UnregisterProtocol(const std::string& scheme) {
|
||||
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
|
||||
if (it == protocol_handlers_.end())
|
||||
return node::ThrowError("The scheme has not been registered");
|
||||
g_handlers.erase(it);
|
||||
|
||||
content::BrowserThread::PostTask(content::BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
base::Bind(&UnregisterProtocolInIO, scheme));
|
||||
protocol_handlers_.erase(it);
|
||||
BrowserThread::PostTask(BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
base::Bind(&Protocol::UnregisterProtocolInIO,
|
||||
base::Unretained(this), scheme));
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::IsHandledProtocol(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string scheme;
|
||||
if (!FromV8Arguments(args, &scheme))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
args.GetReturnValue().Set(GetRequestJobFactory()->IsHandledProtocol(scheme));
|
||||
bool Protocol::IsHandledProtocol(const std::string& scheme) {
|
||||
return job_factory_->IsHandledProtocol(scheme);
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::InterceptProtocol(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string scheme;
|
||||
RefCountedV8Function callback;
|
||||
if (!FromV8Arguments(args, &scheme, &callback))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
void Protocol::InterceptProtocol(const std::string& scheme,
|
||||
const JsProtocolHandler& callback) {
|
||||
if (!job_factory_->HasProtocolHandler(scheme))
|
||||
return node::ThrowError("Scheme does not exist.");
|
||||
|
||||
if (!GetRequestJobFactory()->HasProtocolHandler(scheme))
|
||||
return node::ThrowError("Cannot intercept procotol");
|
||||
|
||||
if (ContainsKey(g_handlers, scheme))
|
||||
if (ContainsKey(protocol_handlers_, scheme))
|
||||
return node::ThrowError("Cannot intercept custom procotols");
|
||||
|
||||
if (AtomBrowserContext::Get()->url_request_context_getter() == NULL)
|
||||
return node::ThrowError(kEarlyUseProtocolError);
|
||||
|
||||
// Store the handler in a map.
|
||||
g_handlers[scheme] = callback;
|
||||
|
||||
content::BrowserThread::PostTask(content::BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
base::Bind(&InterceptProtocolInIO, scheme));
|
||||
protocol_handlers_[scheme] = callback;
|
||||
BrowserThread::PostTask(BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
base::Bind(&Protocol::InterceptProtocolInIO,
|
||||
base::Unretained(this), scheme));
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::UninterceptProtocol(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string scheme;
|
||||
if (!FromV8Arguments(args, &scheme))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
if (AtomBrowserContext::Get()->url_request_context_getter() == NULL)
|
||||
return node::ThrowError(kEarlyUseProtocolError);
|
||||
|
||||
// Erase the handler from map.
|
||||
HandlersMap::iterator it(g_handlers.find(scheme));
|
||||
if (it == g_handlers.end())
|
||||
void Protocol::UninterceptProtocol(const std::string& scheme) {
|
||||
ProtocolHandlersMap::iterator it(protocol_handlers_.find(scheme));
|
||||
if (it == protocol_handlers_.end())
|
||||
return node::ThrowError("The scheme has not been registered");
|
||||
g_handlers.erase(it);
|
||||
|
||||
content::BrowserThread::PostTask(content::BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
base::Bind(&UninterceptProtocolInIO,
|
||||
scheme));
|
||||
protocol_handlers_.erase(it);
|
||||
BrowserThread::PostTask(BrowserThread::IO,
|
||||
FROM_HERE,
|
||||
base::Bind(&Protocol::UninterceptProtocolInIO,
|
||||
base::Unretained(this), scheme));
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::RegisterProtocolInIO(const std::string& scheme) {
|
||||
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
||||
AtomURLRequestJobFactory* job_factory(GetRequestJobFactory());
|
||||
job_factory->SetProtocolHandler(scheme, new CustomProtocolHandler);
|
||||
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
||||
|
||||
content::BrowserThread::PostTask(content::BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&EmitEventInUI,
|
||||
"registered",
|
||||
scheme));
|
||||
job_factory_->SetProtocolHandler(scheme, new CustomProtocolHandler(this));
|
||||
BrowserThread::PostTask(BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&Protocol::EmitEventInUI,
|
||||
base::Unretained(this),
|
||||
"registered", scheme));
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::UnregisterProtocolInIO(const std::string& scheme) {
|
||||
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
||||
AtomURLRequestJobFactory* job_factory(GetRequestJobFactory());
|
||||
job_factory->SetProtocolHandler(scheme, NULL);
|
||||
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
||||
|
||||
content::BrowserThread::PostTask(content::BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&EmitEventInUI,
|
||||
"unregistered",
|
||||
scheme));
|
||||
job_factory_->SetProtocolHandler(scheme, NULL);
|
||||
BrowserThread::PostTask(BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&Protocol::EmitEventInUI,
|
||||
base::Unretained(this),
|
||||
"unregistered", scheme));
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::InterceptProtocolInIO(const std::string& scheme) {
|
||||
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
||||
AtomURLRequestJobFactory* job_factory(GetRequestJobFactory());
|
||||
ProtocolHandler* original_handler = job_factory->GetProtocolHandler(scheme);
|
||||
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
||||
|
||||
ProtocolHandler* original_handler = job_factory_->GetProtocolHandler(scheme);
|
||||
if (original_handler == NULL) {
|
||||
content::BrowserThread::PostTask(
|
||||
content::BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&EmitEventInUI,
|
||||
"error",
|
||||
"There is no protocol handler to intercpet"));
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
|
||||
&Protocol::EmitEventInUI,
|
||||
base::Unretained(this),
|
||||
"error", "There is no protocol handler to intercpet"));
|
||||
return;
|
||||
}
|
||||
|
||||
job_factory->ReplaceProtocol(scheme,
|
||||
new CustomProtocolHandler(original_handler));
|
||||
|
||||
content::BrowserThread::PostTask(content::BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&EmitEventInUI,
|
||||
"intercepted",
|
||||
scheme));
|
||||
job_factory_->ReplaceProtocol(
|
||||
scheme, new CustomProtocolHandler(this, original_handler));
|
||||
BrowserThread::PostTask(BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&Protocol::EmitEventInUI,
|
||||
base::Unretained(this),
|
||||
"intercepted", scheme));
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::UninterceptProtocolInIO(const std::string& scheme) {
|
||||
DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
|
||||
AtomURLRequestJobFactory* job_factory(GetRequestJobFactory());
|
||||
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
|
||||
|
||||
// Check if the protocol handler is intercepted.
|
||||
CustomProtocolHandler* handler = static_cast<CustomProtocolHandler*>(
|
||||
job_factory->GetProtocolHandler(scheme));
|
||||
job_factory_->GetProtocolHandler(scheme));
|
||||
if (handler->original_handler() == NULL) {
|
||||
content::BrowserThread::PostTask(
|
||||
content::BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&EmitEventInUI,
|
||||
"error",
|
||||
"The protocol is not intercpeted"));
|
||||
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind(
|
||||
&Protocol::EmitEventInUI,
|
||||
base::Unretained(this),
|
||||
"error", "The protocol is not intercpeted"));
|
||||
return;
|
||||
}
|
||||
|
||||
// Reset the protocol handler to the orignal one and delete current
|
||||
// protocol handler.
|
||||
// Reset the protocol handler to the orignal one and delete current protocol
|
||||
// handler.
|
||||
ProtocolHandler* original_handler = handler->ReleaseDefaultProtocolHandler();
|
||||
delete job_factory->ReplaceProtocol(scheme, original_handler);
|
||||
delete job_factory_->ReplaceProtocol(scheme, original_handler);
|
||||
BrowserThread::PostTask(BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&Protocol::EmitEventInUI,
|
||||
base::Unretained(this),
|
||||
"unintercepted", scheme));
|
||||
}
|
||||
|
||||
content::BrowserThread::PostTask(content::BrowserThread::UI,
|
||||
FROM_HERE,
|
||||
base::Bind(&EmitEventInUI,
|
||||
"unintercepted",
|
||||
scheme));
|
||||
void Protocol::EmitEventInUI(const std::string& event,
|
||||
const std::string& parameter) {
|
||||
base::ListValue args;
|
||||
args.AppendString(parameter);
|
||||
Emit(event, args);
|
||||
}
|
||||
|
||||
// static
|
||||
void Protocol::Initialize(v8::Handle<v8::Object> target) {
|
||||
// Remember the protocol object, used for emitting event later.
|
||||
g_protocol_object.reset(target);
|
||||
|
||||
// Make sure the job factory has been created.
|
||||
AtomBrowserContext::Get()->url_request_context_getter()->
|
||||
GetURLRequestContext();
|
||||
|
||||
NODE_SET_METHOD(target, "registerProtocol", RegisterProtocol);
|
||||
NODE_SET_METHOD(target, "unregisterProtocol", UnregisterProtocol);
|
||||
NODE_SET_METHOD(target, "isHandledProtocol", IsHandledProtocol);
|
||||
NODE_SET_METHOD(target, "interceptProtocol", InterceptProtocol);
|
||||
NODE_SET_METHOD(target, "uninterceptProtocol", UninterceptProtocol);
|
||||
mate::Handle<Protocol> Protocol::Create(v8::Isolate* isolate) {
|
||||
return CreateHandle(isolate, new Protocol);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_browser_protocol, atom::api::Protocol::Initialize)
|
||||
namespace {
|
||||
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
mate::Dictionary dict(isolate, exports);
|
||||
dict.Set("protocol", atom::api::Protocol::Create(isolate));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NODE_MODULE(atom_browser_protocol, Initialize)
|
||||
|
|
|
@ -8,36 +8,68 @@
|
|||
#include <string>
|
||||
#include <map>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
#include "atom/browser/api/event_emitter.h"
|
||||
#include "base/callback.h"
|
||||
#include "native_mate/handle.h"
|
||||
|
||||
namespace net {
|
||||
class URLRequest;
|
||||
}
|
||||
|
||||
namespace atom {
|
||||
|
||||
class AtomURLRequestJobFactory;
|
||||
|
||||
namespace api {
|
||||
|
||||
class Protocol {
|
||||
class Protocol : public mate::EventEmitter {
|
||||
public:
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
typedef base::Callback<v8::Handle<v8::Value>(const net::URLRequest*)>
|
||||
JsProtocolHandler;
|
||||
|
||||
static mate::Handle<Protocol> Create(v8::Isolate* isolate);
|
||||
|
||||
JsProtocolHandler GetProtocolHandler(const std::string& scheme);
|
||||
|
||||
protected:
|
||||
Protocol();
|
||||
|
||||
// mate::Wrappable implementations:
|
||||
virtual mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||
v8::Isolate* isolate);
|
||||
|
||||
private:
|
||||
static void RegisterProtocol(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void UnregisterProtocol(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsHandledProtocol(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
typedef std::map<std::string, JsProtocolHandler> ProtocolHandlersMap;
|
||||
|
||||
static void InterceptProtocol(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void UninterceptProtocol(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
// Register/unregister an networking |scheme| which would be handled by
|
||||
// |callback|.
|
||||
void RegisterProtocol(const std::string& scheme,
|
||||
const JsProtocolHandler& callback);
|
||||
void UnregisterProtocol(const std::string& scheme);
|
||||
|
||||
static void RegisterProtocolInIO(const std::string& scheme);
|
||||
static void UnregisterProtocolInIO(const std::string& scheme);
|
||||
// Returns whether a scheme has been registered.
|
||||
// FIXME Should accept a callback and be asynchronous so we do not have to use
|
||||
// locks.
|
||||
bool IsHandledProtocol(const std::string& scheme);
|
||||
|
||||
static void InterceptProtocolInIO(const std::string& scheme);
|
||||
static void UninterceptProtocolInIO(const std::string& scheme);
|
||||
// Intercept/unintercept an existing protocol handler.
|
||||
void InterceptProtocol(const std::string& scheme,
|
||||
const JsProtocolHandler& callback);
|
||||
void UninterceptProtocol(const std::string& scheme);
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(Protocol);
|
||||
// The networking related operations have to be done in IO thread.
|
||||
void RegisterProtocolInIO(const std::string& scheme);
|
||||
void UnregisterProtocolInIO(const std::string& scheme);
|
||||
void InterceptProtocolInIO(const std::string& scheme);
|
||||
void UninterceptProtocolInIO(const std::string& scheme);
|
||||
|
||||
// Do protocol.emit(event, parameter) under UI thread.
|
||||
void EmitEventInUI(const std::string& event, const std::string& parameter);
|
||||
|
||||
AtomURLRequestJobFactory* job_factory_;
|
||||
ProtocolHandlersMap protocol_handlers_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Protocol);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
|
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
|
@ -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<v8::Object> target);
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Handle<v8::ObjectTemplate> prototype);
|
||||
|
||||
NativeWindow* window() { return window_.get(); }
|
||||
NativeWindow* window() const { return window_.get(); }
|
||||
|
||||
protected:
|
||||
explicit Window(v8::Handle<v8::Object> 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<v8::Value>& args);
|
||||
static void Destroy(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
// APIs for NativeWindow.
|
||||
static void Close(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Focus(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsFocused(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Show(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Hide(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsVisible(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Maximize(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Unmaximize(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Minimize(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Restore(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetFullscreen(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsFullscreen(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetSize(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetSize(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetMinimumSize(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetMinimumSize(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetMaximumSize(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetMaximumSize(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetResizable(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsResizable(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetAlwaysOnTop(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsAlwaysOnTop(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Center(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetPosition(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetPosition(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetTitle(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetTitle(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void FlashFrame(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SetKiosk(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsKiosk(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void OpenDevTools(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void CloseDevTools(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsDevToolsOpened(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void InspectElement(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void DebugDevTools(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void FocusOnWebView(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void BlurWebView(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsWebViewFocused(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void CapturePage(const v8::FunctionCallbackInfo<v8::Value>& 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<int> GetSize();
|
||||
void SetMinimumSize(int width, int height);
|
||||
std::vector<int> GetMinimumSize();
|
||||
void SetMaximumSize(int width, int height);
|
||||
std::vector<int> GetMaximumSize();
|
||||
void SetResizable(bool resizable);
|
||||
bool IsResizable();
|
||||
void SetAlwaysOnTop(bool top);
|
||||
bool IsAlwaysOnTop();
|
||||
void Center();
|
||||
void SetPosition(int x, int y);
|
||||
std::vector<int> 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<v8::Value>& args);
|
||||
static void IsLoading(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsWaitingForResponse(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Stop(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetRoutingID(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetProcessID(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void IsCrashed(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
string16 GetPageTitle();
|
||||
bool IsLoading();
|
||||
bool IsWaitingForResponse();
|
||||
void Stop();
|
||||
int GetRoutingID();
|
||||
int GetProcessID();
|
||||
bool IsCrashed();
|
||||
|
||||
// APIs for devtools.
|
||||
static void GetDevTools(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void ExecuteJavaScriptInDevTools(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
mate::Dictionary GetDevTools(v8::Isolate* isolate);
|
||||
void ExecuteJavaScriptInDevTools(const std::string& code);
|
||||
|
||||
// APIs for NavigationController.
|
||||
static void LoadURL(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetURL(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void CanGoBack(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void CanGoForward(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void CanGoToOffset(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GoBack(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GoForward(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GoToIndex(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GoToOffset(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Reload(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void ReloadIgnoringCache(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
// Called when capturePage is done.
|
||||
void OnCapturePageDone(const RefCountedV8Function& callback,
|
||||
const std::vector<unsigned char>& 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<NativeWindow> window_;
|
||||
|
||||
|
|
|
@ -6,21 +6,20 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
#include "atom/browser/api/event.h"
|
||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
||||
#include "base/logging.h"
|
||||
#include "atom/browser/api/atom_api_event.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "base/values.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
AtomBrowserBindings::AtomBrowserBindings() {
|
||||
}
|
||||
|
||||
AtomBrowserBindings::~AtomBrowserBindings() {
|
||||
}
|
||||
|
||||
void AtomBrowserBindings::OnRendererMessage(int process_id,
|
||||
int routing_id,
|
||||
const string16& channel,
|
||||
|
@ -33,7 +32,7 @@ void AtomBrowserBindings::OnRendererMessage(int process_id,
|
|||
// process.emit(channel, 'message', process_id, routing_id);
|
||||
std::vector<v8::Handle<v8::Value>> arguments;
|
||||
arguments.reserve(3 + args.GetSize());
|
||||
arguments.push_back(ToV8Value(channel));
|
||||
arguments.push_back(mate::ConvertToV8(node_isolate, channel));
|
||||
const base::Value* value;
|
||||
if (args.Get(0, &value))
|
||||
arguments.push_back(converter->ToV8Value(value, global_env->context()));
|
||||
|
@ -65,17 +64,17 @@ void AtomBrowserBindings::OnRendererMessageSync(
|
|||
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);
|
||||
|
||||
// Create the event object.
|
||||
v8::Handle<v8::Object> event = api::Event::CreateV8Object();
|
||||
api::Event::Unwrap<api::Event>(event)->SetSenderAndMessage(sender, message);
|
||||
mate::Handle<mate::Event> event = mate::Event::Create(node_isolate);
|
||||
event->SetSenderAndMessage(sender, message);
|
||||
|
||||
// process.emit(channel, 'sync-message', event, process_id, routing_id);
|
||||
std::vector<v8::Handle<v8::Value>> arguments;
|
||||
arguments.reserve(3 + args.GetSize());
|
||||
arguments.push_back(ToV8Value(channel));
|
||||
arguments.push_back(mate::ConvertToV8(node_isolate, channel));
|
||||
const base::Value* value;
|
||||
if (args.Get(0, &value))
|
||||
arguments.push_back(converter->ToV8Value(value, global_env->context()));
|
||||
arguments.push_back(event);
|
||||
arguments.push_back(event.ToV8());
|
||||
arguments.push_back(v8::Integer::New(process_id));
|
||||
arguments.push_back(v8::Integer::New(routing_id));
|
||||
|
||||
|
|
|
@ -5,9 +5,8 @@
|
|||
#ifndef ATOM_BROWSER_API_ATOM_BROWSER_BINDINGS_H_
|
||||
#define ATOM_BROWSER_API_ATOM_BROWSER_BINDINGS_H_
|
||||
|
||||
#include "base/strings/string16.h"
|
||||
#include "atom/common/api/atom_bindings.h"
|
||||
#include "atom/common/v8/scoped_persistent.h"
|
||||
#include "base/strings/string16.h"
|
||||
|
||||
namespace base {
|
||||
class ListValue;
|
||||
|
@ -26,7 +25,6 @@ namespace atom {
|
|||
class AtomBrowserBindings : public AtomBindings {
|
||||
public:
|
||||
AtomBrowserBindings();
|
||||
virtual ~AtomBrowserBindings();
|
||||
|
||||
// Called when received a message from renderer.
|
||||
void OnRendererMessage(int process_id,
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
// 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.
|
||||
|
||||
#include "atom/browser/api/event.h"
|
||||
|
||||
#include "atom/common/api/api_messages.h"
|
||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||
#include "content/public/browser/web_contents.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
Event::Event()
|
||||
: sender_(NULL),
|
||||
message_(NULL),
|
||||
prevent_default_(false) {
|
||||
}
|
||||
|
||||
Event::~Event() {
|
||||
}
|
||||
|
||||
ObjectTemplateBuilder Event::GetObjectTemplateBuilder(v8::Isolate* isolate) {
|
||||
return ObjectTemplateBuilder(isolate)
|
||||
.SetMethod("preventDefault", &Event::PreventDefault)
|
||||
.SetMethod("sendReply", &Event::SendReply);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void Event::PreventDefault() {
|
||||
prevent_default_ = true;
|
||||
}
|
||||
|
||||
bool Event::SendReply(const string16& json) {
|
||||
if (message_ == NULL || sender_ == NULL)
|
||||
return false;
|
||||
|
||||
AtomViewHostMsg_Message_Sync::WriteReplyParams(message_, json);
|
||||
return sender_->Send(message_);
|
||||
}
|
||||
|
||||
// static
|
||||
Handle<Event> Event::Create(v8::Isolate* isolate) {
|
||||
return CreateHandle(isolate, new Event);
|
||||
}
|
||||
|
||||
} // namespace mate
|
|
@ -0,0 +1,57 @@
|
|||
// 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_BROWSER_API_EVENT_H_
|
||||
#define ATOM_BROWSER_API_EVENT_H_
|
||||
|
||||
#include "content/public/browser/web_contents_observer.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
#include "native_mate/handle.h"
|
||||
|
||||
namespace IPC {
|
||||
class Message;
|
||||
}
|
||||
|
||||
namespace mate {
|
||||
|
||||
class Event : public Wrappable,
|
||||
public content::WebContentsObserver {
|
||||
public:
|
||||
static Handle<Event> Create(v8::Isolate* isolate);
|
||||
|
||||
// Pass the sender and message to be replied.
|
||||
void SetSenderAndMessage(content::WebContents* sender, IPC::Message* message);
|
||||
|
||||
// event.PreventDefault().
|
||||
void PreventDefault();
|
||||
|
||||
// event.sendReply(json), used for replying synchronous message.
|
||||
bool SendReply(const string16& json);
|
||||
|
||||
// Whether event.preventDefault() is called.
|
||||
bool prevent_default() const { return prevent_default_; }
|
||||
|
||||
protected:
|
||||
Event();
|
||||
virtual ~Event();
|
||||
|
||||
// Wrappable implementations:
|
||||
virtual ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate);
|
||||
|
||||
// content::WebContentsObserver implementations:
|
||||
virtual void WebContentsDestroyed(content::WebContents*) OVERRIDE;
|
||||
|
||||
private:
|
||||
// Replyer for the synchronous messages.
|
||||
content::WebContents* sender_;
|
||||
IPC::Message* message_;
|
||||
|
||||
bool prevent_default_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Event);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // ATOM_BROWSER_API_EVENT_H_
|
|
@ -0,0 +1,53 @@
|
|||
// 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.
|
||||
|
||||
#include "atom/browser/api/event_emitter.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "atom/browser/api/event.h"
|
||||
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "base/values.h"
|
||||
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
EventEmitter::EventEmitter() {
|
||||
}
|
||||
|
||||
bool EventEmitter::Emit(const base::StringPiece& name) {
|
||||
return Emit(name, base::ListValue());
|
||||
}
|
||||
|
||||
bool EventEmitter::Emit(const base::StringPiece& name,
|
||||
const base::ListValue& args) {
|
||||
v8::Locker locker(node_isolate);
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
|
||||
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
|
||||
scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
|
||||
|
||||
mate::Handle<mate::Event> event = mate::Event::Create(node_isolate);
|
||||
|
||||
// v8_args = [name, event, args...];
|
||||
std::vector<v8::Handle<v8::Value>> v8_args;
|
||||
v8_args.reserve(args.GetSize() + 2);
|
||||
v8_args.push_back(mate::StringToV8(node_isolate, name));
|
||||
v8_args.push_back(event.ToV8());
|
||||
for (size_t i = 0; i < args.GetSize(); i++) {
|
||||
const base::Value* value(NULL);
|
||||
if (args.Get(i, &value))
|
||||
v8_args.push_back(converter->ToV8Value(value, context));
|
||||
}
|
||||
|
||||
// this.emit.apply(this, v8_args);
|
||||
node::MakeCallback(
|
||||
GetWrapper(node_isolate), "emit", v8_args.size(), &v8_args[0]);
|
||||
|
||||
return event->prevent_default();
|
||||
}
|
||||
|
||||
} // namespace mate
|
|
@ -0,0 +1,33 @@
|
|||
// 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_BROWSER_API_EVENT_EMITTER_H_
|
||||
#define ATOM_BROWSER_API_EVENT_EMITTER_H_
|
||||
|
||||
#include "native_mate/wrappable.h"
|
||||
|
||||
namespace base {
|
||||
class ListValue;
|
||||
}
|
||||
|
||||
namespace mate {
|
||||
|
||||
// Provide helperers to emit event in JavaScript.
|
||||
class EventEmitter : public Wrappable {
|
||||
protected:
|
||||
EventEmitter();
|
||||
|
||||
// this.emit(name);
|
||||
bool Emit(const base::StringPiece& name);
|
||||
|
||||
// this.emit(name, args...);
|
||||
bool Emit(const base::StringPiece& name, const base::ListValue& args);
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(EventEmitter);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // ATOM_BROWSER_API_EVENT_EMITTER_H_
|
|
@ -2,10 +2,8 @@ EventEmitter = require('events').EventEmitter
|
|||
|
||||
bindings = process.atomBinding 'app'
|
||||
|
||||
Application = bindings.Application
|
||||
Application::__proto__ = EventEmitter.prototype
|
||||
|
||||
app = new Application
|
||||
app = bindings.app
|
||||
app.__proto__ = EventEmitter.prototype
|
||||
|
||||
app.getHomeDir = ->
|
||||
process.env[if process.platform is 'win32' then 'USERPROFILE' else 'HOME']
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
AutoUpdater = process.atomBinding('auto_updater').AutoUpdater
|
||||
autoUpdater = process.atomBinding('auto_updater').autoUpdater
|
||||
EventEmitter = require('events').EventEmitter
|
||||
|
||||
AutoUpdater::__proto__ = EventEmitter.prototype
|
||||
autoUpdater.__proto__ = EventEmitter.prototype
|
||||
|
||||
autoUpdater = new AutoUpdater
|
||||
autoUpdater.on 'update-downloaded-raw', (args...) ->
|
||||
args[3] = new Date(args[3]) # releaseDate
|
||||
@emit 'update-downloaded', args..., => @quitAndInstall()
|
||||
|
|
|
@ -19,7 +19,7 @@ BrowserWindow::_init = ->
|
|||
|
||||
# Remove the window from weak map immediately when it's destroyed, since we
|
||||
# could be iterating windows before GC happended.
|
||||
@once 'destroyed', ->
|
||||
@once 'closed', ->
|
||||
BrowserWindow.windows.remove id if BrowserWindow.windows.has id
|
||||
|
||||
# Tell the rpc server that a render view has been deleted and we need to
|
||||
|
|
|
@ -30,7 +30,7 @@ module.exports =
|
|||
String(options.defaultPath),
|
||||
properties,
|
||||
window,
|
||||
callback
|
||||
(success, result) -> callback if success then result
|
||||
|
||||
showSaveDialog: (window, options, callback) ->
|
||||
unless window?.constructor is BrowserWindow
|
||||
|
@ -46,7 +46,7 @@ module.exports =
|
|||
binding.showSaveDialog String(options.title),
|
||||
String(options.defaultPath),
|
||||
window,
|
||||
callback
|
||||
(success, result) -> callback if success then result
|
||||
|
||||
showMessageBox: (window, options, callback) ->
|
||||
unless window?.constructor is BrowserWindow
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
bindings = process.atomBinding 'power_monitor'
|
||||
powerMonitor = process.atomBinding('power_monitor').powerMonitor
|
||||
EventEmitter = require('events').EventEmitter
|
||||
|
||||
PowerMonitor = bindings.PowerMonitor
|
||||
PowerMonitor::__proto__ = EventEmitter.prototype
|
||||
powerMonitor.__proto__ = EventEmitter.prototype
|
||||
|
||||
module.exports = new PowerMonitor
|
||||
module.exports = powerMonitor
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
protocol = process.atomBinding 'protocol'
|
||||
protocol = process.atomBinding('protocol').protocol
|
||||
EventEmitter = require('events').EventEmitter
|
||||
|
||||
protocol[key] = value for key, value of EventEmitter.prototype
|
||||
protocol.__proto__ = EventEmitter.prototype
|
||||
|
||||
protocol.RequestStringJob =
|
||||
class RequestStringJob
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
#include "ui/gfx/win/dpi.h"
|
||||
#endif
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
|
|
|
@ -99,6 +99,10 @@ NativeWindow::NativeWindow(content::WebContents* web_contents,
|
|||
}
|
||||
|
||||
NativeWindow::~NativeWindow() {
|
||||
// Make sure we have the OnRenderViewDeleted message sent even when the window
|
||||
// is destroyed directly.
|
||||
DestroyWebContents();
|
||||
|
||||
// It's possible that the windows gets destroyed before it's closed, in that
|
||||
// case we need to ensure the OnWindowClosed message is still notified.
|
||||
NotifyWindowClosed();
|
||||
|
@ -297,6 +301,21 @@ void NativeWindow::CapturePage(const gfx::Rect& rect,
|
|||
callback));
|
||||
}
|
||||
|
||||
void NativeWindow::DestroyWebContents() {
|
||||
if (!inspectable_web_contents_)
|
||||
return;
|
||||
|
||||
// The OnRenderViewDeleted is not called when the WebContents is destroyed
|
||||
// directly (e.g. when closing the window), so we make sure it's always
|
||||
// emitted to users by sending it before window is closed..
|
||||
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
|
||||
OnRenderViewDeleted(
|
||||
GetWebContents()->GetRenderProcessHost()->GetID(),
|
||||
GetWebContents()->GetRoutingID()));
|
||||
|
||||
inspectable_web_contents_.reset();
|
||||
}
|
||||
|
||||
void NativeWindow::CloseWebContents() {
|
||||
bool prevent_default = false;
|
||||
FOR_EACH_OBSERVER(NativeWindowObserver,
|
||||
|
@ -347,14 +366,6 @@ void NativeWindow::NotifyWindowClosed() {
|
|||
if (is_closed_)
|
||||
return;
|
||||
|
||||
// The OnRenderViewDeleted is not called when the WebContents is destroyed
|
||||
// directly (e.g. when closing the window), so we make sure it's always
|
||||
// emitted to users by sending it before window is closed..
|
||||
FOR_EACH_OBSERVER(NativeWindowObserver, observers_,
|
||||
OnRenderViewDeleted(
|
||||
GetWebContents()->GetRenderProcessHost()->GetID(),
|
||||
GetWebContents()->GetRoutingID()));
|
||||
|
||||
is_closed_ = true;
|
||||
FOR_EACH_OBSERVER(NativeWindowObserver, observers_, OnWindowClosed());
|
||||
|
||||
|
@ -365,10 +376,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
|
||||
|
@ -444,6 +451,9 @@ void NativeWindow::MoveContents(content::WebContents* source,
|
|||
}
|
||||
|
||||
void NativeWindow::CloseContents(content::WebContents* source) {
|
||||
// Destroy the WebContents before we close the window.
|
||||
DestroyWebContents();
|
||||
|
||||
// When the web contents is gone, close the window immediately, but the
|
||||
// memory will not be freed until you call delete.
|
||||
// In this way, it would be safe to manage windows via smart pointers. If you
|
||||
|
@ -451,8 +461,6 @@ void NativeWindow::CloseContents(content::WebContents* source) {
|
|||
// overriding the OnWindowClosed method in the observer.
|
||||
CloseImmediately();
|
||||
|
||||
NotifyWindowClosed();
|
||||
|
||||
// Do not sent "unresponsive" event after window is closed.
|
||||
window_unresposive_closure_.Cancel();
|
||||
}
|
||||
|
|
|
@ -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<NativeWindow> GetWeakPtr() {
|
||||
return weak_factory_.GetWeakPtr();
|
||||
}
|
||||
|
@ -170,6 +173,11 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
|
|||
int child_process_id);
|
||||
void OverrideWebkitPrefs(const GURL& url, WebPreferences* prefs);
|
||||
|
||||
// Public API used by platform-dependent delegates and observers to send UI
|
||||
// related notifications.
|
||||
void NotifyWindowClosed();
|
||||
void NotifyWindowBlur();
|
||||
|
||||
void AddObserver(NativeWindowObserver* obs) {
|
||||
observers_.AddObserver(obs);
|
||||
}
|
||||
|
@ -193,12 +201,6 @@ class NativeWindow : public brightray::DefaultWebContentsDelegate,
|
|||
inspectable_web_contents_.get());
|
||||
}
|
||||
|
||||
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<DraggableRegion>& regions) = 0;
|
||||
|
|
|
@ -121,10 +121,7 @@ NativeWindowGtk::NativeWindowGtk(content::WebContents* web_contents,
|
|||
}
|
||||
|
||||
NativeWindowGtk::~NativeWindowGtk() {
|
||||
ui::ActiveWindowWatcherX::RemoveObserver(this);
|
||||
|
||||
if (window_)
|
||||
gtk_widget_destroy(GTK_WIDGET(window_));
|
||||
CloseImmediately();
|
||||
}
|
||||
|
||||
void NativeWindowGtk::Close() {
|
||||
|
@ -132,6 +129,12 @@ void NativeWindowGtk::Close() {
|
|||
}
|
||||
|
||||
void NativeWindowGtk::CloseImmediately() {
|
||||
if (window_ == NULL)
|
||||
return;
|
||||
|
||||
NotifyWindowClosed();
|
||||
ui::ActiveWindowWatcherX::RemoveObserver(this);
|
||||
|
||||
gtk_widget_destroy(GTK_WIDGET(window_));
|
||||
window_ = NULL;
|
||||
}
|
||||
|
|
|
@ -57,8 +57,6 @@ class NativeWindowMac : public NativeWindow {
|
|||
virtual bool HasModalDialog() OVERRIDE;
|
||||
virtual gfx::NativeWindow GetNativeWindow() OVERRIDE;
|
||||
|
||||
void NotifyWindowBlur() { NativeWindow::NotifyWindowBlur(); }
|
||||
|
||||
// Returns true if |point| in local Cocoa coordinate system falls within
|
||||
// the draggable region.
|
||||
bool IsWithinDraggableRegion(NSPoint point) const;
|
||||
|
|
|
@ -65,6 +65,7 @@ static const CGFloat kAtomWindowCornerRadius = 4.0;
|
|||
}
|
||||
|
||||
- (void)windowWillClose:(NSNotification*)notification {
|
||||
shell_->NotifyWindowClosed();
|
||||
[self autorelease];
|
||||
}
|
||||
|
||||
|
|
|
@ -438,7 +438,7 @@ bool NativeWindowWin::AcceleratorPressed(
|
|||
}
|
||||
|
||||
void NativeWindowWin::DeleteDelegate() {
|
||||
// Do nothing, window is managed by users.
|
||||
NotifyWindowClosed();
|
||||
}
|
||||
|
||||
views::View* NativeWindowWin::GetInitiallyFocusedView() {
|
||||
|
|
|
@ -2,64 +2,40 @@
|
|||
// 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_clipboard.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "ui/base/clipboard/clipboard.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
// static
|
||||
void Clipboard::Has(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string format_string;
|
||||
if (!FromV8Arguments(args, &format_string))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
namespace {
|
||||
|
||||
bool Has(const std::string& format_string) {
|
||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
||||
|
||||
args.GetReturnValue().Set(
|
||||
clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD));
|
||||
return clipboard->IsFormatAvailable(format, ui::Clipboard::BUFFER_STANDARD);
|
||||
}
|
||||
|
||||
// static
|
||||
void Clipboard::Read(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string format_string;
|
||||
if (!FromV8Arguments(args, &format_string))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
std::string Read(const std::string& format_string) {
|
||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||
ui::Clipboard::FormatType format(ui::Clipboard::GetFormatType(format_string));
|
||||
|
||||
std::string data;
|
||||
clipboard->ReadData(format, &data);
|
||||
|
||||
args.GetReturnValue().Set(ToV8Value(data));
|
||||
return data;
|
||||
}
|
||||
|
||||
// static
|
||||
void Clipboard::ReadText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string ReadText() {
|
||||
ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
|
||||
|
||||
std::string data;
|
||||
clipboard->ReadAsciiText(ui::Clipboard::BUFFER_STANDARD, &data);
|
||||
|
||||
args.GetReturnValue().Set(ToV8Value(data));
|
||||
return data;
|
||||
}
|
||||
|
||||
// static
|
||||
void Clipboard::WriteText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string text;
|
||||
if (!FromV8Arguments(args, &text))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
void WriteText(const std::string text) {
|
||||
ui::Clipboard::ObjectMap object_map;
|
||||
object_map[ui::Clipboard::CBF_TEXT].push_back(
|
||||
std::vector<char>(text.begin(), text.end()));
|
||||
|
@ -68,22 +44,19 @@ void Clipboard::WriteText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|||
clipboard->WriteObjects(ui::Clipboard::BUFFER_STANDARD, object_map);
|
||||
}
|
||||
|
||||
// static
|
||||
void Clipboard::Clear(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
void Clear() {
|
||||
ui::Clipboard::GetForCurrentThread()->Clear(ui::Clipboard::BUFFER_STANDARD);
|
||||
}
|
||||
|
||||
// static
|
||||
void Clipboard::Initialize(v8::Handle<v8::Object> target) {
|
||||
NODE_SET_METHOD(target, "has", Has);
|
||||
NODE_SET_METHOD(target, "read", Read);
|
||||
NODE_SET_METHOD(target, "readText", ReadText);
|
||||
NODE_SET_METHOD(target, "writeText", WriteText);
|
||||
NODE_SET_METHOD(target, "clear", Clear);
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||
dict.SetMethod("has", &Has);
|
||||
dict.SetMethod("read", &Read);
|
||||
dict.SetMethod("readText", &ReadText);
|
||||
dict.SetMethod("writeText", &WriteText);
|
||||
dict.SetMethod("clear", &Clear);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
} // namespace
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_common_clipboard, atom::api::Clipboard::Initialize)
|
||||
NODE_MODULE(atom_common_clipboard, Initialize)
|
||||
|
|
|
@ -1,33 +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_CLIPBOARD_H_
|
||||
#define ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class Clipboard {
|
||||
public:
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
|
||||
private:
|
||||
static void Has(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Read(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void ReadText(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void WriteText(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Clear(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(Clipboard);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_API_ATOM_API_CLIPBOARD_H_
|
|
@ -2,40 +2,47 @@
|
|||
// 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_crash_reporter.h"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
#include "atom/common/crash_reporter/crash_reporter.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "base/bind.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
namespace mate {
|
||||
|
||||
namespace api {
|
||||
template<>
|
||||
struct Converter<std::map<std::string, std::string> > {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
std::map<std::string, std::string>* out) {
|
||||
if (!val->IsObject())
|
||||
return false;
|
||||
|
||||
// static
|
||||
void CrashReporter::Start(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
std::string product_name, company_name, submit_url;
|
||||
bool auto_submit, skip_system;
|
||||
std::map<std::string, std::string> dict;
|
||||
if (!FromV8Arguments(args, &product_name, &company_name, &submit_url,
|
||||
&auto_submit, &skip_system, &dict))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
v8::Handle<v8::Object> dict = val->ToObject();
|
||||
v8::Handle<v8::Array> keys = dict->GetOwnPropertyNames();
|
||||
for (uint32_t i = 0; i < keys->Length(); ++i) {
|
||||
v8::Handle<v8::Value> key = keys->Get(i);
|
||||
(*out)[V8ToString(key)] = V8ToString(dict->Get(key));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
crash_reporter::CrashReporter::GetInstance()->Start(
|
||||
product_name, company_name, submit_url, auto_submit, skip_system, dict);
|
||||
} // namespace mate
|
||||
|
||||
namespace {
|
||||
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
using crash_reporter::CrashReporter;
|
||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||
dict.SetMethod("start",
|
||||
base::Bind(&CrashReporter::Start,
|
||||
base::Unretained(CrashReporter::GetInstance())));
|
||||
}
|
||||
|
||||
// static
|
||||
void CrashReporter::Initialize(v8::Handle<v8::Object> target) {
|
||||
NODE_SET_METHOD(target, "start", Start);
|
||||
}
|
||||
} // namespace
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_common_crash_reporter, atom::api::CrashReporter::Initialize)
|
||||
NODE_MODULE(atom_common_crash_reporter, Initialize)
|
||||
|
|
|
@ -1,29 +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_CRASH_REPORTER_H_
|
||||
#define ATOM_COMMON_API_ATOM_API_CRASH_REPORTER_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class CrashReporter {
|
||||
public:
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
|
||||
private:
|
||||
static void Start(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(CrashReporter);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_API_ATOM_API_CRASH_REPORTER_H_
|
|
@ -1,81 +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 <vector>
|
||||
|
||||
#include "atom/browser/api/atom_api_event.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "base/logging.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
EventEmitter::EventEmitter(v8::Handle<v8::Object> wrapper) {
|
||||
Wrap(wrapper);
|
||||
}
|
||||
|
||||
EventEmitter::~EventEmitter() {
|
||||
// Use Locker in browser process.
|
||||
scoped_ptr<v8::Locker> locker;
|
||||
if (node::g_standalone_mode)
|
||||
locker.reset(new v8::Locker(node_isolate));
|
||||
|
||||
// Clear the aligned pointer, it should have been done by ObjectWrap but
|
||||
// somehow node v0.11.x changed this behaviour.
|
||||
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) {
|
||||
// Use Locker in browser process.
|
||||
scoped_ptr<v8::Locker> locker;
|
||||
if (node::g_standalone_mode)
|
||||
locker.reset(new v8::Locker(node_isolate));
|
||||
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
|
||||
v8::Handle<v8::Context> context = v8::Context::GetCurrent();
|
||||
scoped_ptr<V8ValueConverter> converter(new V8ValueConverter);
|
||||
|
||||
v8::Handle<v8::Object> v8_event = Event::CreateV8Object();
|
||||
Event* event = Event::Unwrap<Event>(v8_event);
|
||||
|
||||
// Generate arguments for calling handle.emit.
|
||||
std::vector<v8::Handle<v8::Value>> 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
|
|
@ -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 <string>
|
||||
|
||||
#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<v8::Object> wrapper);
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(EventEmitter);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_API_ATOM_API_EVENT_EMITTER_H_
|
|
@ -1,5 +1,4 @@
|
|||
// 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.
|
||||
|
||||
|
@ -7,9 +6,11 @@
|
|||
|
||||
#include <algorithm>
|
||||
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "base/logging.h"
|
||||
#include "native_mate/constructor.h"
|
||||
#include "native_mate/object_template_builder.h"
|
||||
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
|
@ -22,11 +23,38 @@ IDWeakMap::IDWeakMap()
|
|||
IDWeakMap::~IDWeakMap() {
|
||||
}
|
||||
|
||||
bool IDWeakMap::Has(int key) const {
|
||||
int32_t IDWeakMap::Add(v8::Isolate* isolate, v8::Handle<v8::Object> object) {
|
||||
int32_t key = GetNextID();
|
||||
object->SetHiddenValue(mate::StringToV8(isolate, "IDWeakMapKey"),
|
||||
mate::Converter<int32_t>::ToV8(isolate, key));
|
||||
|
||||
map_[key] = new mate::RefCountedPersistent<v8::Object>(object);
|
||||
map_[key]->MakeWeak(this, WeakCallback);
|
||||
return key;
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> IDWeakMap::Get(int32_t key) {
|
||||
if (!Has(key)) {
|
||||
node::ThrowError("Invalid key");
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
return map_[key]->NewHandle();
|
||||
}
|
||||
|
||||
bool IDWeakMap::Has(int32_t key) const {
|
||||
return map_.find(key) != map_.end();
|
||||
}
|
||||
|
||||
void IDWeakMap::Erase(int key) {
|
||||
std::vector<int32_t> IDWeakMap::Keys() const {
|
||||
std::vector<int32_t> keys;
|
||||
keys.reserve(map_.size());
|
||||
for (auto it = map_.begin(); it != map_.end(); ++it)
|
||||
keys.push_back(it->first);
|
||||
return keys;
|
||||
}
|
||||
|
||||
void IDWeakMap::Remove(int32_t key) {
|
||||
if (Has(key))
|
||||
map_.erase(key);
|
||||
else
|
||||
|
@ -37,106 +65,46 @@ int IDWeakMap::GetNextID() {
|
|||
return ++next_id_;
|
||||
}
|
||||
|
||||
// static
|
||||
void IDWeakMap::BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Handle<v8::ObjectTemplate> prototype) {
|
||||
mate::ObjectTemplateBuilder(isolate, prototype)
|
||||
.SetMethod("add", &IDWeakMap::Add)
|
||||
.SetMethod("get", &IDWeakMap::Get)
|
||||
.SetMethod("has", &IDWeakMap::Has)
|
||||
.SetMethod("keys", &IDWeakMap::Keys)
|
||||
.SetMethod("remove", &IDWeakMap::Remove);
|
||||
}
|
||||
|
||||
// static
|
||||
void IDWeakMap::WeakCallback(v8::Isolate* isolate,
|
||||
v8::Persistent<v8::Object>* value,
|
||||
IDWeakMap* self) {
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Local<v8::Object> local = v8::Local<v8::Object>::New(isolate, *value);
|
||||
self->Erase(
|
||||
FromV8Value(local->GetHiddenValue(v8::String::New("IDWeakMapKey"))));
|
||||
}
|
||||
|
||||
// static
|
||||
void IDWeakMap::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
(new IDWeakMap)->Wrap(args.This());
|
||||
}
|
||||
|
||||
// static
|
||||
void IDWeakMap::Add(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
if (!args[0]->IsObject())
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
||||
|
||||
int key = self->GetNextID();
|
||||
v8::Local<v8::Object> v8_value = args[0]->ToObject();
|
||||
v8_value->SetHiddenValue(v8::String::New("IDWeakMapKey"), ToV8Value(key));
|
||||
|
||||
self->map_[key] = new RefCountedPersistent<v8::Object>(v8_value);
|
||||
self->map_[key]->MakeWeak(self, WeakCallback);
|
||||
|
||||
args.GetReturnValue().Set(key);
|
||||
}
|
||||
|
||||
// static
|
||||
void IDWeakMap::Get(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
int key;
|
||||
if (!FromV8Arguments(args, &key))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
||||
if (!self->Has(key))
|
||||
return node::ThrowError("Invalid key");
|
||||
|
||||
args.GetReturnValue().Set(self->map_[key]->NewHandle());
|
||||
}
|
||||
|
||||
// static
|
||||
void IDWeakMap::Has(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
int key;
|
||||
if (!FromV8Arguments(args, &key))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
||||
args.GetReturnValue().Set(self->Has(key));
|
||||
}
|
||||
|
||||
// static
|
||||
void IDWeakMap::Keys(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
||||
|
||||
v8::Local<v8::Array> keys = v8::Array::New(self->map_.size());
|
||||
|
||||
int i = 0;
|
||||
for (auto el = self->map_.begin(); el != self->map_.end(); ++el) {
|
||||
keys->Set(i, ToV8Value(el->first));
|
||||
++i;
|
||||
}
|
||||
|
||||
args.GetReturnValue().Set(keys);
|
||||
}
|
||||
|
||||
// static
|
||||
void IDWeakMap::Remove(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
int key;
|
||||
if (!FromV8Arguments(args, &key))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
IDWeakMap* self = Unwrap<IDWeakMap>(args.This());
|
||||
if (!self->Has(key))
|
||||
return node::ThrowError("Invalid key");
|
||||
|
||||
self->Erase(key);
|
||||
}
|
||||
|
||||
// static
|
||||
void IDWeakMap::Initialize(v8::Handle<v8::Object> target) {
|
||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
t->SetClassName(v8::String::NewSymbol("IDWeakMap"));
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "add", Add);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "get", Get);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "has", Has);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "keys", Keys);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "remove", Remove);
|
||||
|
||||
target->Set(v8::String::NewSymbol("IDWeakMap"), t->GetFunction());
|
||||
v8::Local<v8::Object> object = v8::Local<v8::Object>::New(isolate, *value);
|
||||
int32_t key = object->GetHiddenValue(
|
||||
mate::StringToV8(isolate, "IDWeakMapKey"))->Int32Value();
|
||||
self->Remove(key);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_common_id_weak_map, atom::api::IDWeakMap::Initialize)
|
||||
|
||||
namespace {
|
||||
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
using atom::api::IDWeakMap;
|
||||
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
v8::Local<v8::Function> constructor = mate::CreateConstructor<IDWeakMap>(
|
||||
isolate,
|
||||
"IDWeakMap",
|
||||
base::Bind(&mate::NewOperatorFactory<IDWeakMap>));
|
||||
exports->Set(mate::StringToV8(isolate, "IDWeakMap"), constructor);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NODE_MODULE(atom_common_id_weak_map, Initialize)
|
||||
|
|
|
@ -7,41 +7,43 @@
|
|||
#define ATOM_COMMON_API_ATOM_API_ID_WEAK_MAP_H_
|
||||
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "atom/common/v8/scoped_persistent.h"
|
||||
#include "base/basictypes.h"
|
||||
#include "vendor/node/src/node_object_wrap.h"
|
||||
#include "native_mate/scoped_persistent.h"
|
||||
#include "native_mate/wrappable.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
// Like ES6's WeakMap, but the key is Integer and the value is Weak Pointer.
|
||||
class IDWeakMap : public node::ObjectWrap {
|
||||
class IDWeakMap : public mate::Wrappable {
|
||||
public:
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
IDWeakMap();
|
||||
|
||||
static void BuildPrototype(v8::Isolate* isolate,
|
||||
v8::Handle<v8::ObjectTemplate> prototype);
|
||||
|
||||
private:
|
||||
IDWeakMap();
|
||||
virtual ~IDWeakMap();
|
||||
|
||||
bool Has(int key) const;
|
||||
void Erase(int key);
|
||||
int32_t Add(v8::Isolate* isolate, v8::Handle<v8::Object> object);
|
||||
v8::Handle<v8::Value> Get(int32_t key);
|
||||
bool Has(int32_t key) const;
|
||||
std::vector<int32_t> Keys() const;
|
||||
void Remove(int32_t key);
|
||||
int GetNextID();
|
||||
|
||||
static void WeakCallback(v8::Isolate* isolate,
|
||||
v8::Persistent<v8::Object>* value,
|
||||
IDWeakMap* self);
|
||||
|
||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Add(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Get(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Has(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Keys(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Remove(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
int32_t next_id_;
|
||||
|
||||
int next_id_;
|
||||
std::map<int, RefCountedV8Object> map_;
|
||||
typedef scoped_refptr<mate::RefCountedPersistent<v8::Object> >
|
||||
RefCountedV8Object;
|
||||
std::map<int32_t, RefCountedV8Object> map_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(IDWeakMap);
|
||||
};
|
||||
|
|
|
@ -2,12 +2,10 @@
|
|||
// 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_screen.h"
|
||||
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "ui/gfx/screen.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
#if defined(TOOLKIT_GTK)
|
||||
#include "base/command_line.h"
|
||||
|
@ -16,14 +14,7 @@
|
|||
#include "ui/gfx/gtk_util.h"
|
||||
#endif
|
||||
|
||||
#define UNWRAP_SCREEN_AND_CHECK \
|
||||
Screen* self = ObjectWrap::Unwrap<Screen>(args.This()); \
|
||||
if (self == NULL) \
|
||||
return node::ThrowError("Screen is already destroyed")
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
namespace mate {
|
||||
|
||||
namespace {
|
||||
|
||||
|
@ -42,70 +33,74 @@ gfx::Display AdaptToWindowManager(const gfx::Display& display) {
|
|||
return changed;
|
||||
}
|
||||
|
||||
v8::Handle<v8::Object> DisplayToV8Value(const gfx::Display& raw) {
|
||||
gfx::Display display(AdaptToWindowManager(raw));
|
||||
v8::Handle<v8::Object> obj = v8::Object::New();
|
||||
obj->Set(ToV8Value("bounds"), ToV8Value(display.bounds()));
|
||||
obj->Set(ToV8Value("workArea"), ToV8Value(display.work_area()));
|
||||
obj->Set(ToV8Value("size"), ToV8Value(display.size()));
|
||||
obj->Set(ToV8Value("workAreaSize"), ToV8Value(display.work_area_size()));
|
||||
obj->Set(ToV8Value("scaleFactor"), ToV8Value(display.device_scale_factor()));
|
||||
return obj;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Screen::Screen(v8::Handle<v8::Object> wrapper)
|
||||
: EventEmitter(wrapper),
|
||||
screen_(gfx::Screen::GetNativeScreen()) {
|
||||
}
|
||||
template<>
|
||||
struct Converter<gfx::Point> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const gfx::Point& val) {
|
||||
return mate::ObjectTemplateBuilder(isolate).SetValue("x", val.x())
|
||||
.SetValue("y", val.y())
|
||||
.Build()->NewInstance();
|
||||
}
|
||||
};
|
||||
|
||||
Screen::~Screen() {
|
||||
}
|
||||
template<>
|
||||
struct Converter<gfx::Size> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const gfx::Size& val) {
|
||||
return mate::ObjectTemplateBuilder(isolate).SetValue("width", val.width())
|
||||
.SetValue("height", val.height())
|
||||
.Build()->NewInstance();
|
||||
}
|
||||
};
|
||||
|
||||
// static
|
||||
void Screen::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
v8::HandleScope scope(args.GetIsolate());
|
||||
template<>
|
||||
struct Converter<gfx::Rect> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const gfx::Rect& val) {
|
||||
return mate::ObjectTemplateBuilder(isolate).SetValue("x", val.x())
|
||||
.SetValue("y", val.y())
|
||||
.SetValue("width", val.width())
|
||||
.SetValue("height", val.height())
|
||||
.Build()->NewInstance();
|
||||
}
|
||||
};
|
||||
|
||||
if (!args.IsConstructCall())
|
||||
return node::ThrowError("Require constructor call");
|
||||
template<>
|
||||
struct Converter<gfx::Display> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const gfx::Display& val) {
|
||||
gfx::Display display(AdaptToWindowManager(val));
|
||||
return mate::ObjectTemplateBuilder(isolate)
|
||||
.SetValue("bounds", display.bounds())
|
||||
.SetValue("workArea", display.work_area())
|
||||
.SetValue("size", display.size())
|
||||
.SetValue("workAreaSize", display.work_area_size())
|
||||
.SetValue("scaleFactor", display.device_scale_factor())
|
||||
.Build()->NewInstance();
|
||||
}
|
||||
};
|
||||
|
||||
new Screen(args.This());
|
||||
}
|
||||
} // namespace mate
|
||||
|
||||
// static
|
||||
void Screen::GetCursorScreenPoint(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_SCREEN_AND_CHECK;
|
||||
args.GetReturnValue().Set(ToV8Value(self->screen_->GetCursorScreenPoint()));
|
||||
}
|
||||
namespace {
|
||||
|
||||
// static
|
||||
void Screen::GetPrimaryDisplay(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
UNWRAP_SCREEN_AND_CHECK;
|
||||
gfx::Display display = self->screen_->GetPrimaryDisplay();
|
||||
args.GetReturnValue().Set(DisplayToV8Value(display));
|
||||
}
|
||||
|
||||
// static
|
||||
void Screen::Initialize(v8::Handle<v8::Object> target) {
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
#if defined(TOOLKIT_GTK)
|
||||
gfx::GdkInitFromCommandLine(*CommandLine::ForCurrentProcess());
|
||||
#endif
|
||||
|
||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
|
||||
t->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
t->SetClassName(v8::String::NewSymbol("Screen"));
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "getCursorScreenPoint", GetCursorScreenPoint);
|
||||
NODE_SET_PROTOTYPE_METHOD(t, "getPrimaryDisplay", GetPrimaryDisplay);
|
||||
|
||||
target->Set(v8::String::NewSymbol("Screen"), t->GetFunction());
|
||||
gfx::Screen* screen = gfx::Screen::GetNativeScreen();
|
||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||
dict.SetMethod("getCursorScreenPoint",
|
||||
base::Bind(&gfx::Screen::GetCursorScreenPoint,
|
||||
base::Unretained(screen)));
|
||||
dict.SetMethod("getPrimaryDisplay",
|
||||
base::Bind(&gfx::Screen::GetPrimaryDisplay,
|
||||
base::Unretained(screen)));
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
} // namespace
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_common_screen, atom::api::Screen::Initialize)
|
||||
NODE_MODULE(atom_common_screen, Initialize)
|
||||
|
|
|
@ -1,44 +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_SCREEN_H_
|
||||
#define ATOM_COMMON_API_ATOM_API_SCREEN_H_
|
||||
|
||||
#include "atom/common/api/atom_api_event_emitter.h"
|
||||
|
||||
namespace gfx {
|
||||
class Screen;
|
||||
}
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class Screen : public EventEmitter {
|
||||
public:
|
||||
virtual ~Screen();
|
||||
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
|
||||
protected:
|
||||
explicit Screen(v8::Handle<v8::Object> wrapper);
|
||||
|
||||
private:
|
||||
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
static void GetCursorScreenPoint(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetPrimaryDisplay(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
gfx::Screen* screen_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(Screen);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_API_ATOM_API_SCREEN_H_
|
|
@ -2,73 +2,26 @@
|
|||
// 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_shell.h"
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "atom/common/platform_util.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "url/gurl.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 "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
namespace {
|
||||
|
||||
namespace api {
|
||||
|
||||
// static
|
||||
void Shell::ShowItemInFolder(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
base::FilePath file_path;
|
||||
if (!FromV8Arguments(args, &file_path))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
platform_util::ShowItemInFolder(file_path);
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||
dict.SetMethod("showItemInFolder", &platform_util::ShowItemInFolder);
|
||||
dict.SetMethod("openItem", &platform_util::OpenItem);
|
||||
dict.SetMethod("openExternal", &platform_util::OpenExternal);
|
||||
dict.SetMethod("moveItemToTrash", &platform_util::MoveItemToTrash);
|
||||
dict.SetMethod("beep", &platform_util::Beep);
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::OpenItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
base::FilePath file_path;
|
||||
if (!FromV8Arguments(args, &file_path))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
} // namespace
|
||||
|
||||
platform_util::OpenItem(file_path);
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::OpenExternal(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
GURL url;
|
||||
if (!FromV8Arguments(args, &url))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
platform_util::OpenExternal(url);
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::MoveItemToTrash(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
base::FilePath file_path;
|
||||
if (!FromV8Arguments(args, &file_path))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
platform_util::MoveItemToTrash(file_path);
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::Beep(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
platform_util::Beep();
|
||||
}
|
||||
|
||||
// static
|
||||
void Shell::Initialize(v8::Handle<v8::Object> target) {
|
||||
NODE_SET_METHOD(target, "showItemInFolder", ShowItemInFolder);
|
||||
NODE_SET_METHOD(target, "openItem", OpenItem);
|
||||
NODE_SET_METHOD(target, "openExternal", OpenExternal);
|
||||
NODE_SET_METHOD(target, "moveItemToTrash", MoveItemToTrash);
|
||||
NODE_SET_METHOD(target, "beep", Beep);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_common_shell, atom::api::Shell::Initialize)
|
||||
NODE_MODULE(atom_common_shell, Initialize)
|
||||
|
|
|
@ -1,33 +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_SHELL_H_
|
||||
#define ATOM_COMMON_API_ATOM_API_SHELL_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class Shell {
|
||||
public:
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
|
||||
private:
|
||||
static void ShowItemInFolder(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void OpenItem(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void OpenExternal(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void MoveItemToTrash(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Beep(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(Shell);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_API_ATOM_API_SHELL_H_
|
|
@ -3,59 +3,54 @@
|
|||
// found in the LICENSE file.
|
||||
|
||||
#include "atom/common/api/object_life_monitor.h"
|
||||
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "v8/include/v8-profiler.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace {
|
||||
|
||||
void CreateObjectWithName(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
v8::Handle<v8::Object> CreateObjectWithName(v8::Handle<v8::String> name) {
|
||||
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New();
|
||||
t->SetClassName(args[0]->ToString());
|
||||
args.GetReturnValue().Set(t->GetFunction()->NewInstance());
|
||||
t->SetClassName(name);
|
||||
return t->GetFunction()->NewInstance();
|
||||
}
|
||||
|
||||
void GetHiddenValue(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
args.GetReturnValue().Set(
|
||||
args[0]->ToObject()->GetHiddenValue(args[1]->ToString()));
|
||||
v8::Handle<v8::Value> GetHiddenValue(v8::Handle<v8::Object> object,
|
||||
v8::Handle<v8::String> key) {
|
||||
return object->GetHiddenValue(key);
|
||||
}
|
||||
|
||||
void SetHiddenValue(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
args[0]->ToObject()->SetHiddenValue(args[1]->ToString(), args[2]);
|
||||
void SetHiddenValue(v8::Handle<v8::Object> object,
|
||||
v8::Handle<v8::String> key,
|
||||
v8::Handle<v8::Value> value) {
|
||||
object->SetHiddenValue(key, value);
|
||||
}
|
||||
|
||||
void GetObjectHash(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
args.GetReturnValue().Set(args[0]->ToObject()->GetIdentityHash());
|
||||
int32_t GetObjectHash(v8::Handle<v8::Object> object) {
|
||||
return object->GetIdentityHash();
|
||||
}
|
||||
|
||||
void SetDestructor(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
ObjectLifeMonitor::BindTo(args[0]->ToObject(), args[1]);
|
||||
void SetDestructor(v8::Handle<v8::Object> object,
|
||||
v8::Handle<v8::Function> callback) {
|
||||
atom::ObjectLifeMonitor::BindTo(object, callback);
|
||||
}
|
||||
|
||||
void TakeHeapSnapshot(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
void TakeHeapSnapshot() {
|
||||
node::node_isolate->GetHeapProfiler()->TakeHeapSnapshot(
|
||||
v8::String::New("test"));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void InitializeV8Util(v8::Handle<v8::Object> target) {
|
||||
NODE_SET_METHOD(target, "createObjectWithName", CreateObjectWithName);
|
||||
NODE_SET_METHOD(target, "getHiddenValue", GetHiddenValue);
|
||||
NODE_SET_METHOD(target, "setHiddenValue", SetHiddenValue);
|
||||
NODE_SET_METHOD(target, "getObjectHash", GetObjectHash);
|
||||
NODE_SET_METHOD(target, "setDestructor", SetDestructor);
|
||||
NODE_SET_METHOD(target, "takeHeapSnapshot", TakeHeapSnapshot);
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||
dict.SetMethod("createObjectWithName", &CreateObjectWithName);
|
||||
dict.SetMethod("getHiddenValue", &GetHiddenValue);
|
||||
dict.SetMethod("setHiddenValue", &SetHiddenValue);
|
||||
dict.SetMethod("getObjectHash", &GetObjectHash);
|
||||
dict.SetMethod("setDestructor", &SetDestructor);
|
||||
dict.SetMethod("takeHeapSnapshot", &TakeHeapSnapshot);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
} // namespace
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_common_v8_util, atom::api::InitializeV8Util)
|
||||
NODE_MODULE(atom_common_v8_util, Initialize)
|
||||
|
|
|
@ -7,25 +7,29 @@
|
|||
#include <string>
|
||||
|
||||
#include "atom/common/atom_version.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "atom/common/native_mate_converters/function_converter.h"
|
||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||
#include "base/callback.h"
|
||||
#include "base/logging.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
// Defined in atom_extensions.cc.
|
||||
node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser);
|
||||
|
||||
namespace {
|
||||
|
||||
static int kMaxCallStackSize = 200; // Same with WebKit.
|
||||
|
||||
// Async handle to wake up uv loop.
|
||||
static uv_async_t g_next_tick_uv_handle;
|
||||
uv_async_t g_next_tick_uv_handle;
|
||||
|
||||
// Async handle to execute the stored v8 callback.
|
||||
static uv_async_t g_callback_uv_handle;
|
||||
uv_async_t g_callback_uv_handle;
|
||||
|
||||
// Stored v8 callback, to be called by the async handler.
|
||||
RefCountedV8Function g_v8_callback;
|
||||
base::Closure g_v8_callback;
|
||||
|
||||
// Dummy class type that used for crashing the program.
|
||||
struct DummyClass { bool crash; };
|
||||
|
@ -50,9 +54,7 @@ void UvCallNextTick(uv_async_t* handle, int status) {
|
|||
|
||||
// Async handler to execute the stored v8 callback.
|
||||
void UvOnCallback(uv_async_t* handle, int status) {
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
v8::Handle<v8::Object> global = v8::Context::GetCurrent()->Global();
|
||||
g_v8_callback->NewHandle()->Call(global, 0, NULL);
|
||||
g_v8_callback.Run();
|
||||
}
|
||||
|
||||
// Called when there is a fatal error in V8, we just crash the process here so
|
||||
|
@ -62,50 +64,15 @@ void FatalErrorCallback(const char* location, const char* message) {
|
|||
static_cast<DummyClass*>(NULL)->crash = true;
|
||||
}
|
||||
|
||||
v8::Handle<v8::Object> DumpStackFrame(v8::Handle<v8::StackFrame> stack_frame) {
|
||||
v8::Local<v8::Object> result = v8::Object::New();
|
||||
result->Set(ToV8Value("line"), ToV8Value(stack_frame->GetLineNumber()));
|
||||
result->Set(ToV8Value("column"), ToV8Value(stack_frame->GetColumn()));
|
||||
|
||||
v8::Handle<v8::String> script = stack_frame->GetScriptName();
|
||||
if (!script.IsEmpty())
|
||||
result->Set(ToV8Value("script"), script);
|
||||
|
||||
v8::Handle<v8::String> function = stack_frame->GetScriptNameOrSourceURL();
|
||||
if (!function.IsEmpty())
|
||||
result->Set(ToV8Value("function"), function);
|
||||
return result;
|
||||
v8::Handle<v8::Value> DumpStackFrame(v8::Isolate* isolate,
|
||||
v8::Handle<v8::StackFrame> stack_frame) {
|
||||
mate::Dictionary frame_dict(isolate);
|
||||
frame_dict.Set("line", stack_frame->GetLineNumber());
|
||||
frame_dict.Set("column", stack_frame->GetColumn());
|
||||
return mate::ConvertToV8(isolate, frame_dict);;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// Defined in atom_extensions.cc.
|
||||
node::node_module_struct* GetBuiltinModule(const char *name, bool is_browser);
|
||||
|
||||
AtomBindings::AtomBindings() {
|
||||
uv_async_init(uv_default_loop(), &g_next_tick_uv_handle, UvCallNextTick);
|
||||
uv_async_init(uv_default_loop(), &g_callback_uv_handle, UvOnCallback);
|
||||
v8::V8::SetFatalErrorHandler(FatalErrorCallback);
|
||||
}
|
||||
|
||||
AtomBindings::~AtomBindings() {
|
||||
}
|
||||
|
||||
void AtomBindings::BindTo(v8::Handle<v8::Object> process) {
|
||||
NODE_SET_METHOD(process, "atomBinding", Binding);
|
||||
NODE_SET_METHOD(process, "crash", Crash);
|
||||
NODE_SET_METHOD(process, "activateUvLoop", ActivateUVLoop);
|
||||
NODE_SET_METHOD(process, "log", Log);
|
||||
NODE_SET_METHOD(process, "getCurrentStackTrace", GetCurrentStackTrace);
|
||||
NODE_SET_METHOD(process, "scheduleCallback", ScheduleCallback);
|
||||
|
||||
process->Get(v8::String::New("versions"))->ToObject()->
|
||||
Set(v8::String::New("atom-shell"), v8::String::New(ATOM_VERSION_STRING));
|
||||
}
|
||||
|
||||
// static
|
||||
void AtomBindings::Binding(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
v8::Local<v8::String> module = args[0]->ToString();
|
||||
v8::Handle<v8::Value> Binding(v8::Handle<v8::String> module) {
|
||||
v8::String::Utf8Value module_v(module);
|
||||
node::node_module_struct* modp;
|
||||
|
||||
|
@ -128,65 +95,78 @@ void AtomBindings::Binding(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|||
process->Set(bc_name, binding_cache);
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> exports;
|
||||
|
||||
if (binding_cache->Has(module)) {
|
||||
exports = binding_cache->Get(module)->ToObject();
|
||||
return args.GetReturnValue().Set(exports);
|
||||
}
|
||||
if (binding_cache->Has(module))
|
||||
return binding_cache->Get(module)->ToObject();
|
||||
|
||||
if ((modp = GetBuiltinModule(*module_v, is_browser)) != NULL) {
|
||||
exports = v8::Object::New();
|
||||
v8::Local<v8::Object> exports = v8::Object::New();
|
||||
// Internal bindings don't have a "module" object,
|
||||
// only exports.
|
||||
modp->register_func(exports, v8::Undefined());
|
||||
binding_cache->Set(module, exports);
|
||||
return args.GetReturnValue().Set(exports);
|
||||
return exports;
|
||||
}
|
||||
|
||||
return node::ThrowError("No such module");
|
||||
node::ThrowError("No such module");
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
// static
|
||||
void AtomBindings::Crash(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
void Crash() {
|
||||
static_cast<DummyClass*>(NULL)->crash = true;
|
||||
}
|
||||
|
||||
// static
|
||||
void AtomBindings::ActivateUVLoop(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
void ActivateUVLoop() {
|
||||
uv_async_send(&g_next_tick_uv_handle);
|
||||
}
|
||||
|
||||
// static
|
||||
void AtomBindings::Log(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
v8::String::Utf8Value str(args[0]);
|
||||
logging::LogMessage("CONSOLE", 0, 0).stream() << *str;
|
||||
void Log(const string16& message) {
|
||||
logging::LogMessage("CONSOLE", 0, 0).stream() << message;
|
||||
}
|
||||
|
||||
// static
|
||||
void AtomBindings::GetCurrentStackTrace(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
int stack_limit = kMaxCallStackSize;
|
||||
FromV8Arguments(args, &stack_limit);
|
||||
|
||||
v8::Handle<v8::Value> GetCurrentStackTrace(v8::Isolate* isolate,
|
||||
int stack_limit) {
|
||||
v8::Local<v8::StackTrace> stack_trace = v8::StackTrace::CurrentStackTrace(
|
||||
stack_limit, v8::StackTrace::kDetailed);
|
||||
|
||||
int frame_count = stack_trace->GetFrameCount();
|
||||
v8::Local<v8::Array> result = v8::Array::New(frame_count);
|
||||
for (int i = 0; i < frame_count; ++i)
|
||||
result->Set(i, DumpStackFrame(stack_trace->GetFrame(i)));
|
||||
result->Set(i, DumpStackFrame(isolate, stack_trace->GetFrame(i)));
|
||||
|
||||
args.GetReturnValue().Set(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
// static
|
||||
void AtomBindings::ScheduleCallback(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
if (!FromV8Arguments(args, &g_v8_callback))
|
||||
return node::ThrowTypeError("Bad arguments");
|
||||
void ScheduleCallback(const base::Closure& callback) {
|
||||
g_v8_callback = callback;
|
||||
uv_async_send(&g_callback_uv_handle);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
AtomBindings::AtomBindings() {
|
||||
uv_async_init(uv_default_loop(), &g_next_tick_uv_handle, UvCallNextTick);
|
||||
uv_async_init(uv_default_loop(), &g_callback_uv_handle, UvOnCallback);
|
||||
v8::V8::SetFatalErrorHandler(FatalErrorCallback);
|
||||
}
|
||||
|
||||
AtomBindings::~AtomBindings() {
|
||||
}
|
||||
|
||||
void AtomBindings::BindTo(v8::Handle<v8::Object> process) {
|
||||
v8::Isolate* isolate = v8::Isolate::GetCurrent();
|
||||
mate::Dictionary dict(isolate, process);
|
||||
dict.SetMethod("atomBinding", &Binding);
|
||||
dict.SetMethod("crash", &Crash);
|
||||
dict.SetMethod("activateUvLoop", &ActivateUVLoop);
|
||||
dict.SetMethod("log", &Log);
|
||||
dict.SetMethod("getCurrentStackTrace", &GetCurrentStackTrace);
|
||||
dict.SetMethod("scheduleCallback", &ScheduleCallback);
|
||||
|
||||
v8::Handle<v8::Object> versions;
|
||||
if (dict.Get("versions", &versions))
|
||||
versions->Set(mate::StringToV8(isolate, "atom-shell"),
|
||||
mate::StringToV8(isolate, ATOM_VERSION_STRING));
|
||||
}
|
||||
|
||||
} // namespace atom
|
||||
|
|
|
@ -6,6 +6,8 @@
|
|||
#define ATOM_COMMON_API_ATOM_BINDINGS_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "base/callback_forward.h"
|
||||
#include "base/strings/string16.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
@ -20,14 +22,6 @@ class AtomBindings {
|
|||
virtual void BindTo(v8::Handle<v8::Object> process);
|
||||
|
||||
private:
|
||||
static void Binding(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Crash(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void ActivateUVLoop(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void Log(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void GetCurrentStackTrace(
|
||||
const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void ScheduleCallback(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(AtomBindings);
|
||||
};
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
#include "base/strings/string_util.h"
|
||||
#include "vendor/node/src/node_version.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
|
|
|
@ -1,3 +1 @@
|
|||
{Screen} = process.atomBinding 'screen'
|
||||
|
||||
module.exports = new Screen
|
||||
module.exports = process.atomBinding 'screen'
|
||||
|
|
|
@ -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<v8::Object>* value,
|
||||
ObjectLifeMonitor* self);
|
||||
|
||||
ScopedPersistent<v8::Object> handle_;
|
||||
mate::ScopedPersistent<v8::Object> handle_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ObjectLifeMonitor);
|
||||
};
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
// 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.
|
||||
|
||||
#include "atom/common/browser_v8_locker.h"
|
||||
|
||||
namespace node {
|
||||
extern bool g_standalone_mode;
|
||||
}
|
||||
|
||||
namespace atom {
|
||||
|
||||
BrowserV8Locker::BrowserV8Locker(v8::Isolate* isolate) {
|
||||
if (node::g_standalone_mode)
|
||||
locker_.reset(new v8::Locker(isolate));
|
||||
}
|
||||
|
||||
BrowserV8Locker::~BrowserV8Locker() {
|
||||
}
|
||||
|
||||
} // namespace atom
|
|
@ -0,0 +1,28 @@
|
|||
// 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_BROWSER_V8_LOCKER_H_
|
||||
#define ATOM_COMMON_BROWSER_V8_LOCKER_H_
|
||||
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
// Like v8::Locker, but only do lock when in browser process.
|
||||
class BrowserV8Locker {
|
||||
public:
|
||||
explicit BrowserV8Locker(v8::Isolate* isolate);
|
||||
~BrowserV8Locker();
|
||||
|
||||
private:
|
||||
void* operator new(size_t size);
|
||||
void operator delete(void*, size_t);
|
||||
|
||||
scoped_ptr<v8::Locker> locker_;
|
||||
};
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_BROWSER_V8_LOCKER_H_
|
|
@ -0,0 +1,36 @@
|
|||
// 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_FILE_PATH_CONVERTER_H_
|
||||
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_FILE_PATH_CONVERTER_H_
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||
#include "base/files/file_path.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
template<>
|
||||
struct Converter<base::FilePath> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const base::FilePath& val) {
|
||||
return Converter<base::FilePath::StringType>::ToV8(isolate, val.value());
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
base::FilePath* out) {
|
||||
base::FilePath::StringType path;
|
||||
if (Converter<base::FilePath::StringType>::FromV8(isolate, val, &path)) {
|
||||
*out = base::FilePath(path);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_FILE_PATH_CONVERTER_H_
|
|
@ -0,0 +1,209 @@
|
|||
// 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_FUNCTION_CONVERTER_H_
|
||||
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_FUNCTION_CONVERTER_H_
|
||||
|
||||
#include "atom/common/browser_v8_locker.h"
|
||||
#include "base/bind.h"
|
||||
#include "base/callback.h"
|
||||
#include "native_mate/constructor.h"
|
||||
#include "native_mate/scoped_persistent.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
namespace internal {
|
||||
|
||||
typedef scoped_refptr<RefCountedPersistent<v8::Function> > SafeV8Function;
|
||||
|
||||
// Helper to convert type to V8 with storage type (const T& to T).
|
||||
template<typename T>
|
||||
v8::Handle<v8::Value> ConvertToV8(v8::Isolate* isolate, T a) {
|
||||
return Converter<typename base::internal::CallbackParamTraits<T>::StorageType>
|
||||
::ToV8(isolate, a);
|
||||
}
|
||||
|
||||
// This set of templates invokes a V8::Function by converting the C++ types.
|
||||
template<typename Sig>
|
||||
struct V8FunctionInvoker;
|
||||
|
||||
template<typename R>
|
||||
struct V8FunctionInvoker<R()> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function) {
|
||||
R ret;
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, 0, NULL));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<>
|
||||
struct V8FunctionInvoker<void()> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function) {
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
holder->Call(holder, 0, NULL);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1>
|
||||
struct V8FunctionInvoker<R(P1)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1) {
|
||||
R ret;
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1>
|
||||
struct V8FunctionInvoker<void(P1)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1) {
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1, typename P2>
|
||||
struct V8FunctionInvoker<R(P1, P2)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2) {
|
||||
R ret;
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1, typename P2>
|
||||
struct V8FunctionInvoker<void(P1, P2)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2) {
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1, typename P2, typename P3>
|
||||
struct V8FunctionInvoker<R(P1, P2, P3)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3) {
|
||||
R ret;
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1, typename P2, typename P3>
|
||||
struct V8FunctionInvoker<void(P1, P2, P3)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3) {
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename R, typename P1, typename P2, typename P3, typename P4>
|
||||
struct V8FunctionInvoker<R(P1, P2, P3, P4)> {
|
||||
static R Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3, P4 a4) {
|
||||
R ret;
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
ConvertToV8(isolate, a4),
|
||||
};
|
||||
v8::Handle<v8::Value> val(holder->Call(holder, arraysize(args), args));
|
||||
Converter<R>::FromV8(isolate, val, &ret);
|
||||
return ret;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename P1, typename P2, typename P3, typename P4>
|
||||
struct V8FunctionInvoker<void(P1, P2, P3, P4)> {
|
||||
static void Go(v8::Isolate* isolate, SafeV8Function function, P1 a1, P2 a2,
|
||||
P3 a3, P4 a4) {
|
||||
atom::BrowserV8Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
v8::Handle<v8::Function> holder = function->NewHandle();
|
||||
v8::Handle<v8::Value> args[] = {
|
||||
ConvertToV8(isolate, a1),
|
||||
ConvertToV8(isolate, a2),
|
||||
ConvertToV8(isolate, a3),
|
||||
ConvertToV8(isolate, a4),
|
||||
};
|
||||
holder->Call(holder, arraysize(args), args);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace internal
|
||||
|
||||
template<typename Sig>
|
||||
struct Converter<base::Callback<Sig> > {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const base::Callback<Sig>& val) {
|
||||
return CreateFunctionTemplate(isolate, val)->GetFunction();
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
base::Callback<Sig>* out) {
|
||||
if (!val->IsFunction())
|
||||
return false;
|
||||
|
||||
internal::SafeV8Function function(
|
||||
new RefCountedPersistent<v8::Function>(val));
|
||||
*out = base::Bind(&internal::V8FunctionInvoker<Sig>::Go, isolate, function);
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_FUNCTION_CONVERTER_H_
|
|
@ -0,0 +1,36 @@
|
|||
// 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 <string>
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
#include "url/gurl.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
template<>
|
||||
struct Converter<GURL> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const GURL& val) {
|
||||
return ConvertToV8(isolate, val.spec());
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
GURL* out) {
|
||||
std::string url;
|
||||
if (Converter<std::string>::FromV8(isolate, val, &url)) {
|
||||
*out = GURL(url);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_GURL_CONVERTER_H_
|
|
@ -0,0 +1,31 @@
|
|||
// 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_STRING16_CONVERTER_H_
|
||||
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_
|
||||
|
||||
#include "base/strings/string16.h"
|
||||
#include "native_mate/converter.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
template<>
|
||||
struct Converter<string16> {
|
||||
static v8::Handle<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const string16& val) {
|
||||
return v8::String::New(reinterpret_cast<const uint16_t*>(val.data()),
|
||||
val.size());
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
string16* out) {
|
||||
v8::String::Value s(val);
|
||||
out->assign(reinterpret_cast<const char16*>(*s), s.length());
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_STRING16_CONVERTER_H_
|
|
@ -2,7 +2,7 @@
|
|||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "atom/common/v8/v8_value_converter.h"
|
||||
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
|
@ -2,8 +2,8 @@
|
|||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_COMMON_V8_V8_VALUE_CONVERTER_H_
|
||||
#define ATOM_COMMON_V8_V8_VALUE_CONVERTER_H_
|
||||
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_V8_VALUE_CONVERTER_H_
|
||||
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_V8_VALUE_CONVERTER_H_
|
||||
|
||||
#include <map>
|
||||
|
||||
|
@ -77,4 +77,4 @@ class V8ValueConverter {
|
|||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_V8_V8_VALUE_CONVERTER_H_
|
||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_V8_VALUE_CONVERTER_H_
|
|
@ -0,0 +1,40 @@
|
|||
// 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.
|
||||
|
||||
#include "atom/common/native_mate_converters/value_converter.h"
|
||||
|
||||
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
||||
#include "base/values.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
bool Converter<base::DictionaryValue>::FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
base::DictionaryValue* out) {
|
||||
scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
|
||||
scoped_ptr<base::Value> value(converter->FromV8Value(
|
||||
val, v8::Context::GetCurrent()));
|
||||
if (value->IsType(base::Value::TYPE_DICTIONARY)) {
|
||||
out->Swap(static_cast<DictionaryValue*>(value.get()));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool Converter<base::ListValue>::FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
base::ListValue* out) {
|
||||
scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
|
||||
scoped_ptr<base::Value> value(converter->FromV8Value(
|
||||
val, v8::Context::GetCurrent()));
|
||||
if (value->IsType(base::Value::TYPE_LIST)) {
|
||||
out->Swap(static_cast<ListValue*>(value.get()));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace mate
|
|
@ -0,0 +1,33 @@
|
|||
// 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_VALUE_CONVERTER_H_
|
||||
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_VALUE_CONVERTER_H_
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
|
||||
namespace base {
|
||||
class DictionaryValue;
|
||||
class ListValue;
|
||||
}
|
||||
|
||||
namespace mate {
|
||||
|
||||
template<>
|
||||
struct Converter<base::DictionaryValue> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
base::DictionaryValue* out);
|
||||
};
|
||||
|
||||
template<>
|
||||
struct Converter<base::ListValue> {
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Handle<v8::Value> val,
|
||||
base::ListValue* out);
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_VALUE_CONVERTER_H_
|
|
@ -7,10 +7,11 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "atom/common/browser_v8_locker.h"
|
||||
#include "base/command_line.h"
|
||||
#include "base/message_loop/message_loop.h"
|
||||
#include "base/base_paths.h"
|
||||
#include "base/files/file_path.h"
|
||||
#include "base/message_loop/message_loop.h"
|
||||
#include "base/path_service.h"
|
||||
#include "content/public/browser/browser_thread.h"
|
||||
|
||||
|
@ -18,7 +19,7 @@
|
|||
#include "base/strings/utf_string_conversions.h"
|
||||
#endif
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
using content::BrowserThread;
|
||||
|
||||
|
@ -199,9 +200,7 @@ void NodeBindings::UvRunOnce() {
|
|||
DCHECK(!is_browser_ || BrowserThread::CurrentlyOn(BrowserThread::UI));
|
||||
|
||||
// Use Locker in browser process.
|
||||
scoped_ptr<v8::Locker> locker;
|
||||
if (is_browser_)
|
||||
locker.reset(new v8::Locker(node_isolate));
|
||||
BrowserV8Locker locker(node_isolate);
|
||||
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
#include <sys/time.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
|
|
|
@ -2,8 +2,8 @@
|
|||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef ATOM_COMMON_V8_NODE_COMMON_H_
|
||||
#define ATOM_COMMON_V8_NODE_COMMON_H_
|
||||
#ifndef ATOM_COMMON_NODE_INCLUDES_H_
|
||||
#define ATOM_COMMON_NODE_INCLUDES_H_
|
||||
|
||||
// Include common headers for using node APIs.
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -24,4 +25,4 @@ namespace atom {
|
|||
extern node::Environment* global_env;
|
||||
}
|
||||
|
||||
#endif // ATOM_COMMON_V8_NODE_COMMON_H_
|
||||
#endif // ATOM_COMMON_NODE_INCLUDES_H_
|
|
@ -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<typename T> inline
|
||||
void SwapOrAssign(T& v1, const T& v2) {
|
||||
__if_exists(T::swap) {
|
||||
v1.swap(const_cast<T&>(v2));
|
||||
}
|
||||
|
||||
__if_not_exists(T::swap) {
|
||||
v1 = v2;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T> 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 <typename T>
|
||||
class HasSwapMethod {
|
||||
typedef char one;
|
||||
typedef long two;
|
||||
template <typename C> static one test(char[sizeof(&C::swap)]) ;
|
||||
template <typename C> static two test(...);
|
||||
public:
|
||||
enum { value = sizeof(test<T>(0)) == sizeof(char) };
|
||||
};
|
||||
|
||||
template<bool B, class T = void>
|
||||
struct enable_if {};
|
||||
|
||||
template<class T>
|
||||
struct enable_if<true, T> { typedef T type; };
|
||||
|
||||
template<typename T> inline
|
||||
typename enable_if<HasSwapMethod<T>::value>::type SwapOrAssign(
|
||||
T& v1, const T& v2) {
|
||||
v1.swap(const_cast<T&>(v2));
|
||||
}
|
||||
|
||||
template<typename T> inline
|
||||
typename enable_if<!HasSwapMethod<T>::value>::type SwapOrAssign(
|
||||
T& v1, const T& v2) {
|
||||
v1 = v2;
|
||||
}
|
||||
#endif // !defined(OS_WIN)
|
||||
|
||||
} // namespace internal
|
||||
|
||||
#endif // ATOM_COMMON_SWAP_OR_ASSIGN_H_
|
|
@ -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 <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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/v8/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<v8::Value> 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<const char16*>(*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<v8::Object> rect = value_->ToObject();
|
||||
v8::Handle<v8::Value> x = rect->Get(v8::String::New("x"));
|
||||
v8::Handle<v8::Value> y = rect->Get(v8::String::New("y"));
|
||||
v8::Handle<v8::Value> width = rect->Get(v8::String::New("width"));
|
||||
v8::Handle<v8::Value> 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<base::Value>() {
|
||||
scoped_ptr<atom::V8ValueConverter> converter(new atom::V8ValueConverter);
|
||||
return scoped_ptr<base::Value>(
|
||||
converter->FromV8Value(value_, v8::Context::GetCurrent()));
|
||||
}
|
||||
|
||||
template<class T>
|
||||
operator std::vector<T>() {
|
||||
std::vector<T> array;
|
||||
v8::Handle<v8::Array> v8_array = v8::Handle<v8::Array>::Cast(value_);
|
||||
for (uint32_t i = 0; i < v8_array->Length(); ++i)
|
||||
array.push_back(FromV8Value(v8_array->Get(i)));
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
template<class K, class V>
|
||||
operator std::map<K, V>() {
|
||||
std::map<K, V> dict;
|
||||
v8::Handle<v8::Object> v8_dict = value_->ToObject();
|
||||
v8::Handle<v8::Array> v8_keys = v8_dict->GetOwnPropertyNames();
|
||||
for (uint32_t i = 0; i < v8_keys->Length(); ++i) {
|
||||
v8::Handle<v8::Value> 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<Window>(value_->ToObject());
|
||||
if (window && window->window())
|
||||
return window->window();
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
operator atom::RefCountedV8Function() {
|
||||
v8::Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(value_);
|
||||
return atom::RefCountedV8Function(
|
||||
new atom::RefCountedPersistent<v8::Function>(func));
|
||||
}
|
||||
|
||||
v8::Handle<v8::Value> value_;
|
||||
};
|
||||
|
||||
// Convert arbitrary supported native type to V8 value.
|
||||
inline v8::Handle<v8::Value> ToV8Value(int i) {
|
||||
return v8::Integer::New(i);
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(bool b) {
|
||||
return v8::Boolean::New(b);
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(float f) {
|
||||
return v8::Number::New(f);
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(double f) {
|
||||
return v8::Number::New(f);
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(const char* s) {
|
||||
return v8::String::New(s);
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(const std::string& s) {
|
||||
return v8::String::New(s.data(), s.size());
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(const string16& s) {
|
||||
return v8::String::New(reinterpret_cast<const uint16_t*>(s.data()), s.size());
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(const GURL& url) {
|
||||
return ToV8Value(url.spec());
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(const base::FilePath& path) {
|
||||
std::string path_string(path.AsUTF8Unsafe());
|
||||
return v8::String::New(path_string.data(), path_string.size());
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(void* whatever) {
|
||||
return v8::Undefined();
|
||||
}
|
||||
|
||||
template<class T> inline
|
||||
v8::Handle<v8::Value> ToV8Value(const std::vector<T>& arr) {
|
||||
v8::Handle<v8::Array> 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<v8::Value> ToV8Value(const gfx::Point& point) {
|
||||
v8::Handle<v8::Object> obj = v8::Object::New();
|
||||
obj->Set(ToV8Value("x"), ToV8Value(point.x()));
|
||||
obj->Set(ToV8Value("y"), ToV8Value(point.y()));
|
||||
return obj;
|
||||
}
|
||||
|
||||
inline v8::Handle<v8::Value> ToV8Value(const gfx::Rect& rect) {
|
||||
v8::Handle<v8::Object> 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<v8::Value> ToV8Value(const gfx::Size& size) {
|
||||
v8::Handle<v8::Object> 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<class T> inline
|
||||
bool V8ValueCanBeConvertedTo(v8::Handle<v8::Value> value) {
|
||||
return false;
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<int>(v8::Handle<v8::Value> value) {
|
||||
return value->IsNumber();
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<bool>(v8::Handle<v8::Value> value) {
|
||||
return value->IsBoolean();
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<std::string>(v8::Handle<v8::Value> value) {
|
||||
return value->IsString();
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<string16>(v8::Handle<v8::Value> value) {
|
||||
return V8ValueCanBeConvertedTo<std::string>(value);
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<GURL>(v8::Handle<v8::Value> value) {
|
||||
return V8ValueCanBeConvertedTo<std::string>(value);
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<base::FilePath>(v8::Handle<v8::Value> value) {
|
||||
return V8ValueCanBeConvertedTo<std::string>(value);
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<gfx::Rect>(v8::Handle<v8::Value> value) {
|
||||
return value->IsObject();
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<scoped_ptr<base::Value>>(
|
||||
v8::Handle<v8::Value> value) {
|
||||
return value->IsObject();
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<std::vector<std::string>>(
|
||||
v8::Handle<v8::Value> value) {
|
||||
return value->IsArray();
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<std::map<std::string, std::string>>(
|
||||
v8::Handle<v8::Value> value) {
|
||||
return value->IsObject();
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<atom::NativeWindow*>(v8::Handle<v8::Value> value) {
|
||||
using atom::api::Window;
|
||||
if (value->IsObject()) {
|
||||
Window* window = Window::Unwrap<Window>(value->ToObject());
|
||||
if (window && window->window())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<> inline
|
||||
bool V8ValueCanBeConvertedTo<atom::RefCountedV8Function>(
|
||||
v8::Handle<v8::Value> value) {
|
||||
return value->IsFunction();
|
||||
}
|
||||
|
||||
// Check and convert V8's Arguments to native types.
|
||||
template<typename T1> inline
|
||||
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
T1* value,
|
||||
int index = 0) {
|
||||
if (!V8ValueCanBeConvertedTo<T1>(args[index]))
|
||||
return false;
|
||||
internal::SwapOrAssign(*value,
|
||||
FromV8Value(args[index]).operator T1());
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T1, typename T2> inline
|
||||
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
T1* a1,
|
||||
T2* a2) {
|
||||
return FromV8Arguments<T1>(args, a1) && FromV8Arguments<T2>(args, a2, 1);
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename T3> inline
|
||||
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
T1* a1,
|
||||
T2* a2,
|
||||
T3* a3) {
|
||||
return FromV8Arguments<T1, T2>(args, a1, a2) &&
|
||||
FromV8Arguments<T3>(args, a3, 2);
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename T3, typename T4> inline
|
||||
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
T1* a1,
|
||||
T2* a2,
|
||||
T3* a3,
|
||||
T4* a4) {
|
||||
return FromV8Arguments<T1, T2, T3>(args, a1, a2, a3) &&
|
||||
FromV8Arguments<T4>(args, a4, 3);
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename T3, typename T4, typename T5> inline
|
||||
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
T1* a1,
|
||||
T2* a2,
|
||||
T3* a3,
|
||||
T4* a4,
|
||||
T5* a5) {
|
||||
return FromV8Arguments<T1, T2, T3, T4>(args, a1, a2, a3, a4) &&
|
||||
FromV8Arguments<T5>(args, a5, 4);
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6> inline
|
||||
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
T1* a1,
|
||||
T2* a2,
|
||||
T3* a3,
|
||||
T4* a4,
|
||||
T5* a5,
|
||||
T6* a6) {
|
||||
return FromV8Arguments<T1, T2, T3, T4, T5>(args, a1, a2, a3, a4, a5) &&
|
||||
FromV8Arguments<T6>(args, a6, 5);
|
||||
}
|
||||
|
||||
template<typename T1, typename T2, typename T3, typename T4, typename T5,
|
||||
typename T6, typename T7> inline
|
||||
bool FromV8Arguments(const v8::FunctionCallbackInfo<v8::Value>& args,
|
||||
T1* a1,
|
||||
T2* a2,
|
||||
T3* a3,
|
||||
T4* a4,
|
||||
T5* a5,
|
||||
T6* a6,
|
||||
T7* a7) {
|
||||
return
|
||||
FromV8Arguments<T1, T2, T3, T4, T5, T6>(args, a1, a2, a3, a4, a5, a6) &&
|
||||
FromV8Arguments<T7>(args, a7, 6);
|
||||
}
|
||||
|
||||
#endif // ATOM_COMMON_V8_NATIVE_TYPE_CONVERSIONS_H_
|
|
@ -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 <typename T>
|
||||
class ScopedPersistent {
|
||||
public:
|
||||
ScopedPersistent() {
|
||||
}
|
||||
|
||||
explicit ScopedPersistent(v8::Handle<T> handle) {
|
||||
reset(handle);
|
||||
}
|
||||
|
||||
~ScopedPersistent() {
|
||||
reset();
|
||||
}
|
||||
|
||||
void reset(v8::Handle<T> handle) {
|
||||
if (!handle.IsEmpty())
|
||||
handle_.Reset(GetIsolate(handle), handle);
|
||||
else
|
||||
reset();
|
||||
}
|
||||
|
||||
void reset() {
|
||||
handle_.Reset();
|
||||
}
|
||||
|
||||
bool IsEmpty() const {
|
||||
return handle_.IsEmpty();
|
||||
}
|
||||
|
||||
v8::Handle<T> NewHandle() const {
|
||||
if (handle_.IsEmpty())
|
||||
return v8::Local<T>();
|
||||
return v8::Local<T>::New(GetIsolate(handle_), handle_);
|
||||
}
|
||||
|
||||
v8::Handle<T> NewHandle(v8::Isolate* isolate) const {
|
||||
if (handle_.IsEmpty())
|
||||
return v8::Local<T>();
|
||||
return v8::Local<T>::New(isolate, handle_);
|
||||
}
|
||||
|
||||
template <typename P>
|
||||
void MakeWeak(P* parameters,
|
||||
typename v8::WeakReferenceCallbacks<T, P>::Revivable callback) {
|
||||
handle_.MakeWeak(parameters, callback);
|
||||
}
|
||||
|
||||
private:
|
||||
template <typename U>
|
||||
static v8::Isolate* GetIsolate(v8::Handle<U> 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<v8::Context> context_handle) {
|
||||
if (!context_handle.IsEmpty())
|
||||
return context_handle->GetIsolate();
|
||||
return v8::Isolate::GetCurrent();
|
||||
}
|
||||
static v8::Isolate* GetIsolate(
|
||||
v8::Handle<v8::ObjectTemplate> template_handle) {
|
||||
return v8::Isolate::GetCurrent();
|
||||
}
|
||||
template <typename U>
|
||||
static v8::Isolate* GetIsolate(const U& any_handle) {
|
||||
return v8::Isolate::GetCurrent();
|
||||
}
|
||||
|
||||
v8::Persistent<T> handle_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(ScopedPersistent);
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
class RefCountedPersistent : public ScopedPersistent<T>,
|
||||
public base::RefCounted<RefCountedPersistent<T>> {
|
||||
public:
|
||||
RefCountedPersistent() {}
|
||||
|
||||
explicit RefCountedPersistent(v8::Handle<T> handle)
|
||||
: ScopedPersistent<T>(handle) {
|
||||
}
|
||||
|
||||
protected:
|
||||
friend class base::RefCounted<RefCountedPersistent<T>>;
|
||||
|
||||
~RefCountedPersistent() {}
|
||||
|
||||
private:
|
||||
DISALLOW_COPY_AND_ASSIGN(RefCountedPersistent);
|
||||
};
|
||||
|
||||
typedef scoped_refptr<RefCountedPersistent<v8::Function>> RefCountedV8Function;
|
||||
typedef scoped_refptr<RefCountedPersistent<v8::Object>> RefCountedV8Object;
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_COMMON_V8_SCOPED_PERSISTENT_H_
|
|
@ -2,24 +2,20 @@
|
|||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "atom/renderer/api/atom_api_renderer_ipc.h"
|
||||
|
||||
#include "atom/common/api/api_messages.h"
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||
#include "atom/common/native_mate_converters/value_converter.h"
|
||||
#include "content/public/renderer/render_view.h"
|
||||
#include "native_mate/dictionary.h"
|
||||
#include "third_party/WebKit/public/web/WebFrame.h"
|
||||
#include "third_party/WebKit/public/web/WebView.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
using content::RenderView;
|
||||
using WebKit::WebFrame;
|
||||
using WebKit::WebView;
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
namespace {
|
||||
|
||||
RenderView* GetCurrentRenderView() {
|
||||
|
@ -34,65 +30,43 @@ RenderView* GetCurrentRenderView() {
|
|||
return RenderView::FromWebView(view);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
void RendererIPC::Send(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
string16 channel;
|
||||
scoped_ptr<base::Value> arguments;
|
||||
if (!FromV8Arguments(args, &channel, &arguments))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
|
||||
void Send(const string16& channel, const base::ListValue& arguments) {
|
||||
RenderView* render_view = GetCurrentRenderView();
|
||||
if (render_view == NULL)
|
||||
return;
|
||||
|
||||
bool success = render_view->Send(new AtomViewHostMsg_Message(
|
||||
render_view->GetRoutingID(),
|
||||
channel,
|
||||
*static_cast<base::ListValue*>(arguments.get())));
|
||||
render_view->GetRoutingID(), channel, arguments));
|
||||
|
||||
if (!success)
|
||||
return node::ThrowError("Unable to send AtomViewHostMsg_Message");
|
||||
node::ThrowError("Unable to send AtomViewHostMsg_Message");
|
||||
}
|
||||
|
||||
// static
|
||||
void RendererIPC::SendSync(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
||||
string16 channel;
|
||||
scoped_ptr<base::Value> arguments;
|
||||
if (!FromV8Arguments(args, &channel, &arguments))
|
||||
return node::ThrowTypeError("Bad argument");
|
||||
string16 SendSync(const string16& channel, const base::ListValue& arguments) {
|
||||
string16 json;
|
||||
|
||||
RenderView* render_view = GetCurrentRenderView();
|
||||
if (render_view == NULL)
|
||||
return;
|
||||
return json;
|
||||
|
||||
string16 json;
|
||||
IPC::SyncMessage* message = new AtomViewHostMsg_Message_Sync(
|
||||
render_view->GetRoutingID(),
|
||||
channel,
|
||||
*static_cast<base::ListValue*>(arguments.get()),
|
||||
&json);
|
||||
render_view->GetRoutingID(), channel, arguments, &json);
|
||||
// Enable the UI thread in browser to receive messages.
|
||||
message->EnableMessagePumping();
|
||||
bool success = render_view->Send(message);
|
||||
|
||||
if (!success)
|
||||
return node::ThrowError("Unable to send AtomViewHostMsg_Message_Sync");
|
||||
node::ThrowError("Unable to send AtomViewHostMsg_Message_Sync");
|
||||
|
||||
args.GetReturnValue().Set(ToV8Value(json));
|
||||
return json;
|
||||
}
|
||||
|
||||
// static
|
||||
void RendererIPC::Initialize(v8::Handle<v8::Object> target) {
|
||||
v8::HandleScope handle_scope(node_isolate);
|
||||
|
||||
NODE_SET_METHOD(target, "send", Send);
|
||||
NODE_SET_METHOD(target, "sendSync", SendSync);
|
||||
void Initialize(v8::Handle<v8::Object> exports) {
|
||||
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
|
||||
dict.SetMethod("send", &Send);
|
||||
dict.SetMethod("sendSync", &SendSync);
|
||||
}
|
||||
|
||||
} // namespace api
|
||||
} // namespace
|
||||
|
||||
} // namespace atom
|
||||
|
||||
NODE_MODULE(atom_renderer_ipc, atom::api::RendererIPC::Initialize)
|
||||
NODE_MODULE(atom_renderer_ipc, Initialize)
|
||||
|
|
|
@ -1,30 +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_RENDERER_API_ATOM_API_RENDERER_IPC_H_
|
||||
#define ATOM_RENDERER_API_ATOM_API_RENDERER_IPC_H_
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "v8/include/v8.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
namespace api {
|
||||
|
||||
class RendererIPC {
|
||||
public:
|
||||
static void Initialize(v8::Handle<v8::Object> target);
|
||||
|
||||
private:
|
||||
static void Send(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
static void SendSync(const v8::FunctionCallbackInfo<v8::Value>& args);
|
||||
|
||||
DISALLOW_IMPLICIT_CONSTRUCTORS(RendererIPC);
|
||||
};
|
||||
|
||||
} // namespace api
|
||||
|
||||
} // namespace atom
|
||||
|
||||
#endif // ATOM_RENDERER_API_ATOM_API_RENDERER_IPC_H_
|
|
@ -6,12 +6,15 @@
|
|||
|
||||
#include <vector>
|
||||
|
||||
#include "atom/common/v8/native_type_conversions.h"
|
||||
#include "atom/common/native_mate_converters/string16_converter.h"
|
||||
#include "atom/common/native_mate_converters/v8_value_converter.h"
|
||||
#include "base/memory/scoped_ptr.h"
|
||||
#include "base/values.h"
|
||||
#include "content/public/renderer/render_view.h"
|
||||
#include "third_party/WebKit/public/web/WebFrame.h"
|
||||
#include "third_party/WebKit/public/web/WebView.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
|
@ -64,7 +67,7 @@ void AtomRendererBindings::OnBrowserMessage(content::RenderView* render_view,
|
|||
|
||||
std::vector<v8::Handle<v8::Value>> arguments;
|
||||
arguments.reserve(1 + args.GetSize());
|
||||
arguments.push_back(ToV8Value(channel));
|
||||
arguments.push_back(mate::ConvertToV8(node_isolate, channel));
|
||||
|
||||
for (size_t i = 0; i < args.GetSize(); i++) {
|
||||
const base::Value* value;
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
#include "third_party/WebKit/public/web/WebFrame.h"
|
||||
#include "third_party/WebKit/public/web/WebView.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
using WebKit::WebFrame;
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
#include "third_party/WebKit/public/web/WebDocument.h"
|
||||
#include "third_party/WebKit/public/web/WebFrame.h"
|
||||
|
||||
#include "atom/common/v8/node_common.h"
|
||||
#include "atom/common/node_includes.h"
|
||||
|
||||
namespace atom {
|
||||
|
||||
|
|
|
@ -8,8 +8,8 @@ from lib.util import execute
|
|||
|
||||
IGNORE_FILES = [
|
||||
os.path.join('atom', 'app', 'atom_main.cc'),
|
||||
os.path.join('atom', 'browser', 'atom_application_mac.h'),
|
||||
os.path.join('atom', 'browser', 'atom_application_delegate_mac.h'),
|
||||
os.path.join('atom', 'browser', 'mac', 'atom_application.h'),
|
||||
os.path.join('atom', 'browser', 'mac', 'atom_application_delegate.h'),
|
||||
os.path.join('atom', 'browser', 'native_window_mac.h'),
|
||||
os.path.join('atom', 'browser', 'resources', 'win', 'resource.h'),
|
||||
os.path.join('atom', 'browser', 'ui', 'cocoa', 'event_processing_window.h'),
|
||||
|
|
|
@ -14,7 +14,7 @@ describe 'browser-window module', ->
|
|||
w.on 'loading-state-changed', (event, isLoading) ->
|
||||
if (!isLoading)
|
||||
w.close()
|
||||
w.on 'destroyed', ->
|
||||
w.on 'closed', ->
|
||||
test = path.join(fixtures, 'api', 'unload')
|
||||
content = fs.readFileSync(test)
|
||||
fs.unlinkSync(test)
|
||||
|
|
|
@ -61,12 +61,10 @@ describe 'ipc module', ->
|
|||
msg = ipc.sendChannelSync 'echo', 'test'
|
||||
assert.equal msg, 'test'
|
||||
|
||||
it 'does not crash when reply is not sent and both browser and event are destroyed', (done) ->
|
||||
it 'does not crash when reply is not sent and browser is destroyed', (done) ->
|
||||
w = new BrowserWindow(show: false)
|
||||
remote.require('ipc').once 'send-sync-message', (event) ->
|
||||
event.returnValue = null
|
||||
|
||||
w.destroy()
|
||||
event.destroy()
|
||||
done()
|
||||
w.loadUrl 'file://' + path.join(fixtures, 'api', 'send-sync-message.html')
|
||||
|
|
|
@ -8,7 +8,7 @@ describe 'protocol module', ->
|
|||
describe 'protocol.registerProtocol', ->
|
||||
it 'throws error when scheme is already registered', (done) ->
|
||||
register = -> protocol.registerProtocol('test1', ->)
|
||||
protocol.once 'registered', (scheme) ->
|
||||
protocol.once 'registered', (event, scheme) ->
|
||||
assert.equal scheme, 'test1'
|
||||
assert.throws register, /The scheme is already registered/
|
||||
protocol.unregisterProtocol 'test1'
|
||||
|
@ -85,28 +85,28 @@ describe 'protocol module', ->
|
|||
describe 'protocol.interceptProtocol', ->
|
||||
it 'throws error when scheme is not a registered one', ->
|
||||
register = -> protocol.interceptProtocol('test-intercept', ->)
|
||||
assert.throws register, /Cannot intercept procotol/
|
||||
assert.throws register, /Scheme does not exist/
|
||||
|
||||
it 'throws error when scheme is a custom protocol', (done) ->
|
||||
protocol.once 'unregistered', (scheme) ->
|
||||
protocol.once 'unregistered', (event, scheme) ->
|
||||
assert.equal scheme, 'atom'
|
||||
done()
|
||||
protocol.once 'registered', (scheme) ->
|
||||
protocol.once 'registered', (event, scheme) ->
|
||||
assert.equal scheme, 'atom'
|
||||
register = -> protocol.interceptProtocol('test-intercept', ->)
|
||||
assert.throws register, /Cannot intercept procotol/
|
||||
assert.throws register, /Scheme does not exist/
|
||||
protocol.unregisterProtocol scheme
|
||||
protocol.registerProtocol('atom', ->)
|
||||
|
||||
it 'returns original job when callback returns nothing', (done) ->
|
||||
targetScheme = 'file'
|
||||
protocol.once 'intercepted', (scheme) ->
|
||||
protocol.once 'intercepted', (event, scheme) ->
|
||||
assert.equal scheme, targetScheme
|
||||
free = -> protocol.uninterceptProtocol targetScheme
|
||||
$.ajax
|
||||
url: "#{targetScheme}://#{__filename}",
|
||||
success: ->
|
||||
protocol.once 'unintercepted', (scheme) ->
|
||||
protocol.once 'unintercepted', (event, scheme) ->
|
||||
assert.equal scheme, targetScheme
|
||||
done()
|
||||
free()
|
||||
|
|
|
@ -1 +1 @@
|
|||
Subproject commit dab731f2329285a63c0c2cff30793a34fde91c39
|
||||
Subproject commit bee8a48dd0db57fa604378d1e879a6a06d10068d
|
|
@ -0,0 +1 @@
|
|||
Subproject commit f033e23ea3156db611e801af96eabd81d51ba85a
|
Загрузка…
Ссылка в новой задаче