Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(754)

Side by Side Diff: chrome/browser/chrome_process_finder_win.cc

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

Powered by Google App Engine
This is Rietveld 408576698