]> khamidou — GitHub mirrors - electron.git/commitdiff
Simplify the usage of singleton
authorCheng Zhao <zcbenz@gmail.com>
Thu, 22 Oct 2015 06:59:12 +0000 (14:59 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 22 Oct 2015 06:59:12 +0000 (14:59 +0800)
atom/browser/api/atom_api_app.cc
atom/browser/api/atom_api_app.h
chromium_src/chrome/browser/process_singleton_posix.cc
chromium_src/chrome/browser/process_singleton_startup_lock.cc
chromium_src/chrome/browser/process_singleton_startup_lock.h

index c46f0748e3b6d71af2a601b41b844b59cb13292f..2dfc3e056da59a36ed1147935d392e1cb854044f 100644 (file)
@@ -163,10 +163,7 @@ void App::OnQuit() {
   Emit("quit");
 
   if (process_singleton_.get()) {
-    if (process_notify_result_ == ProcessSingleton::PROCESS_NONE) {
-      process_singleton_->Cleanup();
-    }
-
+    process_singleton_->Cleanup();
     process_singleton_.reset();
   }
 }
@@ -196,9 +193,8 @@ void App::OnFinishLaunching() {
   auto handle = Session::CreateFrom(isolate(), browser_context);
   default_session_.Reset(isolate(), handle.ToV8());
 
-  if (process_singleton_.get()) {
+  if (process_singleton_startup_lock_.get())
     process_singleton_startup_lock_->Unlock();
-  }
 
   Emit("ready");
 }
@@ -282,35 +278,31 @@ v8::Local<v8::Value> App::DefaultSession(v8::Isolate* isolate) {
 }
 
 bool App::MakeSingleInstance(ProcessSingleton::NotificationCallback callback) {
-  base::FilePath userDir;
-  PathService::Get(brightray::DIR_USER_DATA, &userDir);
+  if (process_singleton_.get())
+    return false;
 
-  if (!process_singleton_.get()) {
-    auto browser = Browser::Get();
-    process_singleton_startup_lock_.reset(
-      new ProcessSingletonStartupLock(callback));
+  base::FilePath user_dir;
+  PathService::Get(brightray::DIR_USER_DATA, &user_dir);
 
-    process_singleton_.reset(
+  process_singleton_startup_lock_.reset(
+      new ProcessSingletonStartupLock(callback));
+  process_singleton_.reset(
       new ProcessSingleton(
-        userDir,
+        user_dir,
         process_singleton_startup_lock_->AsNotificationCallback()));
 
-    if (browser->is_ready()) {
-      process_singleton_startup_lock_->Unlock();
-    }
-
-    process_notify_result_ = process_singleton_->NotifyOtherProcessOrCreate();
-  }
+  if (Browser::Get()->is_ready())
+    process_singleton_startup_lock_->Unlock();
 
-  switch (process_notify_result_) {
+  switch (process_singleton_->NotifyOtherProcessOrCreate()) {
     case ProcessSingleton::NotifyResult::PROCESS_NONE:
       return false;
     case ProcessSingleton::NotifyResult::LOCK_ERROR:
     case ProcessSingleton::NotifyResult::PROFILE_IN_USE:
     case ProcessSingleton::NotifyResult::PROCESS_NOTIFIED:
+      process_singleton_.reset();
+      process_singleton_startup_lock_.reset();
       return true;
-    default:
-      return false;
   }
 }
 
index 314eeea8811d6b113fbe5335d852c8124c0a0c6f..8d55fc27c3b2ce7601a9c568e47be7d0383783cf 100644 (file)
@@ -80,7 +80,6 @@ class App : public mate::EventEmitter,
 
   scoped_ptr<ProcessSingleton> process_singleton_;
   scoped_ptr<ProcessSingletonStartupLock> process_singleton_startup_lock_;
-  ProcessSingleton::NotifyResult process_notify_result_;
 
   DISALLOW_COPY_AND_ASSIGN(App);
 };
index 5ab6eb6d8a78bdf7980e662e2a1af0ec3bfb54f2..4117dff8863e182b89dcebc577c405106e4d4d74 100644 (file)
@@ -1046,4 +1046,4 @@ void ProcessSingleton::KillProcess(int pid) {
   // progress of shutting down and finishes before we try to kill it).
   DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: "
                                     << base::safe_strerror(errno);
-}
\ No newline at end of file
+}
index b97ada658c35c4721ae84559f5fc3c3caf4e3247..f564de7dca4da21c89b560512f13e31ad90484b5 100644 (file)
@@ -50,4 +50,4 @@ bool ProcessSingletonStartupLock::NotificationCallbackImpl(
   } else {
     return original_callback_.Run(command_line, current_directory);
   }
-}
\ No newline at end of file
+}
index 3df51d8b177b6aa13e93b46a7ba2c1f55f94c1ff..187dd35bce3d95a15b1008331a86f5702665871a 100644 (file)
@@ -54,4 +54,4 @@ class ProcessSingletonStartupLock : public base::NonThreadSafe {
   DISALLOW_COPY_AND_ASSIGN(ProcessSingletonStartupLock);
 };
 
-#endif  // CHROME_BROWSER_PROCESS_SINGLETON_STARTUP_LOCK_H_
\ No newline at end of file
+#endif  // CHROME_BROWSER_PROCESS_SINGLETON_STARTUP_LOCK_H_