Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_ |
| 6 | #define CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_ |
| 7 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 8 | #include "base/files/file_path.h" |
Avi Drissman | 02e49e58 | 2023-01-07 01:23:18 | [diff] [blame] | 9 | #include "base/functional/callback.h" |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 10 | #include "chrome/browser/process_singleton.h" |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 11 | #include "chrome/browser/process_singleton_startup_lock.h" |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 12 | |
Peter Kasting | cce2444 | 2023-10-04 17:13:25 | [diff] [blame] | 13 | // Composes a `ProcessSingleton` with a `ProcessSingletonStartupLock`. |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 14 | // |
Peter Kasting | cce2444 | 2023-10-04 17:13:25 | [diff] [blame] | 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()`. |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 18 | class ChromeProcessSingleton { |
| 19 | public: |
Etienne Bergeron | c929d5f | 2022-07-19 15:35:23 | [diff] [blame] | 20 | explicit ChromeProcessSingleton(const base::FilePath& user_data_dir); |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 21 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 22 | ChromeProcessSingleton(const ChromeProcessSingleton&) = delete; |
| 23 | ChromeProcessSingleton& operator=(const ChromeProcessSingleton&) = delete; |
| 24 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 25 | ~ChromeProcessSingleton(); |
| 26 | |
| 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(); |
| 33 | |
Etienne Bergeron | 15cfea8 | 2022-08-02 14:56:30 | [diff] [blame] | 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. |
| 38 | void StartWatching(); |
| 39 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 40 | // Clear any lock state during shutdown. |
| 41 | void Cleanup(); |
| 42 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 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. |
Etienne Bergeron | c929d5f | 2022-07-19 15:35:23 | [diff] [blame] | 46 | void Unlock( |
| 47 | const ProcessSingleton::NotificationCallback& notification_callback); |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 48 | |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame] | 49 | bool IsSingletonInstanceForTesting() const { return is_singleton_instance_; } |
| 50 | |
Etienne Bergeron | b5c46ba | 2022-08-30 15:53:12 | [diff] [blame] | 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(); |
| 57 | |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame] | 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(); |
| 62 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 63 | private: |
Orko Garai | aebe57b | 2024-02-23 00:33:03 | [diff] [blame^] | 64 | bool NotificationCallback(base::CommandLine command_line, |
Etienne Bergeron | c929d5f | 2022-07-19 15:35:23 | [diff] [blame] | 65 | const base::FilePath& current_directory); |
| 66 | |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame] | 67 | // Whether or not this instance is the running single instance. |
| 68 | bool is_singleton_instance_ = false; |
| 69 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 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 |
| 75 | // client. |
| 76 | ProcessSingletonStartupLock startup_lock_; |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 77 | |
| 78 | // The basic ProcessSingleton |
| 79 | ProcessSingleton process_singleton_; |
Etienne Bergeron | c929d5f | 2022-07-19 15:35:23 | [diff] [blame] | 80 | ProcessSingleton::NotificationCallback notification_callback_; |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | #endif // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_ |