Move all the browser.cc code over to atom_api_app.cc
authorPaul Betts <paul@paulbetts.org>
Wed, 21 Oct 2015 20:04:50 +0000 (13:04 -0700)
committerPaul Betts <paul@paulbetts.org>
Wed, 21 Oct 2015 20:04:50 +0000 (13:04 -0700)
atom/browser/api/atom_api_app.cc
atom/browser/api/atom_api_app.h
atom/browser/browser.cc
atom/browser/browser.h

index 96f1ce0253722aecfebcb8a9952e616c6cc0b2f0..97d5cf6aa2dfb9606ca1752c060a163d344d8157 100644 (file)
@@ -161,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 +195,10 @@ void App::OnFinishLaunching() {
       AtomBrowserMainParts::Get()->browser_context());
   auto handle = Session::CreateFrom(isolate(), browser_context);
   default_session_.Reset(isolate(), handle.ToV8());
+  
+  if (process_singleton_.get()) {
+    process_singleton_->Unlock();
+  }
 
   Emit("ready");
 }
@@ -269,28 +281,32 @@ v8::Local<v8::Value> App::DefaultSession(v8::Isolate* isolate) {
     return v8::Local<v8::Value>::New(isolate, default_session_);
 }
 
-bool App::MakeSingleInstance(v8::Local<v8::Function> callback) {
-  auto browser = Browser::Get();
-  if (browser->InitializeSingleInstance()) {
-    single_instance_callback_ = callback;
-
-    ProcessSingleton::NotificationCallback cb;
-    mate::Converter<ProcessSingleton::NotificationCallback>::FromV8(
-        isolate(), single_instance_callback_, &cb);
-
-    browser->SetSingleInstanceCallback(cb);
+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_.reset(new AtomProcessSingleton(userDir, callback));
+    
+    if (browser->is_ready()) {
+      process_singleton_->Unlock();
+    }
+
+    process_notify_result_ = process_singleton_->NotifyOtherProcessOrCreate();
   }
 
-  switch (browser->GetSingleInstanceResult()) {
+  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;
   }
-
-  return false;
 }
 
 mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder(
index b10b40e5c9675cff5fbc8a76bfdab12742fb8787..82ae045020306ddb8b9386a4e4e6c05f2905bb68 100644 (file)
@@ -8,6 +8,7 @@
 #include <string>
 
 #include "atom/browser/api/event_emitter.h"
+#include "atom/browser/atom_process_singleton.h"
 #include "atom/browser/browser_observer.h"
 #include "atom/common/native_mate_converters/callback.h"
 #include "chrome/browser/process_singleton.h"
@@ -70,13 +71,15 @@ class App : public mate::EventEmitter,
 
   void AllowNTLMCredentialsForAllDomains(bool should_allow);
 
-  bool MakeSingleInstance(v8::Local<v8::Function> callback);
+  bool MakeSingleInstance(ProcessSingleton::NotificationCallback callback);
 
   std::string GetLocale();
   v8::Local<v8::Value> DefaultSession(v8::Isolate* isolate);
 
-  v8::Local<v8::Function> single_instance_callback_;
   v8::Global<v8::Value> default_session_;
+  
+  scoped_ptr<AtomProcessSingleton> process_singleton_;
+  ProcessSingleton::NotifyResult process_notify_result_;
 
   DISALLOW_COPY_AND_ASSIGN(App);
 };
index 6d39cdcc8b96fca7d1c0a48e67c46d6537107667..d8bb94103cd9dc4ca55690b66dcaf45d610c693d 100644 (file)
@@ -9,8 +9,6 @@
 #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"
 
@@ -19,8 +17,7 @@ namespace atom {
 Browser::Browser()
     : is_quiting_(false),
       is_ready_(false),
-      is_shutdown_(false),
-      process_notify_callback_set_(false) {
+      is_shutdown_(false) {
   WindowList::AddObserver(this);
 }
 
@@ -117,11 +114,6 @@ void Browser::WillFinishLaunching() {
 
 void Browser::DidFinishLaunching() {
   is_ready_ = true;
-
-  if (process_notify_callback_set_) {
-    process_singleton_->Unlock();
-  }
-
   FOR_EACH_OBSERVER(BrowserObserver, observers_, OnFinishLaunching());
 }
 
@@ -148,14 +140,6 @@ void Browser::NotifyAndShutdown() {
     return;
   }
 
-  if (process_notify_callback_set_) {
-    process_notify_callback_.Reset();
-
-    if (process_notify_result_ == ProcessSingleton::PROCESS_NONE) {
-      process_singleton_->Cleanup();
-    }
-  }
-
   Shutdown();
 }
 
@@ -168,34 +152,6 @@ bool Browser::HandleBeforeQuit() {
   return !prevent_default;
 }
 
-bool Browser::InitializeSingleInstance() {
-  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)));
-
-  if (is_ready_) {
-    process_singleton_->Unlock();
-  }
-
-  process_notify_result_ = process_singleton_->NotifyOtherProcessOrCreate();
-  return true;
-}
-
-ProcessSingleton::NotifyResult Browser::GetSingleInstanceResult() {
-  LOG(ERROR) << "Process Notify Result: " << process_notify_result_;
-  return process_notify_result_;
-}
-
-void Browser::SetSingleInstanceCallback(
-    ProcessSingleton::NotificationCallback callback) {
-  process_notify_callback_ = callback;
-  process_notify_callback_set_ = true;
-}
-
 void Browser::OnWindowCloseCancelled(NativeWindow* window) {
   if (is_quiting_)
     // Once a beforeunload handler has prevented the closing, we think the quit
@@ -210,14 +166,4 @@ 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_set_) {
-    return process_notify_callback_.Run(command_line, current_directory);
-  } else {
-    return true;    // We'll handle this, not a different process
-  }
-}
-
 }  // namespace atom
index f9b86b6924a0e31c8136d047359df8eac2b1612e..3c5abd2f04059c25c887986f6c477179e200ab32 100644 (file)
@@ -9,10 +9,8 @@
 #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"
 
@@ -66,11 +64,6 @@ class Browser : public WindowListObserver {
   // Clear the recent documents list.
   void ClearRecentDocuments();
 
-  bool InitializeSingleInstance();
-  ProcessSingleton::NotifyResult GetSingleInstanceResult();
-  void SetSingleInstanceCallback(
-      ProcessSingleton::NotificationCallback callback);
-
 #if defined(OS_MACOSX)
   // Bounce the dock icon.
   enum BounceType {
@@ -160,10 +153,6 @@ class Browser : public WindowListObserver {
   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_;
 
@@ -176,11 +165,6 @@ 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_;
-  bool process_notify_callback_set_;
-
 #if defined(OS_WIN)
   base::string16 app_user_model_id_;
 #endif