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

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

Powered by Google App Engine
This is Rietveld 408576698