1 // Copyright 2013 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.
5 #include "chrome/browser/chrome_process_finder_win.h"
10 #include "base/command_line.h"
11 #include "base/files/file_path.h"
12 #include "base/files/file_util.h"
13 #include "base/logging.h"
14 #include "base/process/process.h"
15 #include "base/process/process_info.h"
16 #include "base/strings/string_number_conversions.h"
17 #include "base/strings/stringprintf.h"
18 #include "base/strings/utf_string_conversions.h"
19 #include "base/win/message_window.h"
20 #include "base/win/scoped_handle.h"
21 #include "base/win/win_util.h"
22 #include "base/win/windows_version.h"
23 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_switches.h"
29 int timeout_in_milliseconds
= 20 * 1000;
35 HWND
FindRunningChromeWindow(const base::FilePath
& user_data_dir
) {
36 return base::win::MessageWindow::FindWindow(user_data_dir
.value());
39 NotifyChromeResult
AttemptToNotifyRunningChrome(HWND remote_window
,
41 DCHECK(remote_window
);
43 DWORD thread_id
= GetWindowThreadProcessId(remote_window
, &process_id
);
44 if (!thread_id
|| !process_id
)
47 base::CommandLine
command_line(*base::CommandLine::ForCurrentProcess());
48 command_line
.AppendSwitchASCII(
49 switches::kOriginalProcessStartTime
,
51 base::CurrentProcessInfo::CreationTime().ToInternalValue()));
54 command_line
.AppendSwitch(switches::kFastStart
);
56 // Send the command line to the remote chrome window.
57 // Format is "START\0<<<current directory>>>\0<<<commandline>>>".
58 std::wstring
to_send(L
"START\0", 6); // want the NULL in the string.
59 base::FilePath cur_dir
;
60 if (!base::GetCurrentDirectory(&cur_dir
))
62 to_send
.append(cur_dir
.value());
63 to_send
.append(L
"\0", 1); // Null separator.
64 to_send
.append(command_line
.GetCommandLineString());
65 to_send
.append(L
"\0", 1); // Null separator.
67 // Allow the current running browser window to make itself the foreground
68 // window (otherwise it will just flash in the taskbar).
69 ::AllowSetForegroundWindow(process_id
);
73 cds
.cbData
= static_cast<DWORD
>((to_send
.length() + 1) * sizeof(wchar_t));
74 cds
.lpData
= const_cast<wchar_t*>(to_send
.c_str());
76 if (::SendMessageTimeout(remote_window
, WM_COPYDATA
, NULL
,
77 reinterpret_cast<LPARAM
>(&cds
), SMTO_ABORTIFHUNG
,
78 timeout_in_milliseconds
, &result
)) {
79 return result
? NOTIFY_SUCCESS
: NOTIFY_FAILED
;
82 // It is possible that the process owning this window may have died by now.
83 if (!::IsWindow(remote_window
))
86 // If the window couldn't be notified but still exists, assume it is hung.
87 return NOTIFY_WINDOW_HUNG
;
90 base::TimeDelta
SetNotificationTimeoutForTesting(base::TimeDelta new_timeout
) {
91 base::TimeDelta old_timeout
=
92 base::TimeDelta::FromMilliseconds(timeout_in_milliseconds
);
93 timeout_in_milliseconds
= new_timeout
.InMilliseconds();