[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / chrome / browser / first_run / first_run_internal_win.cc
blobb28e839165e2dc07837850c4bf83cf6d581bf798
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
3 // found in the LICENSE file.
5 #include "chrome/browser/first_run/first_run_internal.h"
7 #include <windows.h>
8 #include <shellapi.h>
10 #include "base/base_paths.h"
11 #include "base/callback.h"
12 #include "base/command_line.h"
13 #include "base/file_util.h"
14 #include "base/files/file_path.h"
15 #include "base/path_service.h"
16 #include "base/process/kill.h"
17 #include "base/process/launch.h"
18 #include "base/process/process.h"
19 #include "base/threading/sequenced_worker_pool.h"
20 #include "base/time/time.h"
21 #include "base/win/metro.h"
22 #include "chrome/common/chrome_constants.h"
23 #include "chrome/common/chrome_paths.h"
24 #include "chrome/common/chrome_switches.h"
25 #include "chrome/installer/util/google_update_settings.h"
26 #include "chrome/installer/util/install_util.h"
27 #include "chrome/installer/util/master_preferences.h"
28 #include "chrome/installer/util/master_preferences_constants.h"
29 #include "chrome/installer/util/util_constants.h"
30 #include "content/public/browser/browser_thread.h"
31 #include "grit/locale_settings.h"
32 #include "ui/base/l10n/l10n_util.h"
33 #include "ui/base/win/shell.h"
35 namespace {
37 // Launches the setup exe with the given parameter/value on the command-line.
38 // For non-metro Windows, it waits for its termination, returns its exit code
39 // in |*ret_code|, and returns true if the exit code is valid.
40 // For metro Windows, it launches setup via ShellExecuteEx and returns in order
41 // to bounce the user back to the desktop, then returns immediately.
42 bool LaunchSetupForEula(const base::FilePath::StringType& value,
43 int* ret_code) {
44 base::FilePath exe_dir;
45 if (!PathService::Get(base::DIR_MODULE, &exe_dir))
46 return false;
47 exe_dir = exe_dir.Append(installer::kInstallerDir);
48 base::FilePath exe_path = exe_dir.Append(installer::kSetupExe);
49 base::ProcessHandle ph;
51 CommandLine cl(CommandLine::NO_PROGRAM);
52 cl.AppendSwitchNative(installer::switches::kShowEula, value);
54 if (base::win::IsMetroProcess()) {
55 cl.AppendSwitch(installer::switches::kShowEulaForMetro);
57 // This obscure use of the 'log usage' mask for windows 8 is documented here
58 // http://go.microsoft.com/fwlink/?LinkID=243079. It causes the desktop
59 // process to receive focus. Pass SEE_MASK_FLAG_NO_UI to avoid hangs if an
60 // error occurs since the UI can't be shown from a metro process.
61 ui::win::OpenAnyViaShell(exe_path.value(),
62 exe_dir.value(),
63 cl.GetCommandLineString(),
64 SEE_MASK_FLAG_LOG_USAGE | SEE_MASK_FLAG_NO_UI);
65 return false;
66 } else {
67 CommandLine setup_path(exe_path);
68 setup_path.AppendArguments(cl, false);
70 int exit_code = 0;
71 if (!base::LaunchProcess(setup_path, base::LaunchOptions(), &ph) ||
72 !base::WaitForExitCode(ph, &exit_code)) {
73 return false;
76 *ret_code = exit_code;
77 return true;
81 // Returns true if the EULA is required but has not been accepted by this user.
82 // The EULA is considered having been accepted if the user has gotten past
83 // first run in the "other" environment (desktop or metro).
84 bool IsEULANotAccepted(installer::MasterPreferences* install_prefs) {
85 bool value = false;
86 if (install_prefs->GetBool(installer::master_preferences::kRequireEula,
87 &value) && value) {
88 base::FilePath eula_sentinel;
89 // Be conservative and show the EULA if the path to the sentinel can't be
90 // determined.
91 if (!InstallUtil::GetEULASentinelFilePath(&eula_sentinel) ||
92 !base::PathExists(eula_sentinel)) {
93 return true;
96 return false;
99 // Writes the EULA to a temporary file, returned in |*eula_path|, and returns
100 // true if successful.
101 bool WriteEULAtoTempFile(base::FilePath* eula_path) {
102 std::string terms = l10n_util::GetStringUTF8(IDS_TERMS_HTML);
103 return (!terms.empty() &&
104 base::CreateTemporaryFile(eula_path) &&
105 base::WriteFile(*eula_path, terms.data(), terms.size()) != -1);
108 // Creates the sentinel indicating that the EULA was required and has been
109 // accepted.
110 bool CreateEULASentinel() {
111 base::FilePath eula_sentinel;
112 return InstallUtil::GetEULASentinelFilePath(&eula_sentinel) &&
113 base::CreateDirectory(eula_sentinel.DirName()) &&
114 base::WriteFile(eula_sentinel, "", 0) != -1;
117 } // namespace
119 namespace first_run {
120 namespace internal {
122 void DoPostImportPlatformSpecificTasks(Profile* /* profile */) {
123 // Trigger the Active Setup command for system-level Chromes to finish
124 // configuring this user's install (e.g. per-user shortcuts).
125 // Delay the task slightly to give Chrome launch I/O priority while also
126 // making sure shortcuts are created promptly to avoid annoying the user by
127 // re-creating shortcuts he previously deleted.
128 static const int64 kTiggerActiveSetupDelaySeconds = 5;
129 base::FilePath chrome_exe;
130 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
131 NOTREACHED();
132 } else if (!InstallUtil::IsPerUserInstall(chrome_exe.value().c_str())) {
133 content::BrowserThread::GetBlockingPool()->PostDelayedTask(
134 FROM_HERE,
135 base::Bind(&InstallUtil::TriggerActiveSetupCommand),
136 base::TimeDelta::FromSeconds(kTiggerActiveSetupDelaySeconds));
140 bool IsFirstRunSentinelPresent() {
141 base::FilePath sentinel;
142 if (!GetFirstRunSentinelFilePath(&sentinel) || base::PathExists(sentinel))
143 return true;
145 // Copy any legacy first run sentinel file for Windows user-level installs
146 // from the application directory to the user data directory.
147 base::FilePath exe_path;
148 if (PathService::Get(base::DIR_EXE, &exe_path) &&
149 InstallUtil::IsPerUserInstall(exe_path.value().c_str())) {
150 base::FilePath legacy_sentinel = exe_path.Append(chrome::kFirstRunSentinel);
151 if (base::PathExists(legacy_sentinel)) {
152 // Copy the file instead of moving it to avoid breaking developer builds
153 // where the sentinel is dropped beside chrome.exe by a build action.
154 bool migrated = base::CopyFile(legacy_sentinel, sentinel);
155 DPCHECK(migrated);
156 // The sentinel is present regardless of whether or not it was migrated.
157 return true;
161 return false;
164 bool ShowPostInstallEULAIfNeeded(installer::MasterPreferences* install_prefs) {
165 if (IsEULANotAccepted(install_prefs)) {
166 // Show the post-installation EULA. This is done by setup.exe and the
167 // result determines if we continue or not. We wait here until the user
168 // dismisses the dialog.
170 // The actual eula text is in a resource in chrome. We extract it to
171 // a text file so setup.exe can use it as an inner frame.
172 base::FilePath inner_html;
173 if (WriteEULAtoTempFile(&inner_html)) {
174 int retcode = 0;
175 if (!LaunchSetupForEula(inner_html.value(), &retcode) ||
176 (retcode != installer::EULA_ACCEPTED &&
177 retcode != installer::EULA_ACCEPTED_OPT_IN)) {
178 LOG(WARNING) << "EULA flow requires fast exit.";
179 return false;
181 CreateEULASentinel();
183 if (retcode == installer::EULA_ACCEPTED) {
184 VLOG(1) << "EULA : no collection";
185 GoogleUpdateSettings::SetCollectStatsConsent(false);
186 } else if (retcode == installer::EULA_ACCEPTED_OPT_IN) {
187 VLOG(1) << "EULA : collection consent";
188 GoogleUpdateSettings::SetCollectStatsConsent(true);
192 return true;
195 base::FilePath MasterPrefsPath() {
196 // The standard location of the master prefs is next to the chrome binary.
197 base::FilePath master_prefs;
198 if (!PathService::Get(base::DIR_EXE, &master_prefs))
199 return base::FilePath();
200 return master_prefs.AppendASCII(installer::kDefaultMasterPrefs);
203 } // namespace internal
204 } // namespace first_run