Simplify the usage of singleton

This commit is contained in:
Cheng Zhao 2015-10-22 14:59:12 +08:00
parent acb2c099f6
commit 310954713f
5 changed files with 18 additions and 27 deletions

View file

@ -163,10 +163,7 @@ void App::OnQuit() {
Emit("quit"); Emit("quit");
if (process_singleton_.get()) { if (process_singleton_.get()) {
if (process_notify_result_ == ProcessSingleton::PROCESS_NONE) { process_singleton_->Cleanup();
process_singleton_->Cleanup();
}
process_singleton_.reset(); process_singleton_.reset();
} }
} }
@ -196,9 +193,8 @@ void App::OnFinishLaunching() {
auto handle = Session::CreateFrom(isolate(), browser_context); auto handle = Session::CreateFrom(isolate(), browser_context);
default_session_.Reset(isolate(), handle.ToV8()); default_session_.Reset(isolate(), handle.ToV8());
if (process_singleton_.get()) { if (process_singleton_startup_lock_.get())
process_singleton_startup_lock_->Unlock(); process_singleton_startup_lock_->Unlock();
}
Emit("ready"); Emit("ready");
} }
@ -282,35 +278,31 @@ v8::Local<v8::Value> App::DefaultSession(v8::Isolate* isolate) {
} }
bool App::MakeSingleInstance(ProcessSingleton::NotificationCallback callback) { bool App::MakeSingleInstance(ProcessSingleton::NotificationCallback callback) {
base::FilePath userDir; if (process_singleton_.get())
PathService::Get(brightray::DIR_USER_DATA, &userDir); return false;
if (!process_singleton_.get()) { base::FilePath user_dir;
auto browser = Browser::Get(); PathService::Get(brightray::DIR_USER_DATA, &user_dir);
process_singleton_startup_lock_.reset(
process_singleton_startup_lock_.reset(
new ProcessSingletonStartupLock(callback)); new ProcessSingletonStartupLock(callback));
process_singleton_.reset(
process_singleton_.reset(
new ProcessSingleton( new ProcessSingleton(
userDir, user_dir,
process_singleton_startup_lock_->AsNotificationCallback())); process_singleton_startup_lock_->AsNotificationCallback()));
if (browser->is_ready()) { if (Browser::Get()->is_ready())
process_singleton_startup_lock_->Unlock(); process_singleton_startup_lock_->Unlock();
}
process_notify_result_ = process_singleton_->NotifyOtherProcessOrCreate(); switch (process_singleton_->NotifyOtherProcessOrCreate()) {
}
switch (process_notify_result_) {
case ProcessSingleton::NotifyResult::PROCESS_NONE: case ProcessSingleton::NotifyResult::PROCESS_NONE:
return false; return false;
case ProcessSingleton::NotifyResult::LOCK_ERROR: case ProcessSingleton::NotifyResult::LOCK_ERROR:
case ProcessSingleton::NotifyResult::PROFILE_IN_USE: case ProcessSingleton::NotifyResult::PROFILE_IN_USE:
case ProcessSingleton::NotifyResult::PROCESS_NOTIFIED: case ProcessSingleton::NotifyResult::PROCESS_NOTIFIED:
process_singleton_.reset();
process_singleton_startup_lock_.reset();
return true; return true;
default:
return false;
} }
} }

View file

@ -80,7 +80,6 @@ class App : public mate::EventEmitter,
scoped_ptr<ProcessSingleton> process_singleton_; scoped_ptr<ProcessSingleton> process_singleton_;
scoped_ptr<ProcessSingletonStartupLock> process_singleton_startup_lock_; scoped_ptr<ProcessSingletonStartupLock> process_singleton_startup_lock_;
ProcessSingleton::NotifyResult process_notify_result_;
DISALLOW_COPY_AND_ASSIGN(App); DISALLOW_COPY_AND_ASSIGN(App);
}; };