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