2013-05-02 16:05:09 +00:00
|
|
|
// Copyright (c) 2013 GitHub, Inc. All rights reserved.
|
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
|
|
|
|
2014-03-16 01:13:06 +00:00
|
|
|
#include <string>
|
2014-08-13 12:24:35 +00:00
|
|
|
#include <vector>
|
2014-03-16 01:13:06 +00:00
|
|
|
|
2014-08-19 13:26:45 +00:00
|
|
|
#include "atom/browser/atom_browser_context.h"
|
|
|
|
#include "atom/browser/browser.h"
|
|
|
|
#include "atom/common/native_mate_converters/file_path_converter.h"
|
|
|
|
#include "atom/common/native_mate_converters/gurl_converter.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"
|
2014-08-12 15:01:56 +00:00
|
|
|
#include "base/environment.h"
|
|
|
|
#include "base/files/file_path.h"
|
|
|
|
#include "base/path_service.h"
|
2014-08-19 13:26:45 +00:00
|
|
|
#include "native_mate/callback.h"
|
2014-04-17 09:13:46 +00:00
|
|
|
#include "native_mate/dictionary.h"
|
|
|
|
#include "native_mate/object_template_builder.h"
|
2014-10-11 11:11:34 +00:00
|
|
|
#include "net/base/load_flags.h"
|
2014-08-19 13:26:45 +00:00
|
|
|
#include "net/proxy/proxy_service.h"
|
|
|
|
#include "net/url_request/url_request_context.h"
|
|
|
|
#include "net/url_request/url_request_context_getter.h"
|
2013-12-11 07:48:19 +00:00
|
|
|
|
2014-04-17 05:45:14 +00:00
|
|
|
#include "atom/common/node_includes.h"
|
2013-05-02 16:05:09 +00:00
|
|
|
|
2014-08-12 15:01:56 +00:00
|
|
|
#if defined(OS_LINUX)
|
|
|
|
#include "base/nix/xdg_util.h"
|
|
|
|
#endif
|
|
|
|
|
2014-04-17 09:13:46 +00:00
|
|
|
using atom::Browser;
|
|
|
|
|
2013-05-02 16:05:09 +00:00
|
|
|
namespace atom {
|
|
|
|
|
|
|
|
namespace api {
|
|
|
|
|
2014-08-19 13:26:45 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ResolveProxyHelper {
|
|
|
|
public:
|
|
|
|
ResolveProxyHelper(const GURL& url, App::ResolveProxyCallback callback)
|
|
|
|
: callback_(callback) {
|
|
|
|
net::ProxyService* proxy_service = AtomBrowserContext::Get()->
|
|
|
|
url_request_context_getter()->GetURLRequestContext()->proxy_service();
|
|
|
|
|
|
|
|
// Start the request.
|
|
|
|
int result = proxy_service->ResolveProxy(
|
2014-10-11 11:11:34 +00:00
|
|
|
url, net::LOAD_NORMAL, &proxy_info_,
|
2014-08-19 13:26:45 +00:00
|
|
|
base::Bind(&ResolveProxyHelper::OnResolveProxyCompleted,
|
|
|
|
base::Unretained(this)),
|
2014-10-11 11:11:34 +00:00
|
|
|
&pac_req_, nullptr, net::BoundNetLog());
|
2014-08-19 13:26:45 +00:00
|
|
|
|
|
|
|
// Completed synchronously.
|
|
|
|
if (result != net::ERR_IO_PENDING)
|
|
|
|
OnResolveProxyCompleted(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnResolveProxyCompleted(int result) {
|
|
|
|
std::string proxy;
|
|
|
|
if (result == net::OK)
|
|
|
|
proxy = proxy_info_.ToPacString();
|
|
|
|
callback_.Run(proxy);
|
|
|
|
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
App::ResolveProxyCallback callback_;
|
|
|
|
net::ProxyInfo proxy_info_;
|
|
|
|
net::ProxyService::PacRequest* pac_req_;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-04-17 09:13:46 +00:00
|
|
|
App::App() {
|
2013-05-03 02:53:54 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2014-09-25 13:47:54 +00:00
|
|
|
void App::OnQuit() {
|
|
|
|
Emit("quit");
|
|
|
|
}
|
|
|
|
|
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);
|
2014-04-17 09:13:46 +00:00
|
|
|
*prevent_default = Emit("open-file", args);
|
2013-05-30 08:03:10 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 08:10:38 +00:00
|
|
|
void App::OnOpenURL(const std::string& url) {
|
|
|
|
base::ListValue args;
|
|
|
|
args.AppendString(url);
|
2014-04-17 09:13:46 +00:00
|
|
|
Emit("open-url", args);
|
2013-07-10 08:10:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-08-15 14:52:16 +00:00
|
|
|
base::FilePath App::GetDataPath() {
|
2014-08-12 15:01:56 +00:00
|
|
|
base::FilePath path;
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
scoped_ptr<base::Environment> env(base::Environment::Create());
|
|
|
|
path = base::nix::GetXDGDirectory(env.get(),
|
|
|
|
base::nix::kXdgConfigHomeEnvVar,
|
|
|
|
base::nix::kDotConfigDir);
|
|
|
|
#else
|
2014-08-19 13:26:45 +00:00
|
|
|
PathService::Get(base::DIR_APP_DATA, &path);
|
2014-08-12 15:01:56 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-17 03:33:55 +00:00
|
|
|
return path.Append(base::FilePath::FromUTF8Unsafe(
|
|
|
|
Browser::Get()->GetName()));
|
2014-08-12 15:01:56 +00:00
|
|
|
}
|
|
|
|
|
2014-08-19 13:26:45 +00:00
|
|
|
void App::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
|
|
|
|
new ResolveProxyHelper(url, callback);
|
|
|
|
}
|
|
|
|
|
2014-09-18 11:12:24 +00:00
|
|
|
void App::SetDesktopName(const std::string& desktop_name) {
|
|
|
|
#if defined(OS_LINUX)
|
|
|
|
scoped_ptr<base::Environment> env(base::Environment::Create());
|
|
|
|
env->SetVar("CHROME_DESKTOP", desktop_name);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-04-17 09:13:46 +00:00
|
|
|
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,
|
2014-08-12 15:01:56 +00:00
|
|
|
base::Unretained(browser)))
|
2014-08-19 13:26:45 +00:00
|
|
|
.SetMethod("getDataPath", &App::GetDataPath)
|
2014-09-18 11:12:24 +00:00
|
|
|
.SetMethod("resolveProxy", &App::ResolveProxy)
|
|
|
|
.SetMethod("setDesktopName", &App::SetDesktopName);
|
2013-05-30 11:24:47 +00:00
|
|
|
}
|
|
|
|
|
2013-06-19 05:43:48 +00:00
|
|
|
// static
|
2014-04-17 09:13:46 +00:00
|
|
|
mate::Handle<App> App::Create(v8::Isolate* isolate) {
|
|
|
|
return CreateHandle(isolate, new App);
|
2013-12-05 02:26:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 09:13:46 +00:00
|
|
|
} // namespace api
|
2013-12-05 02:32:58 +00:00
|
|
|
|
2014-04-17 09:13:46 +00:00
|
|
|
} // namespace atom
|
2013-12-05 02:32:58 +00:00
|
|
|
|
|
|
|
|
2014-04-17 09:13:46 +00:00
|
|
|
namespace {
|
2013-05-17 07:39:44 +00:00
|
|
|
|
2014-04-17 09:13:46 +00:00
|
|
|
void AppendSwitch(const std::string& switch_string, mate::Arguments* args) {
|
2013-09-24 01:41:54 +00:00
|
|
|
std::string value;
|
2014-04-17 09:13:46 +00:00
|
|
|
if (args->GetNext(&value))
|
|
|
|
CommandLine::ForCurrentProcess()->AppendSwitchASCII(switch_string, value);
|
|
|
|
else
|
|
|
|
CommandLine::ForCurrentProcess()->AppendSwitch(switch_string);
|
2013-05-17 07:39:44 +00:00
|
|
|
}
|
|
|
|
|
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-04-17 09:13:46 +00:00
|
|
|
#endif
|
2013-08-06 08:19:56 +00:00
|
|
|
|
2014-06-29 12:48:44 +00:00
|
|
|
void Initialize(v8::Handle<v8::Object> exports, v8::Handle<v8::Value> unused,
|
|
|
|
v8::Handle<v8::Context> context, void* priv) {
|
|
|
|
v8::Isolate* isolate = context->GetIsolate();
|
2014-04-17 09:13:46 +00:00
|
|
|
Browser* browser = Browser::Get();
|
|
|
|
CommandLine* command_line = CommandLine::ForCurrentProcess();
|
2013-08-06 08:19:56 +00:00
|
|
|
|
2014-04-17 09:13:46 +00:00
|
|
|
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)));
|
2013-08-06 08:19:56 +00:00
|
|
|
#if defined(OS_MACOSX)
|
2014-04-17 09:13:46 +00:00
|
|
|
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)));
|
2014-06-25 03:55:33 +00:00
|
|
|
dict.SetMethod("dockHide",
|
|
|
|
base::Bind(&Browser::DockHide, base::Unretained(browser)));
|
|
|
|
dict.SetMethod("dockShow",
|
|
|
|
base::Bind(&Browser::DockShow, base::Unretained(browser)));
|
2014-04-17 09:13:46 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-06-29 12:48:44 +00:00
|
|
|
NODE_MODULE_CONTEXT_AWARE_BUILTIN(atom_browser_app, Initialize)
|