OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 // Implementation of the CommandExecuteImpl class which implements the |
| 5 // IExecuteCommand and related interfaces for handling ShellExecute based |
| 6 // launches of the Chrome browser. |
| 7 |
| 8 #include "win8/delegate_execute/command_execute_impl.h" |
| 9 |
| 10 #include "base/file_util.h" |
| 11 #include "base/path_service.h" |
| 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/win/scoped_co_mem.h" |
| 14 #include "base/win/scoped_handle.h" |
| 15 #include "chrome/common/chrome_constants.h" |
| 16 #include "chrome/common/chrome_paths.h" |
| 17 #include "chrome/common/chrome_switches.h" |
| 18 #include "win8/delegate_execute/chrome_util.h" |
| 19 |
| 20 // CommandExecuteImpl is resposible for activating chrome in Windows 8. The |
| 21 // flow is complicated and this tries to highlight the important events. |
| 22 // The current approach is to have a single instance of chrome either |
| 23 // running in desktop or metro mode. If there is no current instance then |
| 24 // the desktop shortcut launches desktop chrome and the metro tile or search |
| 25 // charm launches metro chrome. |
| 26 // If chrome is running then focus/activation is given to the existing one |
| 27 // regarless of what launch point the user used. |
| 28 // |
| 29 // The general flow when chrome is the default browser is as follows: |
| 30 // |
| 31 // 1- User interacts with launch point (icon, tile, search, shellexec, etc) |
| 32 // 2- Windows finds the appid for launch item and resolves it to chrome |
| 33 // 3- Windows activates CommandExecuteImpl inside a surrogate process |
| 34 // 4- Windows calls the following sequence of entry points: |
| 35 // CommandExecuteImpl::SetShowWindow |
| 36 // CommandExecuteImpl::SetPosition |
| 37 // CommandExecuteImpl::SetDirectory |
| 38 // CommandExecuteImpl::SetParameter |
| 39 // CommandExecuteImpl::SetNoShowUI |
| 40 // CommandExecuteImpl::SetSelection |
| 41 // CommandExecuteImpl::Initialize |
| 42 // Up to this point the code basically just gathers values passed in, like |
| 43 // the launch scheme (or url) and the activation verb. |
| 44 // 5- Windows calls CommandExecuteImpl::Getvalue() |
| 45 // Here we need to return AHE_IMMERSIVE or AHE_DESKTOP. That depends on: |
| 46 // a) if run in high-integrity return AHE_DESKTOP |
| 47 // b) if chrome is running return the AHE_ mode of chrome |
| 48 // c) if the current process is inmmersive return AHE_IMMERSIVE |
| 49 // d) if the protocol is file and IExecuteCommandHost::GetUIMode() is not |
| 50 // ECHUIM_DESKTOP then return AHE_IMMERSIVE |
| 51 // e) if none of the above return AHE_DESKTOP |
| 52 // 6- If we returned AHE_DESKTOP in step 5 then CommandExecuteImpl::Execute() |
| 53 // is called, here we call GetLaunchMode() which: |
| 54 // a) if chrome is running return the mode of chrome or |
| 55 // b) return IExecuteCommandHost::GetUIMode() |
| 56 // 7- If GetLaunchMode() returns |
| 57 // a) ECHUIM_DESKTOP we call LaunchDestopChrome() that calls ::CreateProcess |
| 58 // b) else we call one of the IApplicationActivationManager activation |
| 59 // functions depending on the parameters passed in step 4. |
| 60 // |
| 61 // Some examples will help clarify the common cases. |
| 62 // |
| 63 // I - No chrome running, taskbar icon launch: |
| 64 // a) Scheme is 'file', Verb is 'open' |
| 65 // b) GetValue() returns at e) step : AHE_DESKTOP |
| 66 // c) Execute() calls LaunchDestopChrome() |
| 67 // --> desktop chrome runs |
| 68 // II- No chrome running, tile activation launch: |
| 69 // a) Scheme is 'file', Verb is 'open' |
| 70 // b) GetValue() returns at d) step : AHE_IMMERSIVE |
| 71 // c) Windows does not call us back and just activates chrome |
| 72 // --> metro chrome runs |
| 73 // III- No chrome running, url link click on metro app: |
| 74 // a) Scheme is 'http', Verb is 'open' |
| 75 // b) Getvalue() returns at e) step : AHE_DESKTOP |
| 76 // c) Execute() calls IApplicationActivationManager::ActivateForProtocol |
| 77 // --> metro chrome runs |
| 78 // |
| 79 CommandExecuteImpl::CommandExecuteImpl() |
| 80 : integrity_level_(base::INTEGRITY_UNKNOWN), |
| 81 launch_scheme_(INTERNET_SCHEME_DEFAULT), |
| 82 chrome_mode_(ECHUIM_SYSTEM_LAUNCHER) { |
| 83 memset(&start_info_, 0, sizeof(start_info_)); |
| 84 start_info_.cb = sizeof(start_info_); |
| 85 // We need to query the user data dir of chrome so we need chrome's |
| 86 // path provider. |
| 87 chrome::RegisterPathProvider(); |
| 88 } |
| 89 |
| 90 // CommandExecuteImpl |
| 91 STDMETHODIMP CommandExecuteImpl::SetKeyState(DWORD key_state) { |
| 92 AtlTrace("In %hs\n", __FUNCTION__); |
| 93 return S_OK; |
| 94 } |
| 95 |
| 96 STDMETHODIMP CommandExecuteImpl::SetParameters(LPCWSTR params) { |
| 97 AtlTrace("In %hs [%S]\n", __FUNCTION__, params); |
| 98 if (params) { |
| 99 parameters_ = params; |
| 100 } |
| 101 return S_OK; |
| 102 } |
| 103 |
| 104 STDMETHODIMP CommandExecuteImpl::SetPosition(POINT pt) { |
| 105 AtlTrace("In %hs\n", __FUNCTION__); |
| 106 return S_OK; |
| 107 } |
| 108 |
| 109 STDMETHODIMP CommandExecuteImpl::SetShowWindow(int show) { |
| 110 AtlTrace("In %hs show=%d\n", __FUNCTION__, show); |
| 111 start_info_.wShowWindow = show; |
| 112 start_info_.dwFlags |= STARTF_USESHOWWINDOW; |
| 113 return S_OK; |
| 114 } |
| 115 |
| 116 STDMETHODIMP CommandExecuteImpl::SetNoShowUI(BOOL no_show_ui) { |
| 117 AtlTrace("In %hs no_show=%d\n", __FUNCTION__, no_show_ui); |
| 118 return S_OK; |
| 119 } |
| 120 |
| 121 STDMETHODIMP CommandExecuteImpl::SetDirectory(LPCWSTR directory) { |
| 122 AtlTrace("In %hs\n", __FUNCTION__); |
| 123 return S_OK; |
| 124 } |
| 125 |
| 126 STDMETHODIMP CommandExecuteImpl::GetValue(enum AHE_TYPE* pahe) { |
| 127 AtlTrace("In %hs\n", __FUNCTION__); |
| 128 |
| 129 if (!GetLaunchScheme(&display_name_, &launch_scheme_)) { |
| 130 AtlTrace("Failed to get scheme, E_FAIL\n"); |
| 131 return E_FAIL; |
| 132 } |
| 133 |
| 134 if (integrity_level_ == base::HIGH_INTEGRITY) { |
| 135 // Metro mode apps don't work in high integrity mode. |
| 136 AtlTrace("High integrity, AHE_DESKTOP\n"); |
| 137 *pahe = AHE_DESKTOP; |
| 138 return S_OK; |
| 139 } |
| 140 |
| 141 FilePath user_data_dir; |
| 142 if (!PathService::Get(chrome::DIR_USER_DATA, &user_data_dir)) { |
| 143 AtlTrace("Failed to get chrome's data dir path, E_FAIL\n"); |
| 144 return E_FAIL; |
| 145 } |
| 146 |
| 147 HWND chrome_window = ::FindWindowEx(HWND_MESSAGE, NULL, |
| 148 chrome::kMessageWindowClass, |
| 149 user_data_dir.value().c_str()); |
| 150 if (chrome_window) { |
| 151 AtlTrace("Found chrome window %p\n", chrome_window); |
| 152 // The failure cases below are deemed to happen due to the inherently racy |
| 153 // procedure of going from chrome's window to process handle during which |
| 154 // chrome might have exited. Failing here would probably just cause the |
| 155 // user to retry at which point we would do the right thing. |
| 156 DWORD chrome_pid = 0; |
| 157 ::GetWindowThreadProcessId(chrome_window, &chrome_pid); |
| 158 if (!chrome_pid) { |
| 159 AtlTrace("Failed to get chrome's PID, E_FAIL\n"); |
| 160 return E_FAIL; |
| 161 } |
| 162 base::win::ScopedHandle process( |
| 163 ::OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, chrome_pid)); |
| 164 if (!process.IsValid()) { |
| 165 AtlTrace("Failed to open chrome's process [%d], E_FAIL\n", chrome_pid); |
| 166 return E_FAIL; |
| 167 } |
| 168 if (IsImmersiveProcess(process.Get())) { |
| 169 AtlTrace("Chrome [%d] is inmmersive, AHE_IMMERSIVE\n", chrome_pid); |
| 170 chrome_mode_ = ECHUIM_IMMERSIVE; |
| 171 *pahe = AHE_IMMERSIVE; |
| 172 } else { |
| 173 AtlTrace("Chrome [%d] is Desktop, AHE_DESKTOP\n"); |
| 174 chrome_mode_ = ECHUIM_DESKTOP; |
| 175 *pahe = AHE_DESKTOP; |
| 176 } |
| 177 return S_OK; |
| 178 } |
| 179 |
| 180 if (IsImmersiveProcess(GetCurrentProcess())) { |
| 181 AtlTrace("Current process is inmmersive, AHE_IMMERSIVE\n"); |
| 182 *pahe = AHE_IMMERSIVE; |
| 183 return S_OK; |
| 184 } |
| 185 |
| 186 if ((launch_scheme_ == INTERNET_SCHEME_FILE) && |
| 187 (GetLaunchMode() != ECHUIM_DESKTOP)) { |
| 188 AtlTrace("INTERNET_SCHEME_FILE, mode != ECHUIM_DESKTOP, AHE_IMMERSIVE\n"); |
| 189 *pahe = AHE_IMMERSIVE; |
| 190 return S_OK; |
| 191 } |
| 192 |
| 193 AtlTrace("Fallback is AHE_DESKTOP\n"); |
| 194 *pahe = AHE_DESKTOP; |
| 195 return S_OK; |
| 196 } |
| 197 |
| 198 STDMETHODIMP CommandExecuteImpl::Execute() { |
| 199 AtlTrace("In %hs\n", __FUNCTION__); |
| 200 |
| 201 if (integrity_level_ == base::HIGH_INTEGRITY) |
| 202 return LaunchDesktopChrome(); |
| 203 |
| 204 EC_HOST_UI_MODE mode = GetLaunchMode(); |
| 205 if (mode == ECHUIM_DESKTOP) |
| 206 return LaunchDesktopChrome(); |
| 207 |
| 208 HRESULT hr = E_FAIL; |
| 209 CComPtr<IApplicationActivationManager> activation_manager; |
| 210 hr = activation_manager.CoCreateInstance(CLSID_ApplicationActivationManager); |
| 211 if (!activation_manager) { |
| 212 AtlTrace("Failed to get the activation manager, error 0x%x\n", hr); |
| 213 return S_OK; |
| 214 } |
| 215 |
| 216 string16 app_id = delegate_execute::GetAppId(chrome_exe_); |
| 217 |
| 218 DWORD pid = 0; |
| 219 if (launch_scheme_ == INTERNET_SCHEME_FILE) { |
| 220 AtlTrace("Activating for file\n"); |
| 221 hr = activation_manager->ActivateApplication(app_id.c_str(), |
| 222 verb_.c_str(), |
| 223 AO_NOERRORUI, |
| 224 &pid); |
| 225 } else { |
| 226 AtlTrace("Activating for protocol\n"); |
| 227 hr = activation_manager->ActivateForProtocol(app_id.c_str(), |
| 228 item_array_, |
| 229 &pid); |
| 230 } |
| 231 if (hr == E_APPLICATION_NOT_REGISTERED) { |
| 232 AtlTrace("Metro chrome is not registered, launching in desktop\n"); |
| 233 return LaunchDesktopChrome(); |
| 234 } |
| 235 AtlTrace("Metro Chrome launch, pid=%d, returned 0x%x\n", pid, hr); |
| 236 return S_OK; |
| 237 } |
| 238 |
| 239 STDMETHODIMP CommandExecuteImpl::Initialize(LPCWSTR name, |
| 240 IPropertyBag* bag) { |
| 241 AtlTrace("In %hs\n", __FUNCTION__); |
| 242 if (!FindChromeExe(&chrome_exe_)) |
| 243 return E_FAIL; |
| 244 delegate_execute::UpdateChromeIfNeeded(chrome_exe_); |
| 245 if (name) { |
| 246 AtlTrace("Verb is %S\n", name); |
| 247 verb_ = name; |
| 248 } |
| 249 |
| 250 base::GetProcessIntegrityLevel(base::GetCurrentProcessHandle(), |
| 251 &integrity_level_); |
| 252 if (integrity_level_ == base::HIGH_INTEGRITY) { |
| 253 AtlTrace("Delegate execute launched in high integrity level\n"); |
| 254 } |
| 255 return S_OK; |
| 256 } |
| 257 |
| 258 STDMETHODIMP CommandExecuteImpl::SetSelection(IShellItemArray* item_array) { |
| 259 AtlTrace("In %hs\n", __FUNCTION__); |
| 260 item_array_ = item_array; |
| 261 return S_OK; |
| 262 } |
| 263 |
| 264 STDMETHODIMP CommandExecuteImpl::GetSelection(REFIID riid, void** selection) { |
| 265 AtlTrace("In %hs\n", __FUNCTION__); |
| 266 return S_OK; |
| 267 } |
| 268 |
| 269 STDMETHODIMP CommandExecuteImpl::AllowForegroundTransfer(void* reserved) { |
| 270 AtlTrace("In %hs\n", __FUNCTION__); |
| 271 return S_OK; |
| 272 } |
| 273 |
| 274 // Returns false if chrome.exe cannot be found. |
| 275 // static |
| 276 bool CommandExecuteImpl::FindChromeExe(FilePath* chrome_exe) { |
| 277 AtlTrace("In %hs\n", __FUNCTION__); |
| 278 // Look for chrome.exe one folder above delegate_execute.exe (as expected in |
| 279 // Chrome installs). Failing that, look for it alonside delegate_execute.exe. |
| 280 FilePath dir_exe; |
| 281 if (!PathService::Get(base::DIR_EXE, &dir_exe)) { |
| 282 AtlTrace("Failed to get current exe path\n"); |
| 283 return false; |
| 284 } |
| 285 |
| 286 *chrome_exe = dir_exe.DirName().Append(chrome::kBrowserProcessExecutableName); |
| 287 if (!file_util::PathExists(*chrome_exe)) { |
| 288 *chrome_exe = dir_exe.Append(chrome::kBrowserProcessExecutableName); |
| 289 if (!file_util::PathExists(*chrome_exe)) { |
| 290 AtlTrace("Failed to find chrome exe file\n"); |
| 291 return false; |
| 292 } |
| 293 } |
| 294 |
| 295 AtlTrace("Got chrome exe path as %ls\n", chrome_exe->value().c_str()); |
| 296 return true; |
| 297 } |
| 298 |
| 299 bool CommandExecuteImpl::GetLaunchScheme( |
| 300 string16* display_name, INTERNET_SCHEME* scheme) { |
| 301 if (!item_array_) |
| 302 return false; |
| 303 |
| 304 ATLASSERT(display_name); |
| 305 ATLASSERT(scheme); |
| 306 |
| 307 DWORD count = 0; |
| 308 item_array_->GetCount(&count); |
| 309 |
| 310 if (count != 1) { |
| 311 AtlTrace("Cannot handle %d elements in the IShellItemArray\n", count); |
| 312 return false; |
| 313 } |
| 314 |
| 315 CComPtr<IEnumShellItems> items; |
| 316 item_array_->EnumItems(&items); |
| 317 CComPtr<IShellItem> shell_item; |
| 318 HRESULT hr = items->Next(1, &shell_item, &count); |
| 319 if (hr != S_OK) { |
| 320 AtlTrace("Failed to read element from the IShellItemsArray\n"); |
| 321 return false; |
| 322 } |
| 323 |
| 324 base::win::ScopedCoMem<wchar_t> name; |
| 325 hr = shell_item->GetDisplayName(SIGDN_URL, &name); |
| 326 if (hr != S_OK) { |
| 327 AtlTrace("Failed to get display name\n"); |
| 328 return false; |
| 329 } |
| 330 |
| 331 AtlTrace("Display name is [%ls]\n", name); |
| 332 |
| 333 wchar_t scheme_name[16]; |
| 334 URL_COMPONENTS components = {0}; |
| 335 components.lpszScheme = scheme_name; |
| 336 components.dwSchemeLength = sizeof(scheme_name)/sizeof(scheme_name[0]); |
| 337 |
| 338 components.dwStructSize = sizeof(components); |
| 339 if (!InternetCrackUrlW(name, 0, 0, &components)) { |
| 340 AtlTrace("Failed to crack url %ls\n", name); |
| 341 return false; |
| 342 } |
| 343 |
| 344 AtlTrace("Launch scheme is [%ls] (%d)\n", scheme_name, components.nScheme); |
| 345 *display_name = name; |
| 346 *scheme = components.nScheme; |
| 347 return true; |
| 348 } |
| 349 |
| 350 HRESULT CommandExecuteImpl::LaunchDesktopChrome() { |
| 351 AtlTrace("In %hs\n", __FUNCTION__); |
| 352 string16 display_name = display_name_; |
| 353 |
| 354 switch (launch_scheme_) { |
| 355 case INTERNET_SCHEME_FILE: |
| 356 // If anything other than chrome.exe is passed in the display name we |
| 357 // should honor it. For e.g. If the user clicks on a html file when |
| 358 // chrome is the default we should treat it as a parameter to be passed |
| 359 // to chrome. |
| 360 if (display_name.find(L"chrome.exe") != string16::npos) |
| 361 display_name.clear(); |
| 362 break; |
| 363 |
| 364 default: |
| 365 break; |
| 366 } |
| 367 |
| 368 string16 command_line = L"\""; |
| 369 command_line += chrome_exe_.value(); |
| 370 command_line += L"\""; |
| 371 |
| 372 if (!parameters_.empty()) { |
| 373 AtlTrace("Adding parameters %ls to command line\n", parameters_.c_str()); |
| 374 command_line += L" "; |
| 375 command_line += parameters_.c_str(); |
| 376 } |
| 377 |
| 378 if (!display_name.empty()) { |
| 379 command_line += L" -- "; |
| 380 command_line += display_name; |
| 381 } |
| 382 |
| 383 AtlTrace("Formatted command line is %ls\n", command_line.c_str()); |
| 384 |
| 385 PROCESS_INFORMATION proc_info = {0}; |
| 386 BOOL ret = CreateProcess(NULL, const_cast<LPWSTR>(command_line.c_str()), |
| 387 NULL, NULL, FALSE, 0, NULL, NULL, &start_info_, |
| 388 &proc_info); |
| 389 if (ret) { |
| 390 AtlTrace("Process id is %d\n", proc_info.dwProcessId); |
| 391 CloseHandle(proc_info.hProcess); |
| 392 CloseHandle(proc_info.hThread); |
| 393 } else { |
| 394 AtlTrace("Process launch failed, error %d\n", ::GetLastError()); |
| 395 } |
| 396 |
| 397 return S_OK; |
| 398 } |
| 399 |
| 400 EC_HOST_UI_MODE CommandExecuteImpl::GetLaunchMode() { |
| 401 // We are to return chrome's mode if chrome exists else we query our embedder |
| 402 // IServiceProvider and learn the mode from them. |
| 403 static bool launch_mode_determined = false; |
| 404 static EC_HOST_UI_MODE launch_mode = ECHUIM_DESKTOP; |
| 405 |
| 406 const char* modes[] = { "Desktop", "Inmmersive", "SysLauncher", "??" }; |
| 407 |
| 408 if (launch_mode_determined) |
| 409 return launch_mode; |
| 410 |
| 411 if (chrome_mode_ != ECHUIM_SYSTEM_LAUNCHER) { |
| 412 launch_mode = chrome_mode_; |
| 413 AtlTrace("Launch mode is that of chrome, %s\n", modes[launch_mode]); |
| 414 launch_mode_determined = true; |
| 415 return launch_mode; |
| 416 } |
| 417 |
| 418 if (parameters_ == ASCIIToWide(switches::kForceImmersive)) { |
| 419 launch_mode = ECHUIM_IMMERSIVE; |
| 420 AtlTrace("Launch mode forced to %s\n", modes[launch_mode]); |
| 421 launch_mode_determined = true; |
| 422 return launch_mode; |
| 423 } |
| 424 |
| 425 CComPtr<IExecuteCommandHost> host; |
| 426 CComQIPtr<IServiceProvider> service_provider = m_spUnkSite; |
| 427 if (service_provider) { |
| 428 service_provider->QueryService(IID_IExecuteCommandHost, &host); |
| 429 if (host) { |
| 430 host->GetUIMode(&launch_mode); |
| 431 } else { |
| 432 AtlTrace("Failed to get IID_IExecuteCommandHost. Assuming desktop\n"); |
| 433 } |
| 434 } else { |
| 435 AtlTrace("Failed to get IServiceProvider. Assuming desktop mode\n"); |
| 436 } |
| 437 AtlTrace("Launch mode is %s\n", modes[launch_mode]); |
| 438 launch_mode_determined = true; |
| 439 return launch_mode; |
| 440 } |
OLD | NEW |