1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/chrome_process_singleton.h"
11 ChromeProcessSingleton* g_chrome_process_singleton_ = nullptr;
15 ChromeProcessSingleton::ChromeProcessSingleton(
16 const base::FilePath& user_data_dir)
18 base::BindRepeating(&ChromeProcessSingleton::NotificationCallback,
19 base::Unretained(this))),
20 process_singleton_(user_data_dir,
21 startup_lock_.AsNotificationCallback()) {}
23 ChromeProcessSingleton::~ChromeProcessSingleton() = default;
25 ProcessSingleton::NotifyResult
26 ChromeProcessSingleton::NotifyOtherProcessOrCreate() {
27 CHECK(!is_singleton_instance_);
28 ProcessSingleton::NotifyResult result =
29 process_singleton_.NotifyOtherProcessOrCreate();
30 if (result == ProcessSingleton::PROCESS_NONE) {
31 is_singleton_instance_ = true;
36 void ChromeProcessSingleton::StartWatching() {
37 process_singleton_.StartWatching();
40 void ChromeProcessSingleton::Cleanup() {
41 if (is_singleton_instance_) {
42 process_singleton_.Cleanup();
46 void ChromeProcessSingleton::Unlock(
47 const ProcessSingleton::NotificationCallback& notification_callback) {
48 notification_callback_ = notification_callback;
49 startup_lock_.Unlock();
53 void ChromeProcessSingleton::CreateInstance(
54 const base::FilePath& user_data_dir) {
55 DCHECK(!g_chrome_process_singleton_);
56 DCHECK(!user_data_dir.empty());
57 g_chrome_process_singleton_ = new ChromeProcessSingleton(user_data_dir);
61 void ChromeProcessSingleton::DeleteInstance() {
62 if (g_chrome_process_singleton_) {
63 delete g_chrome_process_singleton_;
64 g_chrome_process_singleton_ = nullptr;
69 ChromeProcessSingleton* ChromeProcessSingleton::GetInstance() {
70 CHECK(g_chrome_process_singleton_);
71 return g_chrome_process_singleton_;
75 bool ChromeProcessSingleton::IsSingletonInstance() {
76 return g_chrome_process_singleton_ &&
77 g_chrome_process_singleton_->is_singleton_instance_;
80 bool ChromeProcessSingleton::NotificationCallback(
81 const base::CommandLine& command_line,
82 const base::FilePath& current_directory) {
83 DCHECK(notification_callback_);
84 return notification_callback_.Run(command_line, current_directory);