Do not announce robot account token before account ID is available
[chromium-blink-merge.git] / chrome / installer / util / installation_validator.h
blob28e7a83e6fbc608a001a09ce9b9cc3e953769024
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_
8 #include <map>
9 #include <set>
10 #include <utility>
11 #include <vector>
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"
18 namespace base {
19 class CommandLine;
20 class FilePath;
23 namespace installer {
25 class InstallationState;
26 class AppCommand;
27 class ProductState;
29 // A class that validates the state of an installation. Violations are logged
30 // via LOG(ERROR).
31 class InstallationValidator {
32 public:
33 class ProductBits {
34 public:
35 // Bits that form the components of an installation type.
36 enum {
37 CHROME_SINGLE = 0x01,
38 CHROME_MULTI = 0x02,
39 CHROME_FRAME_SINGLE = 0x04,
40 CHROME_FRAME_MULTI = 0x08,
42 }; // class ProductBits
44 // Identifiers of all valid installation types.
45 enum InstallationType {
46 NO_PRODUCTS = 0,
47 CHROME_SINGLE =
48 ProductBits::CHROME_SINGLE,
49 CHROME_MULTI =
50 ProductBits::CHROME_MULTI,
51 CHROME_FRAME_SINGLE =
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,
57 CHROME_FRAME_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,
68 bool system_level,
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);
76 protected:
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,
81 bool* is_valid);
82 typedef std::map<base::string16, CommandValidatorFn> CommandExpectations;
84 // An interface to product-specific validation rules.
85 class ProductRules {
86 public:
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 {
101 public:
102 virtual BrowserDistribution::Type distribution_type() const override;
103 virtual void AddUninstallSwitchExpectations(
104 const ProductContext& ctx,
105 SwitchExpectations* expectations) const override;
106 virtual void AddRenameSwitchExpectations(
107 const ProductContext& ctx,
108 SwitchExpectations* expectations) const override;
109 virtual bool UsageStatsAllowed(const ProductContext& ctx) const override;
112 // Validation rules for Chrome Frame.
113 class ChromeFrameRules : public ProductRules {
114 public:
115 virtual BrowserDistribution::Type distribution_type() const override;
116 virtual void AddUninstallSwitchExpectations(
117 const ProductContext& ctx,
118 SwitchExpectations* expectations) const override;
119 virtual void AddRenameSwitchExpectations(
120 const ProductContext& ctx,
121 SwitchExpectations* expectations) const override;
122 virtual bool UsageStatsAllowed(const ProductContext& ctx) const override;
125 // Validation rules for the multi-install Chrome binaries.
126 class ChromeBinariesRules : public ProductRules {
127 public:
128 virtual BrowserDistribution::Type distribution_type() const override;
129 virtual void AddUninstallSwitchExpectations(
130 const ProductContext& ctx,
131 SwitchExpectations* expectations) const override;
132 virtual void AddRenameSwitchExpectations(
133 const ProductContext& ctx,
134 SwitchExpectations* expectations) const override;
135 virtual 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())),
147 state(state_in),
148 rules(rules_in) {
151 const InstallationState& machine_state;
152 bool system_install;
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,
166 bool* is_valid);
167 static void ValidateOnOsUpgradeCommand(const ProductContext& ctx,
168 const AppCommand& app_cmd,
169 bool* is_valid);
170 static void ValidateAppCommandExpectations(
171 const ProductContext& ctx,
172 const CommandExpectations& expectations,
173 bool* is_valid);
174 static void ValidateBinaries(const InstallationState& machine_state,
175 bool system_install,
176 const ProductState& binaries_state,
177 bool* is_valid);
178 static void ValidateSetupPath(const ProductContext& ctx,
179 const base::FilePath& setup_exe,
180 const base::string16& purpose,
181 bool* is_valid);
182 static void ValidateCommandExpectations(const ProductContext& ctx,
183 const base::CommandLine& command,
184 const SwitchExpectations& expected,
185 const base::string16& source,
186 bool* is_valid);
187 static void ValidateUninstallCommand(const ProductContext& ctx,
188 const base::CommandLine& command,
189 const base::string16& source,
190 bool* is_valid);
191 static void ValidateRenameCommand(const ProductContext& ctx,
192 bool* is_valid);
193 static void ValidateOldVersionValues(const ProductContext& ctx,
194 bool* is_valid);
195 static void ValidateMultiInstallProduct(const ProductContext& ctx,
196 bool* is_valid);
197 static void ValidateAppCommands(const ProductContext& ctx,
198 bool* is_valid);
199 static void ValidateUsageStats(const ProductContext& ctx,
200 bool* is_valid);
201 static void ValidateProduct(const InstallationState& machine_state,
202 bool system_install,
203 const ProductState& product_state,
204 const ProductRules& rules,
205 bool* is_valid);
207 // A collection of all valid installation types.
208 static const InstallationType kInstallationTypes[];
210 private:
211 DISALLOW_IMPLICIT_CONSTRUCTORS(InstallationValidator);
214 } // namespace installer
216 #endif // CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_