OLD | NEW |
(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 "base/file_util.h" |
| 8 #include "base/process_util.h" |
| 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/win/metro.h" |
| 12 #include "base/win/scoped_handle.h" |
| 13 #include "base/win/win_util.h" |
| 14 #include "base/win/windows_version.h" |
| 15 #include "chrome/browser/ui/metro_chrome_win.h" |
| 16 #include "chrome/common/chrome_constants.h" |
| 17 #include "net/base/escape.h" |
| 18 |
| 19 |
| 20 namespace { |
| 21 const int kTimeoutInSeconds = 20; |
| 22 } |
| 23 |
| 24 namespace chrome { |
| 25 |
| 26 HWND FindRunningChromeWindow(const base::FilePath& user_data_dir) { |
| 27 return FindWindowEx(HWND_MESSAGE, NULL, chrome::kMessageWindowClass, |
| 28 user_data_dir.value().c_str()); |
| 29 } |
| 30 |
| 31 bool AttemptToNotifyRunningChrome(HWND remote_window) { |
| 32 const char kSearchUrl[] = |
| 33 "http://www.google.com/search?q=%s&sourceid=chrome&ie=UTF-8"; |
| 34 DWORD process_id = 0; |
| 35 DWORD thread_id = GetWindowThreadProcessId(remote_window, &process_id); |
| 36 if (!thread_id || !process_id) |
| 37 return false; |
| 38 |
| 39 if (base::win::IsMetroProcess()) { |
| 40 // Interesting corner case. We are launched as a metro process but we |
| 41 // found another chrome running. Since metro enforces single instance then |
| 42 // the other chrome must be desktop chrome and this must be a search charm |
| 43 // activation. This scenario is unique; other cases should be properly |
| 44 // handled by the delegate_execute which will not activate a second chrome. |
| 45 string16 terms; |
| 46 base::win::MetroLaunchType launch = base::win::GetMetroLaunchParams(&terms); |
| 47 if (launch != base::win::METRO_SEARCH) { |
| 48 LOG(WARNING) << "In metro mode, but and launch is " << launch; |
| 49 } else { |
| 50 std::string query = net::EscapeQueryParamValue(UTF16ToUTF8(terms), true); |
| 51 std::string url = base::StringPrintf(kSearchUrl, query.c_str()); |
| 52 SHELLEXECUTEINFOA sei = { sizeof(sei) }; |
| 53 sei.fMask = SEE_MASK_FLAG_LOG_USAGE; |
| 54 sei.nShow = SW_SHOWNORMAL; |
| 55 sei.lpFile = url.c_str(); |
| 56 OutputDebugStringA(sei.lpFile); |
| 57 sei.lpDirectory = ""; |
| 58 ::ShellExecuteExA(&sei); |
| 59 } |
| 60 return true; |
| 61 } |
| 62 |
| 63 base::win::ScopedHandle process_handle; |
| 64 if (base::win::GetVersion() >= base::win::VERSION_WIN8 && |
| 65 base::OpenProcessHandleWithAccess( |
| 66 process_id, PROCESS_QUERY_INFORMATION, |
| 67 process_handle.Receive()) && |
| 68 base::win::IsProcessImmersive(process_handle.Get())) { |
| 69 chrome::ActivateMetroChrome(); |
| 70 return true; |
| 71 } |
| 72 |
| 73 // Send the command line to the remote chrome window. |
| 74 // Format is "START\0<<<current directory>>>\0<<<commandline>>>". |
| 75 std::wstring to_send(L"START\0", 6); // want the NULL in the string. |
| 76 base::FilePath cur_dir; |
| 77 file_util::GetCurrentDirectory(&cur_dir); |
| 78 to_send.append(cur_dir.value()); |
| 79 to_send.append(L"\0", 1); // Null separator. |
| 80 to_send.append(GetCommandLineW()); |
| 81 to_send.append(L"\0", 1); // Null separator. |
| 82 |
| 83 // Allow the current running browser window making itself the foreground |
| 84 // window (otherwise it will just flash in the taskbar). |
| 85 AllowSetForegroundWindow(process_id); |
| 86 |
| 87 COPYDATASTRUCT cds; |
| 88 cds.dwData = 0; |
| 89 cds.cbData = static_cast<DWORD>((to_send.length() + 1) * sizeof(wchar_t)); |
| 90 cds.lpData = const_cast<wchar_t*>(to_send.c_str()); |
| 91 DWORD_PTR result = 0; |
| 92 if (SendMessageTimeout(remote_window, |
| 93 WM_COPYDATA, |
| 94 NULL, |
| 95 reinterpret_cast<LPARAM>(&cds), |
| 96 SMTO_ABORTIFHUNG, |
| 97 kTimeoutInSeconds * 1000, |
| 98 &result)) { |
| 99 return !!result; |
| 100 } |
| 101 return false; |
| 102 } |
| 103 |
| 104 } // namespace chrome |
OLD | NEW |