[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 1 | // 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] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 5 | #include "chrome/browser/process_singleton.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 6 | |
[email protected] | a92b864 | 2009-05-05 23:38:56 | [diff] [blame] | 7 | #include "app/l10n_util.h" |
[email protected] | 4a0765a | 2009-05-08 23:12:25 | [diff] [blame] | 8 | #include "app/win_util.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 9 | #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] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 13 | #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] | 74d1bb0 | 2009-03-03 00:41:23 | [diff] [blame] | 19 | #include "chrome/common/result_codes.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 20 | #include "grit/chromium_strings.h" |
| 21 | #include "grit/generated_resources.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 22 | |
| 23 | namespace { |
| 24 | |
[email protected] | f891fb3 | 2009-04-08 00:20:32 | [diff] [blame] | 25 | // Checks the visibility of the enumerated window and signals once a visible |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 26 | // window has been found. |
| 27 | BOOL 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] | f891fb3 | 2009-04-08 00:20:32 | [diff] [blame] | 36 | // Look for a Chrome instance that uses the same profile directory. |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 37 | ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 38 | : window_(NULL), locked_(false), foreground_window_(NULL) { |
[email protected] | f891fb3 | 2009-04-08 00:20:32 | [diff] [blame] | 39 | // 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] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 42 | user_data_dir.ToWStringHack().c_str()); |
[email protected] | f891fb3 | 2009-04-08 00:20:32 | [diff] [blame] | 43 | if (!remote_window_) |
| 44 | Create(); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 47 | ProcessSingleton::~ProcessSingleton() { |
[email protected] | aef15bd | 2009-04-27 20:51:51 | [diff] [blame] | 48 | if (window_) { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 49 | DestroyWindow(window_); |
[email protected] | aef15bd | 2009-04-27 20:51:51 | [diff] [blame] | 50 | UnregisterClass(chrome::kMessageWindowClass, GetModuleHandle(NULL)); |
| 51 | } |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 52 | } |
| 53 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 54 | bool ProcessSingleton::NotifyOtherProcess() { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 55 | 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] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 73 | // 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] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 79 | AllowSetForegroundWindow(process_id); |
| 80 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 81 | 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] | 8b08cbd | 2009-08-04 05:34:19 | [diff] [blame^] | 91 | kTimeoutInSeconds * 1000, |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 92 | &result)) { |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 93 | // 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] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 98 | return true; |
| 99 | } |
| 100 | |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 101 | // 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] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 107 | // 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] | cd4fd15 | 2009-02-09 19:28:41 | [diff] [blame] | 125 | base::KillProcessById(process_id, ResultCodes::HUNG, true); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 126 | remote_window_ = NULL; |
| 127 | return false; |
| 128 | } |
| 129 | |
[email protected] | f891fb3 | 2009-04-08 00:20:32 | [diff] [blame] | 130 | // 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] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 133 | void ProcessSingleton::Create() { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 134 | DCHECK(!remote_window_); |
[email protected] | f891fb3 | 2009-04-08 00:20:32 | [diff] [blame] | 135 | if (window_) |
| 136 | return; |
| 137 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 138 | HINSTANCE hinst = GetModuleHandle(NULL); |
| 139 | |
| 140 | WNDCLASSEX wc = {0}; |
| 141 | wc.cbSize = sizeof(wc); |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 142 | wc.lpfnWndProc = ProcessSingleton::WndProcStatic; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 143 | wc.hInstance = hinst; |
| 144 | wc.lpszClassName = chrome::kMessageWindowClass; |
[email protected] | aef15bd | 2009-04-27 20:51:51 | [diff] [blame] | 145 | ATOM clazz = RegisterClassEx(&wc); |
| 146 | DCHECK(clazz); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 147 | |
| 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] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 160 | LRESULT ProcessSingleton::OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds) { |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 161 | // 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] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 172 | // Ignore the request if the browser process is already in shutdown path. |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 173 | 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] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 177 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 178 | // We should have enough room for the shortest command (min_message_size) |
[email protected] | 366ffe5 | 2009-04-24 22:02:23 | [diff] [blame] | 179 | // 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] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 181 | static const int min_message_size = 7; |
[email protected] | 366ffe5 | 2009-04-24 22:02:23 | [diff] [blame] | 182 | if (cds->cbData < min_message_size * sizeof(wchar_t) || |
| 183 | cds->cbData % sizeof(wchar_t) != 0) { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 184 | 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] | 366ffe5 | 2009-04-24 22:02:23 | [diff] [blame] | 189 | DCHECK(cds->lpData); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 190 | 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] | f7011fcb | 2009-01-28 21:54:32 | [diff] [blame] | 235 | FilePath user_data_dir; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 236 | 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] | 0303f31c | 2009-02-02 06:42:05 | [diff] [blame] | 245 | |
| 246 | // Run the browser startup sequence again, with the command line of the |
| 247 | // signalling process. |
[email protected] | 7c27203a | 2009-03-10 21:55:21 | [diff] [blame] | 248 | BrowserInit::ProcessCommandLine(parsed_command_line, cur_dir, false, |
[email protected] | 0303f31c | 2009-02-02 06:42:05 | [diff] [blame] | 249 | profile, NULL); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 250 | return TRUE; |
| 251 | } |
| 252 | return TRUE; |
| 253 | } |
| 254 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 255 | LRESULT CALLBACK ProcessSingleton::WndProc(HWND hwnd, UINT message, |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 256 | WPARAM wparam, LPARAM lparam) { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 257 | 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 | } |