Home | History | Annotate | Download | only in first_run
      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.
      4 
      5 #include "chrome/browser/first_run/first_run_internal.h"
      6 
      7 #include <windows.h>
      8 #include <shellapi.h>
      9 
     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"
     34 
     35 namespace {
     36 
     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;
     50 
     51   CommandLine cl(CommandLine::NO_PROGRAM);
     52   cl.AppendSwitchNative(installer::switches::kShowEula, value);
     53 
     54   CommandLine* browser_command_line = CommandLine::ForCurrentProcess();
     55   if (browser_command_line->HasSwitch(switches::kChromeFrame)) {
     56     cl.AppendSwitch(switches::kChromeFrame);
     57   }
     58 
     59   if (base::win::IsMetroProcess()) {
     60     cl.AppendSwitch(installer::switches::kShowEulaForMetro);
     61 
     62     // This obscure use of the 'log usage' mask for windows 8 is documented here
     63     // http://go.microsoft.com/fwlink/?LinkID=243079. It causes the desktop
     64     // process to receive focus. Pass SEE_MASK_FLAG_NO_UI to avoid hangs if an
     65     // error occurs since the UI can't be shown from a metro process.
     66     ui::win::OpenAnyViaShell(exe_path.value(),
     67                              exe_dir.value(),
     68                              cl.GetCommandLineString(),
     69                              SEE_MASK_FLAG_LOG_USAGE | SEE_MASK_FLAG_NO_UI);
     70     return false;
     71   } else {
     72     CommandLine setup_path(exe_path);
     73     setup_path.AppendArguments(cl, false);
     74 
     75     int exit_code = 0;
     76     if (!base::LaunchProcess(setup_path, base::LaunchOptions(), &ph) ||
     77         !base::WaitForExitCode(ph, &exit_code)) {
     78       return false;
     79     }
     80 
     81     *ret_code = exit_code;
     82     return true;
     83   }
     84 }
     85 
     86 // Populates |path| with the path to |file| in the sentinel directory. This is
     87 // the application directory for user-level installs, and the default user data
     88 // dir for system-level installs. Returns false on error.
     89 bool GetSentinelFilePath(const wchar_t* file, base::FilePath* path) {
     90   base::FilePath exe_path;
     91   if (!PathService::Get(base::DIR_EXE, &exe_path))
     92     return false;
     93   if (InstallUtil::IsPerUserInstall(exe_path.value().c_str()))
     94     *path = exe_path;
     95   else if (!PathService::Get(chrome::DIR_USER_DATA, path))
     96     return false;
     97   *path = path->Append(file);
     98   return true;
     99 }
    100 
    101 bool GetEULASentinelFilePath(base::FilePath* path) {
    102   return GetSentinelFilePath(installer::kEULASentinelFile, path);
    103 }
    104 
    105 // Returns true if the EULA is required but has not been accepted by this user.
    106 // The EULA is considered having been accepted if the user has gotten past
    107 // first run in the "other" environment (desktop or metro).
    108 bool IsEULANotAccepted(installer::MasterPreferences* install_prefs) {
    109   bool value = false;
    110   if (install_prefs->GetBool(installer::master_preferences::kRequireEula,
    111           &value) && value) {
    112     base::FilePath eula_sentinel;
    113     // Be conservative and show the EULA if the path to the sentinel can't be
    114     // determined.
    115     if (!GetEULASentinelFilePath(&eula_sentinel) ||
    116         !base::PathExists(eula_sentinel)) {
    117       return true;
    118     }
    119   }
    120   return false;
    121 }
    122 
    123 // Writes the EULA to a temporary file, returned in |*eula_path|, and returns
    124 // true if successful.
    125 bool WriteEULAtoTempFile(base::FilePath* eula_path) {
    126   std::string terms = l10n_util::GetStringUTF8(IDS_TERMS_HTML);
    127   return (!terms.empty() &&
    128           file_util::CreateTemporaryFile(eula_path) &&
    129           file_util::WriteFile(*eula_path, terms.data(), terms.size()) != -1);
    130 }
    131 
    132 // Creates the sentinel indicating that the EULA was required and has been
    133 // accepted.
    134 bool CreateEULASentinel() {
    135   base::FilePath eula_sentinel;
    136   if (!GetEULASentinelFilePath(&eula_sentinel))
    137     return false;
    138 
    139   return (file_util::CreateDirectory(eula_sentinel.DirName()) &&
    140           file_util::WriteFile(eula_sentinel, "", 0) != -1);
    141 }
    142 
    143 }  // namespace
    144 
    145 namespace first_run {
    146 namespace internal {
    147 
    148 void DoPostImportPlatformSpecificTasks(Profile* /* profile */) {
    149   // Trigger the Active Setup command for system-level Chromes to finish
    150   // configuring this user's install (e.g. per-user shortcuts).
    151   // Delay the task slightly to give Chrome launch I/O priority while also
    152   // making sure shortcuts are created promptly to avoid annoying the user by
    153   // re-creating shortcuts he previously deleted.
    154   static const int64 kTiggerActiveSetupDelaySeconds = 5;
    155   base::FilePath chrome_exe;
    156   if (!PathService::Get(base::FILE_EXE, &chrome_exe)) {
    157     NOTREACHED();
    158   } else if (!InstallUtil::IsPerUserInstall(chrome_exe.value().c_str())) {
    159     content::BrowserThread::GetBlockingPool()->PostDelayedTask(
    160         FROM_HERE,
    161         base::Bind(&InstallUtil::TriggerActiveSetupCommand),
    162         base::TimeDelta::FromSeconds(kTiggerActiveSetupDelaySeconds));
    163   }
    164 }
    165 
    166 bool GetFirstRunSentinelFilePath(base::FilePath* path) {
    167   return GetSentinelFilePath(chrome::kFirstRunSentinel, path);
    168 }
    169 
    170 bool ShowPostInstallEULAIfNeeded(installer::MasterPreferences* install_prefs) {
    171   if (IsEULANotAccepted(install_prefs)) {
    172     // Show the post-installation EULA. This is done by setup.exe and the
    173     // result determines if we continue or not. We wait here until the user
    174     // dismisses the dialog.
    175 
    176     // The actual eula text is in a resource in chrome. We extract it to
    177     // a text file so setup.exe can use it as an inner frame.
    178     base::FilePath inner_html;
    179     if (WriteEULAtoTempFile(&inner_html)) {
    180       int retcode = 0;
    181       if (!LaunchSetupForEula(inner_html.value(), &retcode) ||
    182           (retcode != installer::EULA_ACCEPTED &&
    183            retcode != installer::EULA_ACCEPTED_OPT_IN)) {
    184         LOG(WARNING) << "EULA flow requires fast exit.";
    185         return false;
    186       }
    187       CreateEULASentinel();
    188 
    189       if (retcode == installer::EULA_ACCEPTED) {
    190         VLOG(1) << "EULA : no collection";
    191         GoogleUpdateSettings::SetCollectStatsConsent(false);
    192       } else if (retcode == installer::EULA_ACCEPTED_OPT_IN) {
    193         VLOG(1) << "EULA : collection consent";
    194         GoogleUpdateSettings::SetCollectStatsConsent(true);
    195       }
    196     }
    197   }
    198   return true;
    199 }
    200 
    201 base::FilePath MasterPrefsPath() {
    202   // The standard location of the master prefs is next to the chrome binary.
    203   base::FilePath master_prefs;
    204   if (!PathService::Get(base::DIR_EXE, &master_prefs))
    205     return base::FilePath();
    206   return master_prefs.AppendASCII(installer::kDefaultMasterPrefs);
    207 }
    208 
    209 }  // namespace internal
    210 }  // namespace first_run
    211