]> khamidou — GitHub mirrors - electron.git/commitdiff
Set up Browser to create the process singleton
authorPaul Betts <paul@paulbetts.org>
Mon, 19 Oct 2015 23:00:30 +0000 (16:00 -0700)
committerPaul Betts <paul@paulbetts.org>
Tue, 20 Oct 2015 20:15:12 +0000 (13:15 -0700)
atom/browser/browser.cc
atom/browser/browser.h

index d8bb94103cd9dc4ca55690b66dcaf45d610c693d..10d6cc8330ebe3996cff83df006a730158865ca6 100644 (file)
@@ -9,6 +9,8 @@
 #include "atom/browser/atom_browser_main_parts.h"
 #include "atom/browser/window_list.h"
 #include "base/message_loop/message_loop.h"
+#include "base/path_service.h"
+#include "brightray/browser/brightray_paths.h"
 #include "content/public/browser/client_certificate_delegate.h"
 #include "net/ssl/ssl_cert_request_info.h"
 
@@ -17,7 +19,18 @@ namespace atom {
 Browser::Browser()
     : is_quiting_(false),
       is_ready_(false),
-      is_shutdown_(false) {
+      is_shutdown_(false),
+      process_notify_callback_(NULL) {
+  base::FilePath userDir;
+  PathService::Get(brightray::DIR_USER_DATA, &userDir);
+  
+  auto no_refcount_this = base::Unretained(this);
+  process_singleton_.reset(new AtomProcessSingleton(
+    userDir, 
+    base::Bind(&Browser::OnProcessSingletonNotification, no_refcount_this)));
+    
+  process_notify_result_ = process_singleton_->NotifyOtherProcessOrCreate();
+  
   WindowList::AddObserver(this);
 }
 
@@ -114,6 +127,7 @@ void Browser::WillFinishLaunching() {
 
 void Browser::DidFinishLaunching() {
   is_ready_ = true;
+  process_singleton_->Unlock()  ;
   FOR_EACH_OBSERVER(BrowserObserver, observers_, OnFinishLaunching());
 }
 
@@ -139,7 +153,8 @@ void Browser::NotifyAndShutdown() {
     is_quiting_ = false;
     return;
   }
-
+  
+  process_singleton_->Cleanup();
   Shutdown();
 }
 
@@ -152,6 +167,14 @@ bool Browser::HandleBeforeQuit() {
   return !prevent_default;
 }
 
+ProcessSingleton::NotifyResult Browser::GetSingleInstanceResult() {
+  return process_notify_result_;
+}
+  
+void Browser::SetSingleInstanceCallback(ProcessSingleton::NotificationCallback* callback) {
+  process_notify_callback_ = callback;
+}
+
 void Browser::OnWindowCloseCancelled(NativeWindow* window) {
   if (is_quiting_)
     // Once a beforeunload handler has prevented the closing, we think the quit
@@ -166,4 +189,14 @@ void Browser::OnWindowAllClosed() {
     FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWindowAllClosed());
 }
 
+bool Browser::OnProcessSingletonNotification(
+    const base::CommandLine& command_line,
+    const base::FilePath& current_directory) {
+  if (process_notify_callback_) {
+    return (*process_notify_callback_).Run(command_line, current_directory);
+  } else {
+    return true;    // We'll handle this, not a different process
+  }
+}
+
 }  // namespace atom
index 3c5abd2f04059c25c887986f6c477179e200ab32..4dffeb093fab2d82f5910f51fd88bd4cd9797843 100644 (file)
@@ -9,8 +9,10 @@
 #include <vector>
 
 #include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
 #include "base/compiler_specific.h"
 #include "base/observer_list.h"
+#include "atom/browser/atom_process_singleton.h"
 #include "atom/browser/browser_observer.h"
 #include "atom/browser/window_list_observer.h"
 
@@ -63,6 +65,9 @@ class Browser : public WindowListObserver {
 
   // Clear the recent documents list.
   void ClearRecentDocuments();
+  
+  ProcessSingleton::NotifyResult GetSingleInstanceResult();
+  void SetSingleInstanceCallback(ProcessSingleton::NotificationCallback* callback);
 
 #if defined(OS_MACOSX)
   // Bounce the dock icon.
@@ -152,6 +157,10 @@ class Browser : public WindowListObserver {
   // WindowListObserver implementations:
   void OnWindowCloseCancelled(NativeWindow* window) override;
   void OnWindowAllClosed() override;
+  
+  bool OnProcessSingletonNotification(
+    const base::CommandLine& command_line,
+    const base::FilePath& current_directory);
 
   // Observers of the browser.
   base::ObserverList<BrowserObserver> observers_;
@@ -164,6 +173,10 @@ class Browser : public WindowListObserver {
 
   std::string version_override_;
   std::string name_override_;
+  
+  scoped_ptr<AtomProcessSingleton> process_singleton_;
+  ProcessSingleton::NotifyResult process_notify_result_;
+  ProcessSingleton::NotificationCallback* process_notify_callback_;
 
 #if defined(OS_WIN)
   base::string16 app_user_model_id_;