blob: 7567e5b9aa239b3f2901832a0e7aee9a64861ed1 [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
Etienne Bergeronb5c46ba2022-08-30 15:53:1251 // Create the chrome process singleton instance for the current process.
52 static void CreateInstance(const base::FilePath& user_data_dir);
53 // Delete the chrome process singleton instance.
54 static void DeleteInstance();
55 // Retrieve the chrome process singleton instance for the current process.
56 static ChromeProcessSingleton* GetInstance();
57
Etienne Bergeron7a0ef612023-09-15 18:28:0858 // Returns true if this process is the singleton instance (i.e., a
59 // ProcessSingleton has been created and NotifyOtherProcessOrCreate() has
60 // returned PROCESS_NONE).
61 static bool IsSingletonInstance();
62
[email protected]9a47c432013-04-19 20:33:5563 private:
Orko Garaiaebe57b2024-02-23 00:33:0364 bool NotificationCallback(base::CommandLine command_line,
Etienne Bergeronc929d5f2022-07-19 15:35:2365 const base::FilePath& current_directory);
66
Etienne Bergeron7a0ef612023-09-15 18:28:0867 // Whether or not this instance is the running single instance.
68 bool is_singleton_instance_ = false;
69
[email protected]9a47c432013-04-19 20:33:5570 // We compose these two locks with the client-supplied notification callback.
71 // First |modal_dialog_lock_| will discard any notifications that arrive while
72 // a modal dialog is active. Otherwise, it will pass the notification to
73 // |startup_lock_|, which will queue notifications until |Unlock()| is called.
74 // Notifications passing through both locks are finally delivered to our
75 // client.
76 ProcessSingletonStartupLock startup_lock_;
[email protected]9a47c432013-04-19 20:33:5577
78 // The basic ProcessSingleton
79 ProcessSingleton process_singleton_;
Etienne Bergeronc929d5f2022-07-19 15:35:2380 ProcessSingleton::NotificationCallback notification_callback_;
[email protected]9a47c432013-04-19 20:33:5581};
82
83#endif // CHROME_BROWSER_CHROME_PROCESS_SINGLETON_H_