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 #ifndef CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_
6 #define CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_
8 #include "base/files/file_path.h"
9 #include "base/functional/callback.h"
10 #include "chrome/browser/process_singleton.h"
11 #include "chrome/browser/process_singleton_startup_lock.h"
13 // Composes a `ProcessSingleton` with a `ProcessSingletonStartupLock`.
15 // Notifications from `ProcessSingleton` will be queued up until `Unlock()` is
16 // called. Once unlocked, notifications will be passed to the
17 // `NotificationCallback` passed to `Unlock()`.
18 class ChromeProcessSingleton {
20 explicit ChromeProcessSingleton(const base::FilePath& user_data_dir);
22 ChromeProcessSingleton(const ChromeProcessSingleton&) = delete;
23 ChromeProcessSingleton& operator=(const ChromeProcessSingleton&) = delete;
25 ~ChromeProcessSingleton();
27 // Notify another process, if available. Otherwise sets ourselves as the
28 // singleton instance. Returns PROCESS_NONE if we became the singleton
29 // instance. Callers are guaranteed to either have notified an existing
30 // process or have grabbed the singleton (unless the profile is locked by an
31 // unreachable process).
32 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate();
34 // Start watching for notifications from other processes. After this call,
35 // the notifications sent by other process can be processed. This call
36 // requires the browser threads (UI / IO) to be created. Requests that occur
37 // before calling StartWatching(...) will be blocked and may timeout.
40 // Clear any lock state during shutdown.
43 // Executes previously queued command-line invocations and allows future
44 // invocations to be executed immediately.
45 // This only has an effect the first time it is called.
47 const ProcessSingleton::NotificationCallback& notification_callback);
49 bool IsSingletonInstanceForTesting() const { return is_singleton_instance_; }
51 // Create the chrome process singleton instance for the current process.
52 static void CreateInstance(const base::FilePath& user_data_dir);
53 // Delete the chrome process singleton instance.
54 static void DeleteInstance();
55 // Retrieve the chrome process singleton instance for the current process.
56 static ChromeProcessSingleton* GetInstance();
58 // Returns true if this process is the singleton instance (i.e., a
59 // ProcessSingleton has been created and NotifyOtherProcessOrCreate() has
60 // returned PROCESS_NONE).
61 static bool IsSingletonInstance();
64 bool NotificationCallback(const base::CommandLine& command_line,
65 const base::FilePath& current_directory);
67 // Whether or not this instance is the running single instance.
68 bool is_singleton_instance_ = false;
70 // We compose these two locks with the client-supplied notification callback.
71 // First |modal_dialog_lock_| will discard any notifications that arrive while
72 // a modal dialog is active. Otherwise, it will pass the notification to
73 // |startup_lock_|, which will queue notifications until |Unlock()| is called.
74 // Notifications passing through both locks are finally delivered to our
76 ProcessSingletonStartupLock startup_lock_;
78 // The basic ProcessSingleton
79 ProcessSingleton process_singleton_;
80 ProcessSingleton::NotificationCallback notification_callback_;
83 #endif // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_