[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" |
avi | e4d7b6f | 2015-12-26 00:59:18 | [diff] [blame] | 10 | #include "base/macros.h" |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 11 | #include "chrome/browser/process_singleton.h" |
| 12 | #include "chrome/browser/process_singleton_modal_dialog_lock.h" |
| 13 | #include "chrome/browser/process_singleton_startup_lock.h" |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 14 | |
| 15 | // Composes a basic ProcessSingleton with ProcessSingletonStartupLock and |
| 16 | // ProcessSingletonModalDialogLock. |
| 17 | // |
Greg Thompson | eafda51e | 2017-09-05 23:09:45 | [diff] [blame] | 18 | // Notifications from ProcessSingleton will first close a modal dialog if |
| 19 | // active. Otherwise, until |Unlock()| is called, they will be queued up. Once |
| 20 | // unlocked, notifications will be passed to the client-supplied |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 21 | // NotificationCallback. |
| 22 | // |
Greg Thompson | eafda51e | 2017-09-05 23:09:45 | [diff] [blame] | 23 | // The client must ensure that SetModalDialogNotificationHandler is called |
| 24 | // appropriately when dialogs are displayed or dismissed during startup. If a |
| 25 | // dialog is active, it is closed (via the provided handler) and then the |
| 26 | // notification is processed as normal. |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 27 | class ChromeProcessSingleton { |
| 28 | public: |
| 29 | ChromeProcessSingleton( |
| 30 | const base::FilePath& user_data_dir, |
| 31 | const ProcessSingleton::NotificationCallback& notification_callback); |
| 32 | |
Peter Boström | 53c6c595 | 2021-09-17 09:41:26 | [diff] [blame^] | 33 | ChromeProcessSingleton(const ChromeProcessSingleton&) = delete; |
| 34 | ChromeProcessSingleton& operator=(const ChromeProcessSingleton&) = delete; |
| 35 | |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 36 | ~ChromeProcessSingleton(); |
| 37 | |
| 38 | // Notify another process, if available. Otherwise sets ourselves as the |
| 39 | // singleton instance. Returns PROCESS_NONE if we became the singleton |
| 40 | // instance. Callers are guaranteed to either have notified an existing |
| 41 | // process or have grabbed the singleton (unless the profile is locked by an |
| 42 | // unreachable process). |
| 43 | ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate(); |
| 44 | |
| 45 | // Clear any lock state during shutdown. |
| 46 | void Cleanup(); |
| 47 | |
Greg Thompson | eafda51e | 2017-09-05 23:09:45 | [diff] [blame] | 48 | // Receives a callback to be run to close the active modal dialog, or an empty |
| 49 | // closure if the active dialog is dismissed. |
Alexander Cooper | 4bcb0ce | 2020-07-16 23:10:38 | [diff] [blame] | 50 | void SetModalDialogNotificationHandler( |
| 51 | base::RepeatingClosure notification_handler); |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 52 | |
| 53 | // Executes previously queued command-line invocations and allows future |
| 54 | // invocations to be executed immediately. |
| 55 | // This only has an effect the first time it is called. |
| 56 | void Unlock(); |
| 57 | |
| 58 | private: |
| 59 | // We compose these two locks with the client-supplied notification callback. |
| 60 | // First |modal_dialog_lock_| will discard any notifications that arrive while |
| 61 | // a modal dialog is active. Otherwise, it will pass the notification to |
| 62 | // |startup_lock_|, which will queue notifications until |Unlock()| is called. |
| 63 | // Notifications passing through both locks are finally delivered to our |
| 64 | // client. |
| 65 | ProcessSingletonStartupLock startup_lock_; |
| 66 | ProcessSingletonModalDialogLock modal_dialog_lock_; |
| 67 | |
| 68 | // The basic ProcessSingleton |
| 69 | ProcessSingleton process_singleton_; |
[email protected] | 9a47c43 | 2013-04-19 20:33:55 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | #endif // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_ |