1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_PROCESS_SINGLETON_H_
6 #define CHROME_BROWSER_PROCESS_SINGLETON_H_
8 #include "build/build_config.h"
12 #endif // defined(OS_WIN)
17 #include "base/basictypes.h"
18 #include "base/callback.h"
19 #include "base/command_line.h"
20 #include "base/files/file_path.h"
21 #include "base/logging.h"
22 #include "base/memory/ref_counted.h"
23 #include "base/process.h"
24 #include "base/threading/non_thread_safe.h"
25 #include "ui/gfx/native_widget_types.h"
27 #if defined(OS_LINUX) || defined(OS_OPENBSD)
28 #include "base/files/scoped_temp_dir.h"
29 #endif // defined(OS_LINUX) || defined(OS_OPENBSD)
33 // ProcessSingleton ----------------------------------------------------------
35 // This class allows different browser processes to communicate with
36 // each other. It is named according to the user data directory, so
37 // we can be sure that no more than one copy of the application can be
38 // running at once with a given data directory.
40 // Implementation notes:
41 // - the Windows implementation uses an invisible global message window;
42 // - the Linux implementation uses a Unix domain socket in the user data dir.
44 class ProcessSingleton
: public base::NonThreadSafe
{
54 // Implement this callback to handle notifications from other processes. The
55 // callback will receive the command line and directory with which the other
56 // Chrome process was launched. Return true if the command line will be
57 // handled within the current browser instance or false if the remote process
58 // should handle it (i.e., because the current process is shutting down).
59 typedef base::Callback
<bool(
60 const CommandLine
& command_line
,
61 const base::FilePath
& current_directory
)> NotificationCallback
;
63 ProcessSingleton(const base::FilePath
& user_data_dir
,
64 const NotificationCallback
& notification_callback
);
67 // Notify another process, if available. Otherwise sets ourselves as the
68 // singleton instance. Returns PROCESS_NONE if we became the singleton
69 // instance. Callers are guaranteed to either have notified an existing
70 // process or have grabbed the singleton (unless the profile is locked by an
71 // unreachable process).
72 // TODO(brettw): Make the implementation of this method non-platform-specific
73 // by making Linux re-use the Windows implementation.
74 NotifyResult
NotifyOtherProcessOrCreate();
76 // Sets ourself up as the singleton instance. Returns true on success. If
77 // false is returned, we are not the singleton instance and the caller must
79 // NOTE: Most callers should generally prefer NotifyOtherProcessOrCreate() to
80 // this method, only callers for whom failure is prefered to notifying another
81 // process should call this directly.
84 // Clear any lock state during shutdown.
88 LRESULT
WndProc(HWND hwnd
, UINT message
, WPARAM wparam
, LPARAM lparam
);
91 #if defined(OS_LINUX) || defined(OS_OPENBSD)
92 static void DisablePromptForTesting();
93 #endif // defined(OS_LINUX) || defined(OS_OPENBSD)
96 // Notify another process, if available.
97 // Returns true if another process was found and notified, false if we should
98 // continue with the current process.
99 // On Windows, Create() has to be called before this.
100 NotifyResult
NotifyOtherProcess();
102 #if defined(OS_LINUX) || defined(OS_OPENBSD)
103 // Exposed for testing. We use a timeout on Linux, and in tests we want
104 // this timeout to be short.
105 NotifyResult
NotifyOtherProcessWithTimeout(const CommandLine
& command_line
,
107 bool kill_unresponsive
);
108 NotifyResult
NotifyOtherProcessWithTimeoutOrCreate(
109 const CommandLine
& command_line
,
110 int timeout_seconds
);
111 void OverrideCurrentPidForTesting(base::ProcessId pid
);
112 void OverrideKillCallbackForTesting(
113 const base::Callback
<void(int)>& callback
);
114 #endif // defined(OS_LINUX) || defined(OS_OPENBSD)
117 #if !defined(OS_MACOSX)
118 // Timeout for the current browser process to respond. 20 seconds should be
119 // enough. It's only used in Windows and Linux implementations.
120 static const int kTimeoutInSeconds
= 20;
123 NotificationCallback notification_callback_
; // Handler for notifications.
126 // This ugly behemoth handles startup commands sent from another process.
127 LRESULT
OnCopyData(HWND hwnd
, const COPYDATASTRUCT
* cds
);
129 bool EscapeVirtualization(const base::FilePath
& user_data_dir
);
131 HWND remote_window_
; // The HWND_MESSAGE of another browser.
132 HWND window_
; // The HWND_MESSAGE window.
133 bool is_virtualized_
; // Stuck inside Microsoft Softricity VM environment.
135 base::FilePath user_data_dir_
;
136 #elif defined(OS_LINUX) || defined(OS_OPENBSD)
137 // Return true if the given pid is one of our child processes.
138 // Assumes that the current pid is the root of all pids of the current
140 bool IsSameChromeInstance(pid_t pid
);
142 // Extract the process's pid from a symbol link path and if it is on
143 // the same host, kill the process, unlink the lock file and return true.
144 // If the process is part of the same chrome instance, unlink the lock file
145 // and return true without killing it.
146 // If the process is on a different host, return false.
147 bool KillProcessByLockPath();
149 // Default function to kill a process, overridable by tests.
150 void KillProcess(int pid
);
152 // Allow overriding for tests.
153 base::ProcessId current_pid_
;
155 // Function to call when the other process is hung and needs to be killed.
156 // Allows overriding for tests.
157 base::Callback
<void(int)> kill_callback_
;
159 // Path in file system to the socket.
160 base::FilePath socket_path_
;
162 // Path in file system to the lock.
163 base::FilePath lock_path_
;
165 // Path in file system to the cookie file.
166 base::FilePath cookie_path_
;
168 // Temporary directory to hold the socket.
169 base::ScopedTempDir socket_dir_
;
171 // Helper class for linux specific messages. LinuxWatcher is ref counted
172 // because it posts messages between threads.
174 scoped_refptr
<LinuxWatcher
> watcher_
;
175 #elif defined(OS_MACOSX)
176 // Path in file system to the lock.
177 base::FilePath lock_path_
;
179 // File descriptor associated with the lockfile, valid between
180 // |Create()| and |Cleanup()|. Two instances cannot have a lock on
181 // the same file at the same time.
185 DISALLOW_COPY_AND_ASSIGN(ProcessSingleton
);
188 #endif // CHROME_BROWSER_PROCESS_SINGLETON_H_