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" |
| 11 | #include "chrome/browser/process_singleton_modal_dialog_lock.h" |
| 12 | #include "chrome/browser/process_singleton_startup_lock.h" |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 13 | |
| 14 | // Composes a basic ProcessSingleton with ProcessSingletonStartupLock and |
| 15 | // ProcessSingletonModalDialogLock. |
| 16 | // |
Greg Thompson | eafda51e | 2017-09-05 23:09:45 | [diff] [blame] | 17 | // Notifications from ProcessSingleton will first close a modal dialog if |
| 18 | // active. Otherwise, until |Unlock()| is called, they will be queued up. Once |
| 19 | // unlocked, notifications will be passed to the client-supplied |
Etienne Bergeron | c929d5f | 2022-07-19 15:35:23 | [diff] [blame] | 20 | // NotificationCallback; which is passed as an argument by |Unlock()|. |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 21 | // |
Greg Thompson | eafda51e | 2017-09-05 23:09:45 | [diff] [blame] | 22 | // The client must ensure that SetModalDialogNotificationHandler is called |
| 23 | // appropriately when dialogs are displayed or dismissed during startup. If a |
| 24 | // dialog is active, it is closed (via the provided handler) and then the |
| 25 | // notification is processed as normal. |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 26 | class ChromeProcessSingleton { |
| 27 | public: |
Etienne Bergeron | c929d5f | 2022-07-19 15:35:23 | [diff] [blame] | 28 | explicit ChromeProcessSingleton(const base::FilePath& user_data_dir); |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 29 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 30 | ChromeProcessSingleton(const ChromeProcessSingleton&) = delete; |
| 31 | ChromeProcessSingleton& operator=(const ChromeProcessSingleton&) = delete; |
| 32 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 33 | ~ChromeProcessSingleton(); |
| 34 | |
| 35 | // Notify another process, if available. Otherwise sets ourselves as the |
| 36 | // singleton instance. Returns PROCESS_NONE if we became the singleton |
| 37 | // instance. Callers are guaranteed to either have notified an existing |
| 38 | // process or have grabbed the singleton (unless the profile is locked by an |
| 39 | // unreachable process). |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 40 | ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate(); |
| 41 | |
Etienne Bergeron | 15cfea8 | 2022-08-02 14:56:30 | [diff] [blame] | 42 | // Start watching for notifications from other processes. After this call, |
| 43 | // the notifications sent by other process can be processed. This call |
| 44 | // requires the browser threads (UI / IO) to be created. Requests that occur |
| 45 | // before calling StartWatching(...) will be blocked and may timeout. |
| 46 | void StartWatching(); |
| 47 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 48 | // Clear any lock state during shutdown. |
| 49 | void Cleanup(); |
| 50 | |
Greg Thompson | eafda51e | 2017-09-05 23:09:45 | [diff] [blame] | 51 | // Receives a callback to be run to close the active modal dialog, or an empty |
| 52 | // closure if the active dialog is dismissed. |
Alexander Cooper | 4bcb0ce | 2020-07-16 23:10:38 | [diff] [blame] | 53 | void SetModalDialogNotificationHandler( |
| 54 | base::RepeatingClosure notification_handler); |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 55 | |
| 56 | // Executes previously queued command-line invocations and allows future |
| 57 | // invocations to be executed immediately. |
| 58 | // This only has an effect the first time it is called. |
Etienne Bergeron | c929d5f | 2022-07-19 15:35:23 | [diff] [blame] | 59 | void Unlock( |
| 60 | const ProcessSingleton::NotificationCallback& notification_callback); |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 61 | |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame^] | 62 | bool IsSingletonInstanceForTesting() const { return is_singleton_instance_; } |
| 63 | |
Etienne Bergeron | b5c46ba | 2022-08-30 15:53:12 | [diff] [blame] | 64 | // Create the chrome process singleton instance for the current process. |
| 65 | static void CreateInstance(const base::FilePath& user_data_dir); |
| 66 | // Delete the chrome process singleton instance. |
| 67 | static void DeleteInstance(); |
| 68 | // Retrieve the chrome process singleton instance for the current process. |
| 69 | static ChromeProcessSingleton* GetInstance(); |
| 70 | |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame^] | 71 | // Returns true if this process is the singleton instance (i.e., a |
| 72 | // ProcessSingleton has been created and NotifyOtherProcessOrCreate() has |
| 73 | // returned PROCESS_NONE). |
| 74 | static bool IsSingletonInstance(); |
| 75 | |
Etienne Bergeron | 4614246 | 2023-08-25 09:15:53 | [diff] [blame] | 76 | // Setup the experiment for the early process singleton. Remove this code |
| 77 | // when the experiment is over (http://www.crbug.com/1340599). |
| 78 | static void SetupEarlySingletonFeature(const base::CommandLine& command_line); |
| 79 | static void RegisterEarlySingletonFeature(); |
| 80 | static bool IsEarlySingletonFeatureEnabled(); |
| 81 | static bool ShouldMergeMetrics(); |
| 82 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 83 | private: |
Etienne Bergeron | c929d5f | 2022-07-19 15:35:23 | [diff] [blame] | 84 | bool NotificationCallback(const base::CommandLine& command_line, |
| 85 | const base::FilePath& current_directory); |
| 86 | |
Etienne Bergeron | 7a0ef61 | 2023-09-15 18:28:08 | [diff] [blame^] | 87 | // Whether or not this instance is the running single instance. |
| 88 | bool is_singleton_instance_ = false; |
| 89 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 90 | // We compose these two locks with the client-supplied notification callback. |
| 91 | // First |modal_dialog_lock_| will discard any notifications that arrive while |
| 92 | // a modal dialog is active. Otherwise, it will pass the notification to |
| 93 | // |startup_lock_|, which will queue notifications until |Unlock()| is called. |
| 94 | // Notifications passing through both locks are finally delivered to our |
| 95 | // client. |
| 96 | ProcessSingletonStartupLock startup_lock_; |
| 97 | ProcessSingletonModalDialogLock modal_dialog_lock_; |
| 98 | |
| 99 | // The basic ProcessSingleton |
| 100 | ProcessSingleton process_singleton_; |
Etienne Bergeron | c929d5f | 2022-07-19 15:35:23 | [diff] [blame] | 101 | ProcessSingleton::NotificationCallback notification_callback_; |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 102 | }; |
| 103 | |
| 104 | #endif // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_ |