This source file includes following definitions.
- ProfileToKeyName
- WillLaunchAtLoginWithSwitch
- AutoStartRequested
- CheckAndRemoveDeprecatedBackgroundModeSwitch
- SetWillLaunchAtLogin
- DisableAllAutoStartFeatures
- EnableForegroundStartAtLogin
- DisableForegroundStartAtLogin
- EnableBackgroundStartAtLogin
- DisableBackgroundStartAtLogin
#include "chrome/installer/util/auto_launch_util.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/win_util.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/product.h"
#include "chrome/installer/util/util_constants.h"
#include "crypto/sha2.h"
using base::ASCIIToUTF16;
using base::ASCIIToWide;
namespace auto_launch_util {
const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch";
enum FlagSetting {
FLAG_DISABLE,
FLAG_ENABLE,
FLAG_PRESERVE,
};
base::string16 ProfileToKeyName(const base::string16& profile_directory) {
base::FilePath path;
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kUserDataDir)) {
path = command_line.GetSwitchValuePath(switches::kUserDataDir);
} else {
BrowserDistribution* distribution =
BrowserDistribution::GetSpecificDistribution(
BrowserDistribution::CHROME_BROWSER);
installer::Product product(distribution);
path = product.GetUserDataPath();
}
path = path.Append(profile_directory);
std::string input(path.AsUTF8Unsafe());
uint8 hash[16];
crypto::SHA256HashString(input, hash, sizeof(hash));
std::string hash_string = base::HexEncode(hash, sizeof(hash));
return base::string16(kAutolaunchKeyValue) + ASCIIToWide("_") +
ASCIIToWide(hash_string);
}
bool WillLaunchAtLoginWithSwitch(const base::FilePath& application_path,
const base::string16& profile_directory,
const std::string& command_line_switch) {
base::string16 key_name(ProfileToKeyName(profile_directory));
base::string16 autolaunch;
if (!base::win::ReadCommandFromAutoRun(
HKEY_CURRENT_USER, key_name, &autolaunch)) {
return false;
}
base::FilePath chrome_exe(application_path);
if (chrome_exe.empty()) {
if (!PathService::Get(base::DIR_EXE, &chrome_exe)) {
NOTREACHED();
return false;
}
}
chrome_exe = chrome_exe.Append(installer::kChromeExe);
if (autolaunch.find(chrome_exe.value()) == base::string16::npos)
return false;
return command_line_switch.empty() ||
autolaunch.find(ASCIIToUTF16(command_line_switch)) !=
base::string16::npos;
}
bool AutoStartRequested(const base::string16& profile_directory,
bool window_requested,
const base::FilePath& application_path) {
if (window_requested) {
return WillLaunchAtLoginWithSwitch(application_path,
profile_directory,
switches::kAutoLaunchAtStartup);
} else {
return WillLaunchAtLoginWithSwitch(application_path,
ASCIIToUTF16(chrome::kInitialProfile),
switches::kNoStartupWindow);
}
}
bool CheckAndRemoveDeprecatedBackgroundModeSwitch() {
base::string16 chromium = ASCIIToUTF16("chromium");
base::string16 value;
if (base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER, chromium, &value)) {
if (value.find(ASCIIToUTF16(switches::kNoStartupWindow)) !=
base::string16::npos) {
base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, chromium);
return true;
}
}
return false;
}
void SetWillLaunchAtLogin(const base::FilePath& application_path,
const base::string16& profile_directory,
FlagSetting foreground_mode,
FlagSetting background_mode) {
if (CheckAndRemoveDeprecatedBackgroundModeSwitch()) {
if (profile_directory == ASCIIToUTF16(chrome::kInitialProfile) &&
background_mode == FLAG_PRESERVE) {
background_mode = FLAG_ENABLE;
} else {
NOTREACHED();
}
}
base::string16 key_name(ProfileToKeyName(profile_directory));
bool in_foreground =
foreground_mode == FLAG_ENABLE ||
(foreground_mode == FLAG_PRESERVE &&
WillLaunchAtLoginWithSwitch(application_path,
profile_directory,
switches::kAutoLaunchAtStartup));
bool in_background =
background_mode == FLAG_ENABLE ||
(background_mode == FLAG_PRESERVE &&
WillLaunchAtLoginWithSwitch(application_path,
profile_directory,
switches::kNoStartupWindow));
if (in_foreground || in_background) {
base::FilePath path(application_path);
if (path.empty()) {
if (!PathService::Get(base::DIR_EXE, &path)) {
NOTREACHED();
return;
}
}
base::string16 cmd_line = ASCIIToUTF16("\"");
cmd_line += path.value();
cmd_line += ASCIIToUTF16("\\");
cmd_line += installer::kChromeExe;
cmd_line += ASCIIToUTF16("\"");
if (in_background) {
cmd_line += ASCIIToUTF16(" --");
cmd_line += ASCIIToUTF16(switches::kNoStartupWindow);
}
if (in_foreground) {
cmd_line += ASCIIToUTF16(" --");
cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup);
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kUserDataDir)) {
cmd_line += ASCIIToUTF16(" --");
cmd_line += ASCIIToUTF16(switches::kUserDataDir);
cmd_line += ASCIIToUTF16("=\"");
cmd_line +=
command_line.GetSwitchValuePath(switches::kUserDataDir).value();
cmd_line += ASCIIToUTF16("\"");
}
cmd_line += ASCIIToUTF16(" --");
cmd_line += ASCIIToUTF16(switches::kProfileDirectory);
cmd_line += ASCIIToUTF16("=\"");
cmd_line += profile_directory;
cmd_line += ASCIIToUTF16("\"");
}
base::win::AddCommandToAutoRun(
HKEY_CURRENT_USER, key_name, cmd_line);
} else {
base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name);
}
}
void DisableAllAutoStartFeatures(const base::string16& profile_directory) {
DisableForegroundStartAtLogin(profile_directory);
DisableBackgroundStartAtLogin();
}
void EnableForegroundStartAtLogin(const base::string16& profile_directory,
const base::FilePath& application_path) {
SetWillLaunchAtLogin(
application_path, profile_directory, FLAG_ENABLE, FLAG_PRESERVE);
}
void DisableForegroundStartAtLogin(const base::string16& profile_directory) {
SetWillLaunchAtLogin(
base::FilePath(), profile_directory, FLAG_DISABLE, FLAG_PRESERVE);
}
void EnableBackgroundStartAtLogin() {
SetWillLaunchAtLogin(base::FilePath(),
ASCIIToUTF16(chrome::kInitialProfile),
FLAG_PRESERVE,
FLAG_ENABLE);
}
void DisableBackgroundStartAtLogin() {
SetWillLaunchAtLogin(base::FilePath(),
ASCIIToUTF16(chrome::kInitialProfile),
FLAG_PRESERVE,
FLAG_DISABLE);
}
}