blob: 74e1d51ccdbfaec31b377d0299f39ff7c1a36192 [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
[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"
11#include "base/process_util.h"
12#include "base/win_util.h"
[email protected]fc14cef2009-01-27 22:17:2913#include "chrome/browser/browser_init.h"
14#include "chrome/browser/browser_process.h"
15#include "chrome/browser/profile.h"
16#include "chrome/browser/profile_manager.h"
17#include "chrome/common/chrome_constants.h"
18#include "chrome/common/chrome_paths.h"
[email protected]74d1bb02009-03-03 00:41:2319#include "chrome/common/result_codes.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
[email protected]f891fb32009-04-08 00:20:3225// Checks the visibility of the enumerated window and signals once a visible
[email protected]fc14cef2009-01-27 22:17:2926// 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]f891fb32009-04-08 00:20:3236// Look for a Chrome instance that uses the same profile directory.
[email protected]7c47ae3e2009-02-18 00:34:2137ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir)
[email protected]175a7a22009-05-03 15:57:5338 : window_(NULL), locked_(false), foreground_window_(NULL) {
[email protected]f891fb32009-04-08 00:20:3239 // FindWindoEx and Create() should be one atomic operation in order to not
40 // have a race condition.
41 remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass,
[email protected]f7011fcb2009-01-28 21:54:3242 user_data_dir.ToWStringHack().c_str());
[email protected]f891fb32009-04-08 00:20:3243 if (!remote_window_)
44 Create();
[email protected]fc14cef2009-01-27 22:17:2945}
46
[email protected]7c47ae3e2009-02-18 00:34:2147ProcessSingleton::~ProcessSingleton() {
[email protected]aef15bd2009-04-27 20:51:5148 if (window_) {
[email protected]fc14cef2009-01-27 22:17:2949 DestroyWindow(window_);
[email protected]aef15bd2009-04-27 20:51:5150 UnregisterClass(chrome::kMessageWindowClass, GetModuleHandle(NULL));
51 }
[email protected]fc14cef2009-01-27 22:17:2952}
53
[email protected]7c47ae3e2009-02-18 00:34:2154bool ProcessSingleton::NotifyOtherProcess() {
[email protected]fc14cef2009-01-27 22:17:2955 if (!remote_window_)
56 return false;
57
58 // Found another window, send our command line to it
59 // format is "START\0<<<current directory>>>\0<<<commandline>>>".
60 std::wstring to_send(L"START\0", 6); // want the NULL in the string.
61 std::wstring cur_dir;
62 if (!PathService::Get(base::DIR_CURRENT, &cur_dir))
63 return false;
64 to_send.append(cur_dir);
65 to_send.append(L"\0", 1); // Null separator.
66 to_send.append(GetCommandLineW());
67 to_send.append(L"\0", 1); // Null separator.
68
69 // Allow the current running browser window making itself the foreground
70 // window (otherwise it will just flash in the taskbar).
71 DWORD process_id = 0;
72 DWORD thread_id = GetWindowThreadProcessId(remote_window_, &process_id);
[email protected]0815b6d2009-02-11 00:39:3773 // It is possible that the process owning this window may have died by now.
74 if (!thread_id || !process_id) {
75 remote_window_ = NULL;
76 return false;
77 }
78
[email protected]fc14cef2009-01-27 22:17:2979 AllowSetForegroundWindow(process_id);
80
[email protected]fc14cef2009-01-27 22:17:2981 COPYDATASTRUCT cds;
82 cds.dwData = 0;
83 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t));
84 cds.lpData = const_cast<wchar_t*>(to_send.c_str());
85 DWORD_PTR result = 0;
86 if (SendMessageTimeout(remote_window_,
87 WM_COPYDATA,
88 NULL,
89 reinterpret_cast<LPARAM>(&cds),
90 SMTO_ABORTIFHUNG,
[email protected]8b08cbd2009-08-04 05:34:1991 kTimeoutInSeconds * 1000,
[email protected]fc14cef2009-01-27 22:17:2992 &result)) {
[email protected]0815b6d2009-02-11 00:39:3793 // It is possible that the process owning this window may have died by now.
94 if (!result) {
95 remote_window_ = NULL;
96 return false;
97 }
[email protected]fc14cef2009-01-27 22:17:2998 return true;
99 }
100
[email protected]0815b6d2009-02-11 00:39:37101 // It is possible that the process owning this window may have died by now.
102 if (!IsWindow(remote_window_)) {
103 remote_window_ = NULL;
104 return false;
105 }
106
[email protected]fc14cef2009-01-27 22:17:29107 // The window is hung. Scan for every window to find a visible one.
108 bool visible_window = false;
109 EnumThreadWindows(thread_id,
110 &BrowserWindowEnumeration,
111 reinterpret_cast<LPARAM>(&visible_window));
112
113 // If there is a visible browser window, ask the user before killing it.
114 if (visible_window) {
115 std::wstring text = l10n_util::GetString(IDS_BROWSER_HUNGBROWSER_MESSAGE);
116 std::wstring caption = l10n_util::GetString(IDS_PRODUCT_NAME);
117 if (IDYES != win_util::MessageBox(NULL, text, caption,
118 MB_YESNO | MB_ICONSTOP | MB_TOPMOST)) {
119 // The user denied. Quit silently.
120 return true;
121 }
122 }
123
124 // Time to take action. Kill the browser process.
[email protected]cd4fd152009-02-09 19:28:41125 base::KillProcessById(process_id, ResultCodes::HUNG, true);
[email protected]fc14cef2009-01-27 22:17:29126 remote_window_ = NULL;
127 return false;
128}
129
[email protected]f891fb32009-04-08 00:20:32130// For windows, there is no need to call Create() since the call is made in
131// the constructor but to avoid having more platform specific code in
132// browser_main.cc we tolerate a second call which will do nothing.
[email protected]7c47ae3e2009-02-18 00:34:21133void ProcessSingleton::Create() {
[email protected]fc14cef2009-01-27 22:17:29134 DCHECK(!remote_window_);
[email protected]f891fb32009-04-08 00:20:32135 if (window_)
136 return;
137
[email protected]fc14cef2009-01-27 22:17:29138 HINSTANCE hinst = GetModuleHandle(NULL);
139
140 WNDCLASSEX wc = {0};
141 wc.cbSize = sizeof(wc);
[email protected]7c47ae3e2009-02-18 00:34:21142 wc.lpfnWndProc = ProcessSingleton::WndProcStatic;
[email protected]fc14cef2009-01-27 22:17:29143 wc.hInstance = hinst;
144 wc.lpszClassName = chrome::kMessageWindowClass;
[email protected]aef15bd2009-04-27 20:51:51145 ATOM clazz = RegisterClassEx(&wc);
146 DCHECK(clazz);
[email protected]fc14cef2009-01-27 22:17:29147
148 std::wstring user_data_dir;
149 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
150
151 // Set the window's title to the path of our user data directory so other
152 // Chrome instances can decide if they should forward to us or not.
153 window_ = CreateWindow(chrome::kMessageWindowClass, user_data_dir.c_str(),
154 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hinst, 0);
155 DCHECK(window_);
156
157 win_util::SetWindowUserData(window_, this);
158}
159
[email protected]7c47ae3e2009-02-18 00:34:21160LRESULT ProcessSingleton::OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds) {
[email protected]175a7a22009-05-03 15:57:53161 // If locked, it means we are not ready to process this message because
162 // we are probably in a first run critical phase. We must do this before
163 // doing the IsShuttingDown() check since that returns true during first run
164 // (since g_browser_process hasn't been AddRefModule()d yet).
165 if (locked_) {
166 // Attempt to place ourselves in the foreground / flash the task bar.
167 if (IsWindow(foreground_window_))
168 SetForegroundWindow(foreground_window_);
169 return TRUE;
170 }
171
[email protected]fc14cef2009-01-27 22:17:29172 // Ignore the request if the browser process is already in shutdown path.
[email protected]0815b6d2009-02-11 00:39:37173 if (!g_browser_process || g_browser_process->IsShuttingDown()) {
174 LOG(WARNING) << "Not handling WM_COPYDATA as browser is shutting down";
175 return FALSE;
176 }
[email protected]fc14cef2009-01-27 22:17:29177
[email protected]fc14cef2009-01-27 22:17:29178 // We should have enough room for the shortest command (min_message_size)
[email protected]366ffe52009-04-24 22:02:23179 // and also be a multiple of wchar_t bytes. The shortest command
180 // possible is L"START\0\0" (empty current directory and command line).
[email protected]fc14cef2009-01-27 22:17:29181 static const int min_message_size = 7;
[email protected]366ffe52009-04-24 22:02:23182 if (cds->cbData < min_message_size * sizeof(wchar_t) ||
183 cds->cbData % sizeof(wchar_t) != 0) {
[email protected]fc14cef2009-01-27 22:17:29184 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << cds->cbData;
185 return TRUE;
186 }
187
188 // We split the string into 4 parts on NULLs.
[email protected]366ffe52009-04-24 22:02:23189 DCHECK(cds->lpData);
[email protected]fc14cef2009-01-27 22:17:29190 const std::wstring msg(static_cast<wchar_t*>(cds->lpData),
191 cds->cbData / sizeof(wchar_t));
192 const std::wstring::size_type first_null = msg.find_first_of(L'\0');
193 if (first_null == 0 || first_null == std::wstring::npos) {
194 // no NULL byte, don't know what to do
195 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << msg.length() <<
196 ", first null = " << first_null;
197 return TRUE;
198 }
199
200 // Decode the command, which is everything until the first NULL.
201 if (msg.substr(0, first_null) == L"START") {
202 // Another instance is starting parse the command line & do what it would
203 // have done.
204 LOG(INFO) << "Handling STARTUP request from another process";
205 const std::wstring::size_type second_null =
206 msg.find_first_of(L'\0', first_null + 1);
207 if (second_null == std::wstring::npos ||
208 first_null == msg.length() - 1 || second_null == msg.length()) {
209 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
210 "parts separated by NULLs";
211 return TRUE;
212 }
213
214 // Get current directory.
215 const std::wstring cur_dir =
216 msg.substr(first_null + 1, second_null - first_null);
217
218 const std::wstring::size_type third_null =
219 msg.find_first_of(L'\0', second_null + 1);
220 if (third_null == std::wstring::npos ||
221 third_null == msg.length()) {
222 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
223 "parts separated by NULLs";
224 }
225
226 // Get command line.
227 const std::wstring cmd_line =
228 msg.substr(second_null + 1, third_null - second_null);
229
230 CommandLine parsed_command_line(L"");
231 parsed_command_line.ParseFromString(cmd_line);
232 PrefService* prefs = g_browser_process->local_state();
233 DCHECK(prefs);
234
[email protected]f7011fcb2009-01-28 21:54:32235 FilePath user_data_dir;
[email protected]fc14cef2009-01-27 22:17:29236 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
237 ProfileManager* profile_manager = g_browser_process->profile_manager();
238 Profile* profile = profile_manager->GetDefaultProfile(user_data_dir);
239 if (!profile) {
240 // We should only be able to get here if the profile already exists and
241 // has been created.
242 NOTREACHED();
243 return TRUE;
244 }
[email protected]0303f31c2009-02-02 06:42:05245
246 // Run the browser startup sequence again, with the command line of the
247 // signalling process.
[email protected]7c27203a2009-03-10 21:55:21248 BrowserInit::ProcessCommandLine(parsed_command_line, cur_dir, false,
[email protected]0303f31c2009-02-02 06:42:05249 profile, NULL);
[email protected]fc14cef2009-01-27 22:17:29250 return TRUE;
251 }
252 return TRUE;
253}
254
[email protected]7c47ae3e2009-02-18 00:34:21255LRESULT CALLBACK ProcessSingleton::WndProc(HWND hwnd, UINT message,
[email protected]175a7a22009-05-03 15:57:53256 WPARAM wparam, LPARAM lparam) {
[email protected]fc14cef2009-01-27 22:17:29257 switch (message) {
258 case WM_COPYDATA:
259 return OnCopyData(reinterpret_cast<HWND>(wparam),
260 reinterpret_cast<COPYDATASTRUCT*>(lparam));
261 default:
262 break;
263 }
264
265 return ::DefWindowProc(hwnd, message, wparam, lparam);
266}