Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / first_run / first_run_internal_win.cc
blobd94e5e5a508b8976944c3d00e731129bd5b06a59
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/files/file_path.h"
14 #include "base/files/file_util.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/grit/locale_settings.h"
26 #include "chrome/installer/util/google_update_settings.h"
27 #include "chrome/installer/util/install_util.h"
28 #include "chrome/installer/util/master_preferences.h"
29 #include "chrome/installer/util/master_preferences_constants.h"
30 #include "chrome/installer/util/util_constants.h"
31 #include "content/public/browser/browser_thread.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);
50 base::CommandLine cl(base::CommandLine::NO_PROGRAM);
51 cl.AppendSwitchNative(installer::switches::kShowEula, value);
53 if (base::win::IsMetroProcess()) {
54 cl.AppendSwitch(installer::switches::kShowEulaForMetro);
56 // This obscure use of the 'log usage' mask for windows 8 is documented here
57 // http://go.microsoft.com/fwlink/?LinkID=243079. It causes the desktop
58 // process to receive focus. Pass SEE_MASK_FLAG_NO_UI to avoid hangs if an
59 // error occurs since the UI can't be shown from a metro process.
60 ui::win::OpenAnyViaShell(exe_path.value(),
61 exe_dir.value(),
62 cl.GetCommandLineString(),
63 SEE_MASK_FLAG_LOG_USAGE | SEE_MASK_FLAG_NO_UI);
64 return false;
65 } else {
66 base::CommandLine setup_path(exe_path);
67 setup_path.AppendArguments(cl, false);
69 base::Process process =
70 base::LaunchProcess(setup_path, base::LaunchOptions());
71 int exit_code = 0;
72 if (!process.IsValid() || !process.WaitForExit(&exit_code))
73 return false;
75 *ret_code = exit_code;
76 return true;
80 // Returns true if the EULA is required but has not been accepted by this user.
81 // The EULA is considered having been accepted if the user has gotten past
82 // first run in the "other" environment (desktop or metro).
83 bool IsEULANotAccepted(installer::MasterPreferences* install_prefs) {
84 bool value = false;
85 if (install_prefs->GetBool(installer::master_preferences::kRequireEula,
86 &value) && value) {
87 base::FilePath eula_sentinel;
88 // Be conservative and show the EULA if the path to the sentinel can't be
89 // determined.
90 if (!InstallUtil::GetEULASentinelFilePath(&eula_sentinel) ||
91 !base::PathExists(eula_sentinel)) {
92 return true;
95 return false;
98 // Writes the EULA to a temporary file, returned in |*eula_path|, and returns
99 // true if successful.
100 bool WriteEULAtoTempFile(base::FilePath* eula_path) {
101 std::string terms = l10n_util::GetStringUTF8(IDS_TERMS_HTML);
102 return (!terms.empty() &&
103 base::CreateTemporaryFile(eula_path) &&
104 base::WriteFile(*eula_path, terms.data(), terms.size()) != -1);
107 // Creates the sentinel indicating that the EULA was required and has been
108 // accepted.
109 bool CreateEULASentinel() {
110 base::FilePath eula_sentinel;
111 return InstallUtil::GetEULASentinelFilePath(&eula_sentinel) &&
112 base::CreateDirectory(eula_sentinel.DirName()) &&
113 base::WriteFile(eula_sentinel, "", 0) != -1;
116 } // namespace
118 namespace first_run {
119 namespace internal {
121 void DoPostImportPlatformSpecificTasks(Profile* /* profile */) {
122 // Trigger the Active Setup command for system-level Chromes to finish
123 // configuring this user's install (e.g. per-user shortcuts).
124 // Delay the task slightly to give Chrome launch I/O priority while also
125 // making sure shortcuts are created promptly to avoid annoying the user by
126 // re-creating shortcuts he previously deleted.
127 static const int64 kTiggerActiveSetupDelaySeconds = 5;
128 base::FilePath chrome_exe;
129 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
130 NOTREACHED();
131 } else if (!InstallUtil::IsPerUserInstall(chrome_exe)) {
132 content::BrowserThread::GetBlockingPool()->PostDelayedTask(
133 FROM_HERE,
134 base::Bind(&InstallUtil::TriggerActiveSetupCommand),
135 base::TimeDelta::FromSeconds(kTiggerActiveSetupDelaySeconds));
139 bool IsFirstRunSentinelPresent() {
140 base::FilePath sentinel;
141 if (!GetFirstRunSentinelFilePath(&sentinel) || base::PathExists(sentinel))
142 return true;
144 // Copy any legacy first run sentinel file for Windows user-level installs
145 // from the application directory to the user data directory.
146 base::FilePath exe_path;
147 if (PathService::Get(base::DIR_EXE, &exe_path) &&
148 InstallUtil::IsPerUserInstall(exe_path)) {
149 base::FilePath legacy_sentinel = exe_path.Append(chrome::kFirstRunSentinel);
150 if (base::PathExists(legacy_sentinel)) {
151 // Copy the file instead of moving it to avoid breaking developer builds
152 // where the sentinel is dropped beside chrome.exe by a build action.
153 bool migrated = base::CopyFile(legacy_sentinel, sentinel);
154 DPCHECK(migrated);
155 // The sentinel is present regardless of whether or not it was migrated.
156 return true;
160 return false;
163 bool ShowPostInstallEULAIfNeeded(installer::MasterPreferences* install_prefs) {
164 if (IsEULANotAccepted(install_prefs)) {
165 // Show the post-installation EULA. This is done by setup.exe and the
166 // result determines if we continue or not. We wait here until the user
167 // dismisses the dialog.
169 // The actual eula text is in a resource in chrome. We extract it to
170 // a text file so setup.exe can use it as an inner frame.
171 base::FilePath inner_html;
172 if (WriteEULAtoTempFile(&inner_html)) {
173 int retcode = 0;
174 if (!LaunchSetupForEula(inner_html.value(), &retcode) ||
175 (retcode != installer::EULA_ACCEPTED &&
176 retcode != installer::EULA_ACCEPTED_OPT_IN)) {
177 LOG(WARNING) << "EULA flow requires fast exit.";
178 return false;
180 CreateEULASentinel();
182 if (retcode == installer::EULA_ACCEPTED) {
183 DVLOG(1) << "EULA : no collection";
184 GoogleUpdateSettings::SetCollectStatsConsent(false);
185 } else if (retcode == installer::EULA_ACCEPTED_OPT_IN) {
186 DVLOG(1) << "EULA : collection consent";
187 GoogleUpdateSettings::SetCollectStatsConsent(true);
191 return true;
194 base::FilePath MasterPrefsPath() {
195 // The standard location of the master prefs is next to the chrome binary.
196 base::FilePath master_prefs;
197 if (!PathService::Get(base::DIR_EXE, &master_prefs))
198 return base::FilePath();
199 return master_prefs.AppendASCII(installer::kDefaultMasterPrefs);
202 } // namespace internal
203 } // namespace first_run