blob: 7cd82d27a741f194da5d0b3fcfd9c15c8ea1fa5c [file] [log] [blame]
[email protected]e3ce40a2012-01-31 03:03:031// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]fc14cef2009-01-27 22:17:292// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7c47ae3e2009-02-18 00:34:215#ifndef CHROME_BROWSER_PROCESS_SINGLETON_H_
6#define CHROME_BROWSER_PROCESS_SINGLETON_H_
[email protected]fc14cef2009-01-27 22:17:297
gab25894fe2017-05-30 03:40:368#include "base/sequence_checker.h"
[email protected]19d7e9682009-02-18 22:04:289#include "build/build_config.h"
10
Xiaohan Wang55ae2c012022-01-20 21:49:1111#if BUILDFLAG(IS_WIN)
Bruce Dawson6457cf542021-07-16 01:55:2612#include "base/win/windows_types.h"
Xiaohan Wang55ae2c012022-01-20 21:49:1113#endif // BUILDFLAG(IS_WIN)
[email protected]fc14cef2009-01-27 22:17:2914
[email protected]5d364542012-04-05 07:15:3915#include "base/callback.h"
Hans Wennborg1f38a152020-06-22 06:38:4116#include "base/check.h"
[email protected]edf04b512012-02-23 09:47:4317#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5218#include "base/files/file_path.h"
[email protected]3b63f8f42011-03-28 01:54:1519#include "base/memory/ref_counted.h"
[email protected]d09a4ce1c2013-07-24 17:37:0220#include "base/process/process.h"
[email protected]08397d52011-02-05 01:53:3821#include "ui/gfx/native_widget_types.h"
[email protected]0ff0ff32010-12-21 19:34:4222
Xiaohan Wang55ae2c012022-01-20 21:49:1123#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
[email protected]ea1a3f62012-11-16 20:34:2324#include "base/files/scoped_temp_dir.h"
[email protected]a220b5932013-09-21 03:47:4425#endif
[email protected]fc14cef2009-01-27 22:17:2926
Xiaohan Wang55ae2c012022-01-20 21:49:1127#if BUILDFLAG(IS_WIN)
[email protected]4cf04bb2013-07-11 09:22:3828#include "base/win/message_window.h"
Xiaohan Wang55ae2c012022-01-20 21:49:1129#endif // BUILDFLAG(IS_WIN)
[email protected]4cf04bb2013-07-11 09:22:3830
[email protected]2f3b1cc2014-03-17 23:07:1531namespace base {
[email protected]0189bbd2009-10-12 22:50:3932class CommandLine;
[email protected]2f3b1cc2014-03-17 23:07:1533}
[email protected]0189bbd2009-10-12 22:50:3934
[email protected]7c47ae3e2009-02-18 00:34:2135// ProcessSingleton ----------------------------------------------------------
[email protected]fc14cef2009-01-27 22:17:2936//
[email protected]7c47ae3e2009-02-18 00:34:2137// This class allows different browser processes to communicate with
38// each other. It is named according to the user data directory, so
39// we can be sure that no more than one copy of the application can be
40// running at once with a given data directory.
41//
[email protected]19d7e9682009-02-18 22:04:2842// Implementation notes:
43// - the Windows implementation uses an invisible global message window;
[email protected]e134a722009-02-23 23:54:0244// - the Linux implementation uses a Unix domain socket in the user data dir.
[email protected]fc14cef2009-01-27 22:17:2945
gab25894fe2017-05-30 03:40:3646class ProcessSingleton {
[email protected]fc14cef2009-01-27 22:17:2947 public:
aseren028ea152017-05-16 17:22:4348 // Used to send the reason of remote hang process termination as histogram.
49 enum RemoteHungProcessTerminateReason {
Xiaohan Wang55ae2c012022-01-20 21:49:1150#if BUILDFLAG(IS_WIN)
aseren028ea152017-05-16 17:22:4351 USER_ACCEPTED_TERMINATION = 1,
52 NO_VISIBLE_WINDOW_FOUND = 2,
Xiaohan Wang55ae2c012022-01-20 21:49:1153#elif BUILDFLAG(IS_POSIX)
aseren028ea152017-05-16 17:22:4354 NOTIFY_ATTEMPTS_EXCEEDED = 3,
55 SOCKET_WRITE_FAILED = 4,
56 SOCKET_READ_FAILED = 5,
57#endif
58 REMOTE_HUNG_PROCESS_TERMINATE_REASON_COUNT
59 };
60
61 // Used to send the result of interaction with remote process as histograms in
62 // case when remote process influences on start.
63 enum RemoteProcessInteractionResult {
64 TERMINATE_SUCCEEDED = 0,
65 TERMINATE_FAILED = 1,
66 REMOTE_PROCESS_NOT_FOUND = 2,
Xiaohan Wang55ae2c012022-01-20 21:49:1167#if BUILDFLAG(IS_WIN)
aseren028ea152017-05-16 17:22:4368 TERMINATE_WAIT_TIMEOUT = 3,
69 RUNNING_PROCESS_NOTIFY_ERROR = 4,
Xiaohan Wang55ae2c012022-01-20 21:49:1170#elif BUILDFLAG(IS_POSIX)
aseren028ea152017-05-16 17:22:4371 TERMINATE_NOT_ENOUGH_PERMISSIONS = 5,
72 REMOTE_PROCESS_SHUTTING_DOWN = 6,
73 PROFILE_UNLOCKED = 7,
74 PROFILE_UNLOCKED_BEFORE_KILL = 8,
75 SAME_BROWSER_INSTANCE = 9,
76 SAME_BROWSER_INSTANCE_BEFORE_KILL = 10,
77 FAILED_TO_EXTRACT_PID = 11,
78 INVALID_LOCK_FILE = 12,
79 ORPHANED_LOCK_FILE = 13,
80#endif
Mikhail Atuchina6e8bcd32018-07-06 12:14:0381 USER_REFUSED_TERMINATION = 14,
aseren028ea152017-05-16 17:22:4382 REMOTE_PROCESS_INTERACTION_RESULT_COUNT
83 };
84
gab439f54f2016-10-07 22:24:2285 // Logged as histograms, do not modify these values.
[email protected]9f20a6d02009-08-21 01:18:3786 enum NotifyResult {
gab439f54f2016-10-07 22:24:2287 PROCESS_NONE = 0,
88 PROCESS_NOTIFIED = 1,
89 PROFILE_IN_USE = 2,
90 LOCK_ERROR = 3,
91 LAST_VALUE = LOCK_ERROR
[email protected]9f20a6d02009-08-21 01:18:3792 };
93
gab439f54f2016-10-07 22:24:2294 static constexpr int kNumNotifyResults = LAST_VALUE + 1;
95
[email protected]5d364542012-04-05 07:15:3996 // Implement this callback to handle notifications from other processes. The
97 // callback will receive the command line and directory with which the other
98 // Chrome process was launched. Return true if the command line will be
99 // handled within the current browser instance or false if the remote process
100 // should handle it (i.e., because the current process is shutting down).
siggi2a0214b2015-03-12 14:50:27101 using NotificationCallback =
Alexander Cooper4bcb0ce2020-07-16 23:10:38102 base::RepeatingCallback<bool(const base::CommandLine& command_line,
103 const base::FilePath& current_directory)>;
[email protected]5d364542012-04-05 07:15:39104
[email protected]dd85d452013-03-28 12:39:59105 ProcessSingleton(const base::FilePath& user_data_dir,
106 const NotificationCallback& notification_callback);
Peter Boström53c6c5952021-09-17 09:41:26107
108 ProcessSingleton(const ProcessSingleton&) = delete;
109 ProcessSingleton& operator=(const ProcessSingleton&) = delete;
110
[email protected]7c47ae3e2009-02-18 00:34:21111 ~ProcessSingleton();
[email protected]fc14cef2009-01-27 22:17:29112
[email protected]5d364542012-04-05 07:15:39113 // Notify another process, if available. Otherwise sets ourselves as the
[email protected]dd85d452013-03-28 12:39:59114 // singleton instance. Returns PROCESS_NONE if we became the singleton
[email protected]14649ecd2012-12-05 01:00:24115 // instance. Callers are guaranteed to either have notified an existing
116 // process or have grabbed the singleton (unless the profile is locked by an
117 // unreachable process).
118 // TODO(brettw): Make the implementation of this method non-platform-specific
119 // by making Linux re-use the Windows implementation.
[email protected]dd85d452013-03-28 12:39:59120 NotifyResult NotifyOtherProcessOrCreate();
[email protected]4a44bc32010-05-28 22:22:44121
[email protected]7707b21c2012-12-03 20:42:37122 // Sets ourself up as the singleton instance. Returns true on success. If
123 // false is returned, we are not the singleton instance and the caller must
[email protected]dd85d452013-03-28 12:39:59124 // exit.
[email protected]7707b21c2012-12-03 20:42:37125 // NOTE: Most callers should generally prefer NotifyOtherProcessOrCreate() to
[email protected]d4da57be2014-07-16 17:53:20126 // this method, only callers for whom failure is preferred to notifying
127 // another process should call this directly.
[email protected]dd85d452013-03-28 12:39:59128 bool Create();
[email protected]c0d297952009-09-17 21:00:18129
Etienne Bergeron15cfea82022-08-02 14:56:30130 // Start watching for notifications from other processes.
131 void StartWatching();
132
[email protected]9f20a6d02009-08-21 01:18:37133 // Clear any lock state during shutdown.
134 void Cleanup();
135
Xiaohan Wang55ae2c012022-01-20 21:49:11136#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
[email protected]7707b21c2012-12-03 20:42:37137 static void DisablePromptForTesting();
aseren028ea152017-05-16 17:22:43138 static void SkipIsChromeProcessCheckForTesting(bool skip);
aseren670954482017-06-06 18:14:12139 static void SetUserOptedUnlockInUseProfileForTesting(bool set_unlock);
[email protected]a220b5932013-09-21 03:47:44140#endif
Xiaohan Wang55ae2c012022-01-20 21:49:11141#if BUILDFLAG(IS_WIN)
siggi2a0214b2015-03-12 14:50:27142 // Called to query whether to kill a hung browser process that has visible
143 // windows. Return true to allow killing the hung process.
Alexander Cooper4bcb0ce2020-07-16 23:10:38144 using ShouldKillRemoteProcessCallback = base::RepeatingCallback<bool()>;
siggi2a0214b2015-03-12 14:50:27145 void OverrideShouldKillRemoteProcessCallbackForTesting(
146 const ShouldKillRemoteProcessCallback& display_dialog_callback);
147#endif
[email protected]7707b21c2012-12-03 20:42:37148
149 protected:
150 // Notify another process, if available.
[email protected]14649ecd2012-12-05 01:00:24151 // Returns true if another process was found and notified, false if we should
152 // continue with the current process.
153 // On Windows, Create() has to be called before this.
[email protected]7707b21c2012-12-03 20:42:37154 NotifyResult NotifyOtherProcess();
155
Xiaohan Wang55ae2c012022-01-20 21:49:11156#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
[email protected]7707b21c2012-12-03 20:42:37157 // Exposed for testing. We use a timeout on Linux, and in tests we want
158 // this timeout to be short.
[email protected]2f3b1cc2014-03-17 23:07:15159 NotifyResult NotifyOtherProcessWithTimeout(
160 const base::CommandLine& command_line,
mattma92250e2014-09-09 07:26:16161 int retry_attempts,
162 const base::TimeDelta& timeout,
[email protected]2f3b1cc2014-03-17 23:07:15163 bool kill_unresponsive);
[email protected]7707b21c2012-12-03 20:42:37164 NotifyResult NotifyOtherProcessWithTimeoutOrCreate(
[email protected]2f3b1cc2014-03-17 23:07:15165 const base::CommandLine& command_line,
mattma92250e2014-09-09 07:26:16166 int retry_attempts,
167 const base::TimeDelta& timeout);
[email protected]7707b21c2012-12-03 20:42:37168 void OverrideCurrentPidForTesting(base::ProcessId pid);
169 void OverrideKillCallbackForTesting(
Alexander Cooper4bcb0ce2020-07-16 23:10:38170 const base::RepeatingCallback<void(int)>& callback);
[email protected]a220b5932013-09-21 03:47:44171#endif
[email protected]7707b21c2012-12-03 20:42:37172
[email protected]fc14cef2009-01-27 22:17:29173 private:
[email protected]5d364542012-04-05 07:15:39174 NotificationCallback notification_callback_; // Handler for notifications.
[email protected]19d7e9682009-02-18 22:04:28175
Xiaohan Wang55ae2c012022-01-20 21:49:11176#if BUILDFLAG(IS_WIN)
[email protected]650b2d52013-02-10 03:41:45177 bool EscapeVirtualization(const base::FilePath& user_data_dir);
[email protected]0a194552011-09-14 17:53:35178
[email protected]fc14cef2009-01-27 22:17:29179 HWND remote_window_; // The HWND_MESSAGE of another browser.
[email protected]4cf04bb2013-07-11 09:22:38180 base::win::MessageWindow window_; // The message-only window.
[email protected]0a194552011-09-14 17:53:35181 bool is_virtualized_; // Stuck inside Microsoft Softricity VM environment.
[email protected]2b7ff5b92012-07-17 22:45:18182 HANDLE lock_file_;
[email protected]650b2d52013-02-10 03:41:45183 base::FilePath user_data_dir_;
siggi2a0214b2015-03-12 14:50:27184 ShouldKillRemoteProcessCallback should_kill_remote_process_callback_;
Xiaohan Wang55ae2c012022-01-20 21:49:11185#elif BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
[email protected]65718d92012-05-02 23:02:58186 // Return true if the given pid is one of our child processes.
187 // Assumes that the current pid is the root of all pids of the current
188 // instance.
189 bool IsSameChromeInstance(pid_t pid);
190
191 // Extract the process's pid from a symbol link path and if it is on
aseren670954482017-06-06 18:14:12192 // the same host or is_connected_to_socket is true, kill the process, unlink
193 // the lock file and return true.
[email protected]65718d92012-05-02 23:02:58194 // If the process is part of the same chrome instance, unlink the lock file
195 // and return true without killing it.
aseren670954482017-06-06 18:14:12196 // If the process is on a different host and is_connected_to_socket is false,
197 // display profile in use error dialog (on Linux). If user opted to unlock
198 // profile (on Mac OS X by default), unlink the lock file and return true.
199 // Otherwise return false.
200 bool KillProcessByLockPath(bool is_connected_to_socket);
[email protected]65718d92012-05-02 23:02:58201
202 // Default function to kill a process, overridable by tests.
203 void KillProcess(int pid);
204
205 // Allow overriding for tests.
206 base::ProcessId current_pid_;
207
208 // Function to call when the other process is hung and needs to be killed.
209 // Allows overriding for tests.
Alexander Cooper4bcb0ce2020-07-16 23:10:38210 base::RepeatingCallback<void(int)> kill_callback_;
[email protected]65718d92012-05-02 23:02:58211
[email protected]19d7e9682009-02-18 22:04:28212 // Path in file system to the socket.
[email protected]650b2d52013-02-10 03:41:45213 base::FilePath socket_path_;
[email protected]b674dc732009-05-20 20:41:00214
[email protected]9f20a6d02009-08-21 01:18:37215 // Path in file system to the lock.
[email protected]650b2d52013-02-10 03:41:45216 base::FilePath lock_path_;
[email protected]9f20a6d02009-08-21 01:18:37217
[email protected]53f4826c2010-08-27 01:29:28218 // Path in file system to the cookie file.
[email protected]650b2d52013-02-10 03:41:45219 base::FilePath cookie_path_;
[email protected]53f4826c2010-08-27 01:29:28220
221 // Temporary directory to hold the socket.
[email protected]ea1a3f62012-11-16 20:34:23222 base::ScopedTempDir socket_dir_;
Etienne Bergeron15cfea82022-08-02 14:56:30223 int sock_ = -1;
[email protected]53f4826c2010-08-27 01:29:28224
[email protected]b674dc732009-05-20 20:41:00225 // Helper class for linux specific messages. LinuxWatcher is ref counted
226 // because it posts messages between threads.
227 class LinuxWatcher;
228 scoped_refptr<LinuxWatcher> watcher_;
[email protected]19d7e9682009-02-18 22:04:28229#endif
[email protected]fc14cef2009-01-27 22:17:29230
Xiaohan Wang55ae2c012022-01-20 21:49:11231#if BUILDFLAG(IS_MAC)
Leonard Greyfe15df92017-12-08 17:09:53232 // macOS 10.13 tries to open a new Chrome instance if a user tries to
233 // open an external link after Chrome has updated, but not relaunched.
234 // This method extracts any waiting "open URL" AppleEvent and forwards
235 // it to the running process. Returns true if an event was found and
236 // forwarded.
237 // crbug.com/777863
238 bool WaitForAndForwardOpenURLEvent(pid_t event_destination_pid);
239#endif
240
gab25894fe2017-05-30 03:40:36241 SEQUENCE_CHECKER(sequence_checker_);
[email protected]fc14cef2009-01-27 22:17:29242};
243
[email protected]175a7a22009-05-03 15:57:53244#endif // CHROME_BROWSER_PROCESS_SINGLETON_H_