blob: 69ebffe3cfadc97b793b832ebc0ddfa5b3ec0bb7 [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).
[email protected]9a47c432013-04-19 20:33:5540 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate();
41
Etienne Bergeron15cfea82022-08-02 14:56:3042 // 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]9a47c432013-04-19 20:33:5548 // Clear any lock state during shutdown.
49 void Cleanup();
50
Greg Thompsoneafda51e2017-09-05 23:09:4551 // Receives a callback to be run to close the active modal dialog, or an empty
52 // closure if the active dialog is dismissed.
Alexander Cooper4bcb0ce2020-07-16 23:10:3853 void SetModalDialogNotificationHandler(
54 base::RepeatingClosure notification_handler);
[email protected]9a47c432013-04-19 20:33:5555
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 Bergeronc929d5f2022-07-19 15:35:2359 void Unlock(
60 const ProcessSingleton::NotificationCallback& notification_callback);
[email protected]9a47c432013-04-19 20:33:5561
Etienne Bergeron7a0ef612023-09-15 18:28:0862 bool IsSingletonInstanceForTesting() const { return is_singleton_instance_; }
63
Etienne Bergeronb5c46ba2022-08-30 15:53:1264 // 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 Bergeron7a0ef612023-09-15 18:28:0871 // 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 Bergeron46142462023-08-25 09:15:5376 // 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]9a47c432013-04-19 20:33:5583 private:
Etienne Bergeronc929d5f2022-07-19 15:35:2384 bool NotificationCallback(const base::CommandLine& command_line,
85 const base::FilePath& current_directory);
86
Etienne Bergeron7a0ef612023-09-15 18:28:0887 // Whether or not this instance is the running single instance.
88 bool is_singleton_instance_ = false;
89
[email protected]9a47c432013-04-19 20:33:5590 // 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 Bergeronc929d5f2022-07-19 15:35:23101 ProcessSingleton::NotificationCallback notification_callback_;
[email protected]9a47c432013-04-19 20:33:55102};
103
104#endif // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_