Chromium Code Reviews| Index: chrome/browser/chrome_process_finder_win.cc |
| diff --git a/chrome/browser/chrome_process_finder_win.cc b/chrome/browser/chrome_process_finder_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..dc6e69db46114d73cf1677716515df0032a7356c |
| --- /dev/null |
| +++ b/chrome/browser/chrome_process_finder_win.cc |
| @@ -0,0 +1,177 @@ |
| +// Copyright 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chrome_process_finder_win.h" |
| + |
| +#include <string> |
| + |
| +#include "base/file_util.h" |
| +#include "base/process_info.h" |
| +#include "base/process_util.h" |
| +#include "base/string_number_conversions.h" |
| +#include "base/stringprintf.h" |
| +#include "base/utf_string_conversions.h" |
| +#include "base/win/metro.h" |
| +#include "base/win/scoped_handle.h" |
| +#include "base/win/win_util.h" |
| +#include "base/win/windows_version.h" |
| +#include "chrome/browser/metro_utils/metro_chrome_win.h" |
| +#include "chrome/common/chrome_constants.h" |
| +#include "chrome/common/chrome_switches.h" |
| + |
| + |
| +namespace { |
|
gab
2013/05/13 13:27:43
nit: Add an empty line after start of namespace.
koz (OOO until 15th September)
2013/05/14 08:39:54
Done.
|
| +const int kTimeoutInSeconds = 20; |
| + |
| +// The following is copied from net/base/escape.cc. We don't want to depend on |
| +// net here because this gets compiled into chrome.exe to facilitate |
| +// fast-rendezvous (see https://codereview.chromium.org/14617003/). |
|
gab
2013/05/13 13:27:43
I haven't followed the discussion with regards to
koz (OOO until 15th September)
2013/05/14 08:39:54
It should be possible to move net/base/escape* to
gab
2013/05/15 13:28:03
I'm not an expert with dependencies and link time
koz (OOO until 15th September)
2013/05/17 00:12:41
As the other reviewers have expressed a dislike of
|
| + |
| +// BEGIN COPY from net/base/escape.cc |
| + |
| +// A fast bit-vector map for ascii characters. |
| +// |
| +// Internally stores 256 bits in an array of 8 ints. |
| +// Does quick bit-flicking to lookup needed characters. |
| +struct Charmap { |
| + bool Contains(unsigned char c) const { |
| + return ((map[c >> 5] & (1 << (c & 31))) != 0); |
| + } |
| + |
| + uint32 map[8]; |
| +}; |
| + |
| +const char kHexString[] = "0123456789ABCDEF"; |
| +inline char IntToHex(int i) { |
| + DCHECK_GE(i, 0) << i << " not a hex value"; |
| + DCHECK_LE(i, 15) << i << " not a hex value"; |
| + return kHexString[i]; |
| +} |
| + |
| +// Given text to escape and a Charmap defining which values to escape, |
| +// return an escaped string. If use_plus is true, spaces are converted |
| +// to +, otherwise, if spaces are in the charmap, they are converted to |
| +// %20. |
| +std::string Escape(const std::string& text, const Charmap& charmap, |
| + bool use_plus) { |
| + std::string escaped; |
| + escaped.reserve(text.length() * 3); |
| + for (unsigned int i = 0; i < text.length(); ++i) { |
| + unsigned char c = static_cast<unsigned char>(text[i]); |
| + if (use_plus && ' ' == c) { |
| + escaped.push_back('+'); |
| + } else if (charmap.Contains(c)) { |
| + escaped.push_back('%'); |
| + escaped.push_back(IntToHex(c >> 4)); |
| + escaped.push_back(IntToHex(c & 0xf)); |
| + } else { |
| + escaped.push_back(c); |
| + } |
| + } |
| + return escaped; |
| +} |
| + |
| +// Everything except alphanumerics and !'()*-._~ |
| +// See RFC 2396 for the list of reserved characters. |
| +static const Charmap kQueryCharmap = {{ |
| + 0xffffffffL, 0xfc00987dL, 0x78000001L, 0xb8000001L, |
| + 0xffffffffL, 0xffffffffL, 0xffffffffL, 0xffffffffL |
| +}}; |
| + |
| +std::string EscapeQueryParamValue(const std::string& text, bool use_plus) { |
| + return Escape(text, kQueryCharmap, use_plus); |
| +} |
| + |
| +// END COPY from net/base/escape.cc |
| + |
| +} |
| + |
| +namespace chrome { |
| + |
| +HWND FindRunningChromeWindow(const base::FilePath& user_data_dir) { |
| + return FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass, |
| + user_data_dir.value().c_str()); |
| +} |
| + |
| +bool AttemptToNotifyRunningChrome(HWND remote_window) { |
| + DCHECK(remote_window); |
| + static const char kSearchUrl[] = |
| + "http://www.google.com/search?q=%s&sourceid=chrome&ie=UTF-8"; |
| + DWORD process_id = 0; |
| + DWORD thread_id = GetWindowThreadProcessId(remote_window, &process_id); |
| + if (!thread_id || !process_id) |
| + return false; |
| + |
| + if (base::win::IsMetroProcess()) { |
| + // Interesting corner case. We are launched as a metro process but we |
| + // found another chrome running. Since metro enforces single instance then |
| + // the other chrome must be desktop chrome and this must be a search charm |
| + // activation. This scenario is unique; other cases should be properly |
| + // handled by the delegate_execute which will not activate a second chrome. |
| + string16 terms; |
| + base::win::MetroLaunchType launch = base::win::GetMetroLaunchParams(&terms); |
| + if (launch != base::win::METRO_SEARCH) { |
| + LOG(WARNING) << "In metro mode, but and launch is " << launch; |
| + } else { |
| + std::string query = EscapeQueryParamValue(UTF16ToUTF8(terms), true); |
| + std::string url = base::StringPrintf(kSearchUrl, query.c_str()); |
| + SHELLEXECUTEINFOA sei = { sizeof(sei) }; |
| + sei.fMask = SEE_MASK_FLAG_LOG_USAGE; |
| + sei.nShow = SW_SHOWNORMAL; |
| + sei.lpFile = url.c_str(); |
| + OutputDebugStringA(sei.lpFile); |
| + sei.lpDirectory = ""; |
| + ::ShellExecuteExA(&sei); |
| + } |
| + return true; |
| + } |
| + |
| + base::win::ScopedHandle process_handle; |
| + if (base::win::GetVersion() >= base::win::VERSION_WIN8 && |
| + base::OpenProcessHandleWithAccess( |
| + process_id, PROCESS_QUERY_INFORMATION, |
| + process_handle.Receive()) && |
| + base::win::IsProcessImmersive(process_handle.Get())) { |
| + chrome::ActivateMetroChrome(); |
| + return true; |
|
gab
2013/05/13 13:27:43
Why add a return here? There was none in the origi
koz (OOO until 15th September)
2013/05/14 08:39:54
Good catch, thanks. This was an artifact of when I
|
| + } |
| + |
| + // Send the command line to the remote chrome window. |
|
gab
2013/05/13 13:27:43
Why move this code after the above activation inst
koz (OOO until 15th September)
2013/05/14 08:39:54
Just because there's no reason to have it come bef
|
| + // Format is "START\0<<<current directory>>>\0<<<commandline>>>". |
| + std::wstring to_send(L"START\0", 6); // want the NULL in the string. |
| + base::FilePath cur_dir; |
| + file_util::GetCurrentDirectory(&cur_dir); |
| + to_send.append(cur_dir.value()); |
| + to_send.append(L"\0", 1); // Null separator. |
| + to_send.append(GetCommandLineW()); |
| + // Add the process start time as a flag. |
| + to_send.append(L" --"); |
| + to_send.append(ASCIIToWide(switches::kOriginalProcessStartTime)); |
| + to_send.append(L"="); |
| + to_send.append(base::Int64ToString16( |
| + base::CurrentProcessInfo::CreationTime()->ToInternalValue())); |
| + to_send.append(L"\0", 1); // Null separator. |
| + |
| + // Allow the current running browser window making itself the foreground |
| + // window (otherwise it will just flash in the taskbar). |
| + ::AllowSetForegroundWindow(process_id); |
| + |
| + COPYDATASTRUCT cds; |
| + cds.dwData = 0; |
| + cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t)); |
| + cds.lpData = const_cast<wchar_t*>(to_send.c_str()); |
| + DWORD_PTR result = 0; |
| + if (::SendMessageTimeout(remote_window, |
| + WM_COPYDATA, |
| + NULL, |
| + reinterpret_cast<LPARAM>(&cds), |
| + SMTO_ABORTIFHUNG, |
| + kTimeoutInSeconds * 1000, |
| + &result)) { |
| + return !!result; |
| + } |
| + return false; |
| +} |
| + |
| +} // namespace chrome |