OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/chrome_process_finder_win.h" | |
6 | |
7 #include <shellapi.h> | |
8 #include <string> | |
9 | |
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" | |
25 | |
26 | |
27 namespace { | |
28 | |
29 int timeout_in_milliseconds = 20 * 1000; | |
30 | |
31 } // namespace | |
32 | |
33 namespace chrome { | |
34 | |
35 HWND FindRunningChromeWindow(const base::FilePath& user_data_dir) { | |
36 return base::win::MessageWindow::FindWindow(user_data_dir.value()); | |
37 } | |
38 | |
39 NotifyChromeResult AttemptToNotifyRunningChrome(HWND remote_window, | |
40 bool fast_start) { | |
41 DCHECK(remote_window); | |
42 DWORD process_id = 0; | |
43 DWORD thread_id = GetWindowThreadProcessId(remote_window, &process_id); | |
44 if (!thread_id || !process_id) | |
45 return NOTIFY_FAILED; | |
46 | |
47 base::CommandLine command_line(*base::CommandLine::ForCurrentProcess()); | |
48 command_line.AppendSwitchASCII( | |
49 switches::kOriginalProcessStartTime, | |
50 base::Int64ToString( | |
51 base::CurrentProcessInfo::CreationTime().ToInternalValue())); | |
52 | |
53 if (fast_start) | |
54 command_line.AppendSwitch(switches::kFastStart); | |
55 | |
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)) | |
61 return NOTIFY_FAILED; | |
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. | |
66 | |
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); | |
70 | |
71 COPYDATASTRUCT cds; | |
72 cds.dwData = 0; | |
73 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t)); | |
74 cds.lpData = const_cast<wchar_t*>(to_send.c_str()); | |
75 DWORD_PTR result = 0; | |
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; | |
80 } | |
81 | |
82 // It is possible that the process owning this window may have died by now. | |
83 if (!::IsWindow(remote_window)) | |
84 return NOTIFY_FAILED; | |
85 | |
86 // If the window couldn't be notified but still exists, assume it is hung. | |
87 return NOTIFY_WINDOW_HUNG; | |
88 } | |
89 | |
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(); | |
94 return old_timeout; | |
95 } | |
96 | |
97 } // namespace chrome | |
OLD | NEW |