This source file includes following definitions.
- GetUrlFromShellItem
- LaunchChromeBrowserProcess
- integrity_level_
- SetKeyState
- SetParameters
- SetPosition
- SetShowWindow
- SetNoShowUI
- SetDirectory
- GetValue
- Initialize
- SetSelection
- GetSelection
- AllowForegroundTransfer
- FindChromeExe
- GetLaunchScheme
- LaunchDesktopChrome
- GetLaunchMode
#include "win8/delegate_execute/command_execute_impl.h"
#include <shlguid.h>
#include "base/file_util.h"
#include "base/path_service.h"
#include "base/process/launch.h"
#include "base/process/process_handle.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/message_window.h"
#include "base/win/registry.h"
#include "base/win/scoped_co_mem.h"
#include "base/win/scoped_handle.h"
#include "base/win/scoped_process_information.h"
#include "base/win/win_util.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/install_util.h"
#include "chrome/installer/util/shell_util.h"
#include "chrome/installer/util/util_constants.h"
#include "ui/base/clipboard/clipboard_util_win.h"
#include "win8/delegate_execute/chrome_util.h"
#include "win8/delegate_execute/delegate_execute_util.h"
#include "win8/viewer/metro_viewer_constants.h"
namespace {
HRESULT GetUrlFromShellItem(IShellItem* shell_item, base::string16* url) {
DCHECK(shell_item);
DCHECK(url);
CComPtr<IDataObject> object;
HRESULT hr = shell_item->BindToHandler(NULL,
BHID_DataObject,
IID_IDataObject,
reinterpret_cast<void**>(&object));
if (SUCCEEDED(hr)) {
DCHECK(object);
if (ui::ClipboardUtil::GetPlainText(object, url))
return S_OK;
}
base::win::ScopedCoMem<wchar_t> name;
hr = shell_item->GetDisplayName(SIGDN_URL, &name);
if (hr != S_OK) {
AtlTrace("Failed to get display name\n");
return hr;
}
*url = static_cast<const wchar_t*>(name);
AtlTrace("Retrieved url from display name %ls\n", url->c_str());
return S_OK;
}
bool LaunchChromeBrowserProcess() {
base::FilePath delegate_exe_path;
if (!PathService::Get(base::FILE_EXE, &delegate_exe_path))
return false;
base::FilePath chrome_exe_path =
delegate_exe_path.DirName()
.DirName()
.Append(chrome::kBrowserProcessExecutableName);
if (!base::PathExists(chrome_exe_path)) {
chrome_exe_path =
delegate_exe_path.DirName()
.Append(chrome::kBrowserProcessExecutableName);
}
if (!base::PathExists(chrome_exe_path)) {
AtlTrace("Could not locate chrome.exe at: %ls\n",
chrome_exe_path.value().c_str());
return false;
}
CommandLine cl(chrome_exe_path);
cl.AppendSwitch(switches::kSilentLaunch);
cl.AppendSwitch(switches::kViewerConnect);
base::LaunchOptions launch_options;
launch_options.start_hidden = true;
return base::LaunchProcess(cl, launch_options, NULL);
}
}
bool CommandExecuteImpl::path_provider_initialized_ = false;
CommandExecuteImpl::CommandExecuteImpl()
: parameters_(CommandLine::NO_PROGRAM),
launch_scheme_(INTERNET_SCHEME_DEFAULT),
integrity_level_(base::INTEGRITY_UNKNOWN) {
memset(&start_info_, 0, sizeof(start_info_));
start_info_.cb = sizeof(start_info_);
if (!path_provider_initialized_) {
chrome::RegisterPathProvider();
path_provider_initialized_ = true;
}
}
STDMETHODIMP CommandExecuteImpl::SetKeyState(DWORD key_state) {
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::SetParameters(LPCWSTR params) {
parameters_ = delegate_execute::CommandLineFromParameters(params);
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::SetPosition(POINT pt) {
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::SetShowWindow(int show) {
start_info_.wShowWindow = show;
start_info_.dwFlags |= STARTF_USESHOWWINDOW;
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::SetNoShowUI(BOOL no_show_ui) {
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::SetDirectory(LPCWSTR directory) {
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::GetValue(enum AHE_TYPE* pahe) {
if (!GetLaunchScheme(&display_name_, &launch_scheme_)) {
AtlTrace("Failed to get scheme, E_FAIL\n");
return E_FAIL;
}
EC_HOST_UI_MODE mode = GetLaunchMode();
*pahe = (mode == ECHUIM_DESKTOP) ? AHE_DESKTOP : AHE_IMMERSIVE;
if (*pahe == AHE_IMMERSIVE && verb_ != win8::kMetroViewerConnectVerb)
LaunchChromeBrowserProcess();
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::Execute() {
AtlTrace("In %hs\n", __FUNCTION__);
if (integrity_level_ == base::HIGH_INTEGRITY)
return LaunchDesktopChrome();
EC_HOST_UI_MODE mode = GetLaunchMode();
if (mode == ECHUIM_DESKTOP)
return LaunchDesktopChrome();
HRESULT hr = E_FAIL;
CComPtr<IApplicationActivationManager> activation_manager;
hr = activation_manager.CoCreateInstance(CLSID_ApplicationActivationManager);
if (!activation_manager) {
AtlTrace("Failed to get the activation manager, error 0x%x\n", hr);
return S_OK;
}
BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
bool is_per_user_install = InstallUtil::IsPerUserInstall(
chrome_exe_.value().c_str());
base::string16 app_id = ShellUtil::GetBrowserModelId(
distribution, is_per_user_install);
DWORD pid = 0;
if (launch_scheme_ == INTERNET_SCHEME_FILE &&
display_name_.find(installer::kChromeExe) != base::string16::npos) {
AtlTrace("Activating for file\n");
hr = activation_manager->ActivateApplication(app_id.c_str(),
verb_.c_str(),
AO_NONE,
&pid);
} else {
AtlTrace("Activating for protocol\n");
hr = activation_manager->ActivateForProtocol(app_id.c_str(),
item_array_,
&pid);
}
if (hr == E_APPLICATION_NOT_REGISTERED) {
AtlTrace("Metro chrome is not registered, launching in desktop\n");
return LaunchDesktopChrome();
}
AtlTrace("Metro Chrome launch, pid=%d, returned 0x%x\n", pid, hr);
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::Initialize(LPCWSTR name,
IPropertyBag* bag) {
if (!FindChromeExe(&chrome_exe_))
return E_FAIL;
delegate_execute::UpdateChromeIfNeeded(chrome_exe_);
if (name) {
AtlTrace("Verb is %S\n", name);
verb_ = name;
}
base::GetProcessIntegrityLevel(base::GetCurrentProcessHandle(),
&integrity_level_);
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::SetSelection(IShellItemArray* item_array) {
item_array_ = item_array;
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::GetSelection(REFIID riid, void** selection) {
return S_OK;
}
STDMETHODIMP CommandExecuteImpl::AllowForegroundTransfer(void* reserved) {
return S_OK;
}
bool CommandExecuteImpl::FindChromeExe(base::FilePath* chrome_exe) {
base::FilePath dir_exe;
if (!PathService::Get(base::DIR_EXE, &dir_exe)) {
AtlTrace("Failed to get current exe path\n");
return false;
}
*chrome_exe = dir_exe.DirName().Append(chrome::kBrowserProcessExecutableName);
if (!base::PathExists(*chrome_exe)) {
*chrome_exe = dir_exe.Append(chrome::kBrowserProcessExecutableName);
if (!base::PathExists(*chrome_exe)) {
AtlTrace("Failed to find chrome exe file\n");
return false;
}
}
return true;
}
bool CommandExecuteImpl::GetLaunchScheme(
base::string16* display_name, INTERNET_SCHEME* scheme) {
if (!item_array_)
return false;
ATLASSERT(display_name);
ATLASSERT(scheme);
DWORD count = 0;
item_array_->GetCount(&count);
if (count != 1) {
AtlTrace("Cannot handle %d elements in the IShellItemArray\n", count);
return false;
}
CComPtr<IEnumShellItems> items;
item_array_->EnumItems(&items);
CComPtr<IShellItem> shell_item;
HRESULT hr = items->Next(1, &shell_item, &count);
if (hr != S_OK) {
AtlTrace("Failed to read element from the IShellItemsArray\n");
return false;
}
hr = GetUrlFromShellItem(shell_item, display_name);
if (FAILED(hr)) {
AtlTrace("Failed to get url. Error 0x%x\n", hr);
return false;
}
wchar_t scheme_name[16];
URL_COMPONENTS components = {0};
components.lpszScheme = scheme_name;
components.dwSchemeLength = sizeof(scheme_name)/sizeof(scheme_name[0]);
components.dwStructSize = sizeof(components);
if (!InternetCrackUrlW(display_name->c_str(), 0, 0, &components)) {
AtlTrace("Failed to crack url %ls\n", display_name->c_str());
return false;
}
AtlTrace("Launch scheme is [%ls] (%d)\n", scheme_name, components.nScheme);
*scheme = components.nScheme;
return true;
}
HRESULT CommandExecuteImpl::LaunchDesktopChrome() {
base::string16 display_name = display_name_;
switch (launch_scheme_) {
case INTERNET_SCHEME_FILE:
if (display_name.find(installer::kChromeExe) != base::string16::npos)
display_name.clear();
break;
default:
break;
}
CommandLine chrome(
delegate_execute::MakeChromeCommandLine(chrome_exe_, parameters_,
display_name));
base::string16 command_line(chrome.GetCommandLineString());
AtlTrace("Formatted command line is %ls\n", command_line.c_str());
PROCESS_INFORMATION temp_process_info = {};
BOOL ret = CreateProcess(chrome_exe_.value().c_str(),
const_cast<LPWSTR>(command_line.c_str()),
NULL, NULL, FALSE, 0, NULL, NULL, &start_info_,
&temp_process_info);
if (ret) {
base::win::ScopedProcessInformation proc_info(temp_process_info);
AtlTrace("Process id is %d\n", proc_info.process_id());
AllowSetForegroundWindow(proc_info.process_id());
} else {
AtlTrace("Process launch failed, error %d\n", ::GetLastError());
}
return S_OK;
}
EC_HOST_UI_MODE CommandExecuteImpl::GetLaunchMode() {
static bool launch_mode_determined = false;
static EC_HOST_UI_MODE launch_mode = ECHUIM_DESKTOP;
const char* modes[] = { "Desktop", "Immersive", "SysLauncher", "??" };
if (launch_mode_determined)
return launch_mode;
if (integrity_level_ == base::HIGH_INTEGRITY) {
AtlTrace("High integrity: launching in desktop mode\n");
launch_mode = ECHUIM_DESKTOP;
launch_mode_determined = true;
return launch_mode;
}
base::FilePath chrome_exe;
if (!FindChromeExe(&chrome_exe) ||
ShellUtil::GetChromeDefaultStateFromPath(chrome_exe) !=
ShellUtil::IS_DEFAULT) {
AtlTrace("Chrome is not default: launching in desktop mode\n");
launch_mode = ECHUIM_DESKTOP;
launch_mode_determined = true;
return launch_mode;
}
if (GetAsyncKeyState(VK_SHIFT) && GetAsyncKeyState(VK_F11)) {
AtlTrace("Hotkey: launching in immersive mode\n");
launch_mode = ECHUIM_IMMERSIVE;
launch_mode_determined = true;
return launch_mode;
}
if (parameters_.HasSwitch(switches::kForceImmersive)) {
launch_mode = ECHUIM_IMMERSIVE;
launch_mode_determined = true;
parameters_ = CommandLine(CommandLine::NO_PROGRAM);
} else if (parameters_.HasSwitch(switches::kForceDesktop)) {
launch_mode = ECHUIM_DESKTOP;
launch_mode_determined = true;
parameters_ = CommandLine(CommandLine::NO_PROGRAM);
}
base::win::RegKey reg_key;
LONG key_result = reg_key.Create(HKEY_CURRENT_USER,
chrome::kMetroRegistryPath,
KEY_ALL_ACCESS);
if (key_result != ERROR_SUCCESS) {
AtlTrace("Failed to open HKCU %ls key, error 0x%x\n",
chrome::kMetroRegistryPath,
key_result);
if (!launch_mode_determined) {
launch_mode = ECHUIM_DESKTOP;
launch_mode_determined = true;
}
return launch_mode;
}
if (launch_mode_determined) {
AtlTrace("Launch mode forced by cmdline to %s\n", modes[launch_mode]);
reg_key.WriteValue(chrome::kLaunchModeValue,
static_cast<DWORD>(launch_mode));
return launch_mode;
}
DWORD reg_value;
if (reg_key.ReadValueDW(chrome::kLaunchModeValue,
®_value) != ERROR_SUCCESS) {
launch_mode = ECHUIM_DESKTOP;
AtlTrace("Can't read registry, defaulting to %s\n", modes[launch_mode]);
} else if (reg_value >= ECHUIM_SYSTEM_LAUNCHER) {
AtlTrace("Invalid registry launch mode value %u\n", reg_value);
launch_mode = ECHUIM_DESKTOP;
} else {
launch_mode = static_cast<EC_HOST_UI_MODE>(reg_value);
AtlTrace("Launch mode forced by registry to %s\n", modes[launch_mode]);
}
launch_mode_determined = true;
return launch_mode;
}