This source file includes following definitions.
- GetProfileIdFromPath
- GetAppListAppName
- GetExpectedAppId
- MigrateChromiumShortcutsCallback
- GetAppForProtocolUsingAssocQuery
- GetAppForProtocolUsingRegistry
- GetDefaultWebClientStateFromShellUtilDefaultState
- CanSetAsDefaultBrowser
- SetAsDefaultBrowser
- SetAsDefaultProtocolClient
- SetAsDefaultBrowserInteractive
- SetAsDefaultProtocolClientInteractive
- GetDefaultBrowser
- IsDefaultProtocolClient
- GetApplicationNameForProtocol
- IsFirefoxDefaultBrowser
- GetAppModelIdForProfile
- GetChromiumModelIdForProfile
- GetAppListAppModelIdForProfile
- MigrateChromiumShortcuts
- MigrateShortcutsInPathInternal
- GetStartMenuShortcut
#include "chrome/browser/shell_integration.h"
#include <windows.h>
#include <shlwapi.h>
#include <shobjidl.h>
#include <propkey.h>
#include "base/bind.h"
#include "base/command_line.h"
#include "base/file_util.h"
#include "base/files/file_enumerator.h"
#include "base/message_loop/message_loop.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/registry.h"
#include "base/win/scoped_comptr.h"
#include "base/win/scoped_propvariant.h"
#include "base/win/shortcut.h"
#include "base/win/windows_version.h"
#include "chrome/browser/policy/policy_path_parser.h"
#include "chrome/browser/web_applications/web_app.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths_internal.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/installer/setup/setup_util.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/create_reg_key_work_item.h"
#include "chrome/installer/util/install_util.h"
#include "chrome/installer/util/set_reg_value_work_item.h"
#include "chrome/installer/util/shell_util.h"
#include "chrome/installer/util/util_constants.h"
#include "chrome/installer/util/work_item.h"
#include "chrome/installer/util/work_item_list.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace {
const wchar_t kAppListAppNameSuffix[] = L"AppList";
base::string16 GetProfileIdFromPath(const base::FilePath& profile_path) {
if (profile_path.empty())
return base::string16();
base::FilePath default_user_data_dir;
if (chrome::GetDefaultUserDataDirectory(&default_user_data_dir) &&
profile_path.DirName() == default_user_data_dir &&
profile_path.BaseName().value() ==
base::ASCIIToUTF16(chrome::kInitialProfile)) {
return base::string16();
}
base::string16 basenames = profile_path.DirName().BaseName().value() +
L"." + profile_path.BaseName().value();
base::string16 profile_id;
profile_id.reserve(basenames.size());
for (size_t i = 0; i < basenames.length(); ++i) {
if (IsAsciiAlpha(basenames[i]) ||
IsAsciiDigit(basenames[i]) ||
basenames[i] == L'.')
profile_id += basenames[i];
}
return profile_id;
}
base::string16 GetAppListAppName() {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
base::string16 app_name(dist->GetBaseAppId());
app_name.append(kAppListAppNameSuffix);
return app_name;
}
base::string16 GetExpectedAppId(const CommandLine& command_line,
bool is_per_user_install) {
base::FilePath user_data_dir;
if (command_line.HasSwitch(switches::kUserDataDir))
user_data_dir = command_line.GetSwitchValuePath(switches::kUserDataDir);
else
chrome::GetDefaultUserDataDirectory(&user_data_dir);
policy::path_parser::CheckUserDataDirPolicy(&user_data_dir);
DCHECK(!user_data_dir.empty());
base::FilePath profile_subdir;
if (command_line.HasSwitch(switches::kProfileDirectory)) {
profile_subdir =
command_line.GetSwitchValuePath(switches::kProfileDirectory);
} else {
profile_subdir =
base::FilePath(base::ASCIIToUTF16(chrome::kInitialProfile));
}
DCHECK(!profile_subdir.empty());
base::FilePath profile_path = user_data_dir.Append(profile_subdir);
base::string16 app_name;
if (command_line.HasSwitch(switches::kApp)) {
app_name = base::UTF8ToUTF16(web_app::GenerateApplicationNameFromURL(
GURL(command_line.GetSwitchValueASCII(switches::kApp))));
} else if (command_line.HasSwitch(switches::kAppId)) {
app_name = base::UTF8ToUTF16(
web_app::GenerateApplicationNameFromExtensionId(
command_line.GetSwitchValueASCII(switches::kAppId)));
} else if (command_line.HasSwitch(switches::kShowAppList)) {
app_name = GetAppListAppName();
} else {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
app_name = ShellUtil::GetBrowserModelId(dist, is_per_user_install);
}
DCHECK(!app_name.empty());
return ShellIntegration::GetAppModelIdForProfile(app_name, profile_path);
}
void MigrateChromiumShortcutsCallback() {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));
base::FilePath chrome_exe;
if (!PathService::Get(base::FILE_EXE, &chrome_exe))
return;
static const struct {
int location_id;
const wchar_t* sub_dir;
} kLocations[] = {
{
base::DIR_TASKBAR_PINS,
NULL
}, {
base::DIR_USER_DESKTOP,
NULL
}, {
base::DIR_START_MENU,
NULL
}, {
base::DIR_APP_DATA,
L"Microsoft\\Internet Explorer\\Quick Launch\\User Pinned\\StartMenu"
}
};
for (int i = 0; i < arraysize(kLocations); ++i) {
base::FilePath path;
if (!PathService::Get(kLocations[i].location_id, &path)) {
NOTREACHED();
continue;
}
if (kLocations[i].sub_dir)
path = path.Append(kLocations[i].sub_dir);
bool check_dual_mode = (kLocations[i].location_id == base::DIR_START_MENU);
ShellIntegration::MigrateShortcutsInPathInternal(chrome_exe, path,
check_dual_mode);
}
}
base::string16 GetAppForProtocolUsingAssocQuery(const GURL& url) {
base::string16 url_spec = base::ASCIIToWide(url.possibly_invalid_spec());
if (url_spec.empty())
return base::string16();
DCHECK(*url_spec.rbegin() == ':');
url_spec.erase(url_spec.length() - 1);
wchar_t out_buffer[1024];
DWORD buffer_size = arraysize(out_buffer);
HRESULT hr = AssocQueryString(ASSOCF_IS_PROTOCOL,
ASSOCSTR_FRIENDLYAPPNAME,
url_spec.c_str(),
NULL,
out_buffer,
&buffer_size);
if (FAILED(hr)) {
DLOG(WARNING) << "AssocQueryString failed!";
return base::string16();
}
return base::string16(out_buffer);
}
base::string16 GetAppForProtocolUsingRegistry(const GURL& url) {
base::string16 url_spec = base::ASCIIToWide(url.possibly_invalid_spec());
const base::string16 cmd_key_path =
base::ASCIIToWide(url.scheme() + "\\shell\\open\\command");
base::win::RegKey cmd_key(HKEY_CLASSES_ROOT,
cmd_key_path.c_str(),
KEY_READ);
size_t split_offset = url_spec.find(L':');
if (split_offset == base::string16::npos)
return base::string16();
const base::string16 parameters = url_spec.substr(split_offset + 1,
url_spec.length() - 1);
base::string16 application_to_launch;
if (cmd_key.ReadValue(NULL, &application_to_launch) == ERROR_SUCCESS) {
ReplaceSubstringsAfterOffset(&application_to_launch,
0,
L"%1",
parameters);
return application_to_launch;
}
return base::string16();
}
ShellIntegration::DefaultWebClientState
GetDefaultWebClientStateFromShellUtilDefaultState(
ShellUtil::DefaultState default_state) {
switch (default_state) {
case ShellUtil::NOT_DEFAULT:
return ShellIntegration::NOT_DEFAULT;
case ShellUtil::IS_DEFAULT:
return ShellIntegration::IS_DEFAULT;
default:
DCHECK_EQ(ShellUtil::UNKNOWN_DEFAULT, default_state);
return ShellIntegration::UNKNOWN_DEFAULT;
}
}
}
ShellIntegration::DefaultWebClientSetPermission
ShellIntegration::CanSetAsDefaultBrowser() {
BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
if (distribution->GetDefaultBrowserControlPolicy() !=
BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL)
return SET_DEFAULT_NOT_ALLOWED;
if (ShellUtil::CanMakeChromeDefaultUnattended())
return SET_DEFAULT_UNATTENDED;
else
return SET_DEFAULT_INTERACTIVE;
}
bool ShellIntegration::SetAsDefaultBrowser() {
base::FilePath chrome_exe;
if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
LOG(ERROR) << "Error getting app exe path";
return false;
}
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
if (!ShellUtil::MakeChromeDefault(dist, ShellUtil::CURRENT_USER,
chrome_exe.value(), true)) {
LOG(ERROR) << "Chrome could not be set as default browser.";
return false;
}
VLOG(1) << "Chrome registered as default browser.";
return true;
}
bool ShellIntegration::SetAsDefaultProtocolClient(const std::string& protocol) {
if (protocol.empty())
return false;
base::FilePath chrome_exe;
if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
LOG(ERROR) << "Error getting app exe path";
return false;
}
base::string16 wprotocol(base::UTF8ToUTF16(protocol));
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
if (!ShellUtil::MakeChromeDefaultProtocolClient(dist, chrome_exe.value(),
wprotocol)) {
LOG(ERROR) << "Chrome could not be set as default handler for "
<< protocol << ".";
return false;
}
VLOG(1) << "Chrome registered as default handler for " << protocol << ".";
return true;
}
bool ShellIntegration::SetAsDefaultBrowserInteractive() {
base::FilePath chrome_exe;
if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
NOTREACHED() << "Error getting app exe path";
return false;
}
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
if (!ShellUtil::ShowMakeChromeDefaultSystemUI(dist, chrome_exe.value())) {
LOG(ERROR) << "Failed to launch the set-default-browser Windows UI.";
return false;
}
VLOG(1) << "Set-default-browser Windows UI completed.";
return true;
}
bool ShellIntegration::SetAsDefaultProtocolClientInteractive(
const std::string& protocol) {
base::FilePath chrome_exe;
if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
NOTREACHED() << "Error getting app exe path";
return false;
}
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
base::string16 wprotocol(base::UTF8ToUTF16(protocol));
if (!ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(
dist, chrome_exe.value(), wprotocol)) {
LOG(ERROR) << "Failed to launch the set-default-client Windows UI.";
return false;
}
VLOG(1) << "Set-default-client Windows UI completed.";
return true;
}
ShellIntegration::DefaultWebClientState ShellIntegration::GetDefaultBrowser() {
return GetDefaultWebClientStateFromShellUtilDefaultState(
ShellUtil::GetChromeDefaultState());
}
ShellIntegration::DefaultWebClientState
ShellIntegration::IsDefaultProtocolClient(const std::string& protocol) {
return GetDefaultWebClientStateFromShellUtilDefaultState(
ShellUtil::GetChromeDefaultProtocolClientState(
base::UTF8ToUTF16(protocol)));
}
base::string16 ShellIntegration::GetApplicationNameForProtocol(
const GURL& url) {
if (base::win::GetVersion() >= base::win::VERSION_WIN8)
return GetAppForProtocolUsingAssocQuery(url);
else
return GetAppForProtocolUsingRegistry(url);
}
bool ShellIntegration::IsFirefoxDefaultBrowser() {
bool ff_default = false;
if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
base::string16 app_cmd;
base::win::RegKey key(HKEY_CURRENT_USER,
ShellUtil::kRegVistaUrlPrefs, KEY_READ);
if (key.Valid() && (key.ReadValue(L"Progid", &app_cmd) == ERROR_SUCCESS) &&
app_cmd == L"FirefoxURL")
ff_default = true;
} else {
base::string16 key_path(L"http");
key_path.append(ShellUtil::kRegShellOpen);
base::win::RegKey key(HKEY_CLASSES_ROOT, key_path.c_str(), KEY_READ);
base::string16 app_cmd;
if (key.Valid() && (key.ReadValue(L"", &app_cmd) == ERROR_SUCCESS) &&
base::string16::npos != StringToLowerASCII(app_cmd).find(L"firefox"))
ff_default = true;
}
return ff_default;
}
base::string16 ShellIntegration::GetAppModelIdForProfile(
const base::string16& app_name,
const base::FilePath& profile_path) {
std::vector<base::string16> components;
components.push_back(app_name);
const base::string16 profile_id(GetProfileIdFromPath(profile_path));
if (!profile_id.empty())
components.push_back(profile_id);
return ShellUtil::BuildAppModelId(components);
}
base::string16 ShellIntegration::GetChromiumModelIdForProfile(
const base::FilePath& profile_path) {
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
base::FilePath chrome_exe;
if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
NOTREACHED();
return dist->GetBaseAppId();
}
return GetAppModelIdForProfile(
ShellUtil::GetBrowserModelId(
dist, InstallUtil::IsPerUserInstall(chrome_exe.value().c_str())),
profile_path);
}
base::string16 ShellIntegration::GetAppListAppModelIdForProfile(
const base::FilePath& profile_path) {
return ShellIntegration::GetAppModelIdForProfile(
GetAppListAppName(), profile_path);
}
void ShellIntegration::MigrateChromiumShortcuts() {
if (base::win::GetVersion() < base::win::VERSION_WIN7)
return;
static const int64 kMigrateChromiumShortcutsDelaySeconds = 15;
BrowserThread::PostDelayedTask(
BrowserThread::FILE, FROM_HERE,
base::Bind(&MigrateChromiumShortcutsCallback),
base::TimeDelta::FromSeconds(kMigrateChromiumShortcutsDelaySeconds));
}
int ShellIntegration::MigrateShortcutsInPathInternal(
const base::FilePath& chrome_exe,
const base::FilePath& path,
bool check_dual_mode) {
DCHECK(base::win::GetVersion() >= base::win::VERSION_WIN7);
base::FileEnumerator shortcuts_enum(
path, false,
base::FileEnumerator::FILES, FILE_PATH_LITERAL("*.lnk"));
bool is_per_user_install =
InstallUtil::IsPerUserInstall(chrome_exe.value().c_str());
int shortcuts_migrated = 0;
base::FilePath target_path;
base::string16 arguments;
base::win::ScopedPropVariant propvariant;
for (base::FilePath shortcut = shortcuts_enum.Next(); !shortcut.empty();
shortcut = shortcuts_enum.Next()) {
if (!base::win::ResolveShortcut(shortcut, &target_path, &arguments) ||
chrome_exe != target_path) {
continue;
}
CommandLine command_line(CommandLine::FromString(base::StringPrintf(
L"\"%ls\" %ls", target_path.value().c_str(), arguments.c_str())));
base::string16 expected_app_id(
GetExpectedAppId(command_line, is_per_user_install));
if (expected_app_id.empty())
continue;
base::win::ScopedComPtr<IShellLink> shell_link;
base::win::ScopedComPtr<IPersistFile> persist_file;
if (FAILED(shell_link.CreateInstance(CLSID_ShellLink, NULL,
CLSCTX_INPROC_SERVER)) ||
FAILED(persist_file.QueryFrom(shell_link)) ||
FAILED(persist_file->Load(shortcut.value().c_str(), STGM_READ))) {
DLOG(WARNING) << "Failed loading shortcut at " << shortcut.value();
continue;
}
base::win::ShortcutProperties updated_properties;
base::win::ScopedComPtr<IPropertyStore> property_store;
propvariant.Reset();
if (FAILED(property_store.QueryFrom(shell_link)) ||
property_store->GetValue(PKEY_AppUserModel_ID,
propvariant.Receive()) != S_OK) {
NOTREACHED();
continue;
} else {
switch (propvariant.get().vt) {
case VT_EMPTY:
if (!expected_app_id.empty())
updated_properties.set_app_id(expected_app_id);
break;
case VT_LPWSTR:
if (expected_app_id != base::string16(propvariant.get().pwszVal))
updated_properties.set_app_id(expected_app_id);
break;
default:
NOTREACHED();
continue;
}
}
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
base::string16 default_chromium_model_id(
ShellUtil::GetBrowserModelId(dist, is_per_user_install));
if (check_dual_mode && expected_app_id == default_chromium_model_id) {
propvariant.Reset();
if (property_store->GetValue(PKEY_AppUserModel_IsDualMode,
propvariant.Receive()) != S_OK) {
NOTREACHED();
continue;
} else {
switch (propvariant.get().vt) {
case VT_EMPTY:
updated_properties.set_dual_mode(true);
break;
case VT_BOOL:
if (!propvariant.get().boolVal)
updated_properties.set_dual_mode(true);
break;
default:
NOTREACHED();
continue;
}
}
}
persist_file.Release();
shell_link.Release();
if (updated_properties.options &&
base::win::CreateOrUpdateShortcutLink(
shortcut, updated_properties,
base::win::SHORTCUT_UPDATE_EXISTING)) {
++shortcuts_migrated;
}
}
return shortcuts_migrated;
}
base::FilePath ShellIntegration::GetStartMenuShortcut(
const base::FilePath& chrome_exe) {
static const int kFolderIds[] = {
base::DIR_COMMON_START_MENU,
base::DIR_START_MENU,
};
BrowserDistribution* dist = BrowserDistribution::GetDistribution();
base::string16 shortcut_name(
dist->GetShortcutName(BrowserDistribution::SHORTCUT_CHROME));
base::FilePath shortcut;
size_t folder =
InstallUtil::IsPerUserInstall(chrome_exe.value().c_str()) ? 1 : 0;
for (; folder < arraysize(kFolderIds); ++folder) {
if (!PathService::Get(kFolderIds[folder], &shortcut)) {
NOTREACHED();
continue;
}
shortcut = shortcut.Append(shortcut_name).Append(shortcut_name +
installer::kLnkExt);
if (base::PathExists(shortcut))
return shortcut;
}
return base::FilePath();
}