2013-05-02 16:05:09 +00:00
|
|
|
// 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.
|
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/api/atom_api_app.h"
|
2013-05-02 16:05:09 +00:00
|
|
|
|
2013-05-30 08:03:10 +00:00
|
|
|
#include "base/values.h"
|
2013-05-17 07:39:44 +00:00
|
|
|
#include "base/command_line.h"
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/browser/browser.h"
|
|
|
|
#include "atom/common/v8/native_type_conversions.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2014-03-16 00:30:26 +00:00
|
|
|
#include "atom/common/v8/node_common.h"
|
2013-05-02 16:05:09 +00:00
|
|
|
|
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2013-05-03 02:53:54 +00:00
|
|
|
App::App(v8::Handle<v8::Object> wrapper)
|
|
|
|
: EventEmitter(wrapper) {
|
|
|
|
Browser::Get()->AddObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
App::~App() {
|
|
|
|
Browser::Get()->RemoveObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void App::OnWillQuit(bool* prevent_default) {
|
|
|
|
*prevent_default = Emit("will-quit");
|
|
|
|
}
|
|
|
|
|
|
|
|
void App::OnWindowAllClosed() {
|
|
|
|
Emit("window-all-closed");
|
|
|
|
}
|
|
|
|
|
2013-05-30 08:03:10 +00:00
|
|
|
void App::OnOpenFile(bool* prevent_default, const std::string& file_path) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendString(file_path);
|
|
|
|
*prevent_default = Emit("open-file", &args);
|
|
|
|
}
|
|
|
|
|
2013-07-10 08:10:38 +00:00
|
|
|
void App::OnOpenURL(const std::string& url) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendString(url);
|
|
|
|
Emit("open-url", &args);
|
|
|
|
}
|
|
|
|
|
2014-03-05 10:09:44 +00:00
|
|
|
void App::OnActivateWithNoOpenWindows() {
|
|
|
|
Emit("activate-with-no-open-windows");
|
|
|
|
}
|
|
|
|
|
2013-06-03 07:31:46 +00:00
|
|
|
void App::OnWillFinishLaunching() {
|
|
|
|
Emit("will-finish-launching");
|
|
|
|
}
|
|
|
|
|
2013-05-30 11:12:14 +00:00
|
|
|
void App::OnFinishLaunching() {
|
2013-12-27 03:08:26 +00:00
|
|
|
Emit("ready");
|
2013-05-30 11:12:14 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 02:53:54 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::New(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
v8::HandleScope scope(args.GetIsolate());
|
2013-05-03 02:53:54 +00:00
|
|
|
|
|
|
|
if (!args.IsConstructCall())
|
|
|
|
return node::ThrowError("Require constructor call");
|
|
|
|
|
|
|
|
new App(args.This());
|
|
|
|
}
|
|
|
|
|
2013-05-02 16:05:09 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::Quit(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-05-02 16:05:09 +00:00
|
|
|
Browser::Get()->Quit();
|
|
|
|
}
|
|
|
|
|
2013-05-24 09:59:11 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::Exit(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-05-24 09:59:11 +00:00
|
|
|
exit(args[0]->IntegerValue());
|
|
|
|
}
|
|
|
|
|
2013-05-02 16:05:09 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::Terminate(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-05-02 16:05:09 +00:00
|
|
|
Browser::Get()->Terminate();
|
|
|
|
}
|
|
|
|
|
2013-05-30 11:24:47 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::Focus(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-05-30 11:24:47 +00:00
|
|
|
Browser::Get()->Focus();
|
|
|
|
}
|
|
|
|
|
2013-06-19 05:43:48 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::GetVersion(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
args.GetReturnValue().Set(ToV8Value(Browser::Get()->GetVersion()));
|
2013-06-19 05:43:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 02:26:01 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::SetVersion(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-12-05 02:26:01 +00:00
|
|
|
std::string version;
|
|
|
|
if (!FromV8Arguments(args, &version))
|
|
|
|
return node::ThrowError("Bad argument");
|
|
|
|
|
|
|
|
Browser::Get()->SetVersion(version);
|
|
|
|
}
|
|
|
|
|
2013-12-05 02:32:58 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::GetName(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
|
|
|
return args.GetReturnValue().Set(ToV8Value(Browser::Get()->GetName()));
|
2013-12-05 02:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::SetName(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-12-05 02:32:58 +00:00
|
|
|
std::string name;
|
|
|
|
if (!FromV8Arguments(args, &name))
|
|
|
|
return node::ThrowError("Bad argument");
|
|
|
|
|
|
|
|
Browser::Get()->SetName(name);
|
|
|
|
}
|
|
|
|
|
2013-05-17 07:39:44 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::AppendSwitch(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string switch_string;
|
|
|
|
if (!FromV8Arguments(args, &switch_string))
|
2013-05-17 07:39:44 +00:00
|
|
|
return node::ThrowError("Bad argument");
|
|
|
|
|
|
|
|
if (args.Length() == 1) {
|
|
|
|
CommandLine::ForCurrentProcess()->AppendSwitch(switch_string);
|
|
|
|
} else {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string value = FromV8Value(args[1]);
|
2013-05-17 07:39:44 +00:00
|
|
|
CommandLine::ForCurrentProcess()->AppendSwitchASCII(
|
|
|
|
switch_string, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::AppendArgument(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string value;
|
|
|
|
if (!FromV8Arguments(args, &value))
|
2013-05-17 07:39:44 +00:00
|
|
|
return node::ThrowError("Bad argument");
|
|
|
|
|
|
|
|
CommandLine::ForCurrentProcess()->AppendArg(value);
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:19:56 +00:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::DockBounce(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-12-05 02:35:57 +00:00
|
|
|
std::string type;
|
|
|
|
if (!FromV8Arguments(args, &type))
|
|
|
|
return node::ThrowError("Bad argument");
|
|
|
|
|
2013-08-06 08:19:56 +00:00
|
|
|
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");
|
|
|
|
|
2013-12-11 07:48:19 +00:00
|
|
|
args.GetReturnValue().Set(request_id);
|
2013-08-06 08:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::DockCancelBounce(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
Browser::Get()->DockCancelBounce(FromV8Value(args[0]));
|
2013-08-06 08:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::DockSetBadgeText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
Browser::Get()->DockSetBadgeText(FromV8Value(args[0]));
|
2013-08-06 08:19:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 08:39:31 +00:00
|
|
|
// static
|
2013-12-11 07:48:19 +00:00
|
|
|
void App::DockGetBadgeText(const v8::FunctionCallbackInfo<v8::Value>& args) {
|
2013-08-06 08:39:31 +00:00
|
|
|
std::string text(Browser::Get()->DockGetBadgeText());
|
2013-12-11 07:48:19 +00:00
|
|
|
args.GetReturnValue().Set(ToV8Value(text));
|
2013-08-06 08:39:31 +00:00
|
|
|
}
|
|
|
|
|
2013-08-06 08:19:56 +00:00
|
|
|
#endif // defined(OS_MACOSX)
|
|
|
|
|
2013-05-02 16:05:09 +00:00
|
|
|
// static
|
|
|
|
void App::Initialize(v8::Handle<v8::Object> target) {
|
2013-12-11 07:48:19 +00:00
|
|
|
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(New);
|
2013-05-03 02:53:54 +00:00
|
|
|
t->InstanceTemplate()->SetInternalFieldCount(1);
|
|
|
|
t->SetClassName(v8::String::NewSymbol("Application"));
|
|
|
|
|
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "quit", Quit);
|
2013-05-24 09:59:11 +00:00
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "exit", Exit);
|
2013-05-03 02:53:54 +00:00
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "terminate", Terminate);
|
2013-05-30 11:24:47 +00:00
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "focus", Focus);
|
2013-06-19 05:43:48 +00:00
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "getVersion", GetVersion);
|
2013-12-05 02:26:01 +00:00
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "setVersion", SetVersion);
|
2013-12-05 02:32:58 +00:00
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "getName", GetName);
|
|
|
|
NODE_SET_PROTOTYPE_METHOD(t, "setName", SetName);
|
2013-05-03 02:53:54 +00:00
|
|
|
|
|
|
|
target->Set(v8::String::NewSymbol("Application"), t->GetFunction());
|
2013-05-17 07:39:44 +00:00
|
|
|
|
|
|
|
NODE_SET_METHOD(target, "appendSwitch", AppendSwitch);
|
|
|
|
NODE_SET_METHOD(target, "appendArgument", AppendArgument);
|
2013-08-06 08:19:56 +00:00
|
|
|
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
NODE_SET_METHOD(target, "dockBounce", DockBounce);
|
|
|
|
NODE_SET_METHOD(target, "dockCancelBounce", DockCancelBounce);
|
|
|
|
NODE_SET_METHOD(target, "dockSetBadgeText", DockSetBadgeText);
|
2013-08-06 08:39:31 +00:00
|
|
|
NODE_SET_METHOD(target, "dockGetBadgeText", DockGetBadgeText);
|
2013-08-06 08:19:56 +00:00
|
|
|
#endif // defined(OS_MACOSX)
|
2013-05-02 16:05:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace api
|
|
|
|
|
|
|
|
} // namespace atom
|
|
|
|
|
|
|
|
NODE_MODULE(atom_browser_app, atom::api::App::Initialize)
|