eula_util.cc revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1// Copyright (c) 2013 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/installer/util/eula_util.h"
6
7#include "base/file_util.h"
8#include "base/memory/scoped_ptr.h"
9#include "chrome/installer/util/browser_distribution.h"
10#include "chrome/installer/util/install_util.h"
11#include "chrome/installer/util/installation_state.h"
12#include "chrome/installer/util/master_preferences.h"
13#include "chrome/installer/util/master_preferences_constants.h"
14
15namespace installer {
16
17namespace {
18
19bool IsEULAAcceptanceFlagged(BrowserDistribution* dist) {
20  // Chrome creates the EULA sentinel after the EULA has been accepted when
21  // doing so is required by master_preferences. Assume the EULA has not been
22  // accepted if the path to the sentinel cannot be determined.
23  base::FilePath eula_sentinel;
24  return InstallUtil::GetEULASentinelFilePath(&eula_sentinel) &&
25      base::PathExists(eula_sentinel);
26}
27
28scoped_ptr<MasterPreferences> GetMasterPrefs(const ProductState& prod_state) {
29  // The standard location of the master prefs is next to the chrome binary.
30  base::FilePath master_prefs_path(
31      prod_state.GetSetupPath().DirName().DirName().DirName());
32  scoped_ptr<MasterPreferences> install_prefs(new MasterPreferences(
33      master_prefs_path.AppendASCII(kDefaultMasterPrefs)));
34  if (install_prefs && install_prefs->read_from_file())
35    return install_prefs.Pass();
36
37  return scoped_ptr<MasterPreferences>();
38}
39
40// Attempts to initialize |state| with any Chrome-related product.
41// Returns true if any of these product are installed, otherwise false.
42bool GetAnyChromeProductState(bool system_level, ProductState* state) {
43  return state->Initialize(system_level, BrowserDistribution::CHROME_BROWSER)
44      || state->Initialize(system_level, BrowserDistribution::CHROME_FRAME)
45      || state->Initialize(system_level, BrowserDistribution::CHROME_APP_HOST);
46}
47
48}  // namespace
49
50EULAAcceptanceResponse IsEULAAccepted(bool system_level) {
51  ProductState prod_state;
52
53  if (!system_level) {  // User-level case has seprate flow.
54    // Do not simply check Chrome binaries. Instead, check whether or not
55    // any Chrome-related products is installed, because the presence of any of
56    // these products at user-level implies that the EULA has been accepted.
57    return GetAnyChromeProductState(false, &prod_state)
58        ? QUERY_EULA_ACCEPTED : QUERY_EULA_NOT_ACCEPTED;
59  }
60
61  // System-level. Try to use Chrome binaries product state.
62  if (!prod_state.Initialize(true, BrowserDistribution::CHROME_BINARIES)) {
63    // Fall back to Chrome Browser product state only.
64    if (!prod_state.Initialize(true, BrowserDistribution::CHROME_BROWSER))
65      return QUERY_EULA_FAIL;
66
67    LOG_IF(DFATAL, prod_state.is_multi_install())
68        << "Binaries are not installed, but Chrome is multi-install.";
69  }
70  BrowserDistribution* dist = BrowserDistribution::GetSpecificDistribution(
71      BrowserDistribution::CHROME_BROWSER);
72
73  // Is Google Update waiting for Chrome's EULA to be accepted?
74  DWORD eula_accepted = 0;
75  if (prod_state.GetEulaAccepted(&eula_accepted) && !eula_accepted)
76    return QUERY_EULA_NOT_ACCEPTED;
77
78  if (InstallUtil::IsFirstRunSentinelPresent() || IsEULAAcceptanceFlagged(dist))
79    return QUERY_EULA_ACCEPTED;
80
81  // EULA acceptance not flagged. Now see if it is required.
82  scoped_ptr<MasterPreferences> install_prefs(GetMasterPrefs(prod_state));
83  // If we fail to get master preferences, assume that EULA is not required.
84  if (!install_prefs)
85    return QUERY_EULA_ACCEPTED;
86
87  bool eula_required = false;
88  // If kRequireEula value is absent, assume EULA is not required.
89  if (!install_prefs->GetBool(master_preferences::kRequireEula, &eula_required))
90    return QUERY_EULA_ACCEPTED;
91
92  return eula_required ? QUERY_EULA_NOT_ACCEPTED : QUERY_EULA_ACCEPTED;
93}
94
95}  // namespace installer
96