blob: f5cb830b3ac7277500b0773fd1e138cbc1156ae7 [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 Bergeronb5c46ba2022-08-30 15:53:1262 // Create the chrome process singleton instance for the current process.
63 static void CreateInstance(const base::FilePath& user_data_dir);
64 // Delete the chrome process singleton instance.
65 static void DeleteInstance();
66 // Retrieve the chrome process singleton instance for the current process.
67 static ChromeProcessSingleton* GetInstance();
68
Etienne Bergeron46142462023-08-25 09:15:5369 // Setup the experiment for the early process singleton. Remove this code
70 // when the experiment is over (http://www.crbug.com/1340599).
71 static void SetupEarlySingletonFeature(const base::CommandLine& command_line);
72 static void RegisterEarlySingletonFeature();
73 static bool IsEarlySingletonFeatureEnabled();
74 static bool ShouldMergeMetrics();
75
[email protected]9a47c432013-04-19 20:33:5576 private:
Etienne Bergeronc929d5f2022-07-19 15:35:2377 bool NotificationCallback(const base::CommandLine& command_line,
78 const base::FilePath& current_directory);
79
[email protected]9a47c432013-04-19 20:33:5580 // We compose these two locks with the client-supplied notification callback.
81 // First |modal_dialog_lock_| will discard any notifications that arrive while
82 // a modal dialog is active. Otherwise, it will pass the notification to
83 // |startup_lock_|, which will queue notifications until |Unlock()| is called.
84 // Notifications passing through both locks are finally delivered to our
85 // client.
86 ProcessSingletonStartupLock startup_lock_;
87 ProcessSingletonModalDialogLock modal_dialog_lock_;
88
89 // The basic ProcessSingleton
90 ProcessSingleton process_singleton_;
Etienne Bergeronc929d5f2022-07-19 15:35:2391 ProcessSingleton::NotificationCallback notification_callback_;
[email protected]9a47c432013-04-19 20:33:5592};
93
94#endif // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_