blob: a878cffd556543787fbd5cb38991e2e13a5387de [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
11#if defined(OS_WIN)
[email protected]fc14cef2009-01-27 22:17:2912#include <windows.h>
[email protected]0ff0ff32010-12-21 19:34:4213#endif // defined(OS_WIN)
[email protected]fc14cef2009-01-27 22:17:2914
[email protected]edf04b512012-02-23 09:47:4315#include <set>
16#include <vector>
17
[email protected]5d364542012-04-05 07:15:3918#include "base/callback.h"
[email protected]edf04b512012-02-23 09:47:4319#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5220#include "base/files/file_path.h"
[email protected]b674dc732009-05-20 20:41:0021#include "base/logging.h"
avib896c712015-12-26 02:10:4322#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1523#include "base/memory/ref_counted.h"
[email protected]d09a4ce1c2013-07-24 17:37:0224#include "base/process/process.h"
[email protected]08397d52011-02-05 01:53:3825#include "ui/gfx/native_widget_types.h"
[email protected]0ff0ff32010-12-21 19:34:4226
[email protected]c17ecbe2014-05-01 10:50:0527#if defined(OS_POSIX) && !defined(OS_ANDROID)
[email protected]ea1a3f62012-11-16 20:34:2328#include "base/files/scoped_temp_dir.h"
[email protected]a220b5932013-09-21 03:47:4429#endif
[email protected]fc14cef2009-01-27 22:17:2930
[email protected]4cf04bb2013-07-11 09:22:3831#if defined(OS_WIN)
32#include "base/win/message_window.h"
33#endif // defined(OS_WIN)
34
[email protected]2f3b1cc2014-03-17 23:07:1535namespace base {
[email protected]0189bbd2009-10-12 22:50:3936class CommandLine;
[email protected]2f3b1cc2014-03-17 23:07:1537}
[email protected]0189bbd2009-10-12 22:50:3938
[email protected]7c47ae3e2009-02-18 00:34:2139// ProcessSingleton ----------------------------------------------------------
[email protected]fc14cef2009-01-27 22:17:2940//
[email protected]7c47ae3e2009-02-18 00:34:2141// This class allows different browser processes to communicate with
42// each other. It is named according to the user data directory, so
43// we can be sure that no more than one copy of the application can be
44// running at once with a given data directory.
45//
[email protected]19d7e9682009-02-18 22:04:2846// Implementation notes:
47// - the Windows implementation uses an invisible global message window;
[email protected]e134a722009-02-23 23:54:0248// - the Linux implementation uses a Unix domain socket in the user data dir.
[email protected]fc14cef2009-01-27 22:17:2949
gab25894fe2017-05-30 03:40:3650class ProcessSingleton {
[email protected]fc14cef2009-01-27 22:17:2951 public:
aseren028ea152017-05-16 17:22:4352 // Used to send the reason of remote hang process termination as histogram.
53 enum RemoteHungProcessTerminateReason {
54#if defined(OS_WIN)
55 USER_ACCEPTED_TERMINATION = 1,
56 NO_VISIBLE_WINDOW_FOUND = 2,
57#elif defined(OS_POSIX)
58 NOTIFY_ATTEMPTS_EXCEEDED = 3,
59 SOCKET_WRITE_FAILED = 4,
60 SOCKET_READ_FAILED = 5,
61#endif
62 REMOTE_HUNG_PROCESS_TERMINATE_REASON_COUNT
63 };
64
65 // Used to send the result of interaction with remote process as histograms in
66 // case when remote process influences on start.
67 enum RemoteProcessInteractionResult {
68 TERMINATE_SUCCEEDED = 0,
69 TERMINATE_FAILED = 1,
70 REMOTE_PROCESS_NOT_FOUND = 2,
71#if defined(OS_WIN)
72 TERMINATE_WAIT_TIMEOUT = 3,
73 RUNNING_PROCESS_NOTIFY_ERROR = 4,
74#elif defined(OS_POSIX)
75 TERMINATE_NOT_ENOUGH_PERMISSIONS = 5,
76 REMOTE_PROCESS_SHUTTING_DOWN = 6,
77 PROFILE_UNLOCKED = 7,
78 PROFILE_UNLOCKED_BEFORE_KILL = 8,
79 SAME_BROWSER_INSTANCE = 9,
80 SAME_BROWSER_INSTANCE_BEFORE_KILL = 10,
81 FAILED_TO_EXTRACT_PID = 11,
82 INVALID_LOCK_FILE = 12,
83 ORPHANED_LOCK_FILE = 13,
84#endif
Mikhail Atuchina6e8bcd32018-07-06 12:14:0385 USER_REFUSED_TERMINATION = 14,
aseren028ea152017-05-16 17:22:4386 REMOTE_PROCESS_INTERACTION_RESULT_COUNT
87 };
88
gab439f54f2016-10-07 22:24:2289 // Logged as histograms, do not modify these values.
[email protected]9f20a6d02009-08-21 01:18:3790 enum NotifyResult {
gab439f54f2016-10-07 22:24:2291 PROCESS_NONE = 0,
92 PROCESS_NOTIFIED = 1,
93 PROFILE_IN_USE = 2,
94 LOCK_ERROR = 3,
95 LAST_VALUE = LOCK_ERROR
[email protected]9f20a6d02009-08-21 01:18:3796 };
97
gab439f54f2016-10-07 22:24:2298 static constexpr int kNumNotifyResults = LAST_VALUE + 1;
99
[email protected]5d364542012-04-05 07:15:39100 // Implement this callback to handle notifications from other processes. The
101 // callback will receive the command line and directory with which the other
102 // Chrome process was launched. Return true if the command line will be
103 // handled within the current browser instance or false if the remote process
104 // should handle it (i.e., because the current process is shutting down).
siggi2a0214b2015-03-12 14:50:27105 using NotificationCallback =
106 base::Callback<bool(const base::CommandLine& command_line,
107 const base::FilePath& current_directory)>;
[email protected]5d364542012-04-05 07:15:39108
[email protected]dd85d452013-03-28 12:39:59109 ProcessSingleton(const base::FilePath& user_data_dir,
110 const NotificationCallback& notification_callback);
[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
[email protected]9f20a6d02009-08-21 01:18:37130 // Clear any lock state during shutdown.
131 void Cleanup();
132
[email protected]c17ecbe2014-05-01 10:50:05133#if defined(OS_POSIX) && !defined(OS_ANDROID)
[email protected]7707b21c2012-12-03 20:42:37134 static void DisablePromptForTesting();
aseren028ea152017-05-16 17:22:43135 static void SkipIsChromeProcessCheckForTesting(bool skip);
aseren670954482017-06-06 18:14:12136 static void SetUserOptedUnlockInUseProfileForTesting(bool set_unlock);
[email protected]a220b5932013-09-21 03:47:44137#endif
siggi2a0214b2015-03-12 14:50:27138#if defined(OS_WIN)
139 // Called to query whether to kill a hung browser process that has visible
140 // windows. Return true to allow killing the hung process.
141 using ShouldKillRemoteProcessCallback = base::Callback<bool()>;
142 void OverrideShouldKillRemoteProcessCallbackForTesting(
143 const ShouldKillRemoteProcessCallback& display_dialog_callback);
144#endif
[email protected]7707b21c2012-12-03 20:42:37145
146 protected:
147 // Notify another process, if available.
[email protected]14649ecd2012-12-05 01:00:24148 // Returns true if another process was found and notified, false if we should
149 // continue with the current process.
150 // On Windows, Create() has to be called before this.
[email protected]7707b21c2012-12-03 20:42:37151 NotifyResult NotifyOtherProcess();
152
[email protected]c17ecbe2014-05-01 10:50:05153#if defined(OS_POSIX) && !defined(OS_ANDROID)
[email protected]7707b21c2012-12-03 20:42:37154 // Exposed for testing. We use a timeout on Linux, and in tests we want
155 // this timeout to be short.
[email protected]2f3b1cc2014-03-17 23:07:15156 NotifyResult NotifyOtherProcessWithTimeout(
157 const base::CommandLine& command_line,
mattma92250e2014-09-09 07:26:16158 int retry_attempts,
159 const base::TimeDelta& timeout,
[email protected]2f3b1cc2014-03-17 23:07:15160 bool kill_unresponsive);
[email protected]7707b21c2012-12-03 20:42:37161 NotifyResult NotifyOtherProcessWithTimeoutOrCreate(
[email protected]2f3b1cc2014-03-17 23:07:15162 const base::CommandLine& command_line,
mattma92250e2014-09-09 07:26:16163 int retry_attempts,
164 const base::TimeDelta& timeout);
[email protected]7707b21c2012-12-03 20:42:37165 void OverrideCurrentPidForTesting(base::ProcessId pid);
166 void OverrideKillCallbackForTesting(
167 const base::Callback<void(int)>& callback);
[email protected]a220b5932013-09-21 03:47:44168#endif
[email protected]7707b21c2012-12-03 20:42:37169
[email protected]fc14cef2009-01-27 22:17:29170 private:
[email protected]5d364542012-04-05 07:15:39171 NotificationCallback notification_callback_; // Handler for notifications.
[email protected]19d7e9682009-02-18 22:04:28172
[email protected]95259c62011-10-25 23:23:53173#if defined(OS_WIN)
[email protected]650b2d52013-02-10 03:41:45174 bool EscapeVirtualization(const base::FilePath& user_data_dir);
[email protected]0a194552011-09-14 17:53:35175
[email protected]fc14cef2009-01-27 22:17:29176 HWND remote_window_; // The HWND_MESSAGE of another browser.
[email protected]4cf04bb2013-07-11 09:22:38177 base::win::MessageWindow window_; // The message-only window.
[email protected]0a194552011-09-14 17:53:35178 bool is_virtualized_; // Stuck inside Microsoft Softricity VM environment.
[email protected]2b7ff5b92012-07-17 22:45:18179 HANDLE lock_file_;
[email protected]650b2d52013-02-10 03:41:45180 base::FilePath user_data_dir_;
siggi2a0214b2015-03-12 14:50:27181 ShouldKillRemoteProcessCallback should_kill_remote_process_callback_;
[email protected]c17ecbe2014-05-01 10:50:05182#elif defined(OS_POSIX) && !defined(OS_ANDROID)
[email protected]65718d92012-05-02 23:02:58183 // Return true if the given pid is one of our child processes.
184 // Assumes that the current pid is the root of all pids of the current
185 // instance.
186 bool IsSameChromeInstance(pid_t pid);
187
188 // Extract the process's pid from a symbol link path and if it is on
aseren670954482017-06-06 18:14:12189 // the same host or is_connected_to_socket is true, kill the process, unlink
190 // the lock file and return true.
[email protected]65718d92012-05-02 23:02:58191 // If the process is part of the same chrome instance, unlink the lock file
192 // and return true without killing it.
aseren670954482017-06-06 18:14:12193 // If the process is on a different host and is_connected_to_socket is false,
194 // display profile in use error dialog (on Linux). If user opted to unlock
195 // profile (on Mac OS X by default), unlink the lock file and return true.
196 // Otherwise return false.
197 bool KillProcessByLockPath(bool is_connected_to_socket);
[email protected]65718d92012-05-02 23:02:58198
199 // Default function to kill a process, overridable by tests.
200 void KillProcess(int pid);
201
202 // Allow overriding for tests.
203 base::ProcessId current_pid_;
204
205 // Function to call when the other process is hung and needs to be killed.
206 // Allows overriding for tests.
207 base::Callback<void(int)> kill_callback_;
208
[email protected]19d7e9682009-02-18 22:04:28209 // Path in file system to the socket.
[email protected]650b2d52013-02-10 03:41:45210 base::FilePath socket_path_;
[email protected]b674dc732009-05-20 20:41:00211
[email protected]9f20a6d02009-08-21 01:18:37212 // Path in file system to the lock.
[email protected]650b2d52013-02-10 03:41:45213 base::FilePath lock_path_;
[email protected]9f20a6d02009-08-21 01:18:37214
[email protected]53f4826c2010-08-27 01:29:28215 // Path in file system to the cookie file.
[email protected]650b2d52013-02-10 03:41:45216 base::FilePath cookie_path_;
[email protected]53f4826c2010-08-27 01:29:28217
218 // Temporary directory to hold the socket.
[email protected]ea1a3f62012-11-16 20:34:23219 base::ScopedTempDir socket_dir_;
[email protected]53f4826c2010-08-27 01:29:28220
[email protected]b674dc732009-05-20 20:41:00221 // Helper class for linux specific messages. LinuxWatcher is ref counted
222 // because it posts messages between threads.
223 class LinuxWatcher;
224 scoped_refptr<LinuxWatcher> watcher_;
[email protected]19d7e9682009-02-18 22:04:28225#endif
[email protected]fc14cef2009-01-27 22:17:29226
Leonard Greyfe15df92017-12-08 17:09:53227#if defined(OS_MACOSX)
228 // macOS 10.13 tries to open a new Chrome instance if a user tries to
229 // open an external link after Chrome has updated, but not relaunched.
230 // This method extracts any waiting "open URL" AppleEvent and forwards
231 // it to the running process. Returns true if an event was found and
232 // forwarded.
233 // crbug.com/777863
234 bool WaitForAndForwardOpenURLEvent(pid_t event_destination_pid);
235#endif
236
gab25894fe2017-05-30 03:40:36237 SEQUENCE_CHECKER(sequence_checker_);
238
[email protected]7c47ae3e2009-02-18 00:34:21239 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton);
[email protected]fc14cef2009-01-27 22:17:29240};
241
[email protected]175a7a22009-05-03 15:57:53242#endif // CHROME_BROWSER_PROCESS_SINGLETON_H_