blob: 94647c7d89bc2636b180db67d220e8294264e0b9 [file] [log] [blame]
[email protected]e3ce40a2012-01-31 03:03:031// Copyright (c) 2012 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]b1d7844b2012-08-22 22:37:477#include <shellapi.h>
8
[email protected]fc14cef2009-01-27 22:17:299#include "base/base_paths.h"
[email protected]4cf04bb2013-07-11 09:22:3810#include "base/bind.h"
[email protected]fc14cef2009-01-27 22:17:2911#include "base/command_line.h"
[email protected]57999812013-02-24 05:40:5212#include "base/files/file_path.h"
[email protected]9e9b6e8e2009-12-02 08:45:0113#include "base/path_service.h"
[email protected]84c584462013-05-13 05:05:0014#include "base/process_info.h"
[email protected]fc14cef2009-01-27 22:17:2915#include "base/process_util.h"
[email protected]84c584462013-05-13 05:05:0016#include "base/strings/string_number_conversions.h"
[email protected]76fb05c2013-06-11 04:38:0517#include "base/strings/stringprintf.h"
[email protected]e309f312013-06-07 21:50:0818#include "base/strings/utf_string_conversions.h"
[email protected]84813472013-06-28 00:25:1919#include "base/time/time.h"
[email protected]f394bc62012-08-22 21:42:2920#include "base/win/metro.h"
[email protected]b3d791c92012-10-05 19:01:2421#include "base/win/registry.h"
[email protected]b90d7e802011-01-09 16:32:2022#include "base/win/scoped_handle.h"
[email protected]b3d791c92012-10-05 19:01:2423#include "base/win/win_util.h"
[email protected]6927f2e02012-09-18 00:13:4924#include "base/win/windows_version.h"
[email protected]e9613b52012-11-27 22:35:1325#include "chrome/browser/browser_process.h"
[email protected]f2068f92013-05-20 05:30:1726#include "chrome/browser/browser_process_platform_part.h"
[email protected]10118162013-06-05 00:24:4527#include "chrome/browser/chrome_process_finder_win.h"
28#include "chrome/browser/metro_utils/metro_chrome_win.h"
[email protected]245900672012-10-13 02:27:1129#include "chrome/browser/shell_integration.h"
[email protected]b50892c5f2012-05-13 07:34:1430#include "chrome/browser/ui/simple_message_box.h"
[email protected]fc14cef2009-01-27 22:17:2931#include "chrome/common/chrome_constants.h"
[email protected]b3d791c92012-10-05 19:01:2432#include "chrome/common/chrome_paths.h"
33#include "chrome/common/chrome_paths_internal.h"
[email protected]cfa118a2012-10-10 22:27:5334#include "chrome/common/chrome_switches.h"
[email protected]0a194552011-09-14 17:53:3535#include "chrome/installer/util/wmi.h"
[email protected]b39ef1cb2011-10-25 04:46:5536#include "content/public/common/result_codes.h"
[email protected]34ac8f32009-02-22 23:03:2737#include "grit/chromium_strings.h"
38#include "grit/generated_resources.h"
[email protected]f394bc62012-08-22 21:42:2939#include "net/base/escape.h"
[email protected]c051a1b2011-01-21 23:30:1740#include "ui/base/l10n/l10n_util.h"
[email protected]e7661062011-01-19 22:16:5341#include "ui/base/win/hwnd_util.h"
[email protected]fc14cef2009-01-27 22:17:2942
43namespace {
44
[email protected]2b7ff5b92012-07-17 22:45:1845const char kLockfile[] = "lockfile";
46
[email protected]14649ecd2012-12-05 01:00:2447const int kMetroChromeActivationTimeoutMs = 3000;
48
49// A helper class that acquires the given |mutex| while the AutoLockMutex is in
50// scope.
51class AutoLockMutex {
52 public:
53 explicit AutoLockMutex(HANDLE mutex) : mutex_(mutex) {
[email protected]b9057302013-03-28 12:40:3854 DWORD result = ::WaitForSingleObject(mutex_, INFINITE);
[email protected]14649ecd2012-12-05 01:00:2455 DPCHECK(result == WAIT_OBJECT_0) << "Result = " << result;
56 }
57
58 ~AutoLockMutex() {
[email protected]b9057302013-03-28 12:40:3859 BOOL released = ::ReleaseMutex(mutex_);
[email protected]14649ecd2012-12-05 01:00:2460 DPCHECK(released);
61 }
62
63 private:
64 HANDLE mutex_;
65 DISALLOW_COPY_AND_ASSIGN(AutoLockMutex);
66};
67
68// A helper class that releases the given |mutex| while the AutoUnlockMutex is
69// in scope and immediately re-acquires it when going out of scope.
70class AutoUnlockMutex {
71 public:
72 explicit AutoUnlockMutex(HANDLE mutex) : mutex_(mutex) {
[email protected]b9057302013-03-28 12:40:3873 BOOL released = ::ReleaseMutex(mutex_);
[email protected]14649ecd2012-12-05 01:00:2474 DPCHECK(released);
75 }
76
77 ~AutoUnlockMutex() {
[email protected]b9057302013-03-28 12:40:3878 DWORD result = ::WaitForSingleObject(mutex_, INFINITE);
[email protected]14649ecd2012-12-05 01:00:2479 DPCHECK(result == WAIT_OBJECT_0) << "Result = " << result;
80 }
81
82 private:
83 HANDLE mutex_;
84 DISALLOW_COPY_AND_ASSIGN(AutoUnlockMutex);
85};
[email protected]b3d791c92012-10-05 19:01:2486
[email protected]f891fb32009-04-08 00:20:3287// Checks the visibility of the enumerated window and signals once a visible
[email protected]fc14cef2009-01-27 22:17:2988// window has been found.
89BOOL CALLBACK BrowserWindowEnumeration(HWND window, LPARAM param) {
90 bool* result = reinterpret_cast<bool*>(param);
[email protected]b9057302013-03-28 12:40:3891 *result = ::IsWindowVisible(window) != 0;
[email protected]fc14cef2009-01-27 22:17:2992 // Stops enumeration if a visible window has been found.
93 return !*result;
94}
95
[email protected]edf04b512012-02-23 09:47:4396bool ParseCommandLine(const COPYDATASTRUCT* cds,
97 CommandLine* parsed_command_line,
[email protected]650b2d52013-02-10 03:41:4598 base::FilePath* current_directory) {
[email protected]edf04b512012-02-23 09:47:4399 // We should have enough room for the shortest command (min_message_size)
100 // and also be a multiple of wchar_t bytes. The shortest command
101 // possible is L"START\0\0" (empty current directory and command line).
102 static const int min_message_size = 7;
103 if (cds->cbData < min_message_size * sizeof(wchar_t) ||
104 cds->cbData % sizeof(wchar_t) != 0) {
105 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << cds->cbData;
106 return false;
107 }
108
109 // We split the string into 4 parts on NULLs.
110 DCHECK(cds->lpData);
111 const std::wstring msg(static_cast<wchar_t*>(cds->lpData),
112 cds->cbData / sizeof(wchar_t));
113 const std::wstring::size_type first_null = msg.find_first_of(L'\0');
114 if (first_null == 0 || first_null == std::wstring::npos) {
115 // no NULL byte, don't know what to do
116 LOG(WARNING) << "Invalid WM_COPYDATA, length = " << msg.length() <<
117 ", first null = " << first_null;
118 return false;
119 }
120
121 // Decode the command, which is everything until the first NULL.
122 if (msg.substr(0, first_null) == L"START") {
123 // Another instance is starting parse the command line & do what it would
124 // have done.
125 VLOG(1) << "Handling STARTUP request from another process";
126 const std::wstring::size_type second_null =
127 msg.find_first_of(L'\0', first_null + 1);
128 if (second_null == std::wstring::npos ||
129 first_null == msg.length() - 1 || second_null == msg.length()) {
130 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
131 "parts separated by NULLs";
132 return false;
133 }
134
135 // Get current directory.
[email protected]650b2d52013-02-10 03:41:45136 *current_directory = base::FilePath(msg.substr(first_null + 1,
[email protected]b9057302013-03-28 12:40:38137 second_null - first_null));
[email protected]edf04b512012-02-23 09:47:43138
139 const std::wstring::size_type third_null =
140 msg.find_first_of(L'\0', second_null + 1);
141 if (third_null == std::wstring::npos ||
142 third_null == msg.length()) {
143 LOG(WARNING) << "Invalid format for start command, we need a string in 4 "
144 "parts separated by NULLs";
145 }
146
147 // Get command line.
148 const std::wstring cmd_line =
149 msg.substr(second_null + 1, third_null - second_null);
150 *parsed_command_line = CommandLine::FromString(cmd_line);
151 return true;
152 }
153 return false;
154}
155
[email protected]4cf04bb2013-07-11 09:22:38156bool ProcessLaunchNotification(
157 const ProcessSingleton::NotificationCallback& notification_callback,
158 UINT message,
159 WPARAM wparam,
160 LPARAM lparam,
161 LRESULT* result) {
162 if (message != WM_COPYDATA)
163 return false;
164
165 // Handle the WM_COPYDATA message from another process.
166 HWND hwnd = reinterpret_cast<HWND>(wparam);
167 const COPYDATASTRUCT* cds = reinterpret_cast<COPYDATASTRUCT*>(lparam);
168
169 CommandLine parsed_command_line(CommandLine::NO_PROGRAM);
170 base::FilePath current_directory;
171 if (!ParseCommandLine(cds, &parsed_command_line, &current_directory)) {
172 *result = TRUE;
173 return true;
174 }
175
176 *result = notification_callback.Run(parsed_command_line, current_directory) ?
177 TRUE : FALSE;
178 return true;
179}
180
[email protected]b3d791c92012-10-05 19:01:24181// Returns true if Chrome needs to be relaunched into Windows 8 immersive mode.
182// Following conditions apply:-
183// 1. Windows 8 or greater.
184// 2. Not in Windows 8 immersive mode.
[email protected]245900672012-10-13 02:27:11185// 3. Chrome is default browser.
186// 4. Process integrity level is not high.
187// 5. The profile data directory is the default directory.
188// 6. Last used mode was immersive/machine is a tablet.
[email protected]b3d791c92012-10-05 19:01:24189// TODO(ananta)
190// Move this function to a common place as the Windows 8 delegate_execute
191// handler can possibly use this.
[email protected]650b2d52013-02-10 03:41:45192bool ShouldLaunchInWindows8ImmersiveMode(const base::FilePath& user_data_dir) {
[email protected]e1c9a0e62013-06-05 06:02:23193#if defined(USE_AURA)
194 // Returning false from this function doesn't mean we don't launch immersive
195 // mode in Aura. This function is specifically called in case when we need
196 // to relaunch desktop launched chrome into immersive mode through 'relaunch'
197 // menu. In case of Aura, we will use delegate_execute to do the relaunch.
198 return false;
199#endif
200
[email protected]b3d791c92012-10-05 19:01:24201 if (base::win::GetVersion() < base::win::VERSION_WIN8)
202 return false;
203
204 if (base::win::IsProcessImmersive(base::GetCurrentProcessHandle()))
205 return false;
206
[email protected]89886652012-12-11 18:09:07207 if (ShellIntegration::GetDefaultBrowser() != ShellIntegration::IS_DEFAULT)
[email protected]245900672012-10-13 02:27:11208 return false;
209
[email protected]b3d791c92012-10-05 19:01:24210 base::IntegrityLevel integrity_level = base::INTEGRITY_UNKNOWN;
211 base::GetProcessIntegrityLevel(base::GetCurrentProcessHandle(),
212 &integrity_level);
213 if (integrity_level == base::HIGH_INTEGRITY)
214 return false;
215
[email protected]650b2d52013-02-10 03:41:45216 base::FilePath default_user_data_dir;
[email protected]b3d791c92012-10-05 19:01:24217 if (!chrome::GetDefaultUserDataDirectory(&default_user_data_dir))
218 return false;
219
220 if (default_user_data_dir != user_data_dir)
221 return false;
222
[email protected]e212a3062012-11-14 15:59:07223 base::win::RegKey reg_key;
[email protected]7e416002012-10-13 05:19:21224 DWORD reg_value = 0;
[email protected]e212a3062012-11-14 15:59:07225 if (reg_key.Create(HKEY_CURRENT_USER, chrome::kMetroRegistryPath,
226 KEY_READ) == ERROR_SUCCESS &&
[email protected]7e416002012-10-13 05:19:21227 reg_key.ReadValueDW(chrome::kLaunchModeValue,
228 &reg_value) == ERROR_SUCCESS) {
[email protected]e212a3062012-11-14 15:59:07229 return reg_value == 1;
[email protected]b3d791c92012-10-05 19:01:24230 }
[email protected]bb481b102013-04-24 21:56:12231 return base::win::IsTouchEnabledDevice();
[email protected]b3d791c92012-10-05 19:01:24232}
233
[email protected]fc14cef2009-01-27 22:17:29234} // namespace
235
[email protected]0a194552011-09-14 17:53:35236// Microsoft's Softricity virtualization breaks the sandbox processes.
237// So, if we detect the Softricity DLL we use WMI Win32_Process.Create to
238// break out of the virtualization environment.
239// http://code.google.com/p/chromium/issues/detail?id=43650
[email protected]650b2d52013-02-10 03:41:45240bool ProcessSingleton::EscapeVirtualization(
241 const base::FilePath& user_data_dir) {
[email protected]0a194552011-09-14 17:53:35242 if (::GetModuleHandle(L"sftldr_wow64.dll") ||
243 ::GetModuleHandle(L"sftldr.dll")) {
244 int process_id;
[email protected]b9057302013-03-28 12:40:38245 if (!installer::WMIProcess::Launch(::GetCommandLineW(), &process_id))
[email protected]0a194552011-09-14 17:53:35246 return false;
247 is_virtualized_ = true;
248 // The new window was spawned from WMI, and won't be in the foreground.
249 // So, first we sleep while the new chrome.exe instance starts (because
250 // WaitForInputIdle doesn't work here). Then we poll for up to two more
251 // seconds and make the window foreground if we find it (or we give up).
252 HWND hwnd = 0;
253 ::Sleep(90);
254 for (int tries = 200; tries; --tries) {
[email protected]10118162013-06-05 00:24:45255 hwnd = chrome::FindRunningChromeWindow(user_data_dir);
[email protected]0a194552011-09-14 17:53:35256 if (hwnd) {
257 ::SetForegroundWindow(hwnd);
258 break;
259 }
260 ::Sleep(10);
261 }
262 return true;
263 }
264 return false;
265}
266
[email protected]dd85d452013-03-28 12:39:59267ProcessSingleton::ProcessSingleton(
268 const base::FilePath& user_data_dir,
269 const NotificationCallback& notification_callback)
[email protected]4cf04bb2013-07-11 09:22:38270 : notification_callback_(notification_callback),
[email protected]14649ecd2012-12-05 01:00:24271 is_virtualized_(false), lock_file_(INVALID_HANDLE_VALUE),
272 user_data_dir_(user_data_dir) {
[email protected]fc14cef2009-01-27 22:17:29273}
274
[email protected]7c47ae3e2009-02-18 00:34:21275ProcessSingleton::~ProcessSingleton() {
[email protected]2b7ff5b92012-07-17 22:45:18276 if (lock_file_ != INVALID_HANDLE_VALUE)
[email protected]b9057302013-03-28 12:40:38277 ::CloseHandle(lock_file_);
[email protected]fc14cef2009-01-27 22:17:29278}
279
[email protected]14649ecd2012-12-05 01:00:24280// Code roughly based on Mozilla.
[email protected]9f20a6d02009-08-21 01:18:37281ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcess() {
[email protected]0a194552011-09-14 17:53:35282 if (is_virtualized_)
283 return PROCESS_NOTIFIED; // We already spawned the process in this case.
[email protected]e9613b52012-11-27 22:35:13284 if (lock_file_ == INVALID_HANDLE_VALUE && !remote_window_) {
[email protected]2b7ff5b92012-07-17 22:45:18285 return LOCK_ERROR;
[email protected]e9613b52012-11-27 22:35:13286 } else if (!remote_window_) {
[email protected]9f20a6d02009-08-21 01:18:37287 return PROCESS_NONE;
[email protected]e9613b52012-11-27 22:35:13288 }
[email protected]fc14cef2009-01-27 22:17:29289
[email protected]9c34aa242013-07-03 08:16:42290 switch (chrome::AttemptToNotifyRunningChrome(remote_window_, false)) {
[email protected]10118162013-06-05 00:24:45291 case chrome::NOTIFY_SUCCESS:
292 return PROCESS_NOTIFIED;
293 case chrome::NOTIFY_FAILED:
[email protected]0815b6d2009-02-11 00:39:37294 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37295 return PROCESS_NONE;
[email protected]10118162013-06-05 00:24:45296 case chrome::NOTIFY_WINDOW_HUNG:
297 remote_window_ = NULL;
298 break;
[email protected]fc14cef2009-01-27 22:17:29299 }
300
[email protected]10118162013-06-05 00:24:45301 DWORD process_id = 0;
302 DWORD thread_id = ::GetWindowThreadProcessId(remote_window_, &process_id);
303 if (!thread_id || !process_id) {
[email protected]0815b6d2009-02-11 00:39:37304 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37305 return PROCESS_NONE;
[email protected]0815b6d2009-02-11 00:39:37306 }
307
[email protected]fc14cef2009-01-27 22:17:29308 // The window is hung. Scan for every window to find a visible one.
309 bool visible_window = false;
[email protected]b9057302013-03-28 12:40:38310 ::EnumThreadWindows(thread_id,
311 &BrowserWindowEnumeration,
312 reinterpret_cast<LPARAM>(&visible_window));
[email protected]fc14cef2009-01-27 22:17:29313
314 // If there is a visible browser window, ask the user before killing it.
[email protected]10118162013-06-05 00:24:45315 if (visible_window &&
316 chrome::ShowMessageBox(
317 NULL,
318 l10n_util::GetStringUTF16(IDS_PRODUCT_NAME),
319 l10n_util::GetStringUTF16(IDS_BROWSER_HUNGBROWSER_MESSAGE),
320 chrome::MESSAGE_BOX_TYPE_QUESTION) == chrome::MESSAGE_BOX_RESULT_NO) {
[email protected]5da155e2012-05-26 16:31:16321 // The user denied. Quit silently.
322 return PROCESS_NOTIFIED;
[email protected]fc14cef2009-01-27 22:17:29323 }
324
325 // Time to take action. Kill the browser process.
[email protected]1fcfb202011-07-19 19:53:14326 base::KillProcessById(process_id, content::RESULT_CODE_HUNG, true);
[email protected]fc14cef2009-01-27 22:17:29327 remote_window_ = NULL;
[email protected]9f20a6d02009-08-21 01:18:37328 return PROCESS_NONE;
[email protected]fc14cef2009-01-27 22:17:29329}
330
[email protected]10118162013-06-05 00:24:45331ProcessSingleton::NotifyResult
332ProcessSingleton::NotifyOtherProcessOrCreate() {
[email protected]14649ecd2012-12-05 01:00:24333 ProcessSingleton::NotifyResult result = PROCESS_NONE;
[email protected]dd85d452013-03-28 12:39:59334 if (!Create()) {
[email protected]14649ecd2012-12-05 01:00:24335 result = NotifyOtherProcess();
336 if (result == PROCESS_NONE)
337 result = PROFILE_IN_USE;
[email protected]351f7d42012-12-07 22:42:02338 } else {
[email protected]99106ba2013-05-17 23:36:35339 g_browser_process->platform_part()->PlatformSpecificCommandLineProcessing(
[email protected]351f7d42012-12-07 22:42:02340 *CommandLine::ForCurrentProcess());
[email protected]14649ecd2012-12-05 01:00:24341 }
342 return result;
[email protected]4a44bc32010-05-28 22:22:44343}
344
[email protected]14649ecd2012-12-05 01:00:24345// Look for a Chrome instance that uses the same profile directory. If there
346// isn't one, create a message window with its title set to the profile
347// directory path.
[email protected]dd85d452013-03-28 12:39:59348bool ProcessSingleton::Create() {
[email protected]14649ecd2012-12-05 01:00:24349 static const wchar_t kMutexName[] = L"Local\\ChromeProcessSingletonStartup!";
350 static const wchar_t kMetroActivationEventName[] =
351 L"Local\\ChromeProcessSingletonStartupMetroActivation!";
352
[email protected]10118162013-06-05 00:24:45353 remote_window_ = chrome::FindRunningChromeWindow(user_data_dir_);
[email protected]14649ecd2012-12-05 01:00:24354 if (!remote_window_ && !EscapeVirtualization(user_data_dir_)) {
355 // Make sure we will be the one and only process creating the window.
356 // We use a named Mutex since we are protecting against multi-process
357 // access. As documented, it's clearer to NOT request ownership on creation
358 // since it isn't guaranteed we will get it. It is better to create it
359 // without ownership and explicitly get the ownership afterward.
[email protected]b9057302013-03-28 12:40:38360 base::win::ScopedHandle only_me(::CreateMutex(NULL, FALSE, kMutexName));
[email protected]14649ecd2012-12-05 01:00:24361 DPCHECK(only_me.IsValid());
362
363 AutoLockMutex auto_lock_only_me(only_me);
364
365 // We now own the mutex so we are the only process that can create the
366 // window at this time, but we must still check if someone created it
367 // between the time where we looked for it above and the time the mutex
368 // was given to us.
[email protected]10118162013-06-05 00:24:45369 remote_window_ = chrome::FindRunningChromeWindow(user_data_dir_);
[email protected]14649ecd2012-12-05 01:00:24370
371
372 // In Win8+, a new Chrome process launched in Desktop mode may need to be
373 // transmuted into Metro Chrome (see ShouldLaunchInWindows8ImmersiveMode for
374 // heuristics). To accomplish this, the current Chrome activates Metro
375 // Chrome, releases the startup mutex, and waits for metro Chrome to take
376 // the singleton. From that point onward, the command line for this Chrome
377 // process will be sent to Metro Chrome by the usual channels.
378 if (!remote_window_ && base::win::GetVersion() >= base::win::VERSION_WIN8 &&
379 !base::win::IsMetroProcess()) {
380 // |metro_activation_event| is created right before activating a Metro
381 // Chrome (note that there can only be one Metro Chrome process; by OS
382 // design); all following Desktop processes will then wait for this event
383 // to be signaled by Metro Chrome which will do so as soon as it grabs
384 // this singleton (should any of the waiting processes timeout waiting for
385 // the signal they will try to grab the singleton for themselves which
386 // will result in a forced Desktop Chrome launch in the worst case).
387 base::win::ScopedHandle metro_activation_event(
[email protected]b9057302013-03-28 12:40:38388 ::OpenEvent(SYNCHRONIZE, FALSE, kMetroActivationEventName));
[email protected]14649ecd2012-12-05 01:00:24389 if (!metro_activation_event.IsValid() &&
390 ShouldLaunchInWindows8ImmersiveMode(user_data_dir_)) {
391 // No Metro activation is under way, but the desire is to launch in
392 // Metro mode: activate and rendez-vous with the activated process.
393 metro_activation_event.Set(
[email protected]b9057302013-03-28 12:40:38394 ::CreateEvent(NULL, TRUE, FALSE, kMetroActivationEventName));
[email protected]281b5da2013-03-05 06:13:46395 if (!chrome::ActivateMetroChrome()) {
[email protected]14649ecd2012-12-05 01:00:24396 // Failed to launch immersive Chrome, default to launching on Desktop.
397 LOG(ERROR) << "Failed to launch immersive chrome";
398 metro_activation_event.Close();
399 }
400 }
401
402 if (metro_activation_event.IsValid()) {
403 // Release |only_me| (to let Metro Chrome grab this singleton) and wait
404 // until the event is signaled (i.e. Metro Chrome was successfully
405 // activated). Ignore timeout waiting for |metro_activation_event|.
406 {
407 AutoUnlockMutex auto_unlock_only_me(only_me);
408
[email protected]b9057302013-03-28 12:40:38409 DWORD result = ::WaitForSingleObject(metro_activation_event,
410 kMetroChromeActivationTimeoutMs);
[email protected]14649ecd2012-12-05 01:00:24411 DPCHECK(result == WAIT_OBJECT_0 || result == WAIT_TIMEOUT)
412 << "Result = " << result;
413 }
414
415 // Check if this singleton was successfully grabbed by another process
416 // (hopefully Metro Chrome). Failing to do so, this process will grab
417 // the singleton and launch in Desktop mode.
[email protected]10118162013-06-05 00:24:45418 remote_window_ = chrome::FindRunningChromeWindow(user_data_dir_);
[email protected]14649ecd2012-12-05 01:00:24419 }
420 }
421
422 if (!remote_window_) {
423 // We have to make sure there is no Chrome instance running on another
424 // machine that uses the same profile.
[email protected]650b2d52013-02-10 03:41:45425 base::FilePath lock_file_path = user_data_dir_.AppendASCII(kLockfile);
[email protected]b9057302013-03-28 12:40:38426 lock_file_ = ::CreateFile(lock_file_path.value().c_str(),
427 GENERIC_WRITE,
428 FILE_SHARE_READ,
429 NULL,
430 CREATE_ALWAYS,
431 FILE_ATTRIBUTE_NORMAL |
432 FILE_FLAG_DELETE_ON_CLOSE,
433 NULL);
434 DWORD error = ::GetLastError();
[email protected]14649ecd2012-12-05 01:00:24435 LOG_IF(WARNING, lock_file_ != INVALID_HANDLE_VALUE &&
436 error == ERROR_ALREADY_EXISTS) << "Lock file exists but is writable.";
437 LOG_IF(ERROR, lock_file_ == INVALID_HANDLE_VALUE)
438 << "Lock file can not be created! Error code: " << error;
439
440 if (lock_file_ != INVALID_HANDLE_VALUE) {
[email protected]14649ecd2012-12-05 01:00:24441 // Set the window's title to the path of our user data directory so
442 // other Chrome instances can decide if they should forward to us.
[email protected]4cf04bb2013-07-11 09:22:38443 bool result = window_.CreateNamed(
444 base::Bind(&ProcessLaunchNotification, notification_callback_),
445 user_data_dir_.value());
446 CHECK(result && window_.hwnd());
[email protected]14649ecd2012-12-05 01:00:24447 }
448
449 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
450 // Make sure no one is still waiting on Metro activation whether it
451 // succeeded (i.e., this is the Metro process) or failed.
452 base::win::ScopedHandle metro_activation_event(
[email protected]b9057302013-03-28 12:40:38453 ::OpenEvent(EVENT_MODIFY_STATE, FALSE, kMetroActivationEventName));
[email protected]14649ecd2012-12-05 01:00:24454 if (metro_activation_event.IsValid())
[email protected]b9057302013-03-28 12:40:38455 ::SetEvent(metro_activation_event);
[email protected]14649ecd2012-12-05 01:00:24456 }
457 }
458 }
459
[email protected]4cf04bb2013-07-11 09:22:38460 return window_.hwnd() != NULL;
[email protected]fc14cef2009-01-27 22:17:29461}
462
[email protected]9f20a6d02009-08-21 01:18:37463void ProcessSingleton::Cleanup() {
464}