blob: dc79446b281fc2d08b6cc13ff266bc77d63c3afe [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"
[email protected]9a47c432013-04-19 20:33:5511#include "chrome/browser/process_singleton_startup_lock.h"
[email protected]9a47c432013-04-19 20:33:5512
Peter Kastingcce24442023-10-04 17:13:2513// Composes a `ProcessSingleton` with a `ProcessSingletonStartupLock`.
[email protected]9a47c432013-04-19 20:33:5514//
Peter Kastingcce24442023-10-04 17:13:2515// Notifications from `ProcessSingleton` will be queued up until `Unlock()` is
16// called. Once unlocked, notifications will be passed to the
17// `NotificationCallback` passed to `Unlock()`.
[email protected]9a47c432013-04-19 20:33:5518class ChromeProcessSingleton {
19 public:
Etienne Bergeronc929d5f2022-07-19 15:35:2320 explicit ChromeProcessSingleton(const base::FilePath& user_data_dir);
[email protected]9a47c432013-04-19 20:33:5521
Peter Boström53c6c5952021-09-17 09:41:2622 ChromeProcessSingleton(const ChromeProcessSingleton&) = delete;
23 ChromeProcessSingleton& operator=(const ChromeProcessSingleton&) = delete;
24
[email protected]9a47c432013-04-19 20:33:5525 ~ChromeProcessSingleton();
26
27 // Notify another process, if available. Otherwise sets ourselves as the
28 // singleton instance. Returns PROCESS_NONE if we became the singleton
29 // instance. Callers are guaranteed to either have notified an existing
30 // process or have grabbed the singleton (unless the profile is locked by an
31 // unreachable process).
32 ProcessSingleton::NotifyResult NotifyOtherProcessOrCreate();
33
Etienne Bergeron15cfea82022-08-02 14:56:3034 // Start watching for notifications from other processes. After this call,
35 // the notifications sent by other process can be processed. This call
36 // requires the browser threads (UI / IO) to be created. Requests that occur
37 // before calling StartWatching(...) will be blocked and may timeout.
38 void StartWatching();
39
[email protected]9a47c432013-04-19 20:33:5540 // Clear any lock state during shutdown.
41 void Cleanup();
42
[email protected]9a47c432013-04-19 20:33:5543 // Executes previously queued command-line invocations and allows future
44 // invocations to be executed immediately.
45 // This only has an effect the first time it is called.
Etienne Bergeronc929d5f2022-07-19 15:35:2346 void Unlock(
47 const ProcessSingleton::NotificationCallback& notification_callback);
[email protected]9a47c432013-04-19 20:33:5548
Etienne Bergeron7a0ef612023-09-15 18:28:0849 bool IsSingletonInstanceForTesting() const { return is_singleton_instance_; }
50
Sean Maher401d25e2024-12-04 16:47:2451#if BUILDFLAG(IS_WIN)
52 // Must only be called both after initialization of FeatureList and
53 // NotifyOtherProcessOrCreate().
54 void InitializeFeatures();
55#endif
56
Etienne Bergeronb5c46ba2022-08-30 15:53:1257 // Create the chrome process singleton instance for the current process.
58 static void CreateInstance(const base::FilePath& user_data_dir);
59 // Delete the chrome process singleton instance.
60 static void DeleteInstance();
61 // Retrieve the chrome process singleton instance for the current process.
62 static ChromeProcessSingleton* GetInstance();
63
Etienne Bergeron7a0ef612023-09-15 18:28:0864 // Returns true if this process is the singleton instance (i.e., a
65 // ProcessSingleton has been created and NotifyOtherProcessOrCreate() has
66 // returned PROCESS_NONE).
67 static bool IsSingletonInstance();
68
[email protected]9a47c432013-04-19 20:33:5569 private:
Orko Garaiaebe57b2024-02-23 00:33:0370 bool NotificationCallback(base::CommandLine command_line,
Etienne Bergeronc929d5f2022-07-19 15:35:2371 const base::FilePath& current_directory);
72
Etienne Bergeron7a0ef612023-09-15 18:28:0873 // Whether or not this instance is the running single instance.
74 bool is_singleton_instance_ = false;
75
[email protected]9a47c432013-04-19 20:33:5576 // We compose these two locks with the client-supplied notification callback.
77 // First |modal_dialog_lock_| will discard any notifications that arrive while
78 // a modal dialog is active. Otherwise, it will pass the notification to
79 // |startup_lock_|, which will queue notifications until |Unlock()| is called.
80 // Notifications passing through both locks are finally delivered to our
81 // client.
82 ProcessSingletonStartupLock startup_lock_;
[email protected]9a47c432013-04-19 20:33:5583
84 // The basic ProcessSingleton
85 ProcessSingleton process_singleton_;
Etienne Bergeronc929d5f2022-07-19 15:35:2386 ProcessSingleton::NotificationCallback notification_callback_;
[email protected]9a47c432013-04-19 20:33:55