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