blob: 6ace2f51794fc3d3512980f4f60c30df0937f3dd [file] [log] [blame]
[email protected]f805fe82010-08-03 22:47:101// Copyright (c) 2010 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#include "chrome/browser/process_singleton.h"
[email protected]fc14cef2009-01-27 22:17:296
[email protected]a92b8642009-05-05 23:38:567#include "app/l10n_util.h"
[email protected]4a0765a2009-05-08 23:12:258#include "app/win_util.h"
[email protected]fc14cef2009-01-27 22:17:299#include "base/base_paths.h"
10#include "base/command_line.h"
[email protected]f805fe82010-08-03 22:47:1011#include "base/file_path.h"
[email protected]9e9b6e8e2009-12-02 08:45:0112#include "base/path_service.h"
[email protected]fc14cef2009-01-27 22:17:2913#include "base/process_util.h"
[email protected]bbef41f02010-03-04 16:16:1914#include "base/scoped_handle.h"
[email protected]fc14cef2009-01-27 22:17:2915#include "base/win_util.h"
[email protected]fc14cef2009-01-27 22:17:2916#include "chrome/browser/browser_process.h"
[email protected]6aeac8342010-10-01 20:21:1817#include "chrome/browser/extensions/extensions_startup.h"
[email protected]e313f3b12010-06-25 21:29:1018#include "chrome/browser/platform_util.h"
[email protected]8ecad5e2010-12-02 21:18:3319#include "chrome/browser/profiles/profile.h"
20#include "chrome/browser/profiles/profile_manager.h"
[email protected]f7002802010-11-12 19:50:2821#include "chrome/browser/ui/browser_init.h"
[email protected]fc14cef2009-01-27 22:17:2922#include "chrome/common/chrome_constants.h"
23#include "chrome/common/chrome_paths.h"
[email protected]6aeac8342010-10-01 20:21:1824#include "chrome/common/chrome_switches.h"
[email protected]74d1bb02009-03-03 00:41:2325#include "chrome/common/result_codes.h"
[email protected]bbef41f02010-03-04 16:16:1926#include "chrome/installer/util/browser_distribution.h"
[email protected]34ac8f32009-02-22 23:03:2727#include "grit/chromium_strings.h"
28#include "grit/generated_resources.h"
[email protected]fc14cef2009-01-27 22:17:2929
30namespace {
31
[email protected]f891fb32009-04-08 00:20:3232// Checks the visibility of the enumerated window and signals once a visible
[email protected]fc14cef2009-01-27 22:17:2933// window has been found.
34BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) {
35 bool* result = reinterpret_cast<bool*>(param);
36 *result = IsWindowVisible(window) != 0;
37 // Stops enumeration if a visible window has been found.
38 return !*result;
39}
40
41} // namespace
42
[email protected]f891fb32009-04-08 00:20:3243// Look for a Chrome instance that uses the same profile directory.
[email protected]7c47ae3e2009-02-18 00:34:2144ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir)
[email protected]175a7a22009-05-03 15:57:5345 : window_(NULL), locked_(false), foreground_window_(NULL) {
[email protected]bbef41f02010-03-04 16:16:1946 std::wstring user_data_dir_str(user_data_dir.ToWStringHack());
47 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL,
48 chrome::kMessageWindowClass,
49 user_data_dir_str.c_str());
50 if (!remote_window_) {
51 // Make sure we will be the one and only process creating the window.
52 // We use a named Mutex since we are protecting against multi-process
53 // access. As documented, it's clearer to NOT request ownership on creation
54 // since it isn't guaranteed we will get it. It is better to create it
55 // without ownership and explicitly get the ownership afterward.
56 std::wstring mutex_name(L"Local\\ProcessSingletonStartup!");
57 mutex_name += BrowserDistribution::GetDistribution()->GetAppGuid();
58 ScopedHandle only_me(CreateMutex(NULL, FALSE, mutex_name.c_str()));
59 DCHECK(only_me.Get() != NULL) << "GetLastError = " << GetLastError();
60
61 // This is how we acquire the mutex (as opposed to the initial ownership).
62 DWORD result = WaitForSingleObject(only_me, INFINITE);
63 DCHECK(result == WAIT_OBJECT_0) << "Result = " << result <<
64 "GetLastError = " << GetLastError();
65
66 // We now own the mutex so we are the only process that can create the
67 // window at this time, but we must still check if someone created it
68 // between the time where we looked for it above and the time the mutex
69 // was given to us.
70 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL,
71 chrome::kMessageWindowClass,
72 user_data_dir_str.c_str());
73 if (!remote_window_)
74 Create();
75 BOOL success = ReleaseMutex(only_me);
76 DCHECK(success) << "GetLastError = " << GetLastError();
77 }
[email protected]fc14cef2009-01-27 22:17:2978}
79
[email protected]7c47ae3e2009-02-18 00:34:2180ProcessSingleton::~ProcessSingleton() {
[email protected]aef15bd2009-04-27 20:51:5181 if (window_) {
[email protected]fc14cef2009-01-27 22:17:2982 DestroyWindow(window_);
[email protected]aef15bd2009-04-27 20:51:5183 UnregisterClass(chrome::kMessageWindowClass, GetModuleHandle(NULL));
84 }
[email protected]fc14cef2009-01-27 22:17:2985}
86
[email protected]9f20a6d02009-08-21 01:18:3787ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() {
[email protected]fc14cef2009-01-27 22:17:2988 if (!remote_window_)
[email protected]9f20a6d02009-08-21 01:18:3789 return PROCESS_NONE;
[email protected]fc14cef2009-01-27 22:17:2990
91 // Found another window, send our command line to it
92 // format is "START\0<<<current directory>>>\0<<<commandline>>>".
93 std::wstring to_send(L"START\0", 6); // want the NULL in the string.
[email protected]b9696482010-11-30 23:56:1894 FilePath cur_dir;
[email protected]fc14cef2009-01-27 22:17:2995 if (!PathService::Get(base::DIR_CURRENT, &cur_dir))
[email protected]9f20a6d02009-08-21 01:18:3796 return PROCESS_NONE;
[email protected]b9696482010-11-30 23:56:1897 to_send.append(cur_dir.value());
[email protected]fc14cef2009-01-27 22:17:2998 to_send.append(L"\0", 1); // Null separator.
99 to_send.append(GetCommandLineW());
100 to_send.append(L"\0", 1); // Null separator.
101
102 // Allow the current running browser window making itself the foreground
103 // window (otherwise it will just flash in the taskbar).
104 DWORD process_id = 0;
105 DWORD thread_id = GetWindowThreadProcessId(remote_window_, &process_id);
[email protected]0815b6d2009-02-11 00:39:37106 // It is possible that the process owning this window may have died by now.
107 if (!thread_id || !process_id) {
108 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37109 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37110 }
111
[email protected]fc14cef2009-01-27 22:17:29112 AllowSetForegroundWindow(process_id);
113
[email protected]fc14cef2009-01-27 22:17:29114 COPYDATASTRUCT cds;
115 cds.dwData = 0;
116 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t));
117 cds.lpData = const_cast<wchar_t*>(to_send.c_str());
118 DWORD_PTR result = 0;
119 if (SendMessageTimeout(remote_window_,
120 WM_COPYDATA,
121 NULL,
122 reinterpret_cast<LPARAM>(&cds),
123 SMTO_ABORTIFHUNG,
[email protected]8b08cbd2009-08-04 05:34:19124 kTimeoutInSeconds * 1000,
[email protected]fc14cef2009-01-27 22:17:29125 &result)) {
[email protected]0815b6d2009-02-11 00:39:37126 // It is possible that the process owning this window may have died by now.
127 if (!result) {
128 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37129 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37130 }
[email protected]9f20a6d02009-08-21 01:18:37131 return PROCESS_NOTIFIED;
[email protected]fc14cef2009-01-27 22:17:29132 }
133
[email protected]0815b6d2009-02-11 00:39:37134 // It is possible that the process owning this window may have died by now.
135 if (!IsWindow(remote_window_)) {
136 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37137 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37138 }
139
[email protected]fc14cef2009-01-27 22:17:29140 // The window is hung. Scan for every window to find a visible one.
141 bool visible_window = false;
142 EnumThreadWindows(thread_id,
143 &BrowserWindowEnumeration,
144 reinterpret_cast<LPARAM>(&visible_window));
145
146 // If there is a visible browser window, ask the user before killing it.
147 if (visible_window) {
148 std::wstring text = l10n_util::GetString(IDS_BROWSER_HUNGBROWSER_MESSAGE);
149 std::wstring caption = l10n_util::GetString(IDS_PRODUCT_NAME);
[email protected]e313f3b12010-06-25 21:29:10150 if (!platform_util::SimpleYesNoBox(NULL, caption, text)) {
[email protected]fc14cef2009-01-27 22:17:29151 // The user denied. Quit silently.
[email protected]9f20a6d02009-08-21 01:18:37152 return PROCESS_NOTIFIED;
[email protected]fc14cef2009-01-27 22:17:29153 }
154 }
155
156 // Time to take action. Kill the browser process.
[email protected]cd4fd152009-02-09 19:28:41157 base::KillProcessById(process_id, ResultCodes::HUNG, true);
[email protected]fc14cef2009-01-27 22:17:29158 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37159 return PROCESS_NONE;
[email protected]fc14cef2009-01-27 22:17:29160}
161
[email protected]4a44bc32010-05-28 22:22:44162ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessOrCreate() {
163 NotifyResult result = NotifyOtherProcess();
164 if (result != PROCESS_NONE)
165 return result;
166 return Create() ? PROCESS_NONE : PROFILE_IN_USE;
167}
168
[email protected]f891fb32009-04-08 00:20:32169// For windows, there is no need to call Create() since the call is made in
170// the constructor but to avoid having more platform specific code in
171// browser_main.cc we tolerate a second call which will do nothing.
[email protected]4dd42242010-04-07 02:21:15172bool ProcessSingleton::Create() {
[email protected]fc14cef2009-01-27 22:17:29173 DCHECK(!remote_window_);
[email protected]f891fb32009-04-08 00:20:32174 if (window_)
[email protected]4dd42242010-04-07 02:21:15175 return true;
[email protected]f891fb32009-04-08 00:20:32176
[email protected]fc14cef2009-01-27 22:17:29177 HINSTANCE hinst = GetModuleHandle(NULL);
178
179 WNDCLASSEX wc = {0};
180 wc.cbSize = sizeof(wc);
[email protected]7c47ae3e2009-02-18 00:34:21181 wc.lpfnWndProc = ProcessSingleton::WndProcStatic;
[email protected]fc14cef2009-01-27 22:17:29182 wc.hInstance = hinst;
183 wc.lpszClassName = chrome::kMessageWindowClass;
[email protected]aef15bd2009-04-27 20:51:51184 ATOM clazz = RegisterClassEx(&wc);
185 DCHECK(clazz);
[email protected]fc14cef2009-01-27 22:17:29186
[email protected]ef5ff1b2009-09-09 20:00:13187 FilePath user_data_dir;
[email protected]fc14cef2009-01-27 22:17:29188 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
189
190 // Set the window's title to the path of our user data directory so other
191 // Chrome instances can decide if they should forward to us or not.
[email protected]ef5ff1b2009-09-09 20:00:13192 window_ = CreateWindow(chrome::kMessageWindowClass,
193 user_data_dir.value().c_str(),
[email protected]fc14cef2009-01-27 22:17:29194 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hinst, 0);
195 DCHECK(window_);
196
197 win_util::SetWindowUserData(window_, this);
[email protected]4dd42242010-04-07 02:21:15198 return true;
[email protected]fc14cef2009-01-27 22:17:29199}
200
[email protected]9f20a6d02009-08-21 01:18:37201void ProcessSingleton::Cleanup() {
202}
203
[email protected]7c47ae3e2009-02-18 00:34:21204LRESULT ProcessSingleton::OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds) {
[email protected]175a7a22009-05-03 15:57:53205 // If locked, it means we are not ready to process this message because
[email protected]afd20c022010-06-10 00:48:20206 // we are probably in a first run critical phase.
[email protected]175a7a22009-05-03 15:57:53207 if (locked_) {
208 // Attempt to place ourselves in the foreground / flash the task bar.
209 if (IsWindow(foreground_window_))
210 SetForegroundWindow(foreground_window_);
211 return TRUE;
212 }
213
[email protected]fc14cef2009-01-27 22:17:29214 // Ignore the request if the browser process is already in shutdown path.
[email protected]0815b6d2009-02-11 00:39:37215 if (!g_browser_process || g_browser_process->IsShuttingDown()) {
216 LOG(WARNING) << "Not handling WM_COPYDATA as browser is shutting down";
217 return FALSE;
218 }
[email protected]fc14cef2009-01-27 22:17:29219
[email protected]fc14cef2009-01-27 22:17:29220 // We should have enough room for the shortest command (min_message_size)
[email protected]366ffe52009-04-24 22:02:23221 // and also be a multiple of wchar_t bytes. The shortest command
222 // possible is L"START\0\0" (empty current directory and command line).
[email protected]fc14cef2009-01-27 22:17:29223 static const int min_message_size = 7;
[email protected]366ffe52009-04-24 22:02:23224 if (cds->cbData < min_message_size * sizeof(wchar_t) ||
225 cds->cbData % sizeof(wchar_t) != 0) {
[email protected]fc14cef2009-01-27 22:17:29226 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << cds->cbData;
227 return TRUE;
228 }
229
230 // We split the string into 4 parts on NULLs.
[email protected]366ffe52009-04-24 22:02:23231 DCHECK(cds->lpData);
[email protected]fc14cef2009-01-27 22:17:29232 const std::wstring msg(static_cast<wchar_t*>(cds->lpData),
233 cds->cbData / sizeof(wchar_t));
234 const std::wstring::size_type first_null = msg.find_first_of(L'\0');
235 if (first_null == 0 || first_null == std::wstring::npos) {
236 // no NULL byte, don't know what to do
237 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << msg.length() <<
238 ", first null = " << first_null;
239 return TRUE;
240 }
241
242 // Decode the command, which is everything until the first NULL.
243 if (msg.substr(0, first_null) == L"START") {
244 // Another instance is starting parse the command line & do what it would
245 // have done.
[email protected]8e96e502010-10-21 20:57:12246 VLOG(1) << "Handling STARTUP request from another process";
[email protected]fc14cef2009-01-27 22:17:29247 const std::wstring::size_type second_null =
248 msg.find_first_of(L'\0', first_null + 1);
249 if (second_null == std::wstring::npos ||
250 first_null == msg.length() - 1 || second_null == msg.length()) {
251 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
252 "parts separated by NULLs";
253 return TRUE;
254 }
255
256 // Get current directory.
[email protected]f805fe82010-08-03 22:47:10257 const FilePath cur_dir(msg.substr(first_null + 1,
258 second_null - first_null));
[email protected]fc14cef2009-01-27 22:17:29259
260 const std::wstring::size_type third_null =
261 msg.find_first_of(L'\0', second_null + 1);
262 if (third_null == std::wstring::npos ||
263 third_null == msg.length()) {
264 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
265 "parts separated by NULLs";
266 }
267
268 // Get command line.
269 const std::wstring cmd_line =
270 msg.substr(second_null + 1, third_null - second_null);
271
[email protected]51343d5a2009-10-26 22:39:33272 CommandLine parsed_command_line = CommandLine::FromString(cmd_line);
[email protected]fc14cef2009-01-27 22:17:29273 PrefService* prefs = g_browser_process->local_state();
274 DCHECK(prefs);
275
[email protected]ddf8a4b02010-03-22 23:08:30276 Profile* profile = ProfileManager::GetDefaultProfile();
[email protected]fc14cef2009-01-27 22:17:29277 if (!profile) {
278 // We should only be able to get here if the profile already exists and
279 // has been created.
280 NOTREACHED();
281 return TRUE;
282 }
[email protected]0303f31c2009-02-02 06:42:05283
[email protected]6aeac8342010-10-01 20:21:18284 // Handle the --uninstall-extension startup action. This needs to done here
285 // in the process that is running with the target profile, otherwise the
286 // uninstall will fail to unload and remove all components.
287 if (parsed_command_line.HasSwitch(switches::kUninstallExtension)) {
288 extensions_startup::HandleUninstallExtension(parsed_command_line,
289 profile);
290 return TRUE;
291 }
292
[email protected]0303f31c2009-02-02 06:42:05293 // Run the browser startup sequence again, with the command line of the
294 // signalling process.
[email protected]7c27203a2009-03-10 21:55:21295 BrowserInit::ProcessCommandLine(parsed_command_line, cur_dir, false,
[email protected]0303f31c2009-02-02 06:42:05296 profile, NULL);
[email protected]fc14cef2009-01-27 22:17:29297 return TRUE;
298 }
299 return TRUE;
300}
301
[email protected]7c47ae3e2009-02-18 00:34:21302LRESULT CALLBACK ProcessSingleton::WndProc(HWND hwnd, UINT message,
[email protected]175a7a22009-05-03 15:57:53303 WPARAM wparam, LPARAM lparam) {
[email protected]fc14cef2009-01-27 22:17:29304 switch (message) {
305 case WM_COPYDATA:
306 return OnCopyData(reinterpret_cast<HWND>(wparam),
307 reinterpret_cast<COPYDATASTRUCT*>(lparam));
308 default:
309 break;
310 }
311
312 return ::DefWindowProc(hwnd, message, wparam, lparam);
313}