[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 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 | |
Greg Thompson | eafda51e | 2017-09-05 23:09:45 | [diff] [blame] | 8 | #include "base/callback.h" |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 9 | #include "base/files/file_path.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 |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 20 | // NotificationCallback. |
| 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: |
| 28 | ChromeProcessSingleton( |
| 29 | const base::FilePath& user_data_dir, |
| 30 | const ProcessSingleton::NotificationCallback& notification_callback); |
| 31 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame] | 32 | ChromeProcessSingleton(const ChromeProcessSingleton&) = delete; |
| 33 | ChromeProcessSingleton& operator=(const ChromeProcessSingleton&) = delete; |
| 34 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 35 | ~ChromeProcessSingleton(); |
| 36 | |
| 37 | // Notify another process, if available. Otherwise sets ourselves as the |
| 38 | // singleton instance. Returns PROCESS_NONE if we became the singleton |
| 39 | // instance. Callers are guaranteed to either have notified an existing |
| 40 | // process or have grabbed the singleton (unless the profile is locked by an |
| 41 | // unreachable process). |
Peter Kvitek | b95e223 | 2021-12-10 00:40:08 | [diff] [blame^] | 42 | // The guarantee is a bit different if we're running in Native Headless mode, |
| 43 | // in which case an existing process is not notified and this method returns |
| 44 | // PROFILE_IN_USE if it happens to use the same profile directory. |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 45 | ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate(); |
| 46 | |
| 47 | // Clear any lock state during shutdown. |
| 48 | void Cleanup(); |
| 49 | |
Greg Thompson | eafda51e | 2017-09-05 23:09:45 | [diff] [blame] | 50 | // Receives a callback to be run to close the active modal dialog, or an empty |
| 51 | // closure if the active dialog is dismissed. |
Alexander Cooper | 4bcb0ce | 2020-07-16 23:10:38 | [diff] [blame] | 52 | void SetModalDialogNotificationHandler( |
| 53 | base::RepeatingClosure notification_handler); |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 54 | |
| 55 | // Executes previously queued command-line invocations and allows future |
| 56 | // invocations to be executed immediately. |
| 57 | // This only has an effect the first time it is called. |
| 58 | void Unlock(); |
| 59 | |
| 60 | private: |
| 61 | // We compose these two locks with the client-supplied notification callback. |
| 62 | // First |modal_dialog_lock_| will discard any notifications that arrive while |
| 63 | // a modal dialog is active. Otherwise, it will pass the notification to |
| 64 | // |startup_lock_|, which will queue notifications until |Unlock()| is called. |
| 65 | // Notifications passing through both locks are finally delivered to our |
| 66 | // client. |
| 67 | ProcessSingletonStartupLock startup_lock_; |
| 68 | ProcessSingletonModalDialogLock modal_dialog_lock_; |
| 69 | |
| 70 | // The basic ProcessSingleton |
| 71 | ProcessSingleton process_singleton_; |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 72 | }; |
| 73 | |
| 74 | #endif // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_ |