blob: 190804f42aabb2068c2ddaaa47a6fbd2bf0163cc [file] [log] [blame]
[email protected]fc14cef2009-01-27 22:17:291// Copyright (c) 2009 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.
4
[email protected]7c47ae3e2009-02-18 00:34:215#include "chrome/browser/process_singleton.h"
[email protected]fc14cef2009-01-27 22:17:296
7#include "base/base_paths.h"
8#include "base/command_line.h"
9#include "base/process_util.h"
10#include "base/win_util.h"
[email protected]fc14cef2009-01-27 22:17:2911#include "chrome/browser/browser_init.h"
12#include "chrome/browser/browser_process.h"
13#include "chrome/browser/profile.h"
14#include "chrome/browser/profile_manager.h"
15#include "chrome/common/chrome_constants.h"
16#include "chrome/common/chrome_paths.h"
17#include "chrome/common/l10n_util.h"
[email protected]74d1bb02009-03-03 00:41:2318#include "chrome/common/result_codes.h"
[email protected]fc14cef2009-01-27 22:17:2919#include "chrome/common/win_util.h"
[email protected]34ac8f32009-02-22 23:03:2720#include "grit/chromium_strings.h"
21#include "grit/generated_resources.h"
[email protected]fc14cef2009-01-27 22:17:2922
23namespace {
24
25// Checks the visiblilty of the enumerated window and signals once a visible
26// window has been found.
27BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) {
28 bool* result = reinterpret_cast<bool*>(param);
29 *result = IsWindowVisible(window) != 0;
30 // Stops enumeration if a visible window has been found.
31 return !*result;
32}
33
34} // namespace
35
[email protected]7c47ae3e2009-02-18 00:34:2136ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir)
[email protected]fc14cef2009-01-27 22:17:2937 : window_(NULL),
38 locked_(false) {
39 // Look for a Chrome instance that uses the same profile directory:
40 remote_window_ = FindWindowEx(HWND_MESSAGE,
41 NULL,
42 chrome::kMessageWindowClass,
[email protected]f7011fcb2009-01-28 21:54:3243 user_data_dir.ToWStringHack().c_str());
[email protected]fc14cef2009-01-27 22:17:2944}
45
[email protected]7c47ae3e2009-02-18 00:34:2146ProcessSingleton::~ProcessSingleton() {
[email protected]fc14cef2009-01-27 22:17:2947 if (window_)
48 DestroyWindow(window_);
49}
50
[email protected]7c47ae3e2009-02-18 00:34:2151bool ProcessSingleton::NotifyOtherProcess() {
[email protected]fc14cef2009-01-27 22:17:2952 if (!remote_window_)
53 return false;
54
55 // Found another window, send our command line to it
56 // format is "START\0<<<current directory>>>\0<<<commandline>>>".
57 std::wstring to_send(L"START\0", 6); // want the NULL in the string.
58 std::wstring cur_dir;
59 if (!PathService::Get(base::DIR_CURRENT, &cur_dir))
60 return false;
61 to_send.append(cur_dir);
62 to_send.append(L"\0", 1); // Null separator.
63 to_send.append(GetCommandLineW());
64 to_send.append(L"\0", 1); // Null separator.
65
66 // Allow the current running browser window making itself the foreground
67 // window (otherwise it will just flash in the taskbar).
68 DWORD process_id = 0;
69 DWORD thread_id = GetWindowThreadProcessId(remote_window_, &process_id);
[email protected]0815b6d2009-02-11 00:39:3770 // It is possible that the process owning this window may have died by now.
71 if (!thread_id || !process_id) {
72 remote_window_ = NULL;
73 return false;
74 }
75
[email protected]fc14cef2009-01-27 22:17:2976 AllowSetForegroundWindow(process_id);
77
78 // Gives 20 seconds timeout for the current browser process to respond.
79 const int kTimeout = 20000;
80 COPYDATASTRUCT cds;
81 cds.dwData = 0;
82 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t));
83 cds.lpData = const_cast<wchar_t*>(to_send.c_str());
84 DWORD_PTR result = 0;
85 if (SendMessageTimeout(remote_window_,
86 WM_COPYDATA,
87 NULL,
88 reinterpret_cast<LPARAM>(&cds),
89 SMTO_ABORTIFHUNG,
90 kTimeout,
91 &result)) {
[email protected]0815b6d2009-02-11 00:39:3792 // It is possible that the process owning this window may have died by now.
93 if (!result) {
94 remote_window_ = NULL;
95 return false;
96 }
[email protected]fc14cef2009-01-27 22:17:2997 return true;
98 }
99
[email protected]0815b6d2009-02-11 00:39:37100 // It is possible that the process owning this window may have died by now.
101 if (!IsWindow(remote_window_)) {
102 remote_window_ = NULL;
103 return false;
104 }
105
[email protected]fc14cef2009-01-27 22:17:29106 // The window is hung. Scan for every window to find a visible one.
107 bool visible_window = false;
108 EnumThreadWindows(thread_id,
109 &BrowserWindowEnumeration,
110 reinterpret_cast<LPARAM>(&visible_window));
111
112 // If there is a visible browser window, ask the user before killing it.
113 if (visible_window) {
114 std::wstring text = l10n_util::GetString(IDS_BROWSER_HUNGBROWSER_MESSAGE);
115 std::wstring caption = l10n_util::GetString(IDS_PRODUCT_NAME);
116 if (IDYES != win_util::MessageBox(NULL, text, caption,
117 MB_YESNO | MB_ICONSTOP | MB_TOPMOST)) {
118 // The user denied. Quit silently.
119 return true;
120 }
121 }
122
123 // Time to take action. Kill the browser process.
[email protected]cd4fd152009-02-09 19:28:41124 base::KillProcessById(process_id, ResultCodes::HUNG, true);
[email protected]fc14cef2009-01-27 22:17:29125 remote_window_ = NULL;
126 return false;
127}
128
[email protected]7c47ae3e2009-02-18 00:34:21129void ProcessSingleton::Create() {
[email protected]fc14cef2009-01-27 22:17:29130 DCHECK(!window_);
131 DCHECK(!remote_window_);
132 HINSTANCE hinst = GetModuleHandle(NULL);
133
134 WNDCLASSEX wc = {0};
135 wc.cbSize = sizeof(wc);
[email protected]7c47ae3e2009-02-18 00:34:21136 wc.lpfnWndProc = ProcessSingleton::WndProcStatic;
[email protected]fc14cef2009-01-27 22:17:29137 wc.hInstance = hinst;
138 wc.lpszClassName = chrome::kMessageWindowClass;
139 RegisterClassEx(&wc);
140
141 std::wstring user_data_dir;
142 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
143
144 // Set the window's title to the path of our user data directory so other
145 // Chrome instances can decide if they should forward to us or not.
146 window_ = CreateWindow(chrome::kMessageWindowClass, user_data_dir.c_str(),
147 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hinst, 0);
148 DCHECK(window_);
149
150 win_util::SetWindowUserData(window_, this);
151}
152
[email protected]7c47ae3e2009-02-18 00:34:21153LRESULT ProcessSingleton::OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds) {
[email protected]fc14cef2009-01-27 22:17:29154 // Ignore the request if the browser process is already in shutdown path.
[email protected]0815b6d2009-02-11 00:39:37155 if (!g_browser_process || g_browser_process->IsShuttingDown()) {
156 LOG(WARNING) << "Not handling WM_COPYDATA as browser is shutting down";
157 return FALSE;
158 }
[email protected]fc14cef2009-01-27 22:17:29159
160 // If locked, it means we are not ready to process this message because
161 // we are probably in a first run critical phase.
162 if (locked_)
163 return TRUE;
164
165 // We should have enough room for the shortest command (min_message_size)
166 // and also be a multiple of wchar_t bytes.
167 static const int min_message_size = 7;
168 if (cds->cbData < min_message_size || cds->cbData % sizeof(wchar_t) != 0) {
169 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << cds->cbData;
170 return TRUE;
171 }
172
173 // We split the string into 4 parts on NULLs.
174 const std::wstring msg(static_cast<wchar_t*>(cds->lpData),
175 cds->cbData / sizeof(wchar_t));
176 const std::wstring::size_type first_null = msg.find_first_of(L'\0');
177 if (first_null == 0 || first_null == std::wstring::npos) {
178 // no NULL byte, don't know what to do
179 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << msg.length() <<
180 ", first null = " << first_null;
181 return TRUE;
182 }
183
184 // Decode the command, which is everything until the first NULL.
185 if (msg.substr(0, first_null) == L"START") {
186 // Another instance is starting parse the command line & do what it would
187 // have done.
188 LOG(INFO) << "Handling STARTUP request from another process";
189 const std::wstring::size_type second_null =
190 msg.find_first_of(L'\0', first_null + 1);
191 if (second_null == std::wstring::npos ||
192 first_null == msg.length() - 1 || second_null == msg.length()) {
193 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
194 "parts separated by NULLs";
195 return TRUE;
196 }
197
198 // Get current directory.
199 const std::wstring cur_dir =
200 msg.substr(first_null + 1, second_null - first_null);
201
202 const std::wstring::size_type third_null =
203 msg.find_first_of(L'\0', second_null + 1);
204 if (third_null == std::wstring::npos ||
205 third_null == msg.length()) {
206 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
207 "parts separated by NULLs";
208 }
209
210 // Get command line.
211 const std::wstring cmd_line =
212 msg.substr(second_null + 1, third_null - second_null);
213
214 CommandLine parsed_command_line(L"");
215 parsed_command_line.ParseFromString(cmd_line);
216 PrefService* prefs = g_browser_process->local_state();
217 DCHECK(prefs);
218
[email protected]f7011fcb2009-01-28 21:54:32219 FilePath user_data_dir;
[email protected]fc14cef2009-01-27 22:17:29220 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
221 ProfileManager* profile_manager = g_browser_process->profile_manager();
222 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
223 if (!profile) {
224 // We should only be able to get here if the profile already exists and
225 // has been created.
226 NOTREACHED();
227 return TRUE;
228 }
[email protected]0303f31c2009-02-02 06:42:05229
230 // Run the browser startup sequence again, with the command line of the
231 // signalling process.
[email protected]7c27203a2009-03-10 21:55:21232 BrowserInit::ProcessCommandLine(parsed_command_line, cur_dir, false,
[email protected]0303f31c2009-02-02 06:42:05233 profile, NULL);
[email protected]fc14cef2009-01-27 22:17:29234 return TRUE;
235 }
236 return TRUE;
237}
238
[email protected]7c47ae3e2009-02-18 00:34:21239LRESULT CALLBACK ProcessSingleton::WndProc(HWND hwnd, UINT message,
[email protected]fc14cef2009-01-27 22:17:29240 WPARAM wparam, LPARAM lparam) {
241 switch (message) {
242 case WM_COPYDATA:
243 return OnCopyData(reinterpret_cast<HWND>(wparam),
244 reinterpret_cast<COPYDATASTRUCT*>(lparam));
245 default:
246 break;
247 }
248
249 return ::DefWindowProc(hwnd, message, wparam, lparam);
250}
251
[email protected]7c47ae3e2009-02-18 00:34:21252void ProcessSingleton::HuntForZombieChromeProcesses() {
[email protected]fc14cef2009-01-27 22:17:29253 // Detecting dead renderers is simple:
254 // - The process is named chrome.exe.
255 // - The process' parent doesn't exist anymore.
256 // - The process doesn't have a chrome::kMessageWindowClass window.
257 // If these conditions hold, the process is a zombie renderer or plugin.
258
259 // Retrieve the list of browser processes on start. This list is then used to
260 // detect zombie renderer process or plugin process.
261 class ZombieDetector : public base::ProcessFilter {
262 public:
263 ZombieDetector() {
264 for (HWND window = NULL;;) {
265 window = FindWindowEx(HWND_MESSAGE,
266 window,
267 chrome::kMessageWindowClass,
268 NULL);
269 if (!window)
270 break;
271 DWORD process = 0;
272 GetWindowThreadProcessId(window, &process);
273 if (process)
274 browsers_.push_back(process);
275 }
276 // We are also a browser, regardless of having the window or not.
277 browsers_.push_back(::GetCurrentProcessId());
278 }
279
280 virtual bool Includes(uint32 pid, uint32 parent_pid) const {
281 // Don't kill ourself eh.
282 if (GetCurrentProcessId() == pid)
283 return false;
284
285 // Is this a browser? If so, ignore it.
286 if (std::find(browsers_.begin(), browsers_.end(), pid) != browsers_.end())
287 return false;
288
289 // Is the parent a browser? If so, ignore it.
290 if (std::find(browsers_.begin(), browsers_.end(), parent_pid)
291 != browsers_.end())
292 return false;
293
294 // The chrome process is orphan.
295 return true;
296 }
297
298 protected:
299 std::vector<uint32> browsers_;
300 };
301
302 ZombieDetector zombie_detector;
303 base::KillProcesses(L"chrome.exe", ResultCodes::HUNG, &zombie_detector);
304}