replace OVERRIDE and FINAL with override and final in ash/
[chromium-blink-merge.git] / chrome / browser / chrome_process_finder_win.cc
blob58248244b63ee3af15485853fd82fab884115564
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"
7 #include <shellapi.h>
8 #include <string>
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_handle.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/metro.h"
21 #include "base/win/scoped_handle.h"
22 #include "base/win/win_util.h"
23 #include "base/win/windows_version.h"
24 #include "chrome/browser/metro_utils/metro_chrome_win.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "chrome/common/chrome_switches.h"
29 namespace {
31 const int kTimeoutInSeconds = 20;
33 // The following is copied from net/base/escape.cc. We don't want to depend on
34 // net here because this gets compiled into chrome.exe to facilitate
35 // fast-rendezvous (see https://codereview.chromium.org/14617003/).
37 // TODO(koz): Move these functions out of net/base/escape.cc into base/escape.cc
38 // so we can depend on it directly.
40 // BEGIN COPY from net/base/escape.cc
42 // A fast bit-vector map for ascii characters.
44 // Internally stores 256 bits in an array of 8 ints.
45 // Does quick bit-flicking to lookup needed characters.
46 struct Charmap {
47 bool Contains(unsigned char c) const {
48 return ((map[c >> 5] & (1 << (c & 31))) != 0);
51 uint32 map[8];
54 const char kHexString[] = "0123456789ABCDEF";
55 inline char IntToHex(int i) {
56 DCHECK_GE(i, 0) << i << " not a hex value";
57 DCHECK_LE(i, 15) << i << " not a hex value";
58 return kHexString[i];
61 // Given text to escape and a Charmap defining which values to escape,
62 // return an escaped string. If use_plus is true, spaces are converted
63 // to +, otherwise, if spaces are in the charmap, they are converted to
64 // %20.
65 std::string Escape(const std::string& text, const Charmap& charmap,
66 bool use_plus) {
67 std::string escaped;
68 escaped.reserve(text.length() * 3);
69 for (unsigned int i = 0; i < text.length(); ++i) {
70 unsigned char c = static_cast<unsigned char>(text[i]);
71 if (use_plus && ' ' == c) {
72 escaped.push_back('+');
73 } else if (charmap.Contains(c)) {
74 escaped.push_back('%');
75 escaped.push_back(IntToHex(c >> 4));
76 escaped.push_back(IntToHex(c & 0xf));
77 } else {
78 escaped.push_back(c);
81 return escaped;
84 // Everything except alphanumerics and !'()*-._~
85 // See RFC 2396 for the list of reserved characters.
86 static const Charmap kQueryCharmap = {{
87 0xffffffffL, 0xfc00987dL, 0x78000001L, 0xb8000001L,
88 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL
89 }};
91 std::string EscapeQueryParamValue(const std::string& text, bool use_plus) {
92 return Escape(text, kQueryCharmap, use_plus);
95 // END COPY from net/base/escape.cc
97 } // namespace
99 namespace chrome {
101 HWND FindRunningChromeWindow(const base::FilePath& user_data_dir) {
102 return base::win::MessageWindow::FindWindow(user_data_dir.value());
105 NotifyChromeResult AttemptToNotifyRunningChrome(HWND remote_window,
106 bool fast_start) {
107 DCHECK(remote_window);
108 static const char kSearchUrl[] =
109 "http://www.google.com/search?q=%s&sourceid=chrome&ie=UTF-8";
110 DWORD process_id = 0;
111 DWORD thread_id = GetWindowThreadProcessId(remote_window, &process_id);
112 if (!thread_id || !process_id)
113 return NOTIFY_FAILED;
115 #if !defined(USE_AURA)
116 if (base::win::IsMetroProcess()) {
117 // Interesting corner case. We are launched as a metro process but we
118 // found another chrome running. Since metro enforces single instance then
119 // the other chrome must be desktop chrome and this must be a search charm
120 // activation. This scenario is unique; other cases should be properly
121 // handled by the delegate_execute which will not activate a second chrome.
122 base::string16 terms;
123 base::win::MetroLaunchType launch = base::win::GetMetroLaunchParams(&terms);
124 if (launch != base::win::METRO_SEARCH) {
125 LOG(WARNING) << "In metro mode, but and launch is " << launch;
126 } else {
127 std::string query = EscapeQueryParamValue(base::UTF16ToUTF8(terms), true);
128 std::string url = base::StringPrintf(kSearchUrl, query.c_str());
129 SHELLEXECUTEINFOA sei = { sizeof(sei) };
130 sei.fMask = SEE_MASK_FLAG_LOG_USAGE;
131 sei.nShow = SW_SHOWNORMAL;
132 sei.lpFile = url.c_str();
133 OutputDebugStringA(sei.lpFile);
134 sei.lpDirectory = "";
135 ::ShellExecuteExA(&sei);
137 return NOTIFY_SUCCESS;
140 base::win::ScopedHandle process_handle;
141 if (base::win::GetVersion() >= base::win::VERSION_WIN8 &&
142 base::OpenProcessHandleWithAccess(
143 process_id, PROCESS_QUERY_INFORMATION,
144 process_handle.Receive())) {
145 // Receive() causes the process handle to be set in the destructor of the
146 // temporary receiver object, which does not happen until after the if
147 // statement is complete. So IsProcessImmersive() should only be checked
148 // as part of a separate if statement.
149 if (base::win::IsProcessImmersive(process_handle.Get()))
150 chrome::ActivateMetroChrome();
152 #endif
154 CommandLine command_line(*CommandLine::ForCurrentProcess());
155 command_line.AppendSwitchASCII(
156 switches::kOriginalProcessStartTime,
157 base::Int64ToString(
158 base::CurrentProcessInfo::CreationTime().ToInternalValue()));
160 if (fast_start)
161 command_line.AppendSwitch(switches::kFastStart);
163 // Send the command line to the remote chrome window.
164 // Format is "START\0<<<current directory>>>\0<<<commandline>>>".
165 std::wstring to_send(L"START\0", 6); // want the NULL in the string.
166 base::FilePath cur_dir;
167 if (!base::GetCurrentDirectory(&cur_dir))
168 return NOTIFY_FAILED;
169 to_send.append(cur_dir.value());
170 to_send.append(L"\0", 1); // Null separator.
171 to_send.append(command_line.GetCommandLineString());
172 to_send.append(L"\0", 1); // Null separator.
174 // Allow the current running browser window to make itself the foreground
175 // window (otherwise it will just flash in the taskbar).
176 ::AllowSetForegroundWindow(process_id);
178 COPYDATASTRUCT cds;
179 cds.dwData = 0;
180 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t));
181 cds.lpData = const_cast<wchar_t*>(to_send.c_str());
182 DWORD_PTR result = 0;
183 if (::SendMessageTimeout(remote_window,
184 WM_COPYDATA,
185 NULL,
186 reinterpret_cast<LPARAM>(&cds),
187 SMTO_ABORTIFHUNG,
188 kTimeoutInSeconds * 1000,
189 &result)) {
190 return result ? NOTIFY_SUCCESS : NOTIFY_FAILED;
193 // It is possible that the process owning this window may have died by now.
194 if (!::IsWindow(remote_window))
195 return NOTIFY_FAILED;
197 // If the window couldn't be notified but still exists, assume it is hung.
198 return NOTIFY_WINDOW_HUNG;
201 } // namespace chrome