Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / first_run / first_run_internal_win.cc
blob691382601053f2e9657125f1bb78b9d5ec5dcdbc
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 // Populates |path| with the path to |file| in the sentinel directory. This is
82 // the application directory for user-level installs, and the default user data
83 // dir for system-level installs. Returns false on error.
84 bool GetSentinelFilePath(const wchar_t* file, base::FilePath* path) {
85 base::FilePath exe_path;
86 if (!PathService::Get(base::DIR_EXE, &exe_path))
87 return false;
88 if (InstallUtil::IsPerUserInstall(exe_path.value().c_str()))
89 *path = exe_path;
90 else if (!PathService::Get(chrome::DIR_USER_DATA, path))
91 return false;
92 *path = path->Append(file);
93 return true;
96 bool GetEULASentinelFilePath(base::FilePath* path) {
97 return GetSentinelFilePath(installer::kEULASentinelFile, path);
100 // Returns true if the EULA is required but has not been accepted by this user.
101 // The EULA is considered having been accepted if the user has gotten past
102 // first run in the "other" environment (desktop or metro).
103 bool IsEULANotAccepted(installer::MasterPreferences* install_prefs) {
104 bool value = false;
105 if (install_prefs->GetBool(installer::master_preferences::kRequireEula,
106 &value) && value) {
107 base::FilePath eula_sentinel;
108 // Be conservative and show the EULA if the path to the sentinel can't be
109 // determined.
110 if (!GetEULASentinelFilePath(&eula_sentinel) ||
111 !base::PathExists(eula_sentinel)) {
112 return true;
115 return false;
118 // Writes the EULA to a temporary file, returned in |*eula_path|, and returns
119 // true if successful.
120 bool WriteEULAtoTempFile(base::FilePath* eula_path) {
121 std::string terms = l10n_util::GetStringUTF8(IDS_TERMS_HTML);
122 return (!terms.empty() &&
123 base::CreateTemporaryFile(eula_path) &&
124 file_util::WriteFile(*eula_path, terms.data(), terms.size()) != -1);
127 // Creates the sentinel indicating that the EULA was required and has been
128 // accepted.
129 bool CreateEULASentinel() {
130 base::FilePath eula_sentinel;
131 if (!GetEULASentinelFilePath(&eula_sentinel))
132 return false;
134 return (base::CreateDirectory(eula_sentinel.DirName()) &&
135 file_util::WriteFile(eula_sentinel, "", 0) != -1);
138 } // namespace
140 namespace first_run {
141 namespace internal {
143 void DoPostImportPlatformSpecificTasks(Profile* /* profile */) {
144 // Trigger the Active Setup command for system-level Chromes to finish
145 // configuring this user's install (e.g. per-user shortcuts).
146 // Delay the task slightly to give Chrome launch I/O priority while also
147 // making sure shortcuts are created promptly to avoid annoying the user by
148 // re-creating shortcuts he previously deleted.
149 static const int64 kTiggerActiveSetupDelaySeconds = 5;
150 base::FilePath chrome_exe;
151 if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
152 NOTREACHED();
153 } else if (!InstallUtil::IsPerUserInstall(chrome_exe.value().c_str())) {
154 content::BrowserThread::GetBlockingPool()->PostDelayedTask(
155 FROM_HERE,
156 base::Bind(&InstallUtil::TriggerActiveSetupCommand),
157 base::TimeDelta::FromSeconds(kTiggerActiveSetupDelaySeconds));
161 bool GetFirstRunSentinelFilePath(base::FilePath* path) {
162 return GetSentinelFilePath(chrome::kFirstRunSentinel, path);
165 bool ShowPostInstallEULAIfNeeded(installer::MasterPreferences* install_prefs) {
166 if (IsEULANotAccepted(install_prefs)) {
167 // Show the post-installation EULA. This is done by setup.exe and the
168 // result determines if we continue or not. We wait here until the user
169 // dismisses the dialog.
171 // The actual eula text is in a resource in chrome. We extract it to
172 // a text file so setup.exe can use it as an inner frame.
173 base::FilePath inner_html;
174 if (WriteEULAtoTempFile(&inner_html)) {
175 int retcode = 0;
176 if (!LaunchSetupForEula(inner_html.value(), &retcode) ||
177 (retcode != installer::EULA_ACCEPTED &&
178 retcode != installer::EULA_ACCEPTED_OPT_IN)) {
179 LOG(WARNING) << "EULA flow requires fast exit.";
180 return false;
182 CreateEULASentinel();
184 if (retcode == installer::EULA_ACCEPTED) {
185 VLOG(1) << "EULA : no collection";
186 GoogleUpdateSettings::SetCollectStatsConsent(false);
187 } else if (retcode == installer::EULA_ACCEPTED_OPT_IN) {
188 VLOG(1) << "EULA : collection consent";
189 GoogleUpdateSettings::SetCollectStatsConsent(true);
193 return true;
196 base::FilePath MasterPrefsPath() {
197 // The standard location of the master prefs is next to the chrome binary.
198 base::FilePath master_prefs;
199 if (!PathService::Get(base::DIR_EXE, &master_prefs))
200 return base::FilePath();
201 return master_prefs.AppendASCII(installer::kDefaultMasterPrefs);
204 } // namespace internal
205 } // namespace first_run