This source file includes following definitions.
- LocalizeUrl
- GetWelcomeBackUrl
- FileTimeToHours
- GetDirectoryWriteTimeInHours
- GetDirectoryWriteAgeInHours
- LaunchSetup
- FixDACLsForExecute
- LaunchSetupAsConsoleUser
- SetClient
- CreateExperimentDetails
- LaunchBrowserUserExperiment
- InactiveUserToastExperiment
#include "chrome/installer/util/user_experiment.h"
#include <windows.h>
#include <sddl.h>
#include <wtsapi32.h>
#include <vector>
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/process/launch.h"
#include "base/rand_util.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/win/scoped_handle.h"
#include "base/win/windows_version.h"
#include "chrome/common/attrition_experiments.h"
#include "chrome/common/chrome_result_codes.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/installer/util/browser_distribution.h"
#include "chrome/installer/util/google_update_constants.h"
#include "chrome/installer/util/google_update_settings.h"
#include "chrome/installer/util/helper.h"
#include "chrome/installer/util/install_util.h"
#include "chrome/installer/util/product.h"
#include "content/public/common/result_codes.h"
#pragma comment(lib, "wtsapi32.lib")
namespace installer {
namespace {
const wchar_t kToastExpControlGroup[] = L"01";
const wchar_t kToastExpCancelGroup[] = L"02";
const wchar_t kToastExpUninstallGroup[] = L"04";
const wchar_t kToastExpTriesOkGroup[] = L"18";
const wchar_t kToastExpTriesErrorGroup[] = L"28";
const wchar_t kToastActiveGroup[] = L"40";
const wchar_t kToastUDDirFailure[] = L"40";
const wchar_t kToastExpBaseGroup[] = L"80";
base::string16 LocalizeUrl(const wchar_t* url) {
base::string16 language;
if (!GoogleUpdateSettings::GetLanguage(&language))
language = L"en-US";
return ReplaceStringPlaceholders(url, language.c_str(), NULL);
}
base::string16 GetWelcomeBackUrl() {
const wchar_t kWelcomeUrl[] = L"http://www.google.com/chrome/intl/$1/"
L"welcomeback-new.html";
return LocalizeUrl(kWelcomeUrl);
}
int FileTimeToHours(const FILETIME& time) {
const ULONGLONG k100sNanoSecsToHours = 10000000LL * 60 * 60;
ULARGE_INTEGER uli = {time.dwLowDateTime, time.dwHighDateTime};
return static_cast<int>(uli.QuadPart / k100sNanoSecsToHours);
}
int GetDirectoryWriteTimeInHours(const wchar_t* path) {
DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
base::win::ScopedHandle file(::CreateFileW(path, 0, share, NULL,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL));
if (!file.IsValid())
return -1;
FILETIME time;
return ::GetFileTime(file, NULL, NULL, &time) ? FileTimeToHours(time) : -1;
}
int GetDirectoryWriteAgeInHours(const wchar_t* path) {
int dir_time = GetDirectoryWriteTimeInHours(path);
if (dir_time < 0)
return dir_time;
FILETIME time;
GetSystemTimeAsFileTime(&time);
int now_time = FileTimeToHours(time);
if (dir_time >= now_time)
return 0;
return (now_time - dir_time);
}
bool LaunchSetup(CommandLine* cmd_line, bool system_level_toast) {
const CommandLine& current_cmd_line = *CommandLine::ForCurrentProcess();
if (current_cmd_line.HasSwitch(switches::kVerboseLogging))
cmd_line->AppendSwitch(switches::kVerboseLogging);
if (system_level_toast) {
cmd_line->AppendSwitch(switches::kSystemLevel);
cmd_line->AppendSwitch(switches::kSystemLevelToast);
std::string key(switches::kToastResultsKey);
std::string toast_key = current_cmd_line.GetSwitchValueASCII(key);
if (!toast_key.empty()) {
cmd_line->AppendSwitchASCII(key, toast_key);
base::LaunchOptions options;
options.inherit_handles = true;
return base::LaunchProcess(*cmd_line, options, NULL);
}
}
return base::LaunchProcess(*cmd_line, base::LaunchOptions(), NULL);
}
bool FixDACLsForExecute(const base::FilePath& exe) {
char buff[1024];
DWORD len = sizeof(buff);
PSECURITY_DESCRIPTOR sd = reinterpret_cast<PSECURITY_DESCRIPTOR>(buff);
if (!::GetFileSecurityW(exe.value().c_str(), DACL_SECURITY_INFORMATION,
sd, len, &len)) {
return false;
}
wchar_t* sddl = 0;
if (!::ConvertSecurityDescriptorToStringSecurityDescriptorW(sd,
SDDL_REVISION_1, DACL_SECURITY_INFORMATION, &sddl, NULL))
return false;
base::string16 new_sddl(sddl);
::LocalFree(sddl);
sd = NULL;
const wchar_t kAllowACE[] = L"(A;;GRGX;;;AU)";
if (base::string16::npos != new_sddl.find(L";AU)"))
return false;
size_t pos_insert = new_sddl.find(L"(");
if (base::string16::npos == pos_insert)
return false;
new_sddl.insert(pos_insert, kAllowACE);
if (!::ConvertStringSecurityDescriptorToSecurityDescriptorW(new_sddl.c_str(),
SDDL_REVISION_1, &sd, NULL))
return false;
bool rv = ::SetFileSecurityW(exe.value().c_str(), DACL_SECURITY_INFORMATION,
sd) == TRUE;
::LocalFree(sd);
return rv;
}
bool LaunchSetupAsConsoleUser(CommandLine* cmd_line) {
cmd_line->AppendSwitch(switches::kSystemLevel);
if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kVerboseLogging))
cmd_line->AppendSwitch(switches::kVerboseLogging);
int key = GoogleUpdateSettings::DuplicateGoogleUpdateSystemClientKey();
cmd_line->AppendSwitchASCII(switches::kToastResultsKey,
base::IntToString(key));
if (base::win::GetVersion() > base::win::VERSION_XP) {
if (!FixDACLsForExecute(cmd_line->GetProgram()))
NOTREACHED();
}
DWORD console_id = ::WTSGetActiveConsoleSessionId();
if (console_id == 0xFFFFFFFF) {
PLOG(ERROR) << __FUNCTION__ << " failed to get active session id";
return false;
}
HANDLE user_token;
if (!::WTSQueryUserToken(console_id, &user_token)) {
PLOG(ERROR) << __FUNCTION__ << " failed to get user token for console_id "
<< console_id;
return false;
}
base::LaunchOptions options;
options.as_user = user_token;
options.inherit_handles = true;
options.empty_desktop_name = true;
VLOG(1) << __FUNCTION__ << " launching " << cmd_line->GetCommandLineString();
bool launched = base::LaunchProcess(*cmd_line, options, NULL);
::CloseHandle(user_token);
VLOG(1) << __FUNCTION__ << " result: " << launched;
return launched;
}
void SetClient(const base::string16& experiment_group, bool last_write) {
static int reg_key_handle = -1;
if (reg_key_handle == -1) {
const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
if (cmd_line.HasSwitch(switches::kToastResultsKey)) {
base::StringToInt(
cmd_line.GetSwitchValueNative(switches::kToastResultsKey),
®_key_handle);
} else {
reg_key_handle = 0;
}
}
if (reg_key_handle) {
GoogleUpdateSettings::WriteGoogleUpdateSystemClientKey(
reg_key_handle, google_update::kRegClientField, experiment_group);
if (last_write)
CloseHandle((HANDLE) reg_key_handle);
} else {
GoogleUpdateSettings::SetClient(experiment_group);
}
}
}
bool CreateExperimentDetails(int flavor, ExperimentDetails* experiment) {
struct FlavorDetails {
int heading_id;
int flags;
};
static const int kMax = 4;
using namespace attrition_experiments;
static const struct UserExperimentSpecs {
const wchar_t* locale;
const wchar_t* brands;
int control_group;
const wchar_t* prefix;
FlavorDetails flavors[kMax];
} kExperiments[] = {
{ L"*", L"GGRV",
0,
L"EA",
{ { 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 }
}
},
{ L"*", L"*",
5,
L"DA",
{ { IDS_TRY_TOAST_HEADING3, kToastUiMakeDefault },
{ 0, 0 },
{ 0, 0 },
{ 0, 0 }
}
}
};
base::string16 locale;
GoogleUpdateSettings::GetLanguage(&locale);
if (locale.empty() || (locale == base::ASCIIToWide("en")))
locale = base::ASCIIToWide("en-US");
base::string16 brand;
if (!GoogleUpdateSettings::GetBrand(&brand))
brand = base::ASCIIToWide("");
for (int i = 0; i < arraysize(kExperiments); ++i) {
if (kExperiments[i].locale != locale &&
kExperiments[i].locale != base::ASCIIToWide("*"))
continue;
std::vector<base::string16> brand_codes;
base::SplitString(kExperiments[i].brands, L',', &brand_codes);
if (brand_codes.empty())
return false;
for (std::vector<base::string16>::iterator it = brand_codes.begin();
it != brand_codes.end(); ++it) {
if (*it != brand && *it != L"*")
continue;
const UserExperimentSpecs& match = kExperiments[i];
int num_flavors = 0;
while (match.flavors[num_flavors].heading_id) { ++num_flavors; }
if (!num_flavors)
return false;
if (flavor < 0)
flavor = base::RandInt(0, num_flavors - 1);
experiment->flavor = flavor;
experiment->heading = match.flavors[flavor].heading_id;
experiment->control_group = match.control_group;
const wchar_t prefix[] = { match.prefix[0], match.prefix[1] + flavor, 0 };
experiment->prefix = prefix;
experiment->flags = match.flavors[flavor].flags;
return true;
}
}
return false;
}
void LaunchBrowserUserExperiment(const CommandLine& base_cmd_line,
InstallStatus status,
bool system_level) {
if (system_level) {
if (NEW_VERSION_UPDATED == status) {
CommandLine cmd_line(base_cmd_line);
cmd_line.AppendSwitch(switches::kSystemLevelToast);
LaunchSetupAsConsoleUser(&cmd_line);
return;
}
} else {
if (status != NEW_VERSION_UPDATED && status != REENTRY_SYS_UPDATE) {
return;
}
}
ExperimentDetails experiment;
if (!CreateExperimentDetails(-1, &experiment)) {
VLOG(1) << "Failed to get experiment details.";
return;
}
int flavor = experiment.flavor;
base::string16 base_group = experiment.prefix;
base::string16 brand;
if (GoogleUpdateSettings::GetBrand(&brand) && (brand == L"CHXX")) {
VLOG(1) << "Experiment qualification bypass";
} else {
base::string16 client;
GoogleUpdateSettings::GetClient(&client);
if (client.size() > 2) {
if (base_group == client.substr(0, 2)) {
VLOG(1) << "User already participated in this experiment";
return;
}
}
BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution(
BrowserDistribution::CHROME_BROWSER);
base::FilePath user_data_dir(GetChromeUserDataPath(dist));
const bool experiment_enabled = false;
const int kThirtyDays = 30 * 24;
int dir_age_hours = GetDirectoryWriteAgeInHours(
user_data_dir.value().c_str());
if (!experiment_enabled) {
VLOG(1) << "Toast experiment is disabled.";
return;
} else if (dir_age_hours < 0) {
SetClient(base_group + kToastUDDirFailure, true);
return;
} else if (dir_age_hours < kThirtyDays) {
VLOG(1) << "Chrome used in last " << dir_age_hours << " hours";
SetClient(base_group + kToastActiveGroup, true);
return;
}
double control_group = 1.0 * (100 - experiment.control_group) / 100;
if (base::RandDouble() > control_group) {
SetClient(base_group + kToastExpControlGroup, true);
VLOG(1) << "User is control group";
return;
}
}
VLOG(1) << "User drafted for toast experiment " << flavor;
SetClient(base_group + kToastExpBaseGroup, false);
CommandLine cmd_line(base_cmd_line);
cmd_line.AppendSwitchASCII(switches::kInactiveUserToast,
base::IntToString(flavor));
cmd_line.AppendSwitchASCII(switches::kExperimentGroup,
base::UTF16ToASCII(base_group));
LaunchSetup(&cmd_line, system_level);
}
void InactiveUserToastExperiment(int flavor,
const base::string16& experiment_group,
const Product& product,
const base::FilePath& application_path) {
CommandLine options(CommandLine::NO_PROGRAM);
options.AppendSwitchNative(::switches::kTryChromeAgain,
base::IntToString16(flavor));
base::string16 url(GetWelcomeBackUrl());
options.AppendArg("--");
options.AppendArgNative(url);
DCHECK_NE(base::string16::npos,
options.GetCommandLineString().find(L" -- " + url));
int32 exit_code = 0;
if (!product.LaunchChromeAndWait(application_path, options, &exit_code))
return;
const wchar_t* outcome = NULL;
switch (exit_code) {
case content::RESULT_CODE_NORMAL_EXIT:
outcome = kToastExpTriesOkGroup;
break;
case chrome::RESULT_CODE_NORMAL_EXIT_CANCEL:
outcome = kToastExpCancelGroup;
break;
case chrome::RESULT_CODE_NORMAL_EXIT_EXP2:
outcome = kToastExpUninstallGroup;
break;
default:
outcome = kToastExpTriesErrorGroup;
};
SetClient(experiment_group + outcome, true);
if (outcome != kToastExpUninstallGroup)
return;
bool system_level_toast = CommandLine::ForCurrentProcess()->HasSwitch(
switches::kSystemLevelToast);
CommandLine cmd(InstallUtil::GetChromeUninstallCmd(
system_level_toast, product.distribution()->GetType()));
base::LaunchProcess(cmd, base::LaunchOptions(), NULL);
}
}