[email protected] | e3ce40a | 2012-01-31 03:03:03 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 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 | |
| 7 | #include "base/base_paths.h" |
| 8 | #include "base/command_line.h" |
[email protected] | f805fe8 | 2010-08-03 22:47:10 | [diff] [blame] | 9 | #include "base/file_path.h" |
[email protected] | 9e9b6e8e | 2009-12-02 08:45:01 | [diff] [blame] | 10 | #include "base/path_service.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 11 | #include "base/process_util.h" |
[email protected] | 0f26d7b | 2011-01-05 19:10:44 | [diff] [blame] | 12 | #include "base/utf_string_conversions.h" |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 13 | #include "base/win/scoped_handle.h" |
[email protected] | ecb924c | 2011-03-17 00:34:09 | [diff] [blame] | 14 | #include "base/win/wrapped_window_proc.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 15 | #include "chrome/browser/browser_process.h" |
[email protected] | 6aeac834 | 2010-10-01 20:21:18 | [diff] [blame] | 16 | #include "chrome/browser/extensions/extensions_startup.h" |
[email protected] | 8ecad5e | 2010-12-02 21:18:33 | [diff] [blame] | 17 | #include "chrome/browser/profiles/profile.h" |
| 18 | #include "chrome/browser/profiles/profile_manager.h" |
[email protected] | 80772ed | 2011-08-09 21:11:38 | [diff] [blame] | 19 | #include "chrome/browser/simple_message_box.h" |
[email protected] | f700280 | 2010-11-12 19:50:28 | [diff] [blame] | 20 | #include "chrome/browser/ui/browser_init.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 21 | #include "chrome/common/chrome_constants.h" |
| 22 | #include "chrome/common/chrome_paths.h" |
[email protected] | 6aeac834 | 2010-10-01 20:21:18 | [diff] [blame] | 23 | #include "chrome/common/chrome_switches.h" |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 24 | #include "chrome/installer/util/wmi.h" |
[email protected] | b39ef1cb | 2011-10-25 04:46:55 | [diff] [blame] | 25 | #include "content/public/common/result_codes.h" |
[email protected] | 34ac8f3 | 2009-02-22 23:03:27 | [diff] [blame] | 26 | #include "grit/chromium_strings.h" |
| 27 | #include "grit/generated_resources.h" |
[email protected] | c051a1b | 2011-01-21 23:30:17 | [diff] [blame] | 28 | #include "ui/base/l10n/l10n_util.h" |
[email protected] | e766106 | 2011-01-19 22:16:53 | [diff] [blame] | 29 | #include "ui/base/win/hwnd_util.h" |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 30 | |
| 31 | namespace { |
| 32 | |
[email protected] | f891fb3 | 2009-04-08 00:20:32 | [diff] [blame] | 33 | // Checks the visibility of the enumerated window and signals once a visible |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 34 | // window has been found. |
| 35 | BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) { |
| 36 | bool* result = reinterpret_cast<bool*>(param); |
| 37 | *result = IsWindowVisible(window) != 0; |
| 38 | // Stops enumeration if a visible window has been found. |
| 39 | return !*result; |
| 40 | } |
| 41 | |
[email protected] | e3ce40a | 2012-01-31 03:03:03 | [diff] [blame] | 42 | // This function thunks to the object's version of the windowproc, taking in |
| 43 | // consideration that there are several messages being dispatched before |
| 44 | // WM_NCCREATE which we let windows handle. |
| 45 | LRESULT CALLBACK ThunkWndProc(HWND hwnd, UINT message, |
| 46 | WPARAM wparam, LPARAM lparam) { |
| 47 | ProcessSingleton* singleton = |
| 48 | reinterpret_cast<ProcessSingleton*>(ui::GetWindowUserData(hwnd)); |
| 49 | if (message == WM_NCCREATE) { |
| 50 | CREATESTRUCT* cs = reinterpret_cast<CREATESTRUCT*>(lparam); |
| 51 | singleton = reinterpret_cast<ProcessSingleton*>(cs->lpCreateParams); |
| 52 | CHECK(singleton); |
| 53 | ui::SetWindowUserData(hwnd, singleton); |
| 54 | } else if (!singleton) { |
| 55 | return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 56 | } |
| 57 | return singleton->WndProc(hwnd, message, wparam, lparam); |
| 58 | } |
| 59 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 60 | } // namespace |
| 61 | |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 62 | // Microsoft's Softricity virtualization breaks the sandbox processes. |
| 63 | // So, if we detect the Softricity DLL we use WMI Win32_Process.Create to |
| 64 | // break out of the virtualization environment. |
| 65 | // http://code.google.com/p/chromium/issues/detail?id=43650 |
| 66 | bool ProcessSingleton::EscapeVirtualization(const FilePath& user_data_dir) { |
| 67 | if (::GetModuleHandle(L"sftldr_wow64.dll") || |
| 68 | ::GetModuleHandle(L"sftldr.dll")) { |
| 69 | int process_id; |
| 70 | if (!installer::WMIProcess::Launch(GetCommandLineW(), &process_id)) |
| 71 | return false; |
| 72 | is_virtualized_ = true; |
| 73 | // The new window was spawned from WMI, and won't be in the foreground. |
| 74 | // So, first we sleep while the new chrome.exe instance starts (because |
| 75 | // WaitForInputIdle doesn't work here). Then we poll for up to two more |
| 76 | // seconds and make the window foreground if we find it (or we give up). |
| 77 | HWND hwnd = 0; |
| 78 | ::Sleep(90); |
| 79 | for (int tries = 200; tries; --tries) { |
| 80 | hwnd = FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass, |
| 81 | user_data_dir.value().c_str()); |
| 82 | if (hwnd) { |
| 83 | ::SetForegroundWindow(hwnd); |
| 84 | break; |
| 85 | } |
| 86 | ::Sleep(10); |
| 87 | } |
| 88 | return true; |
| 89 | } |
| 90 | return false; |
| 91 | } |
| 92 | |
[email protected] | f891fb3 | 2009-04-08 00:20:32 | [diff] [blame] | 93 | // Look for a Chrome instance that uses the same profile directory. |
[email protected] | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame^] | 94 | // If there isn't one, create a message window with its title set to |
| 95 | // the profile directory path. |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 96 | ProcessSingleton::ProcessSingleton(const FilePath& user_data_dir) |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 97 | : window_(NULL), locked_(false), foreground_window_(NULL), |
| 98 | is_virtualized_(false) { |
[email protected] | bbef41f0 | 2010-03-04 16:16:19 | [diff] [blame] | 99 | remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, |
| 100 | chrome::kMessageWindowClass, |
[email protected] | 8a205c0 | 2011-02-04 20:41:33 | [diff] [blame] | 101 | user_data_dir.value().c_str()); |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 102 | if (!remote_window_ && !EscapeVirtualization(user_data_dir)) { |
[email protected] | bbef41f0 | 2010-03-04 16:16:19 | [diff] [blame] | 103 | // Make sure we will be the one and only process creating the window. |
| 104 | // We use a named Mutex since we are protecting against multi-process |
| 105 | // access. As documented, it's clearer to NOT request ownership on creation |
| 106 | // since it isn't guaranteed we will get it. It is better to create it |
| 107 | // without ownership and explicitly get the ownership afterward. |
[email protected] | e132804b | 2010-12-22 12:48:25 | [diff] [blame] | 108 | std::wstring mutex_name(L"Local\\ChromeProcessSingletonStartup!"); |
[email protected] | b90d7e80 | 2011-01-09 16:32:20 | [diff] [blame] | 109 | base::win::ScopedHandle only_me( |
| 110 | CreateMutex(NULL, FALSE, mutex_name.c_str())); |
[email protected] | bbef41f0 | 2010-03-04 16:16:19 | [diff] [blame] | 111 | DCHECK(only_me.Get() != NULL) << "GetLastError = " << GetLastError(); |
| 112 | |
| 113 | // This is how we acquire the mutex (as opposed to the initial ownership). |
| 114 | DWORD result = WaitForSingleObject(only_me, INFINITE); |
| 115 | DCHECK(result == WAIT_OBJECT_0) << "Result = " << result << |
| 116 | "GetLastError = " << GetLastError(); |
| 117 | |
| 118 | // We now own the mutex so we are the only process that can create the |
| 119 | // window at this time, but we must still check if someone created it |
| 120 | // between the time where we looked for it above and the time the mutex |
| 121 | // was given to us. |
| 122 | remote_window_ = FindWindowEx(HWND_MESSAGE, NULL, |
| 123 | chrome::kMessageWindowClass, |
[email protected] | 8a205c0 | 2011-02-04 20:41:33 | [diff] [blame] | 124 | user_data_dir.value().c_str()); |
[email protected] | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame^] | 125 | if (!remote_window_) { |
| 126 | HINSTANCE hinst = base::GetModuleFromAddress(&ThunkWndProc); |
| 127 | |
| 128 | WNDCLASSEX wc = {0}; |
| 129 | wc.cbSize = sizeof(wc); |
| 130 | wc.lpfnWndProc = base::win::WrappedWindowProc<ThunkWndProc>; |
| 131 | wc.hInstance = hinst; |
| 132 | wc.lpszClassName = chrome::kMessageWindowClass; |
| 133 | ATOM clazz = ::RegisterClassEx(&wc); |
| 134 | DCHECK(clazz); |
| 135 | |
| 136 | FilePath user_data_dir; |
| 137 | PathService::Get(chrome::DIR_USER_DATA, &user_data_dir); |
| 138 | |
| 139 | // Set the window's title to the path of our user data directory so other |
| 140 | // Chrome instances can decide if they should forward to us or not. |
| 141 | window_ = ::CreateWindow(MAKEINTATOM(clazz), |
| 142 | user_data_dir.value().c_str(), |
| 143 | 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hinst, this); |
| 144 | CHECK(window_); |
| 145 | } |
[email protected] | bbef41f0 | 2010-03-04 16:16:19 | [diff] [blame] | 146 | BOOL success = ReleaseMutex(only_me); |
| 147 | DCHECK(success) << "GetLastError = " << GetLastError(); |
| 148 | } |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 149 | } |
| 150 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 151 | ProcessSingleton::~ProcessSingleton() { |
[email protected] | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame^] | 152 | Cleanup(); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 153 | } |
| 154 | |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 155 | ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() { |
[email protected] | 0a19455 | 2011-09-14 17:53:35 | [diff] [blame] | 156 | if (is_virtualized_) |
| 157 | return PROCESS_NOTIFIED; // We already spawned the process in this case. |
| 158 | else if (!remote_window_) |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 159 | return PROCESS_NONE; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 160 | |
| 161 | // Found another window, send our command line to it |
| 162 | // format is "START\0<<<current directory>>>\0<<<commandline>>>". |
| 163 | std::wstring to_send(L"START\0", 6); // want the NULL in the string. |
[email protected] | b969648 | 2010-11-30 23:56:18 | [diff] [blame] | 164 | FilePath cur_dir; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 165 | if (!PathService::Get(base::DIR_CURRENT, &cur_dir)) |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 166 | return PROCESS_NONE; |
[email protected] | b969648 | 2010-11-30 23:56:18 | [diff] [blame] | 167 | to_send.append(cur_dir.value()); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 168 | to_send.append(L"\0", 1); // Null separator. |
| 169 | to_send.append(GetCommandLineW()); |
| 170 | to_send.append(L"\0", 1); // Null separator. |
| 171 | |
| 172 | // Allow the current running browser window making itself the foreground |
| 173 | // window (otherwise it will just flash in the taskbar). |
| 174 | DWORD process_id = 0; |
| 175 | DWORD thread_id = GetWindowThreadProcessId(remote_window_, &process_id); |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 176 | // It is possible that the process owning this window may have died by now. |
| 177 | if (!thread_id || !process_id) { |
| 178 | remote_window_ = NULL; |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 179 | return PROCESS_NONE; |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 182 | AllowSetForegroundWindow(process_id); |
| 183 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 184 | COPYDATASTRUCT cds; |
| 185 | cds.dwData = 0; |
| 186 | cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t)); |
| 187 | cds.lpData = const_cast<wchar_t*>(to_send.c_str()); |
| 188 | DWORD_PTR result = 0; |
| 189 | if (SendMessageTimeout(remote_window_, |
| 190 | WM_COPYDATA, |
| 191 | NULL, |
| 192 | reinterpret_cast<LPARAM>(&cds), |
| 193 | SMTO_ABORTIFHUNG, |
[email protected] | 8b08cbd | 2009-08-04 05:34:19 | [diff] [blame] | 194 | kTimeoutInSeconds * 1000, |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 195 | &result)) { |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 196 | // It is possible that the process owning this window may have died by now. |
| 197 | if (!result) { |
| 198 | remote_window_ = NULL; |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 199 | return PROCESS_NONE; |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 200 | } |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 201 | return PROCESS_NOTIFIED; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 204 | // It is possible that the process owning this window may have died by now. |
| 205 | if (!IsWindow(remote_window_)) { |
| 206 | remote_window_ = NULL; |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 207 | return PROCESS_NONE; |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 208 | } |
| 209 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 210 | // The window is hung. Scan for every window to find a visible one. |
| 211 | bool visible_window = false; |
| 212 | EnumThreadWindows(thread_id, |
| 213 | &BrowserWindowEnumeration, |
| 214 | reinterpret_cast<LPARAM>(&visible_window)); |
| 215 | |
| 216 | // If there is a visible browser window, ask the user before killing it. |
| 217 | if (visible_window) { |
[email protected] | 80772ed | 2011-08-09 21:11:38 | [diff] [blame] | 218 | string16 text = l10n_util::GetStringUTF16(IDS_BROWSER_HUNGBROWSER_MESSAGE); |
| 219 | string16 caption = l10n_util::GetStringUTF16(IDS_PRODUCT_NAME); |
| 220 | if (!browser::ShowYesNoBox(NULL, caption, text)) { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 221 | // The user denied. Quit silently. |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 222 | return PROCESS_NOTIFIED; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | // Time to take action. Kill the browser process. |
[email protected] | 1fcfb20 | 2011-07-19 19:53:14 | [diff] [blame] | 227 | base::KillProcessById(process_id, content::RESULT_CODE_HUNG, true); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 228 | remote_window_ = NULL; |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 229 | return PROCESS_NONE; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 230 | } |
| 231 | |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 232 | ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessOrCreate() { |
| 233 | NotifyResult result = NotifyOtherProcess(); |
| 234 | if (result != PROCESS_NONE) |
| 235 | return result; |
[email protected] | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame^] | 236 | return window_ ? PROCESS_NONE : PROFILE_IN_USE; |
[email protected] | 4a44bc3 | 2010-05-28 22:22:44 | [diff] [blame] | 237 | } |
| 238 | |
[email protected] | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame^] | 239 | // On Windows, there is no need to call Create() since the message |
| 240 | // window is created in the constructor but to avoid having more |
| 241 | // platform specific code in browser_main.cc we tolerate calls to |
| 242 | // Create(), which will do nothing. |
[email protected] | 4dd4224 | 2010-04-07 02:21:15 | [diff] [blame] | 243 | bool ProcessSingleton::Create() { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 244 | DCHECK(!remote_window_); |
[email protected] | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame^] | 245 | return window_ != NULL; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 246 | } |
| 247 | |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 248 | void ProcessSingleton::Cleanup() { |
[email protected] | 9a18283 | 2012-02-10 18:45:58 | [diff] [blame^] | 249 | // Window classes registered by DLLs are not cleaned up automatically on |
| 250 | // process exit, so we must unregister at the earliest chance possible. |
| 251 | // During the fast shutdown sequence, ProcessSingleton::Cleanup() is |
| 252 | // called if our process was the first to start. Therefore we try cleaning |
| 253 | // up here, and again in the destructor if needed to catch as many cases |
| 254 | // as possible. |
| 255 | if (window_) { |
| 256 | ::DestroyWindow(window_); |
| 257 | ::UnregisterClass(chrome::kMessageWindowClass, |
| 258 | base::GetModuleFromAddress(&ThunkWndProc)); |
| 259 | window_ = NULL; |
| 260 | } |
[email protected] | 9f20a6d0 | 2009-08-21 01:18:37 | [diff] [blame] | 261 | } |
| 262 | |
[email protected] | 7c47ae3e | 2009-02-18 00:34:21 | [diff] [blame] | 263 | LRESULT ProcessSingleton::OnCopyData(HWND hwnd, const COPYDATASTRUCT* cds) { |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 264 | // If locked, it means we are not ready to process this message because |
[email protected] | afd20c02 | 2010-06-10 00:48:20 | [diff] [blame] | 265 | // we are probably in a first run critical phase. |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 266 | if (locked_) { |
[email protected] | 95259c6 | 2011-10-25 23:23:53 | [diff] [blame] | 267 | #if defined(USE_AURA) |
| 268 | NOTIMPLEMENTED(); |
| 269 | #else |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 270 | // Attempt to place ourselves in the foreground / flash the task bar. |
| 271 | if (IsWindow(foreground_window_)) |
| 272 | SetForegroundWindow(foreground_window_); |
[email protected] | 95259c6 | 2011-10-25 23:23:53 | [diff] [blame] | 273 | #endif |
[email protected] | 175a7a2 | 2009-05-03 15:57:53 | [diff] [blame] | 274 | return TRUE; |
| 275 | } |
| 276 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 277 | // Ignore the request if the browser process is already in shutdown path. |
[email protected] | 0815b6d | 2009-02-11 00:39:37 | [diff] [blame] | 278 | if (!g_browser_process || g_browser_process->IsShuttingDown()) { |
| 279 | LOG(WARNING) << "Not handling WM_COPYDATA as browser is shutting down"; |
| 280 | return FALSE; |
| 281 | } |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 282 | |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 283 | // We should have enough room for the shortest command (min_message_size) |
[email protected] | 366ffe5 | 2009-04-24 22:02:23 | [diff] [blame] | 284 | // and also be a multiple of wchar_t bytes. The shortest command |
| 285 | // possible is L"START\0\0" (empty current directory and command line). |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 286 | static const int min_message_size = 7; |
[email protected] | 366ffe5 | 2009-04-24 22:02:23 | [diff] [blame] | 287 | if (cds->cbData < min_message_size * sizeof(wchar_t) || |
| 288 | cds->cbData % sizeof(wchar_t) != 0) { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 289 | LOG(WARNING) << "Invalid WM_COPYDATA, length = " << cds->cbData; |
| 290 | return TRUE; |
| 291 | } |
| 292 | |
| 293 | // We split the string into 4 parts on NULLs. |
[email protected] | 366ffe5 | 2009-04-24 22:02:23 | [diff] [blame] | 294 | DCHECK(cds->lpData); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 295 | const std::wstring msg(static_cast<wchar_t*>(cds->lpData), |
| 296 | cds->cbData / sizeof(wchar_t)); |
| 297 | const std::wstring::size_type first_null = msg.find_first_of(L'\0'); |
| 298 | if (first_null == 0 || first_null == std::wstring::npos) { |
| 299 | // no NULL byte, don't know what to do |
| 300 | LOG(WARNING) << "Invalid WM_COPYDATA, length = " << msg.length() << |
| 301 | ", first null = " << first_null; |
| 302 | return TRUE; |
| 303 | } |
| 304 | |
| 305 | // Decode the command, which is everything until the first NULL. |
| 306 | if (msg.substr(0, first_null) == L"START") { |
| 307 | // Another instance is starting parse the command line & do what it would |
| 308 | // have done. |
[email protected] | 8e96e50 | 2010-10-21 20:57:12 | [diff] [blame] | 309 | VLOG(1) << "Handling STARTUP request from another process"; |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 310 | const std::wstring::size_type second_null = |
| 311 | msg.find_first_of(L'\0', first_null + 1); |
| 312 | if (second_null == std::wstring::npos || |
| 313 | first_null == msg.length() - 1 || second_null == msg.length()) { |
| 314 | LOG(WARNING) << "Invalid format for start command, we need a string in 4 " |
| 315 | "parts separated by NULLs"; |
| 316 | return TRUE; |
| 317 | } |
| 318 | |
| 319 | // Get current directory. |
[email protected] | f805fe8 | 2010-08-03 22:47:10 | [diff] [blame] | 320 | const FilePath cur_dir(msg.substr(first_null + 1, |
| 321 | second_null - first_null)); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 322 | |
| 323 | const std::wstring::size_type third_null = |
| 324 | msg.find_first_of(L'\0', second_null + 1); |
| 325 | if (third_null == std::wstring::npos || |
| 326 | third_null == msg.length()) { |
| 327 | LOG(WARNING) << "Invalid format for start command, we need a string in 4 " |
| 328 | "parts separated by NULLs"; |
| 329 | } |
| 330 | |
| 331 | // Get command line. |
| 332 | const std::wstring cmd_line = |
| 333 | msg.substr(second_null + 1, third_null - second_null); |
| 334 | |
[email protected] | 51343d5a | 2009-10-26 22:39:33 | [diff] [blame] | 335 | CommandLine parsed_command_line = CommandLine::FromString(cmd_line); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 336 | PrefService* prefs = g_browser_process->local_state(); |
| 337 | DCHECK(prefs); |
| 338 | |
[email protected] | 6aeac834 | 2010-10-01 20:21:18 | [diff] [blame] | 339 | // Handle the --uninstall-extension startup action. This needs to done here |
| 340 | // in the process that is running with the target profile, otherwise the |
| 341 | // uninstall will fail to unload and remove all components. |
| 342 | if (parsed_command_line.HasSwitch(switches::kUninstallExtension)) { |
[email protected] | 37b89ca | 2011-12-08 02:21:14 | [diff] [blame] | 343 | // The uninstall extension switch can't be combined with the profile |
| 344 | // directory switch. |
| 345 | DCHECK(!parsed_command_line.HasSwitch(switches::kProfileDirectory)); |
| 346 | |
| 347 | Profile* profile = ProfileManager::GetLastUsedProfile(); |
| 348 | if (!profile) { |
| 349 | // We should only be able to get here if the profile already exists and |
| 350 | // has been created. |
| 351 | NOTREACHED(); |
| 352 | return TRUE; |
| 353 | } |
| 354 | |
[email protected] | ccd875e7 | 2010-12-13 23:49:02 | [diff] [blame] | 355 | ExtensionsStartupUtil ext_startup_util; |
| 356 | ext_startup_util.UninstallExtension(parsed_command_line, profile); |
[email protected] | 6aeac834 | 2010-10-01 20:21:18 | [diff] [blame] | 357 | return TRUE; |
| 358 | } |
| 359 | |
[email protected] | 0303f31c | 2009-02-02 06:42:05 | [diff] [blame] | 360 | // Run the browser startup sequence again, with the command line of the |
| 361 | // signalling process. |
[email protected] | 37b89ca | 2011-12-08 02:21:14 | [diff] [blame] | 362 | BrowserInit::ProcessCommandLineAlreadyRunning(parsed_command_line, cur_dir); |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 363 | return TRUE; |
| 364 | } |
| 365 | return TRUE; |
| 366 | } |
| 367 | |
[email protected] | e3ce40a | 2012-01-31 03:03:03 | [diff] [blame] | 368 | LRESULT ProcessSingleton::WndProc(HWND hwnd, UINT message, |
| 369 | WPARAM wparam, LPARAM lparam) { |
[email protected] | fc14cef | 2009-01-27 22:17:29 | [diff] [blame] | 370 | switch (message) { |
| 371 | case WM_COPYDATA: |
| 372 | return OnCopyData(reinterpret_cast<HWND>(wparam), |
| 373 | reinterpret_cast<COPYDATASTRUCT*>(lparam)); |
| 374 | default: |
| 375 | break; |
| 376 | } |
| 377 | |
| 378 | return ::DefWindowProc(hwnd, message, wparam, lparam); |
| 379 | } |