Merge pull request #3145 from atom/single-instance

Implement Single-Instance for Windows / Linux
This commit is contained in:
Cheng Zhao 2015-10-22 14:18:31 +08:00
commit 04d3eed60e
12 changed files with 1924 additions and 0 deletions

View file

@ -19,6 +19,7 @@
#include "atom/browser/api/atom_api_web_contents.h"
#include "atom/common/native_mate_converters/callback.h"
#include "atom/common/native_mate_converters/file_path_converter.h"
#include "atom/common/native_mate_converters/command_line_converter.h"
#include "atom/common/node_includes.h"
#include "atom/common/options_switches.h"
#include "base/command_line.h"
@ -160,6 +161,14 @@ void App::OnWindowAllClosed() {
void App::OnQuit() {
Emit("quit");
if (process_singleton_.get()) {
if (process_notify_result_ == ProcessSingleton::PROCESS_NONE) {
process_singleton_->Cleanup();
}
process_singleton_.reset();
}
}
void App::OnOpenFile(bool* prevent_default, const std::string& file_path) {
@ -187,6 +196,10 @@ void App::OnFinishLaunching() {
auto handle = Session::CreateFrom(isolate(), browser_context);
default_session_.Reset(isolate(), handle.ToV8());
if (process_singleton_.get()) {
process_singleton_startup_lock_->Unlock();
}
Emit("ready");
}
@ -268,6 +281,39 @@ v8::Local<v8::Value> App::DefaultSession(v8::Isolate* isolate) {
return v8::Local<v8::Value>::New(isolate, default_session_);
}
bool App::MakeSingleInstance(ProcessSingleton::NotificationCallback callback) {
base::FilePath userDir;
PathService::Get(brightray::DIR_USER_DATA, &userDir);
if (!process_singleton_.get()) {
auto browser = Browser::Get();
process_singleton_startup_lock_.reset(
new ProcessSingletonStartupLock(callback));
process_singleton_.reset(
new ProcessSingleton(
userDir,
process_singleton_startup_lock_->AsNotificationCallback()));
if (browser->is_ready()) {
process_singleton_startup_lock_->Unlock();
}
process_notify_result_ = process_singleton_->NotifyOtherProcessOrCreate();
}
switch (process_notify_result_) {
case ProcessSingleton::NotifyResult::PROCESS_NONE:
return false;
case ProcessSingleton::NotifyResult::LOCK_ERROR:
case ProcessSingleton::NotifyResult::PROFILE_IN_USE:
case ProcessSingleton::NotifyResult::PROCESS_NOTIFIED:
return true;
default:
return false;
}
}
mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
auto browser = base::Unretained(Browser::Get());
@ -294,6 +340,7 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
.SetMethod("allowNTLMCredentialsForAllDomains",
&App::AllowNTLMCredentialsForAllDomains)
.SetMethod("getLocale", &App::GetLocale)
.SetMethod("makeSingleInstance", &App::MakeSingleInstance)
.SetProperty("defaultSession", &App::DefaultSession);
}

View file

@ -9,6 +9,9 @@
#include "atom/browser/api/event_emitter.h"
#include "atom/browser/browser_observer.h"
#include "atom/common/native_mate_converters/callback.h"
#include "chrome/browser/process_singleton.h"
#include "chrome/browser/process_singleton_startup_lock.h"
#include "content/public/browser/gpu_data_manager_observer.h"
#include "native_mate/handle.h"
@ -65,13 +68,20 @@ class App : public mate::EventEmitter,
void SetDesktopName(const std::string& desktop_name);
void SetAppUserModelId(const std::string& app_id);
void AllowNTLMCredentialsForAllDomains(bool should_allow);
bool MakeSingleInstance(ProcessSingleton::NotificationCallback callback);
std::string GetLocale();
v8::Local<v8::Value> DefaultSession(v8::Isolate* isolate);
v8::Global<v8::Value> default_session_;
scoped_ptr<ProcessSingleton> process_singleton_;
scoped_ptr<ProcessSingletonStartupLock> process_singleton_startup_lock_;
ProcessSingleton::NotifyResult process_notify_result_;
DISALLOW_COPY_AND_ASSIGN(App);
};

View file

@ -0,0 +1,38 @@
// Copyright (c) 2014 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ATOM_COMMON_NATIVE_MATE_CONVERTERS_COMMAND_LINE_CONVERTER_H_
#define ATOM_COMMON_NATIVE_MATE_CONVERTERS_COMMAND_LINE_CONVERTER_H_
#include <string>
#include "atom/common/native_mate_converters/string16_converter.h"
#include "base/command_line.h"
namespace mate {
template<>
struct Converter<base::CommandLine> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const base::CommandLine& val) {
return Converter<base::CommandLine::StringType>::ToV8(
isolate, val.GetCommandLineString());
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
base::CommandLine* out) {
base::FilePath::StringType path;
if (Converter<base::FilePath::StringType>::FromV8(isolate, val, &path)) {
*out = base::CommandLine(base::FilePath(path));
return true;
} else {
return false;
}
}
};
} // namespace mate
#endif // ATOM_COMMON_NATIVE_MATE_CONVERTERS_COMMAND_LINE_CONVERTER_H_