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

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

Powered by Google App Engine
This is Rietveld 408576698