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 #ifndef CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_
6 #define CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_
13 #include "base/basictypes.h"
14 #include "base/compiler_specific.h"
15 #include "base/strings/string16.h"
16 #include "chrome/installer/util/browser_distribution.h"
25 class InstallationState
;
29 // A class that validates the state of an installation. Violations are logged
31 class InstallationValidator
{
35 // Bits that form the components of an installation type.
39 CHROME_FRAME_SINGLE
= 0x04,
40 CHROME_FRAME_MULTI
= 0x08,
42 }; // class ProductBits
44 // Identifiers of all valid installation types.
45 enum InstallationType
{
48 ProductBits::CHROME_SINGLE
,
50 ProductBits::CHROME_MULTI
,
52 ProductBits::CHROME_FRAME_SINGLE
,
53 CHROME_FRAME_SINGLE_CHROME_SINGLE
=
54 ProductBits::CHROME_FRAME_SINGLE
| ProductBits::CHROME_SINGLE
,
55 CHROME_FRAME_SINGLE_CHROME_MULTI
=
56 ProductBits::CHROME_FRAME_SINGLE
| ProductBits::CHROME_MULTI
,
58 ProductBits::CHROME_FRAME_MULTI
,
59 CHROME_FRAME_MULTI_CHROME_MULTI
=
60 ProductBits::CHROME_FRAME_MULTI
| ProductBits::CHROME_MULTI
,
63 // Validates |machine_state| at user or system level, returning true if valid.
64 // |type| is populated in either case, although it is a best-guess when the
65 // method returns false.
66 static bool ValidateInstallationTypeForState(
67 const InstallationState
& machine_state
,
69 InstallationType
* type
);
71 // Validates the machine's current installation at user or system level,
72 // returning true and populating |type| if valid.
73 static bool ValidateInstallationType(bool system_level
,
74 InstallationType
* type
);
77 struct ProductContext
;
78 typedef std::vector
<std::pair
<std::string
, bool> > SwitchExpectations
;
79 typedef void (*CommandValidatorFn
)(const ProductContext
& ctx
,
80 const AppCommand
& app_cmd
,
82 typedef std::map
<base::string16
, CommandValidatorFn
> CommandExpectations
;
84 // An interface to product-specific validation rules.
87 virtual ~ProductRules() { }
88 virtual BrowserDistribution::Type
distribution_type() const = 0;
89 virtual void AddUninstallSwitchExpectations(
90 const ProductContext
& ctx
,
91 SwitchExpectations
* expectations
) const = 0;
92 virtual void AddRenameSwitchExpectations(
93 const ProductContext
& ctx
,
94 SwitchExpectations
* expectations
) const = 0;
95 // Return true if the rules allow usagestats setting.
96 virtual bool UsageStatsAllowed(const ProductContext
& ctx
) const = 0;
99 // Validation rules for the Chrome browser.
100 class ChromeRules
: public ProductRules
{
102 BrowserDistribution::Type
distribution_type() const override
;
103 void AddUninstallSwitchExpectations(
104 const ProductContext
& ctx
,
105 SwitchExpectations
* expectations
) const override
;
106 void AddRenameSwitchExpectations(
107 const ProductContext
& ctx
,
108 SwitchExpectations
* expectations
) const override
;
109 bool UsageStatsAllowed(const ProductContext
& ctx
) const override
;
112 // Validation rules for Chrome Frame.
113 class ChromeFrameRules
: public ProductRules
{
115 BrowserDistribution::Type
distribution_type() const override
;
116 void AddUninstallSwitchExpectations(
117 const ProductContext
& ctx
,
118 SwitchExpectations
* expectations
) const override
;
119 void AddRenameSwitchExpectations(
120 const ProductContext
& ctx
,
121 SwitchExpectations
* expectations
) const override
;
122 bool UsageStatsAllowed(const ProductContext
& ctx
) const override
;
125 // Validation rules for the multi-install Chrome binaries.
126 class ChromeBinariesRules
: public ProductRules
{
128 BrowserDistribution::Type
distribution_type() const override
;
129 void AddUninstallSwitchExpectations(
130 const ProductContext
& ctx
,
131 SwitchExpectations
* expectations
) const override
;
132 void AddRenameSwitchExpectations(
133 const ProductContext
& ctx
,
134 SwitchExpectations
* expectations
) const override
;
135 bool UsageStatsAllowed(const ProductContext
& ctx
) const override
;
138 struct ProductContext
{
139 ProductContext(const InstallationState
& machine_state_in
,
140 bool system_install_in
,
141 const ProductState
& state_in
,
142 const ProductRules
& rules_in
)
143 : machine_state(machine_state_in
),
144 system_install(system_install_in
),
145 dist(BrowserDistribution::GetSpecificDistribution(
146 rules_in
.distribution_type())),
151 const InstallationState
& machine_state
;
153 BrowserDistribution
* dist
;
154 const ProductState
& state
;
155 const ProductRules
& rules
;
158 // Helper to validate the values of bool elements in AppCommand, and to output
159 // error messages. |flag_expect| is a bit mask specifying the expected
160 // presence/absence of bool variables.
161 static void ValidateAppCommandFlags(
162 const ProductContext
& ctx
,
163 const AppCommand
& app_cmd
,
164 const std::set
<base::string16
>& flags_expected
,
165 const base::string16
& name
,
167 static void ValidateOnOsUpgradeCommand(const ProductContext
& ctx
,
168 const AppCommand
& app_cmd
,
170 static void ValidateAppCommandExpectations(
171 const ProductContext
& ctx
,
172 const CommandExpectations
& expectations
,
174 static void ValidateBinaries(const InstallationState
& machine_state
,
176 const ProductState
& binaries_state
,
178 static void ValidateSetupPath(const ProductContext
& ctx
,
179 const base::FilePath
& setup_exe
,
180 const base::string16
& purpose
,
182 static void ValidateCommandExpectations(const ProductContext
& ctx
,
183 const base::CommandLine
& command
,
184 const SwitchExpectations
& expected
,
185 const base::string16
& source
,
187 static void ValidateUninstallCommand(const ProductContext
& ctx
,
188 const base::CommandLine
& command
,
189 const base::string16
& source
,
191 static void ValidateRenameCommand(const ProductContext
& ctx
,
193 static void ValidateOldVersionValues(const ProductContext
& ctx
,
195 static void ValidateMultiInstallProduct(const ProductContext
& ctx
,
197 static void ValidateAppCommands(const ProductContext
& ctx
,
199 static void ValidateUsageStats(const ProductContext
& ctx
,
201 static void ValidateProduct(const InstallationState
& machine_state
,
203 const ProductState
& product_state
,
204 const ProductRules
& rules
,
207 // A collection of all valid installation types.
208 static const InstallationType kInstallationTypes
[];
211 DISALLOW_IMPLICIT_CONSTRUCTORS(InstallationValidator
);
214 } // namespace installer
216 #endif // CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_