From 7491ae40007018acc7953c1757fae99d342e61f4 Mon Sep 17 00:00:00 2001 From: Paul Betts Date: Mon, 19 Oct 2015 16:00:30 -0700 Subject: [PATCH] Set up Browser to create the process singleton --- atom/browser/browser.cc | 37 +++++++++++++++++++++++++++++++++++-- atom/browser/browser.h | 13 +++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/atom/browser/browser.cc b/atom/browser/browser.cc index d8bb94103cd9..10d6cc8330eb 100644 --- a/atom/browser/browser.cc +++ b/atom/browser/browser.cc @@ -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 diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 3c5abd2f0405..4dffeb093fab 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -9,8 +9,10 @@ #include #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 observers_; @@ -164,6 +173,10 @@ class Browser : public WindowListObserver { std::string version_override_; std::string name_override_; + + scoped_ptr process_singleton_; + ProcessSingleton::NotifyResult process_notify_result_; + ProcessSingleton::NotificationCallback* process_notify_callback_; #if defined(OS_WIN) base::string16 app_user_model_id_;