electron/atom/browser/api/atom_api_app.cc

670 lines
22 KiB
C++
Raw Normal View History

// Copyright (c) 2013 GitHub, Inc.
2014-04-25 09:49:37 +00:00
// Use of this source code is governed by the MIT license that can be
2013-05-02 16:05:09 +00:00
// 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
#include <string>
#include <vector>
2014-11-16 15:04:31 +00:00
#include "atom/browser/api/atom_api_menu.h"
2015-06-23 15:40:41 +00:00
#include "atom/browser/api/atom_api_session.h"
2015-10-28 11:34:01 +00:00
#include "atom/browser/api/atom_api_web_contents.h"
2014-08-19 13:26:45 +00:00
#include "atom/browser/atom_browser_context.h"
#include "atom/browser/atom_browser_main_parts.h"
2014-08-19 13:26:45 +00:00
#include "atom/browser/browser.h"
2015-10-28 11:34:01 +00:00
#include "atom/browser/login_handler.h"
#include "atom/browser/relauncher.h"
#include "atom/common/atom_command_line.h"
#include "atom/common/native_mate_converters/callback.h"
2014-08-19 13:26:45 +00:00
#include "atom/common/native_mate_converters/file_path_converter.h"
2015-11-18 02:39:25 +00:00
#include "atom/common/native_mate_converters/gurl_converter.h"
#include "atom/common/native_mate_converters/image_converter.h"
2016-04-18 15:35:33 +00:00
#include "atom/common/native_mate_converters/net_converter.h"
#include "atom/common/native_mate_converters/value_converter.h"
#include "atom/common/node_includes.h"
#include "atom/common/options_switches.h"
#include "base/command_line.h"
#include "base/environment.h"
#include "base/files/file_path.h"
2016-04-18 05:11:31 +00:00
#include "base/files/file_util.h"
#include "base/path_service.h"
2016-05-10 23:47:50 +00:00
#include "base/strings/string_util.h"
2015-01-19 01:11:27 +00:00
#include "brightray/browser/brightray_paths.h"
2015-11-13 05:05:16 +00:00
#include "chrome/common/chrome_paths.h"
#include "content/public/browser/browser_accessibility_state.h"
#include "content/public/browser/client_certificate_delegate.h"
2015-06-25 12:28:07 +00:00
#include "content/public/browser/gpu_data_manager.h"
2015-11-18 02:39:25 +00:00
#include "content/public/browser/render_frame_host.h"
#include "content/public/common/content_switches.h"
2014-04-17 09:13:46 +00:00
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder.h"
#include "net/ssl/ssl_cert_request_info.h"
2015-09-16 08:16:21 +00:00
#include "ui/base/l10n/l10n_util.h"
#include "ui/gfx/image/image.h"
2015-04-29 07:13:46 +00:00
#if defined(OS_WIN)
#include "base/strings/utf_string_conversions.h"
#endif
2014-04-17 09:13:46 +00:00
using atom::Browser;
2014-11-17 09:19:41 +00:00
namespace mate {
2014-11-17 09:35:51 +00:00
#if defined(OS_WIN)
2014-11-17 09:19:41 +00:00
template<>
struct Converter<Browser::UserTask> {
2015-05-22 11:11:22 +00:00
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
2014-11-17 09:19:41 +00:00
Browser::UserTask* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
if (!dict.Get("program", &(out->program)) ||
!dict.Get("title", &(out->title)))
return false;
2014-11-17 09:35:51 +00:00
if (dict.Get("iconPath", &(out->icon_path)) &&
!dict.Get("iconIndex", &(out->icon_index)))
return false;
dict.Get("arguments", &(out->arguments));
dict.Get("description", &(out->description));
return true;
2014-11-17 09:19:41 +00:00
}
};
2014-11-17 09:35:51 +00:00
#endif
2014-11-17 09:19:41 +00:00
2016-07-07 23:29:09 +00:00
template<>
struct Converter<Browser::LoginItemSettings> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
Browser::LoginItemSettings* out) {
mate::Dictionary dict;
if (!ConvertFromV8(isolate, val, &dict))
return false;
dict.Get("openAtLogin", &(out->open_at_login));
dict.Get("openAsHidden", &(out->open_as_hidden));
return true;
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
Browser::LoginItemSettings val) {
mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate);
dict.Set("openAtLogin", val.open_at_login);
dict.Set("openAsHidden", val.open_as_hidden);
dict.Set("restoreState", val.restore_state);
dict.Set("wasOpenedAtLogin", val.opened_at_login);
dict.Set("wasOpenedAsHidden", val.opened_as_hidden);
2016-07-07 23:29:09 +00:00
return dict.GetHandle();
}
};
2014-11-17 09:19:41 +00:00
} // namespace mate
2013-05-02 16:05:09 +00:00
namespace atom {
namespace api {
2014-08-19 13:26:45 +00:00
namespace {
// Return the path constant from string.
int GetPathConstant(const std::string& name) {
if (name == "appData")
return brightray::DIR_APP_DATA;
else if (name == "userData")
return brightray::DIR_USER_DATA;
else if (name == "cache")
return brightray::DIR_CACHE;
else if (name == "userCache")
return brightray::DIR_USER_CACHE;
else if (name == "home")
return base::DIR_HOME;
2015-01-19 05:31:09 +00:00
else if (name == "temp")
return base::DIR_TEMP;
2015-11-13 05:05:16 +00:00
else if (name == "userDesktop" || name == "desktop")
2015-01-19 05:31:09 +00:00
return base::DIR_USER_DESKTOP;
else if (name == "exe")
return base::FILE_EXE;
else if (name == "module")
return base::FILE_MODULE;
2015-11-13 05:05:16 +00:00
else if (name == "documents")
return chrome::DIR_USER_DOCUMENTS;
else if (name == "downloads")
return chrome::DIR_DEFAULT_DOWNLOADS;
else if (name == "music")
return chrome::DIR_USER_MUSIC;
else if (name == "pictures")
return chrome::DIR_USER_PICTURES;
else if (name == "videos")
return chrome::DIR_USER_VIDEOS;
else if (name == "pepperFlashSystemPlugin")
return chrome::FILE_PEPPER_FLASH_SYSTEM_PLUGIN;
else
return -1;
}
bool NotificationCallbackWrapper(
const ProcessSingleton::NotificationCallback& callback,
2015-10-22 11:02:21 +00:00
const base::CommandLine::StringVector& cmd,
const base::FilePath& cwd) {
// Make sure the callback is called after app gets ready.
if (Browser::Get()->is_ready()) {
callback.Run(cmd, cwd);
} else {
scoped_refptr<base::SingleThreadTaskRunner> task_runner(
base::ThreadTaskRunnerHandle::Get());
task_runner->PostTask(
FROM_HERE, base::Bind(base::IgnoreResult(callback), cmd, cwd));
}
// ProcessSingleton needs to know whether current process is quiting.
return !Browser::Get()->is_shutting_down();
}
void OnClientCertificateSelected(
v8::Isolate* isolate,
std::shared_ptr<content::ClientCertificateDelegate> delegate,
mate::Arguments* args) {
mate::Dictionary cert_data;
if (!args->GetNext(&cert_data)) {
args->ThrowError();
return;
}
v8::Local<v8::Object> data;
if (!cert_data.Get("data", &data))
return;
auto certs = net::X509Certificate::CreateCertificateListFromBytes(
node::Buffer::Data(data), node::Buffer::Length(data),
net::X509Certificate::FORMAT_AUTO);
if (certs.size() > 0)
delegate->ContinueWithCertificate(certs[0].get());
}
2015-10-28 11:34:01 +00:00
void PassLoginInformation(scoped_refptr<LoginHandler> login_handler,
mate::Arguments* args) {
base::string16 username, password;
if (args->GetNext(&username) && args->GetNext(&password))
login_handler->Login(username, password);
else
login_handler->CancelAuth();
}
#if defined(USE_NSS_CERTS)
2016-04-18 15:35:33 +00:00
int ImportIntoCertStore(
2016-04-18 05:11:31 +00:00
CertificateManagerModel* model,
2016-04-18 15:35:33 +00:00
const base::DictionaryValue& options) {
std::string file_data, cert_path;
base::string16 password;
net::CertificateList imported_certs;
int rv = -1;
options.GetString("certificate", &cert_path);
2016-04-18 15:35:33 +00:00
options.GetString("password", &password);
if (!cert_path.empty()) {
if (base::ReadFileToString(base::FilePath(cert_path), &file_data)) {
auto module = model->cert_db()->GetPublicModule();
rv = model->ImportFromPKCS12(module,
file_data,
password,
true,
&imported_certs);
if (imported_certs.size() > 1) {
auto it = imported_certs.begin();
2016-04-18 16:56:37 +00:00
++it; // skip first which would be the client certificate.
2016-04-18 15:35:33 +00:00
for (; it != imported_certs.end(); ++it)
rv &= model->SetCertTrust(it->get(),
2016-04-18 16:56:37 +00:00
net::CA_CERT,
net::NSSCertDatabase::TRUSTED_SSL);
2016-04-18 15:35:33 +00:00
}
}
2016-04-18 05:11:31 +00:00
}
2016-04-18 15:35:33 +00:00
return rv;
2016-04-18 05:11:31 +00:00
}
#endif
2016-04-18 05:11:31 +00:00
2014-08-19 13:26:45 +00:00
} // namespace
2016-04-25 01:17:54 +00:00
App::App(v8::Isolate* isolate) {
2015-11-18 02:07:03 +00:00
static_cast<AtomBrowserClient*>(AtomBrowserClient::Get())->set_delegate(this);
Browser::Get()->AddObserver(this);
2015-06-25 12:28:07 +00:00
content::GpuDataManager::GetInstance()->AddObserver(this);
2016-04-25 01:17:54 +00:00
Init(isolate);
}
App::~App() {
2015-11-18 02:07:03 +00:00
static_cast<AtomBrowserClient*>(AtomBrowserClient::Get())->set_delegate(
nullptr);
Browser::Get()->RemoveObserver(this);
2015-06-25 12:28:07 +00:00
content::GpuDataManager::GetInstance()->RemoveObserver(this);
}
void App::OnBeforeQuit(bool* prevent_default) {
*prevent_default = Emit("before-quit");
}
void App::OnWillQuit(bool* prevent_default) {
*prevent_default = Emit("will-quit");
}
void App::OnWindowAllClosed() {
Emit("window-all-closed");
}
2015-12-10 02:19:51 +00:00
void App::OnQuit() {
int exitCode = AtomBrowserMainParts::Get()->GetExitCode();
Emit("quit", exitCode);
2015-10-21 20:17:56 +00:00
if (process_singleton_.get()) {
2015-10-22 06:59:12 +00:00
process_singleton_->Cleanup();
process_singleton_.reset();
}
2014-09-25 13:47:54 +00:00
}
void App::OnOpenFile(bool* prevent_default, const std::string& file_path) {
*prevent_default = Emit("open-file", file_path);
}
void App::OnOpenURL(const std::string& url) {
Emit("open-url", url);
}
2015-09-15 02:05:53 +00:00
void App::OnActivate(bool has_visible_windows) {
Emit("activate", has_visible_windows);
}
void App::OnWillFinishLaunching() {
Emit("will-finish-launching");
}
void App::OnFinishLaunching() {
Emit("ready");
}
void App::OnAccessibilitySupportChanged() {
Emit("accessibility-support-changed", IsAccessibilitySupportEnabled());
}
2016-05-05 03:26:23 +00:00
#if defined(OS_MACOSX)
void App::OnContinueUserActivity(
bool* prevent_default,
const std::string& type,
const base::DictionaryValue& user_info) {
2016-04-30 05:08:19 +00:00
*prevent_default = Emit("continue-activity", type, user_info);
}
2016-05-05 03:26:23 +00:00
#endif
void App::OnLogin(LoginHandler* login_handler,
const base::DictionaryValue& request_details) {
2015-10-28 13:00:39 +00:00
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
2015-11-18 02:39:25 +00:00
bool prevent_default = Emit(
"login",
WebContents::CreateFrom(isolate(), login_handler->GetWebContents()),
request_details,
2015-11-18 02:39:25 +00:00
login_handler->auth_info(),
base::Bind(&PassLoginInformation, make_scoped_refptr(login_handler)));
2015-10-28 11:34:01 +00:00
2015-10-28 13:00:39 +00:00
// Default behavior is to always cancel the auth.
2015-10-28 11:34:01 +00:00
if (!prevent_default)
login_handler->CancelAuth();
}
2016-03-31 14:21:18 +00:00
void App::OnCreateWindow(const GURL& target_url,
const std::string& frame_name,
WindowOpenDisposition disposition,
int render_process_id,
int render_frame_id) {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
content::RenderFrameHost* rfh =
content::RenderFrameHost::FromID(render_process_id, render_frame_id);
content::WebContents* web_contents =
content::WebContents::FromRenderFrameHost(rfh);
if (web_contents) {
auto api_web_contents = WebContents::CreateFrom(isolate(), web_contents);
2016-04-01 07:34:20 +00:00
api_web_contents->OnCreateWindow(target_url, frame_name, disposition);
2016-03-31 14:21:18 +00:00
}
}
2015-11-18 02:39:25 +00:00
void App::AllowCertificateError(
2016-03-08 14:28:53 +00:00
content::WebContents* web_contents,
2015-11-18 02:39:25 +00:00
int cert_error,
const net::SSLInfo& ssl_info,
const GURL& request_url,
content::ResourceType resource_type,
bool overridable,
bool strict_enforcement,
bool expired_previous_decision,
const base::Callback<void(bool)>& callback,
content::CertificateRequestResultType* request) {
v8::Locker locker(isolate());
v8::HandleScope handle_scope(isolate());
bool prevent_default = Emit("certificate-error",
WebContents::CreateFrom(isolate(), web_contents),
request_url,
2015-11-18 03:35:26 +00:00
net::ErrorToString(cert_error),
2015-11-18 02:39:25 +00:00
ssl_info.cert,
callback);
// Deny the certificate by default.
if (!prevent_default)
*request = content::CERTIFICATE_REQUEST_RESULT_TYPE_DENY;
}
2015-11-18 02:07:03 +00:00
void App::SelectClientCertificate(
content::WebContents* web_contents,
net::SSLCertRequestInfo* cert_request_info,
2016-05-23 01:59:39 +00:00
std::unique_ptr<content::ClientCertificateDelegate> delegate) {
2015-11-18 02:07:03 +00:00
std::shared_ptr<content::ClientCertificateDelegate>
shared_delegate(delegate.release());
bool prevent_default =
Emit("select-client-certificate",
2015-11-18 02:39:25 +00:00
WebContents::CreateFrom(isolate(), web_contents),
2015-11-18 02:07:03 +00:00
cert_request_info->host_and_port.ToString(),
cert_request_info->client_certs,
base::Bind(&OnClientCertificateSelected,
isolate(),
shared_delegate));
// Default to first certificate from the platform store.
if (!prevent_default)
shared_delegate->ContinueWithCertificate(
cert_request_info->client_certs[0].get());
}
2015-06-25 12:28:07 +00:00
void App::OnGpuProcessCrashed(base::TerminationStatus exit_code) {
Emit("gpu-process-crashed");
2015-06-25 12:28:07 +00:00
}
base::FilePath App::GetPath(mate::Arguments* args, const std::string& name) {
bool succeed = false;
base::FilePath path;
int key = GetPathConstant(name);
if (key >= 0)
succeed = PathService::Get(key, &path);
2015-11-13 05:05:16 +00:00
if (!succeed)
args->ThrowError("Failed to get path");
2015-01-19 01:11:27 +00:00
return path;
}
void App::SetPath(mate::Arguments* args,
const std::string& name,
const base::FilePath& path) {
if (!path.IsAbsolute()) {
args->ThrowError("path must be absolute");
return;
}
bool succeed = false;
int key = GetPathConstant(name);
if (key >= 0)
succeed = PathService::OverrideAndCreateIfNeeded(key, path, true, false);
if (!succeed)
args->ThrowError("Failed to set path");
}
2014-09-18 11:12:24 +00:00
void App::SetDesktopName(const std::string& desktop_name) {
#if defined(OS_LINUX)
2016-05-23 01:59:39 +00:00
std::unique_ptr<base::Environment> env(base::Environment::Create());
2014-09-18 11:12:24 +00:00
env->SetVar("CHROME_DESKTOP", desktop_name);
#endif
}
2015-09-16 08:16:21 +00:00
std::string App::GetLocale() {
return l10n_util::GetApplicationLocale("");
}
bool App::MakeSingleInstance(
const ProcessSingleton::NotificationCallback& callback) {
2015-10-22 06:59:12 +00:00
if (process_singleton_.get())
return false;
2015-10-21 20:17:56 +00:00
2015-10-22 06:59:12 +00:00
base::FilePath user_dir;
PathService::Get(brightray::DIR_USER_DATA, &user_dir);
process_singleton_.reset(new ProcessSingleton(
user_dir, base::Bind(NotificationCallbackWrapper, callback)));
2015-10-20 01:02:54 +00:00
2015-10-22 06:59:12 +00:00
switch (process_singleton_->NotifyOtherProcessOrCreate()) {
2015-10-19 23:21:03 +00:00
case ProcessSingleton::NotifyResult::LOCK_ERROR:
case ProcessSingleton::NotifyResult::PROFILE_IN_USE:
case ProcessSingleton::NotifyResult::PROCESS_NOTIFIED:
2015-10-22 06:59:12 +00:00
process_singleton_.reset();
2015-10-19 23:21:03 +00:00
return true;
2015-10-22 08:55:54 +00:00
case ProcessSingleton::NotifyResult::PROCESS_NONE:
default: // Shouldn't be needed, but VS warns if it is not there.
return false;
2015-10-19 23:21:03 +00:00
}
}
void App::ReleaseSingleInstance() {
if (process_singleton_.get()) {
process_singleton_->Cleanup();
process_singleton_.reset();
}
}
bool App::Relaunch(mate::Arguments* js_args) {
// Parse parameters.
bool override_argv = false;
base::FilePath exec_path;
relauncher::StringVector args;
mate::Dictionary options;
if (js_args->GetNext(&options)) {
2016-06-03 03:08:45 +00:00
if (options.Get("execPath", &exec_path) | options.Get("args", &args))
override_argv = true;
}
if (!override_argv) {
#if defined(OS_WIN)
const relauncher::StringVector& argv = atom::AtomCommandLine::wargv();
#else
const relauncher::StringVector& argv = atom::AtomCommandLine::argv();
#endif
return relauncher::RelaunchApp(argv);
}
relauncher::StringVector argv;
argv.reserve(1 + args.size());
if (exec_path.empty()) {
base::FilePath current_exe_path;
PathService::Get(base::FILE_EXE, &current_exe_path);
argv.push_back(current_exe_path.value());
} else {
argv.push_back(exec_path.value());
}
argv.insert(argv.end(), args.begin(), args.end());
return relauncher::RelaunchApp(argv);
}
void App::DisableHardwareAcceleration(mate::Arguments* args) {
if (Browser::Get()->is_ready()) {
args->ThrowError("app.disableHardwareAcceleration() can only be called "
"before app is ready");
return;
}
content::GpuDataManager::GetInstance()->DisableHardwareAcceleration();
}
bool App::IsAccessibilitySupportEnabled() {
auto ax_state = content::BrowserAccessibilityState::GetInstance();
return ax_state->IsAccessibleBrowser();
}
#if defined(USE_NSS_CERTS)
void App::ImportCertificate(
2016-04-18 15:35:33 +00:00
const base::DictionaryValue& options,
2016-04-18 05:11:31 +00:00
const net::CompletionCallback& callback) {
2016-07-12 13:45:15 +00:00
auto browser_context = AtomBrowserContext::From("", false);
2016-04-18 05:11:31 +00:00
if (!certificate_manager_model_) {
2016-05-23 01:59:39 +00:00
std::unique_ptr<base::DictionaryValue> copy = options.CreateDeepCopy();
CertificateManagerModel::Create(
browser_context.get(),
2016-04-18 15:35:33 +00:00
base::Bind(&App::OnCertificateManagerModelCreated,
base::Unretained(this),
base::Passed(&copy),
callback));
2016-04-18 05:11:31 +00:00
return;
}
2016-04-18 15:35:33 +00:00
int rv = ImportIntoCertStore(certificate_manager_model_.get(), options);
2016-04-18 05:11:31 +00:00
callback.Run(rv);
}
void App::OnCertificateManagerModelCreated(
2016-05-23 01:59:39 +00:00
std::unique_ptr<base::DictionaryValue> options,
2016-04-18 05:11:31 +00:00
const net::CompletionCallback& callback,
2016-05-23 01:59:39 +00:00
std::unique_ptr<CertificateManagerModel> model) {
2016-04-18 05:11:31 +00:00
certificate_manager_model_ = std::move(model);
2016-04-18 15:35:33 +00:00
int rv = ImportIntoCertStore(certificate_manager_model_.get(),
*(options.get()));
2016-04-18 05:11:31 +00:00
callback.Run(rv);
}
#endif
2016-04-18 05:11:31 +00:00
2016-04-25 01:17:54 +00:00
// static
mate::Handle<App> App::Create(v8::Isolate* isolate) {
2016-04-25 01:40:19 +00:00
return mate::CreateHandle(isolate, new App(isolate));
2016-04-25 01:17:54 +00:00
}
// static
void App::BuildPrototype(
v8::Isolate* isolate, v8::Local<v8::FunctionTemplate> prototype) {
2016-08-02 10:28:12 +00:00
prototype->SetClassName(mate::StringToV8(isolate, "App"));
auto browser = base::Unretained(Browser::Get());
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("quit", base::Bind(&Browser::Quit, browser))
2015-12-10 02:09:59 +00:00
.SetMethod("exit", base::Bind(&Browser::Exit, browser))
.SetMethod("focus", base::Bind(&Browser::Focus, browser))
.SetMethod("getVersion", base::Bind(&Browser::GetVersion, browser))
.SetMethod("setVersion", base::Bind(&Browser::SetVersion, browser))
.SetMethod("getName", base::Bind(&Browser::GetName, browser))
.SetMethod("setName", base::Bind(&Browser::SetName, browser))
.SetMethod("isReady", base::Bind(&Browser::is_ready, browser))
2014-11-17 05:05:06 +00:00
.SetMethod("addRecentDocument",
base::Bind(&Browser::AddRecentDocument, browser))
2014-11-17 08:13:47 +00:00
.SetMethod("clearRecentDocuments",
base::Bind(&Browser::ClearRecentDocuments, browser))
.SetMethod("setAppUserModelId",
base::Bind(&Browser::SetAppUserModelID, browser))
2016-04-25 05:17:01 +00:00
.SetMethod("isDefaultProtocolClient",
base::Bind(&Browser::IsDefaultProtocolClient, browser))
.SetMethod("setAsDefaultProtocolClient",
base::Bind(&Browser::SetAsDefaultProtocolClient, browser))
.SetMethod("removeAsDefaultProtocolClient",
base::Bind(&Browser::RemoveAsDefaultProtocolClient, browser))
2016-07-01 08:39:01 +00:00
.SetMethod("setBadgeCount", base::Bind(&Browser::SetBadgeCount, browser))
.SetMethod("getBadgeCount", base::Bind(&Browser::GetBadgeCount, browser))
.SetMethod("getLoginItemSettings",
base::Bind(&Browser::GetLoginItemSettings, browser))
.SetMethod("setLoginItemSettings",
base::Bind(&Browser::SetLoginItemSettings, browser))
2016-01-29 21:51:06 +00:00
#if defined(OS_MACOSX)
.SetMethod("hide", base::Bind(&Browser::Hide, browser))
2016-01-30 20:40:32 +00:00
.SetMethod("show", base::Bind(&Browser::Show, browser))
2016-04-30 05:05:36 +00:00
.SetMethod("setUserActivity",
base::Bind(&Browser::SetUserActivity, browser))
2016-07-07 21:29:43 +00:00
.SetMethod("getCurrentActivityType",
base::Bind(&Browser::GetCurrentActivityType, browser))
2016-01-29 21:51:06 +00:00
#endif
2014-11-17 09:19:41 +00:00
#if defined(OS_WIN)
2016-07-01 08:39:01 +00:00
.SetMethod("setUserTasks", base::Bind(&Browser::SetUserTasks, browser))
#endif
#if defined(OS_LINUX)
.SetMethod("isUnityRunning",
base::Bind(&Browser::IsUnityRunning, browser))
2014-11-17 09:19:41 +00:00
#endif
.SetMethod("setPath", &App::SetPath)
.SetMethod("getPath", &App::GetPath)
2015-04-28 22:49:16 +00:00
.SetMethod("setDesktopName", &App::SetDesktopName)
2015-09-16 08:16:21 +00:00
.SetMethod("getLocale", &App::GetLocale)
#if defined(USE_NSS_CERTS)
.SetMethod("importCertificate", &App::ImportCertificate)
2016-04-18 15:35:33 +00:00
#endif
.SetMethod("makeSingleInstance", &App::MakeSingleInstance)
.SetMethod("releaseSingleInstance", &App::ReleaseSingleInstance)
.SetMethod("relaunch", &App::Relaunch)
.SetMethod("isAccessibilitySupportEnabled",
&App::IsAccessibilitySupportEnabled)
.SetMethod("disableHardwareAcceleration",
&App::DisableHardwareAcceleration);
2013-05-30 11:24:47 +00:00
}
2014-04-17 09:13:46 +00:00
} // namespace api
2014-04-17 09:13:46 +00:00
} // namespace atom
2014-04-17 09:13:46 +00:00
namespace {
2014-04-17 09:13:46 +00:00
void AppendSwitch(const std::string& switch_string, mate::Arguments* args) {
2015-03-10 22:27:27 +00:00
auto command_line = base::CommandLine::ForCurrentProcess();
2016-05-10 23:47:50 +00:00
if (base::EndsWith(switch_string, "-path",
base::CompareCase::INSENSITIVE_ASCII) ||
switch_string == switches::kLogNetLog) {
base::FilePath path;
args->GetNext(&path);
command_line->AppendSwitchPath(switch_string, path);
return;
}
std::string value;
2014-04-17 09:13:46 +00:00
if (args->GetNext(&value))
2015-03-10 22:27:27 +00:00
command_line->AppendSwitchASCII(switch_string, value);
2014-04-17 09:13:46 +00:00
else
2015-03-10 22:27:27 +00:00
command_line->AppendSwitch(switch_string);
}
2013-08-06 08:19:56 +00:00
#if defined(OS_MACOSX)
2014-04-17 09:13:46 +00:00
int DockBounce(const std::string& type) {
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);
2014-04-17 09:13:46 +00:00
return request_id;
2013-08-06 08:19:56 +00:00
}
2014-11-16 15:04:31 +00:00
void DockSetMenu(atom::api::Menu* menu) {
Browser::Get()->DockSetMenu(menu->model());
}
2014-04-17 09:13:46 +00:00
#endif
2013-08-06 08:19:56 +00:00
2015-05-22 11:11:22 +00:00
void Initialize(v8::Local<v8::Object> exports, v8::Local<v8::Value> unused,
v8::Local<v8::Context> context, void* priv) {
v8::Isolate* isolate = context->GetIsolate();
2015-03-10 22:27:27 +00:00
auto command_line = base::CommandLine::ForCurrentProcess();
2013-08-06 08:19:56 +00:00
2014-04-17 09:13:46 +00:00
mate::Dictionary dict(isolate, exports);
2016-08-02 11:38:35 +00:00
dict.Set("App", atom::api::App::GetConstructor(isolate)->GetFunction());
2014-04-17 09:13:46 +00:00
dict.Set("app", atom::api::App::Create(isolate));
dict.SetMethod("appendSwitch", &AppendSwitch);
dict.SetMethod("appendArgument",
2015-03-10 22:27:27 +00:00
base::Bind(&base::CommandLine::AppendArg,
2014-04-17 09:13:46 +00:00
base::Unretained(command_line)));
2013-08-06 08:19:56 +00:00
#if defined(OS_MACOSX)
2014-11-16 15:04:31 +00:00
auto browser = base::Unretained(Browser::Get());
2014-04-17 09:13:46 +00:00
dict.SetMethod("dockBounce", &DockBounce);
dict.SetMethod("dockCancelBounce",
2014-11-16 15:04:31 +00:00
base::Bind(&Browser::DockCancelBounce, browser));
dict.SetMethod("dockDownloadFinished",
base::Bind(&Browser::DockDownloadFinished, browser));
2014-04-17 09:13:46 +00:00
dict.SetMethod("dockSetBadgeText",
2014-11-16 15:04:31 +00:00
base::Bind(&Browser::DockSetBadgeText, browser));
2014-04-17 09:13:46 +00:00
dict.SetMethod("dockGetBadgeText",
2014-11-16 15:04:31 +00:00
base::Bind(&Browser::DockGetBadgeText, browser));
dict.SetMethod("dockHide", base::Bind(&Browser::DockHide, browser));
dict.SetMethod("dockShow", base::Bind(&Browser::DockShow, browser));
2016-08-01 22:22:37 +00:00
dict.SetMethod("dockIsVisible", base::Bind(&Browser::DockIsVisible, browser));
2014-11-16 15:04:31 +00:00
dict.SetMethod("dockSetMenu", &DockSetMenu);
dict.SetMethod("dockSetIcon", base::Bind(&Browser::DockSetIcon, browser));
2014-04-17 09:13:46 +00:00
#endif
}
} // namespace
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_app, Initialize)