blob: 54b4d00cd0d138783a00739032b5ca25016eaf13 [file] [log] [blame]
Avi Drissman4a8573c2022-09-09 19:35:541// Copyright 2013 The Chromium Authors
[email protected]9a47c432013-04-19 20:33:552// 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]9a47c432013-04-19 20:33:558#include "base/files/file_path.h"
Avi Drissman02e49e582023-01-07 01:23:189#include "base/functional/callback.h"
[email protected]9a47c432013-04-19 20:33:5510#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]9a47c432013-04-19 20:33:5513
14// Composes a basic ProcessSingleton with ProcessSingletonStartupLock and
15// ProcessSingletonModalDialogLock.
16//
Greg Thompsoneafda51e2017-09-05 23:09:4517// 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 Bergeronc929d5f2022-07-19 15:35:2320// NotificationCallback; which is passed as an argument by |Unlock()|.
[email protected]9a47c432013-04-19 20:33:5521//
Greg Thompsoneafda51e2017-09-05 23:09:4522// 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]9a47c432013-04-19 20:33:5526class ChromeProcessSingleton {
27 public:
Etienne Bergeronc929d5f2022-07-19 15:35:2328 explicit ChromeProcessSingleton(const base::FilePath& user_data_dir);
[email protected]9a47c432013-04-19 20:33:5529
Peter Boström53c6c5952021-09-17 09:41:2630 ChromeProcessSingleton(const ChromeProcessSingleton&) = delete;
31 ChromeProcessSingleton& operator=(const ChromeProcessSingleton&) = delete;
32
[email protected]9a47c432013-04-19 20:33:5533 ~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).
Peter Kvitekb95e2232021-12-10 00:40:0840 // The guarantee is a bit different if we're running in Native Headless mode,
41 // in which case an existing process is not notified and this method returns
42 // PROFILE_IN_USE if it happens to use the same profile directory.
[email protected]9a47c432013-04-19 20:33:5543 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate();
44
Etienne Bergeron15cfea82022-08-02 14:56:3045 // Start watching for notifications from other processes. After this call,
46 // the notifications sent by other process can be processed. This call
47 // requires the browser threads (UI / IO) to be created. Requests that occur
48 // before calling StartWatching(...) will be blocked and may timeout.
49 void StartWatching();
50
[email protected]9a47c432013-04-19 20:33:5551 // Clear any lock state during shutdown.
52 void Cleanup();
53
Greg Thompsoneafda51e2017-09-05 23:09:4554 // Receives a callback to be run to close the active modal dialog, or an empty
55 // closure if the active dialog is dismissed.
Alexander Cooper4bcb0ce2020-07-16 23:10:3856 void SetModalDialogNotificationHandler(
57 base::RepeatingClosure notification_handler);
[email protected]9a47c432013-04-19 20:33:5558
59 // Executes previously queued command-line invocations and allows future
60 // invocations to be executed immediately.
61 // This only has an effect the first time it is called.
Etienne Bergeronc929d5f2022-07-19 15:35:2362 void Unlock(
63 const ProcessSingleton::NotificationCallback& notification_callback);
[email protected]9a47c432013-04-19 20:33:5564
Etienne Bergeronb5c46ba2022-08-30 15:53:1265 // Create the chrome process singleton instance for the current process.
66 static void CreateInstance(const base::FilePath& user_data_dir);
67 // Delete the chrome process singleton instance.
68 static void DeleteInstance();
69 // Retrieve the chrome process singleton instance for the current process.
70 static ChromeProcessSingleton* GetInstance();
71
Etienne Bergeron46142462023-08-25 09:15:5372 // Setup the experiment for the early process singleton. Remove this code
73 // when the experiment is over (http://www.crbug.com/1340599).
74 static void SetupEarlySingletonFeature(const base::CommandLine& command_line);
75 static void RegisterEarlySingletonFeature();
76 static bool IsEarlySingletonFeatureEnabled();
77 static bool ShouldMergeMetrics();
78
[email protected]9a47c432013-04-19 20:33:5579 private:
Etienne Bergeronc929d5f2022-07-19 15:35:2380 bool NotificationCallback(const base::CommandLine& command_line,
81 const base::FilePath& current_directory);
82
[email protected]9a47c432013-04-19 20:33:5583 // We compose these two locks with the client-supplied notification callback.
84 // First |modal_dialog_lock_| will discard any notifications that arrive while
85 // a modal dialog is active. Otherwise, it will pass the notification to
86 // |startup_lock_|, which will queue notifications until |Unlock()| is called.
87 // Notifications passing through both locks are finally delivered to our
88 // client.
89 ProcessSingletonStartupLock startup_lock_;
90 ProcessSingletonModalDialogLock modal_dialog_lock_;
91
92 // The basic ProcessSingleton
93 ProcessSingleton process_singleton_;
Etienne Bergeronc929d5f2022-07-19 15:35:2394 ProcessSingleton::NotificationCallback notification_callback_;
[email protected]9a47c432013-04-19 20:33:5595};
96
97#endif // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_