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 <string> | |
8 | |
9 #include "base/file_util.h" | |
10 #include "base/files/file_path.h" | |
11 #include "base/logging.h" | |
12 #include "base/process_info.h" | |
13 #include "base/process_util.h" | |
14 #include "base/string_number_conversions.h" | |
15 #include "base/stringprintf.h" | |
16 #include "base/utf_string_conversions.h" | |
17 #include "base/win/metro.h" | |
18 #include "base/win/scoped_handle.h" | |
19 #include "base/win/win_util.h" | |
20 #include "base/win/windows_version.h" | |
21 #include "chrome/browser/metro_utils/metro_chrome_win.h" | |
22 #include "chrome/common/chrome_constants.h" | |
23 #include "chrome/common/chrome_switches.h" | |
24 | |
25 | |
26 namespace { | |
27 | |
28 const int kTimeoutInSeconds = 20; | |
29 | |
30 // The following is copied from net/base/escape.cc. We don't want to depend on | |
31 // net here because this gets compiled into chrome.exe to facilitate | |
32 // fast-rendezvous (see https://codereview.chromium.org/14617003/). | |
33 | |
34 // TODO(koz): Move these functions out of net/base/escape.cc into base/escape.cc | |
35 // so we can depend on it directly. | |
36 | |
37 // BEGIN COPY from net/base/escape.cc | |
38 | |
39 // A fast bit-vector map for ascii characters. | |
40 // | |
41 // Internally stores 256 bits in an array of 8 ints. | |
42 // Does quick bit-flicking to lookup needed characters. | |
43 struct Charmap { | |
44 bool Contains(unsigned char c) const { | |
45 return ((map[c >> 5] & (1 << (c & 31))) != 0); | |
46 } | |
47 | |
48 uint32 map[8]; | |
49 }; | |
50 | |
51 const char kHexString[] = "0123456789ABCDEF"; | |
52 inline char IntToHex(int i) { | |
53 DCHECK_GE(i, 0) << i << " not a hex value"; | |
54 DCHECK_LE(i, 15) << i << " not a hex value"; | |
55 return kHexString[i]; | |
56 } | |
57 | |
58 // Given text to escape and a Charmap defining which values to escape, | |
59 // return an escaped string. If use_plus is true, spaces are converted | |
60 // to +, otherwise, if spaces are in the charmap, they are converted to | |
61 // %20. | |
62 std::string Escape(const std::string& text, const Charmap& charmap, | |
63 bool use_plus) { | |
64 std::string escaped; | |
65 escaped.reserve(text.length() * 3); | |
66 for (unsigned int i = 0; i < text.length(); ++i) { | |
67 unsigned char c = static_cast<unsigned char>(text[i]); | |
68 if (use_plus && ' ' == c) { | |
69 escaped.push_back('+'); | |
70 } else if (charmap.Contains(c)) { | |
71 escaped.push_back('%'); | |
72 escaped.push_back(IntToHex(c >> 4)); | |
73 escaped.push_back(IntToHex(c & 0xf)); | |
74 } else { | |
75 escaped.push_back(c); | |
76 } | |
77 } | |
78 return escaped; | |
79 } | |
80 | |
81 // Everything except alphanumerics and !'()*-._~ | |
82 // See RFC 2396 for the list of reserved characters. | |
83 static const Charmap kQueryCharmap = {{ | |
84 0xffffffffL, 0xfc00987dL, 0x78000001L, 0xb8000001L, | |
85 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL | |
86 }}; | |
87 | |
88 std::string EscapeQueryParamValue(const std::string& text, bool use_plus) { | |
89 return Escape(text, kQueryCharmap, use_plus); | |
90 } | |
91 | |
92 // END COPY from net/base/escape.cc | |
93 | |
94 } | |
95 | |
96 namespace chrome { | |
97 | |
98 HWND FindRunningChromeWindow(const base::FilePath& user_data_dir) { | |
99 return FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass, | |
100 user_data_dir.value().c_str()); | |
101 } | |
102 | |
103 NotifyResult AttemptToNotifyRunningChrome(HWND remote_window) { | |
104 DCHECK(remote_window); | |
105 static const char kSearchUrl[] = | |
106 "http://www.google.com/search?q=%s&sourceid=chrome&ie=UTF-8"; | |
107 DWORD process_id = 0; | |
108 DWORD thread_id = GetWindowThreadProcessId(remote_window, &process_id); | |
109 if (!thread_id || !process_id) | |
110 return NOTIFY_FAILED; | |
111 | |
112 if (base::win::IsMetroProcess()) { | |
113 // Interesting corner case. We are launched as a metro process but we | |
114 // found another chrome running. Since metro enforces single instance then | |
115 // the other chrome must be desktop chrome and this must be a search charm | |
116 // activation. This scenario is unique; other cases should be properly | |
117 // handled by the delegate_execute which will not activate a second chrome. | |
118 string16 terms; | |
119 base::win::MetroLaunchType launch = base::win::GetMetroLaunchParams(&terms); | |
120 if (launch != base::win::METRO_SEARCH) { | |
121 LOG(WARNING) << "In metro mode, but and launch is " << launch; | |
122 } else { | |
123 std::string query = EscapeQueryParamValue(UTF16ToUTF8(terms), true); | |
124 std::string url = base::StringPrintf(kSearchUrl, query.c_str()); | |
125 SHELLEXECUTEINFOA sei = { sizeof(sei) }; | |
126 sei.fMask = SEE_MASK_FLAG_LOG_USAGE; | |
127 sei.nShow = SW_SHOWNORMAL; | |
128 sei.lpFile = url.c_str(); | |
129 OutputDebugStringA(sei.lpFile); | |
130 sei.lpDirectory = ""; | |
131 ::ShellExecuteExA(&sei); | |
132 } | |
133 return NOTIFY_SUCCESS; | |
134 } | |
135 | |
136 base::win::ScopedHandle process_handle; | |
137 if (base::win::GetVersion() >= base::win::VERSION_WIN8 && | |
138 base::OpenProcessHandleWithAccess( | |
139 process_id, PROCESS_QUERY_INFORMATION, | |
140 process_handle.Receive()) && | |
141 base::win::IsProcessImmersive(process_handle.Get())) { | |
142 chrome::ActivateMetroChrome(); | |
143 } | |
144 | |
145 // Send the command line to the remote chrome window. | |
146 // Format is "START\0<<<current directory>>>\0<<<commandline>>>". | |
147 std::wstring to_send(L"START\0", 6); // want the NULL in the string. | |
148 base::FilePath cur_dir; | |
149 if (!file_util::GetCurrentDirectory(&cur_dir)) | |
150 return NOTIFY_FAILED; | |
151 to_send.append(cur_dir.value()); | |
152 to_send.append(L"\0", 1); // Null separator. | |
153 to_send.append(GetCommandLineW()); | |
154 // Add the process start time as a flag. | |
155 to_send.append(L" --"); | |
156 to_send.append(ASCIIToWide(switches::kOriginalProcessStartTime)); | |
157 to_send.append(L"="); | |
158 to_send.append(base::Int64ToString16( | |
159 base::CurrentProcessInfo::CreationTime()->ToInternalValue())); | |
160 to_send.append(L"\0", 1); // Null separator. | |
161 | |
162 // Allow the current running browser window to make itself the foreground | |
163 // window (otherwise it will just flash in the taskbar). | |
164 ::AllowSetForegroundWindow(process_id); | |
165 | |
166 COPYDATASTRUCT cds; | |
167 cds.dwData = 0; | |
168 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t)); | |
169 cds.lpData = const_cast<wchar_t*>(to_send.c_str()); | |
170 DWORD_PTR result = 0; | |
171 if (::SendMessageTimeout(remote_window, | |
172 WM_COPYDATA, | |
173 NULL, | |
174 reinterpret_cast<LPARAM>(&cds), | |
175 SMTO_ABORTIFHUNG, | |
176 kTimeoutInSeconds * 1000, | |
177 &result)) { | |
178 if (result) | |
gab
2013/05/21 19:48:40
optional nit: I find using the ternary operator cl
koz (OOO until 15th September)
2013/05/23 06:11:49
Done.
| |
179 return NOTIFY_SUCCESS; | |
180 return NOTIFY_FAILED; | |
181 } | |
182 | |
183 // It is possible that the process owning this window may have died by now. | |
184 if (!::IsWindow(remote_window)) | |
185 return NOTIFY_FAILED; | |
186 | |
187 return NOTIFY_WINDOW_HUNG; | |
gab
2013/05/21 19:48:40
Add a comment like:
// If the window couldn't be
koz (OOO until 15th September)
2013/05/23 06:11:49
Done.
| |
188 } | |
189 | |
190 } // namespace chrome | |
OLD | NEW |