1. Token CRCs are not handled correctly. This adds the corrected Whispernet binary...
[chromium-blink-merge.git] / chrome / browser / chrome_process_finder_win.cc
blob0fe88448bf3ee569a5c537c083d398d2e500d7d5
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.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 base::CommandLine command_line(*base::CommandLine::ForCurrentProcess());
116 command_line.AppendSwitchASCII(
117 switches::kOriginalProcessStartTime,
118 base::Int64ToString(
119 base::CurrentProcessInfo::CreationTime().ToInternalValue()));
121 if (fast_start)
122 command_line.AppendSwitch(switches::kFastStart);
124 // Send the command line to the remote chrome window.
125 // Format is "START\0<<<current directory>>>\0<<<commandline>>>".
126 std::wstring to_send(L"START\0", 6); // want the NULL in the string.
127 base::FilePath cur_dir;
128 if (!base::GetCurrentDirectory(&cur_dir))
129 return NOTIFY_FAILED;
130 to_send.append(cur_dir.value());
131 to_send.append(L"\0", 1); // Null separator.
132 to_send.append(command_line.GetCommandLineString());
133 to_send.append(L"\0", 1); // Null separator.
135 // Allow the current running browser window to make itself the foreground
136 // window (otherwise it will just flash in the taskbar).
137 ::AllowSetForegroundWindow(process_id);
139 COPYDATASTRUCT cds;
140 cds.dwData = 0;
141 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t));
142 cds.lpData = const_cast<wchar_t*>(to_send.c_str());
143 DWORD_PTR result = 0;
144 if (::SendMessageTimeout(remote_window,
145 WM_COPYDATA,
146 NULL,
147 reinterpret_cast<LPARAM>(&cds),
148 SMTO_ABORTIFHUNG,
149 kTimeoutInSeconds * 1000,
150 &result)) {
151 return result ? NOTIFY_SUCCESS : NOTIFY_FAILED;
154 // It is possible that the process owning this window may have died by now.
155 if (!::IsWindow(remote_window))
156 return NOTIFY_FAILED;
158 // If the window couldn't be notified but still exists, assume it is hung.
159 return NOTIFY_WINDOW_HUNG;
162 } // namespace chrome