Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: chrome/installer/util/google_chrome_distribution.cc

Issue 12321061: Pulling user experiment code from BrowserDistribution to a new class. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Reupload with --similarity=90 to prevent patch failure in try jobs. Created 7 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 // 4 //
5 // This file defines specific implementation of BrowserDistribution class for 5 // This file defines specific implementation of BrowserDistribution class for
6 // Google Chrome. 6 // Google Chrome.
7 7
8 #include "chrome/installer/util/google_chrome_distribution.h" 8 #include "chrome/installer/util/google_chrome_distribution.h"
9 9
10 #include <windows.h> 10 #include <windows.h>
11 #include <msi.h> 11 #include <msi.h>
12 #include <sddl.h>
13 #include <wtsapi32.h>
14 #include <vector>
15 12
16 #include "base/command_line.h"
17 #include "base/file_path.h" 13 #include "base/file_path.h"
18 #include "base/json/json_file_value_serializer.h" 14 #include "base/json/json_file_value_serializer.h"
19 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
20 #include "base/path_service.h" 16 #include "base/path_service.h"
21 #include "base/process_util.h"
22 #include "base/rand_util.h"
23 #include "base/string_split.h"
24 #include "base/string_util.h" 17 #include "base/string_util.h"
25 #include "base/stringprintf.h" 18 #include "base/stringprintf.h"
26 #include "base/strings/string_number_conversions.h"
27 #include "base/utf_string_conversions.h" 19 #include "base/utf_string_conversions.h"
28 #include "base/win/registry.h" 20 #include "base/win/registry.h"
29 #include "base/win/windows_version.h" 21 #include "base/win/windows_version.h"
30 #include "chrome/common/attrition_experiments.h"
31 #include "chrome/common/chrome_result_codes.h"
32 #include "chrome/common/chrome_switches.h"
33 #include "chrome/common/net/test_server_locations.h" 22 #include "chrome/common/net/test_server_locations.h"
34 #include "chrome/common/pref_names.h" 23 #include "chrome/common/pref_names.h"
35 #include "chrome/installer/util/channel_info.h" 24 #include "chrome/installer/util/channel_info.h"
36 #include "chrome/installer/util/google_update_constants.h" 25 #include "chrome/installer/util/google_update_constants.h"
37 #include "chrome/installer/util/google_update_settings.h" 26 #include "chrome/installer/util/google_update_settings.h"
38 #include "chrome/installer/util/helper.h" 27 #include "chrome/installer/util/helper.h"
39 #include "chrome/installer/util/install_util.h" 28 #include "chrome/installer/util/install_util.h"
40 #include "chrome/installer/util/l10n_string_util.h" 29 #include "chrome/installer/util/l10n_string_util.h"
41 #include "chrome/installer/util/product.h"
42 #include "chrome/installer/util/util_constants.h" 30 #include "chrome/installer/util/util_constants.h"
43 #include "chrome/installer/util/wmi.h" 31 #include "chrome/installer/util/wmi.h"
44 #include "content/public/common/result_codes.h" 32 #include "content/public/common/result_codes.h"
45 33
46 #include "installer_util_strings.h" // NOLINT 34 #include "installer_util_strings.h" // NOLINT
47 35
48 #pragma comment(lib, "wtsapi32.lib")
49
50 namespace { 36 namespace {
51 37
52 const wchar_t kChromeGuid[] = L"{8A69D345-D564-463c-AFF1-A69D9E530F96}"; 38 const wchar_t kChromeGuid[] = L"{8A69D345-D564-463c-AFF1-A69D9E530F96}";
53 const wchar_t kBrowserAppId[] = L"Chrome"; 39 const wchar_t kBrowserAppId[] = L"Chrome";
54 const wchar_t kCommandExecuteImplUuid[] = 40 const wchar_t kCommandExecuteImplUuid[] =
55 L"{5C65F4B0-3651-4514-B207-D10CB699B14B}"; 41 L"{5C65F4B0-3651-4514-B207-D10CB699B14B}";
56 42
57 // The following strings are the possible outcomes of the toast experiment
58 // as recorded in the |client| field.
59 const wchar_t kToastExpControlGroup[] = L"01";
60 const wchar_t kToastExpCancelGroup[] = L"02";
61 const wchar_t kToastExpUninstallGroup[] = L"04";
62 const wchar_t kToastExpTriesOkGroup[] = L"18";
63 const wchar_t kToastExpTriesErrorGroup[] = L"28";
64 const wchar_t kToastActiveGroup[] = L"40";
65 const wchar_t kToastUDDirFailure[] = L"40";
66 const wchar_t kToastExpBaseGroup[] = L"80";
67
68 // Substitute the locale parameter in uninstall URL with whatever 43 // Substitute the locale parameter in uninstall URL with whatever
69 // Google Update tells us is the locale. In case we fail to find 44 // Google Update tells us is the locale. In case we fail to find
70 // the locale, we use US English. 45 // the locale, we use US English.
71 string16 LocalizeUrl(const wchar_t* url) { 46 string16 LocalizeUrl(const wchar_t* url) {
72 string16 language; 47 string16 language;
73 if (!GoogleUpdateSettings::GetLanguage(&language)) 48 if (!GoogleUpdateSettings::GetLanguage(&language))
74 language = L"en-US"; // Default to US English. 49 language = L"en-US"; // Default to US English.
75 return ReplaceStringPlaceholders(url, language.c_str(), NULL); 50 return ReplaceStringPlaceholders(url, language.c_str(), NULL);
76 } 51 }
77 52
78 string16 GetUninstallSurveyUrl() { 53 string16 GetUninstallSurveyUrl() {
79 const wchar_t kSurveyUrl[] = L"http://www.google.com/support/chrome/bin/" 54 const wchar_t kSurveyUrl[] = L"http://www.google.com/support/chrome/bin/"
80 L"request.py?hl=$1&contact_type=uninstall"; 55 L"request.py?hl=$1&contact_type=uninstall";
81 return LocalizeUrl(kSurveyUrl); 56 return LocalizeUrl(kSurveyUrl);
82 } 57 }
83 58
84 string16 GetWelcomeBackUrl() {
85 const wchar_t kWelcomeUrl[] = L"http://www.google.com/chrome/intl/$1/"
86 L"welcomeback-new.html";
87 return LocalizeUrl(kWelcomeUrl);
88 }
89
90 // Converts FILETIME to hours. FILETIME times are absolute times in
91 // 100 nanosecond units. For example 5:30 pm of June 15, 2009 is 3580464.
92 int FileTimeToHours(const FILETIME& time) {
93 const ULONGLONG k100sNanoSecsToHours = 10000000LL * 60 * 60;
94 ULARGE_INTEGER uli = {time.dwLowDateTime, time.dwHighDateTime};
95 return static_cast<int>(uli.QuadPart / k100sNanoSecsToHours);
96 }
97
98 // Returns the directory last write time in hours since January 1, 1601.
99 // Returns -1 if there was an error retrieving the directory time.
100 int GetDirectoryWriteTimeInHours(const wchar_t* path) {
101 // To open a directory you need to pass FILE_FLAG_BACKUP_SEMANTICS.
102 DWORD share = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
103 HANDLE file = ::CreateFileW(path, 0, share, NULL, OPEN_EXISTING,
104 FILE_FLAG_BACKUP_SEMANTICS, NULL);
105 if (INVALID_HANDLE_VALUE == file)
106 return -1;
107 FILETIME time;
108 if (!::GetFileTime(file, NULL, NULL, &time)) {
109 ::CloseHandle(file);
110 return -1;
111 }
112
113 ::CloseHandle(file);
114 return FileTimeToHours(time);
115 }
116
117 // Returns the directory last-write time age in hours, relative to current
118 // time, so if it returns 14 it means that the directory was last written 14
119 // hours ago. Returns -1 if there was an error retrieving the directory.
120 int GetDirectoryWriteAgeInHours(const wchar_t* path) {
121 int dir_time = GetDirectoryWriteTimeInHours(path);
122 if (dir_time < 0)
123 return dir_time;
124 FILETIME time;
125 GetSystemTimeAsFileTime(&time);
126 int now_time = FileTimeToHours(time);
127 if (dir_time >= now_time)
128 return 0;
129 return (now_time - dir_time);
130 }
131
132 // Launches setup.exe (located at |setup_path|) with |cmd_line|.
133 // If system_level_toast is true, appends --system-level-toast.
134 // If handle to experiment result key was given at startup, re-add it.
135 // Does not wait for the process to terminate.
136 // |cmd_line| may be modified as a result of this call.
137 bool LaunchSetup(CommandLine* cmd_line,
138 const installer::Product& product,
139 bool system_level_toast) {
140 const CommandLine& current_cmd_line = *CommandLine::ForCurrentProcess();
141
142 // Propagate --verbose-logging to the invoked setup.exe.
143 if (current_cmd_line.HasSwitch(installer::switches::kVerboseLogging))
144 cmd_line->AppendSwitch(installer::switches::kVerboseLogging);
145
146 // Pass along product-specific options.
147 product.AppendProductFlags(cmd_line);
148
149 // Re-add the system level toast flag.
150 if (system_level_toast) {
151 cmd_line->AppendSwitch(installer::switches::kSystemLevel);
152 cmd_line->AppendSwitch(installer::switches::kSystemLevelToast);
153
154 // Re-add the toast result key. We need to do this because Setup running as
155 // system passes the key to Setup running as user, but that child process
156 // does not perform the actual toasting, it launches another Setup (as user)
157 // to do so. That is the process that needs the key.
158 std::string key(installer::switches::kToastResultsKey);
159 std::string toast_key = current_cmd_line.GetSwitchValueASCII(key);
160 if (!toast_key.empty()) {
161 cmd_line->AppendSwitchASCII(key, toast_key);
162
163 // Use handle inheritance to make sure the duplicated toast results key
164 // gets inherited by the child process.
165 base::LaunchOptions options;
166 options.inherit_handles = true;
167 return base::LaunchProcess(*cmd_line, options, NULL);
168 }
169 }
170
171 return base::LaunchProcess(*cmd_line, base::LaunchOptions(), NULL);
172 }
173
174 // For System level installs, setup.exe lives in the system temp, which
175 // is normally c:\windows\temp. In many cases files inside this folder
176 // are not accessible for execution by regular user accounts.
177 // This function changes the permissions so that any authenticated user
178 // can launch |exe| later on. This function should only be called if the
179 // code is running at the system level.
180 bool FixDACLsForExecute(const base::FilePath& exe) {
181 // The general strategy to is to add an ACE to the exe DACL the quick
182 // and dirty way: a) read the DACL b) convert it to sddl string c) add the
183 // new ACE to the string d) convert sddl string back to DACL and finally
184 // e) write new dacl.
185 char buff[1024];
186 DWORD len = sizeof(buff);
187 PSECURITY_DESCRIPTOR sd = reinterpret_cast<PSECURITY_DESCRIPTOR>(buff);
188 if (!::GetFileSecurityW(exe.value().c_str(), DACL_SECURITY_INFORMATION,
189 sd, len, &len)) {
190 return false;
191 }
192 wchar_t* sddl = 0;
193 if (!::ConvertSecurityDescriptorToStringSecurityDescriptorW(sd,
194 SDDL_REVISION_1, DACL_SECURITY_INFORMATION, &sddl, NULL))
195 return false;
196 string16 new_sddl(sddl);
197 ::LocalFree(sddl);
198 sd = NULL;
199 // See MSDN for the security descriptor definition language (SDDL) syntax,
200 // in our case we add "A;" generic read 'GR' and generic execute 'GX' for
201 // the nt\authenticated_users 'AU' group, that becomes:
202 const wchar_t kAllowACE[] = L"(A;;GRGX;;;AU)";
203 // We should check that there are no special ACES for the group we
204 // are interested, which is nt\authenticated_users.
205 if (string16::npos != new_sddl.find(L";AU)"))
206 return false;
207 // Specific ACEs (not inherited) need to go to the front. It is ok if we
208 // are the very first one.
209 size_t pos_insert = new_sddl.find(L"(");
210 if (string16::npos == pos_insert)
211 return false;
212 // All good, time to change the dacl.
213 new_sddl.insert(pos_insert, kAllowACE);
214 if (!::ConvertStringSecurityDescriptorToSecurityDescriptorW(new_sddl.c_str(),
215 SDDL_REVISION_1, &sd, NULL))
216 return false;
217 bool rv = ::SetFileSecurityW(exe.value().c_str(), DACL_SECURITY_INFORMATION,
218 sd) == TRUE;
219 ::LocalFree(sd);
220 return rv;
221 }
222
223 // This function launches setup as the currently logged-in interactive
224 // user that is the user whose logon session is attached to winsta0\default.
225 // It assumes that currently we are running as SYSTEM in a non-interactive
226 // windowstation.
227 // The function fails if there is no interactive session active, basically
228 // the computer is on but nobody has logged in locally.
229 // Remote Desktop sessions do not count as interactive sessions; running this
230 // method as a user logged in via remote desktop will do nothing.
231 bool LaunchSetupAsConsoleUser(const base::FilePath& setup_path,
232 const installer::Product& product,
233 const std::string& flag) {
234 CommandLine cmd_line(setup_path);
235 cmd_line.AppendSwitch(flag);
236
237 // Pass along product-specific options.
238 product.AppendProductFlags(&cmd_line);
239
240 // Convey to the invoked setup.exe that it's operating on a system-level
241 // installation.
242 cmd_line.AppendSwitch(installer::switches::kSystemLevel);
243
244 // Propagate --verbose-logging to the invoked setup.exe.
245 if (CommandLine::ForCurrentProcess()->HasSwitch(
246 installer::switches::kVerboseLogging)) {
247 cmd_line.AppendSwitch(installer::switches::kVerboseLogging);
248 }
249
250 // Get the Google Update results key, and pass it on the command line to
251 // the child process.
252 int key = GoogleUpdateSettings::DuplicateGoogleUpdateSystemClientKey();
253 cmd_line.AppendSwitchASCII(installer::switches::kToastResultsKey,
254 base::IntToString(key));
255
256 if (base::win::GetVersion() > base::win::VERSION_XP) {
257 // Make sure that in Vista and Above we have the proper DACLs so
258 // the interactive user can launch it.
259 if (!FixDACLsForExecute(setup_path))
260 NOTREACHED();
261 }
262
263 DWORD console_id = ::WTSGetActiveConsoleSessionId();
264 if (console_id == 0xFFFFFFFF) {
265 PLOG(ERROR) << __FUNCTION__ << " failed to get active session id";
266 return false;
267 }
268 HANDLE user_token;
269 if (!::WTSQueryUserToken(console_id, &user_token)) {
270 PLOG(ERROR) << __FUNCTION__ << " failed to get user token for console_id "
271 << console_id;
272 return false;
273 }
274 // Note: Handle inheritance must be true in order for the child process to be
275 // able to use the duplicated handle above (Google Update results).
276 base::LaunchOptions options;
277 options.as_user = user_token;
278 options.inherit_handles = true;
279 options.empty_desktop_name = true;
280 VLOG(1) << __FUNCTION__ << " launching " << cmd_line.GetCommandLineString();
281 bool launched = base::LaunchProcess(cmd_line, options, NULL);
282 ::CloseHandle(user_token);
283 VLOG(1) << __FUNCTION__ << " result: " << launched;
284 return launched;
285 }
286
287 } // namespace 59 } // namespace
288 60
289 GoogleChromeDistribution::GoogleChromeDistribution() 61 GoogleChromeDistribution::GoogleChromeDistribution()
290 : BrowserDistribution(CHROME_BROWSER), 62 : BrowserDistribution(CHROME_BROWSER),
291 product_guid_(kChromeGuid) { 63 product_guid_(kChromeGuid) {
292 } 64 }
293 65
294 bool GoogleChromeDistribution::BuildUninstallMetricsString( 66 bool GoogleChromeDistribution::BuildUninstallMetricsString(
295 const DictionaryValue* uninstall_metrics_dict, string16* metrics) { 67 const DictionaryValue* uninstall_metrics_dict, string16* metrics) {
296 DCHECK(NULL != metrics); 68 DCHECK(NULL != metrics);
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after
562 // string (if it is present) regardless of whether installer failed or not. 334 // string (if it is present) regardless of whether installer failed or not.
563 // There is no fall-back for full installer :) 335 // There is no fall-back for full installer :)
564 void GoogleChromeDistribution::UpdateInstallStatus(bool system_install, 336 void GoogleChromeDistribution::UpdateInstallStatus(bool system_install,
565 installer::ArchiveType archive_type, 337 installer::ArchiveType archive_type,
566 installer::InstallStatus install_status) { 338 installer::InstallStatus install_status) {
567 GoogleUpdateSettings::UpdateInstallStatus(system_install, 339 GoogleUpdateSettings::UpdateInstallStatus(system_install,
568 archive_type, InstallUtil::GetInstallReturnCode(install_status), 340 archive_type, InstallUtil::GetInstallReturnCode(install_status),
569 product_guid()); 341 product_guid());
570 } 342 }
571 343
572 // A helper function that writes to HKLM if the handle was passed through the
573 // command line, but HKCU otherwise. |experiment_group| is the value to write
574 // and |last_write| is used when writing to HKLM to determine whether to close
575 // the handle when done.
576 void SetClient(const string16& experiment_group, bool last_write) {
577 static int reg_key_handle = -1;
578 if (reg_key_handle == -1) {
579 // If a specific Toast Results key handle (presumably to our HKLM key) was
580 // passed in to the command line (such as for system level installs), we use
581 // it. Otherwise, we write to the key under HKCU.
582 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
583 if (cmd_line.HasSwitch(installer::switches::kToastResultsKey)) {
584 // Get the handle to the key under HKLM.
585 base::StringToInt(cmd_line.GetSwitchValueASCII(
586 installer::switches::kToastResultsKey).c_str(),
587 &reg_key_handle);
588 } else {
589 reg_key_handle = 0;
590 }
591 }
592
593 if (reg_key_handle) {
594 // Use it to write the experiment results.
595 GoogleUpdateSettings::WriteGoogleUpdateSystemClientKey(
596 reg_key_handle, google_update::kRegClientField, experiment_group);
597 if (last_write)
598 CloseHandle((HANDLE) reg_key_handle);
599 } else {
600 // Write to HKCU.
601 GoogleUpdateSettings::SetClient(experiment_group);
602 }
603 }
604
605 bool GoogleChromeDistribution::GetExperimentDetails(
606 UserExperiment* experiment, int flavor) {
607 struct FlavorDetails {
608 int heading_id;
609 int flags;
610 };
611 // Maximum number of experiment flavors we support.
612 static const int kMax = 4;
613 // This struct determines which experiment flavors we show for each locale and
614 // brand.
615 //
616 // Plugin infobar experiment:
617 // The experiment in 2011 used PIxx codes.
618 //
619 // Inactive user toast experiment:
620 // The experiment in Dec 2009 used TGxx and THxx.
621 // The experiment in Feb 2010 used TKxx and TLxx.
622 // The experiment in Apr 2010 used TMxx and TNxx.
623 // The experiment in Oct 2010 used TVxx TWxx TXxx TYxx.
624 // The experiment in Feb 2011 used SJxx SKxx SLxx SMxx.
625 // The experiment in Mar 2012 used ZAxx ZBxx ZCxx.
626 // The experiment in Jan 2013 uses DAxx.
627 using namespace attrition_experiments;
628
629 static const struct UserExperimentDetails {
630 const wchar_t* locale; // Locale to show this experiment for (* for all).
631 const wchar_t* brands; // Brand codes show this experiment for (* for all).
632 int control_group; // Size of the control group, in percentages.
633 const wchar_t* prefix; // The two letter experiment code. The second letter
634 // will be incremented with the flavor.
635 FlavorDetails flavors[kMax];
636 } kExperiments[] = {
637 // The first match from top to bottom is used so this list should be ordered
638 // most-specific rule first.
639 { L"*", L"GGRV", // All locales, GGRV is enterprise.
640 0, // 0 percent control group.
641 L"EA", // Experiment is EAxx, EBxx, etc.
642 // No flavors means no experiment.
643 { { 0, 0 },
644 { 0, 0 },
645 { 0, 0 },
646 { 0, 0 }
647 }
648 },
649 { L"*", L"*", // All locales, all brands.
650 5, // 5 percent control group.
651 L"DA", // Experiment is DAxx.
652 // One single flavor.
653 { { IDS_TRY_TOAST_HEADING3, kMakeDefault },
654 { 0, 0 },
655 { 0, 0 },
656 { 0, 0 }
657 }
658 }
659 };
660
661 string16 locale;
662 GoogleUpdateSettings::GetLanguage(&locale);
663 if (locale.empty() || (locale == ASCIIToWide("en")))
664 locale = ASCIIToWide("en-US");
665
666 string16 brand;
667 if (!GoogleUpdateSettings::GetBrand(&brand))
668 brand = ASCIIToWide(""); // Could still be viable for catch-all rules.
669
670 for (int i = 0; i < arraysize(kExperiments); ++i) {
671 if (kExperiments[i].locale != locale &&
672 kExperiments[i].locale != ASCIIToWide("*"))
673 continue;
674
675 std::vector<string16> brand_codes;
676 base::SplitString(kExperiments[i].brands, L',', &brand_codes);
677 if (brand_codes.empty())
678 return false;
679 for (std::vector<string16>::iterator it = brand_codes.begin();
680 it != brand_codes.end(); ++it) {
681 if (*it != brand && *it != L"*")
682 continue;
683 // We have found our match.
684 const UserExperimentDetails& match = kExperiments[i];
685 // Find out how many flavors we have. Zero means no experiment.
686 int num_flavors = 0;
687 while (match.flavors[num_flavors].heading_id) { ++num_flavors; }
688 if (!num_flavors)
689 return false;
690
691 if (flavor < 0)
692 flavor = base::RandInt(0, num_flavors - 1);
693 experiment->flavor = flavor;
694 experiment->heading = match.flavors[flavor].heading_id;
695 experiment->control_group = match.control_group;
696 const wchar_t prefix[] = { match.prefix[0], match.prefix[1] + flavor, 0 };
697 experiment->prefix = prefix;
698 experiment->flags = match.flavors[flavor].flags;
699 return true;
700 }
701 }
702
703 return false;
704 }
705
706 // Currently we only have one experiment: the inactive user toast. Which only
707 // applies for users doing upgrades.
708
709 //
710 // There are three scenarios when this function is called:
711 // 1- Is a per-user-install and it updated: perform the experiment
712 // 2- Is a system-install and it updated : relaunch as the interactive user
713 // 3- It has been re-launched from the #2 case. In this case we enter
714 // this function with |system_install| true and a REENTRY_SYS_UPDATE status.
715 void GoogleChromeDistribution::LaunchUserExperiment(
716 const base::FilePath& setup_path, installer::InstallStatus status,
717 const Version& version, const installer::Product& product,
718 bool system_level) {
719 VLOG(1) << "LaunchUserExperiment status: " << status << " product: "
720 << product.distribution()->GetAppShortCutName()
721 << " system_level: " << system_level;
722
723 if (system_level) {
724 if (installer::NEW_VERSION_UPDATED == status) {
725 // We need to relaunch as the interactive user.
726 LaunchSetupAsConsoleUser(setup_path, product,
727 installer::switches::kSystemLevelToast);
728 return;
729 }
730 } else {
731 if ((installer::NEW_VERSION_UPDATED != status) &&
732 (installer::REENTRY_SYS_UPDATE != status)) {
733 // We are not updating or in re-launch. Exit.
734 return;
735 }
736 }
737
738 // The |flavor| value ends up being processed by TryChromeDialogView to show
739 // different experiments.
740 UserExperiment experiment;
741 if (!GetExperimentDetails(&experiment, -1)) {
742 VLOG(1) << "Failed to get experiment details.";
743 return;
744 }
745 int flavor = experiment.flavor;
746 string16 base_group = experiment.prefix;
747
748 string16 brand;
749 if (GoogleUpdateSettings::GetBrand(&brand) && (brand == L"CHXX")) {
750 // Testing only: the user automatically qualifies for the experiment.
751 VLOG(1) << "Experiment qualification bypass";
752 } else {
753 // Check that the user was not already drafted in this experiment.
754 string16 client;
755 GoogleUpdateSettings::GetClient(&client);
756 if (client.size() > 2) {
757 if (base_group == client.substr(0, 2)) {
758 VLOG(1) << "User already participated in this experiment";
759 return;
760 }
761 }
762 // Check browser usage inactivity by the age of the last-write time of the
763 // most recently-used chrome user data directory.
764 std::vector<base::FilePath> user_data_dirs;
765 product.GetUserDataPaths(&user_data_dirs);
766 int dir_age_hours = -1;
767 for (size_t i = 0; i < user_data_dirs.size(); ++i) {
768 int this_age = GetDirectoryWriteAgeInHours(
769 user_data_dirs[i].value().c_str());
770 if (this_age >= 0 && (dir_age_hours < 0 || this_age < dir_age_hours))
771 dir_age_hours = this_age;
772 }
773
774 const bool experiment_enabled = false;
775 const int kThirtyDays = 30 * 24;
776
777 if (!experiment_enabled) {
778 VLOG(1) << "Toast experiment is disabled.";
779 return;
780 } else if (dir_age_hours < 0) {
781 // This means that we failed to find the user data dir. The most likely
782 // cause is that this user has not ever used chrome at all which can
783 // happen in a system-level install.
784 SetClient(base_group + kToastUDDirFailure, true);
785 return;
786 } else if (dir_age_hours < kThirtyDays) {
787 // An active user, so it does not qualify.
788 VLOG(1) << "Chrome used in last " << dir_age_hours << " hours";
789 SetClient(base_group + kToastActiveGroup, true);
790 return;
791 }
792 // Check to see if this user belongs to the control group.
793 double control_group = 1.0 * (100 - experiment.control_group) / 100;
794 if (base::RandDouble() > control_group) {
795 SetClient(base_group + kToastExpControlGroup, true);
796 VLOG(1) << "User is control group";
797 return;
798 }
799 }
800
801 VLOG(1) << "User drafted for toast experiment " << flavor;
802 SetClient(base_group + kToastExpBaseGroup, false);
803 // User level: The experiment needs to be performed in a different process
804 // because google_update expects the upgrade process to be quick and nimble.
805 // System level: We have already been relaunched, so we don't need to be
806 // quick, but we relaunch to follow the exact same codepath.
807 CommandLine cmd_line(setup_path);
808 cmd_line.AppendSwitchASCII(installer::switches::kInactiveUserToast,
809 base::IntToString(flavor));
810 cmd_line.AppendSwitchASCII(installer::switches::kExperimentGroup,
811 WideToASCII(base_group));
812 LaunchSetup(&cmd_line, product, system_level);
813 }
814
815 // User qualifies for the experiment. To test, use --try-chrome-again=|flavor|
816 // as a parameter to chrome.exe.
817 void GoogleChromeDistribution::InactiveUserToastExperiment(int flavor,
818 const string16& experiment_group,
819 const installer::Product& installation,
820 const base::FilePath& application_path) {
821 // Add the 'welcome back' url for chrome to show.
822 CommandLine options(CommandLine::NO_PROGRAM);
823 options.AppendSwitchNative(switches::kTryChromeAgain,
824 base::IntToString16(flavor));
825 // Prepend the url with a space.
826 string16 url(GetWelcomeBackUrl());
827 options.AppendArg("--");
828 options.AppendArgNative(url);
829 // The command line should now have the url added as:
830 // "chrome.exe -- <url>"
831 DCHECK_NE(string16::npos,
832 options.GetCommandLineString().find(L" -- " + url));
833
834 // Launch chrome now. It will show the toast UI.
835 int32 exit_code = 0;
836 if (!installation.LaunchChromeAndWait(application_path, options, &exit_code))
837 return;
838
839 // The chrome process has exited, figure out what happened.
840 const wchar_t* outcome = NULL;
841 switch (exit_code) {
842 case content::RESULT_CODE_NORMAL_EXIT:
843 outcome = kToastExpTriesOkGroup;
844 break;
845 case chrome::RESULT_CODE_NORMAL_EXIT_CANCEL:
846 outcome = kToastExpCancelGroup;
847 break;
848 case chrome::RESULT_CODE_NORMAL_EXIT_EXP2:
849 outcome = kToastExpUninstallGroup;
850 break;
851 default:
852 outcome = kToastExpTriesErrorGroup;
853 };
854 // Write to the |client| key for the last time.
855 SetClient(experiment_group + outcome, true);
856
857 if (outcome != kToastExpUninstallGroup)
858 return;
859 // The user wants to uninstall. This is a best effort operation. Note that
860 // we waited for chrome to exit so the uninstall would not detect chrome
861 // running.
862 bool system_level_toast = CommandLine::ForCurrentProcess()->HasSwitch(
863 installer::switches::kSystemLevelToast);
864
865 CommandLine cmd(InstallUtil::GetChromeUninstallCmd(system_level_toast,
866 GetType()));
867 base::LaunchProcess(cmd, base::LaunchOptions(), NULL);
868 }
869
870 bool GoogleChromeDistribution::ShouldSetExperimentLabels() { 344 bool GoogleChromeDistribution::ShouldSetExperimentLabels() {
871 return true; 345 return true;
872 } 346 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698