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.
|
|
|
|
|
|
|
|
#include "browser/api/atom_api_app.h"
|
|
|
|
|
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"
|
2013-05-02 16:05:09 +00:00
|
|
|
#include "browser/browser.h"
|
2013-09-24 01:41:54 +00:00
|
|
|
#include "common/v8_conversions.h"
|
2013-05-02 16:05:09 +00:00
|
|
|
#include "vendor/node/src/node.h"
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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() {
|
|
|
|
Emit("finish-launching");
|
|
|
|
}
|
|
|
|
|
2013-05-03 02:53:54 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::New(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
if (!args.IsConstructCall())
|
|
|
|
return node::ThrowError("Require constructor call");
|
|
|
|
|
|
|
|
new App(args.This());
|
|
|
|
|
|
|
|
return args.This();
|
|
|
|
}
|
|
|
|
|
2013-05-02 16:05:09 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::Quit(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
Browser::Get()->Quit();
|
|
|
|
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-05-24 09:59:11 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::Exit(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
exit(args[0]->IntegerValue());
|
|
|
|
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-05-02 16:05:09 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::Terminate(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
Browser::Get()->Terminate();
|
|
|
|
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-05-30 11:24:47 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::Focus(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
Browser::Get()->Focus();
|
|
|
|
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-06-19 05:43:48 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::GetVersion(const v8::Arguments &args) {
|
2013-12-05 02:35:57 +00:00
|
|
|
return ToV8Value(Browser::Get()->GetVersion());
|
2013-06-19 05:43:48 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 02:26:01 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::SetVersion(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
std::string version;
|
|
|
|
if (!FromV8Arguments(args, &version))
|
|
|
|
return node::ThrowError("Bad argument");
|
|
|
|
|
|
|
|
Browser::Get()->SetVersion(version);
|
|
|
|
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-12-05 02:32:58 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::GetName(const v8::Arguments &args) {
|
2013-12-05 02:35:57 +00:00
|
|
|
return ToV8Value(Browser::Get()->GetName());
|
2013-12-05 02:32:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::SetName(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
std::string name;
|
|
|
|
if (!FromV8Arguments(args, &name))
|
|
|
|
return node::ThrowError("Bad argument");
|
|
|
|
|
|
|
|
Browser::Get()->SetName(name);
|
|
|
|
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-05-17 07:39:44 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::AppendSwitch(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::AppendArgument(const v8::Arguments &args) {
|
|
|
|
v8::HandleScope scope;
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:19:56 +00:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::DockBounce(const v8::Arguments& 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-05 02:35:57 +00:00
|
|
|
return ToV8Value(request_id);
|
2013-08-06 08:19:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::DockCancelBounce(const v8::Arguments& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
Browser::Get()->DockCancelBounce(FromV8Value(args[0]));
|
2013-08-06 08:19:56 +00:00
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::DockSetBadgeText(const v8::Arguments& args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
Browser::Get()->DockSetBadgeText(FromV8Value(args[0]));
|
2013-08-06 08:19:56 +00:00
|
|
|
return v8::Undefined();
|
|
|
|
}
|
|
|
|
|
2013-08-06 08:39:31 +00:00
|
|
|
// static
|
|
|
|
v8::Handle<v8::Value> App::DockGetBadgeText(const v8::Arguments& args) {
|
|
|
|
std::string text(Browser::Get()->DockGetBadgeText());
|
2013-09-24 01:41:54 +00:00
|
|
|
return 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-05-03 02:53:54 +00:00
|
|
|
v8::HandleScope scope;
|
|
|
|
|
|
|
|
v8::Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(App::New);
|
|
|
|
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)
|