Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / browser / chrome_process_finder_win.cc
blob1be25babb3581d63d03dc0ccc7eeb0ee5e10c26d
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 int timeout_in_milliseconds = 20 * 1000;
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 DWORD process_id = 0;
109 DWORD thread_id = GetWindowThreadProcessId(remote_window, &process_id);
110 if (!thread_id || !process_id)
111 return NOTIFY_FAILED;
113 base::CommandLine command_line(*base::CommandLine::ForCurrentProcess());
114 command_line.AppendSwitchASCII(
115 switches::kOriginalProcessStartTime,
116 base::Int64ToString(
117 base::CurrentProcessInfo::CreationTime().ToInternalValue()));
119 if (fast_start)
120 command_line.AppendSwitch(switches::kFastStart);
122 // Send the command line to the remote chrome window.
123 // Format is "START\0<<<current directory>>>\0<<<commandline>>>".
124 std::wstring to_send(L"START\0", 6); // want the NULL in the string.
125 base::FilePath cur_dir;
126 if (!base::GetCurrentDirectory(&cur_dir))
127 return NOTIFY_FAILED;
128 to_send.append(cur_dir.value());
129 to_send.append(L"\0", 1); // Null separator.
130 to_send.append(command_line.GetCommandLineString());
131 to_send.append(L"\0", 1); // Null separator.
133 // Allow the current running browser window to make itself the foreground
134 // window (otherwise it will just flash in the taskbar).
135 ::AllowSetForegroundWindow(process_id);
137 COPYDATASTRUCT cds;
138 cds.dwData = 0;
139 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t));
140 cds.lpData = const_cast<wchar_t*>(to_send.c_str());
141 DWORD_PTR result = 0;
142 if (::SendMessageTimeout(remote_window, WM_COPYDATA, NULL,
143 reinterpret_cast<LPARAM>(&cds), SMTO_ABORTIFHUNG,
144 timeout_in_milliseconds, &result)) {
145 return result ? NOTIFY_SUCCESS : NOTIFY_FAILED;
148 // It is possible that the process owning this window may have died by now.
149 if (!::IsWindow(remote_window))
150 return NOTIFY_FAILED;
152 // If the window couldn't be notified but still exists, assume it is hung.
153 return NOTIFY_WINDOW_HUNG;
156 base::TimeDelta SetNotificationTimeoutForTesting(base::TimeDelta new_timeout) {
157 base::TimeDelta old_timeout =
158 base::TimeDelta::FromMilliseconds(timeout_in_milliseconds);
159 timeout_in_milliseconds = new_timeout.InMilliseconds();
160 return old_timeout;
163 } // namespace chrome