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 #ifndef CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_ 6 #define CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_ 7 8 #include <map> 9 #include <set> 10 #include <utility> 11 #include <vector> 12 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" 17 18 namespace base { 19 class CommandLine; 20 class FilePath; 21 } 22 23 namespace installer { 24 25 class InstallationState; 26 class AppCommand; 27 class ProductState; 28 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, 41 CHROME_APP_HOST = 0x10, 42 }; 43 }; // class ProductBits 44 45 // Identifiers of all valid installation types. 46 enum InstallationType { 47 NO_PRODUCTS = 0, 48 CHROME_SINGLE = 49 ProductBits::CHROME_SINGLE, 50 CHROME_MULTI = 51 ProductBits::CHROME_MULTI, 52 CHROME_FRAME_SINGLE = 53 ProductBits::CHROME_FRAME_SINGLE, 54 CHROME_FRAME_SINGLE_CHROME_SINGLE = 55 ProductBits::CHROME_FRAME_SINGLE | ProductBits::CHROME_SINGLE, 56 CHROME_FRAME_SINGLE_CHROME_MULTI = 57 ProductBits::CHROME_FRAME_SINGLE | ProductBits::CHROME_MULTI, 58 CHROME_FRAME_MULTI = 59 ProductBits::CHROME_FRAME_MULTI, 60 CHROME_FRAME_MULTI_CHROME_MULTI = 61 ProductBits::CHROME_FRAME_MULTI | ProductBits::CHROME_MULTI, 62 CHROME_APP_HOST = 63 ProductBits::CHROME_APP_HOST, 64 CHROME_APP_HOST_CHROME_FRAME_SINGLE = 65 ProductBits::CHROME_APP_HOST | ProductBits::CHROME_FRAME_SINGLE, 66 CHROME_APP_HOST_CHROME_FRAME_SINGLE_CHROME_MULTI = 67 ProductBits::CHROME_APP_HOST | ProductBits::CHROME_FRAME_SINGLE | 68 ProductBits::CHROME_MULTI, 69 CHROME_APP_HOST_CHROME_FRAME_MULTI = 70 ProductBits::CHROME_APP_HOST | ProductBits::CHROME_FRAME_MULTI, 71 CHROME_APP_HOST_CHROME_FRAME_MULTI_CHROME_MULTI = 72 ProductBits::CHROME_APP_HOST | ProductBits::CHROME_FRAME_MULTI | 73 ProductBits::CHROME_MULTI, 74 CHROME_APP_HOST_CHROME_MULTI = 75 ProductBits::CHROME_APP_HOST | ProductBits::CHROME_MULTI, 76 }; 77 78 // Validates |machine_state| at user or system level, returning true if valid. 79 // |type| is populated in either case, although it is a best-guess when the 80 // method returns false. 81 static bool ValidateInstallationTypeForState( 82 const InstallationState& machine_state, 83 bool system_level, 84 InstallationType* type); 85 86 // Validates the machine's current installation at user or system level, 87 // returning true and populating |type| if valid. 88 static bool ValidateInstallationType(bool system_level, 89 InstallationType* type); 90 91 protected: 92 struct ProductContext; 93 typedef std::vector<std::pair<std::string, bool> > SwitchExpectations; 94 typedef void (*CommandValidatorFn)(const ProductContext& ctx, 95 const AppCommand& app_cmd, 96 bool* is_valid); 97 typedef std::map<base::string16, CommandValidatorFn> CommandExpectations; 98 99 // An interface to product-specific validation rules. 100 class ProductRules { 101 public: 102 virtual ~ProductRules() { } 103 virtual BrowserDistribution::Type distribution_type() const = 0; 104 virtual void AddUninstallSwitchExpectations( 105 const ProductContext& ctx, 106 SwitchExpectations* expectations) const = 0; 107 virtual void AddRenameSwitchExpectations( 108 const ProductContext& ctx, 109 SwitchExpectations* expectations) const = 0; 110 // Return true if the rules allow usagestats setting. 111 virtual bool UsageStatsAllowed(const ProductContext& ctx) const = 0; 112 }; 113 114 // Validation rules for the Chrome browser. 115 class ChromeRules : public ProductRules { 116 public: 117 virtual BrowserDistribution::Type distribution_type() const OVERRIDE; 118 virtual void AddUninstallSwitchExpectations( 119 const ProductContext& ctx, 120 SwitchExpectations* expectations) const OVERRIDE; 121 virtual void AddRenameSwitchExpectations( 122 const ProductContext& ctx, 123 SwitchExpectations* expectations) const OVERRIDE; 124 virtual bool UsageStatsAllowed(const ProductContext& ctx) const OVERRIDE; 125 }; 126 127 // Validation rules for Chrome Frame. 128 class ChromeFrameRules : public ProductRules { 129 public: 130 virtual BrowserDistribution::Type distribution_type() const OVERRIDE; 131 virtual void AddUninstallSwitchExpectations( 132 const ProductContext& ctx, 133 SwitchExpectations* expectations) const OVERRIDE; 134 virtual void AddRenameSwitchExpectations( 135 const ProductContext& ctx, 136 SwitchExpectations* expectations) const OVERRIDE; 137 virtual bool UsageStatsAllowed(const ProductContext& ctx) const OVERRIDE; 138 }; 139 140 // Validation rules for Chrome App Host. 141 class ChromeAppHostRules : public ProductRules { 142 public: 143 virtual BrowserDistribution::Type distribution_type() const OVERRIDE; 144 virtual void AddUninstallSwitchExpectations( 145 const ProductContext& ctx, 146 SwitchExpectations* expectations) const OVERRIDE; 147 virtual void AddRenameSwitchExpectations( 148 const ProductContext& ctx, 149 SwitchExpectations* expectations) const OVERRIDE; 150 virtual bool UsageStatsAllowed(const ProductContext& ctx) const OVERRIDE; 151 }; 152 153 // Validation rules for the multi-install Chrome binaries. 154 class ChromeBinariesRules : public ProductRules { 155 public: 156 virtual BrowserDistribution::Type distribution_type() const OVERRIDE; 157 virtual void AddUninstallSwitchExpectations( 158 const ProductContext& ctx, 159 SwitchExpectations* expectations) const OVERRIDE; 160 virtual void AddRenameSwitchExpectations( 161 const ProductContext& ctx, 162 SwitchExpectations* expectations) const OVERRIDE; 163 virtual bool UsageStatsAllowed(const ProductContext& ctx) const OVERRIDE; 164 }; 165 166 struct ProductContext { 167 ProductContext(const InstallationState& machine_state_in, 168 bool system_install_in, 169 const ProductState& state_in, 170 const ProductRules& rules_in) 171 : machine_state(machine_state_in), 172 system_install(system_install_in), 173 dist(BrowserDistribution::GetSpecificDistribution( 174 rules_in.distribution_type())), 175 state(state_in), 176 rules(rules_in) { 177 } 178 179 const InstallationState& machine_state; 180 bool system_install; 181 BrowserDistribution* dist; 182 const ProductState& state; 183 const ProductRules& rules; 184 }; 185 186 // Helper to validate the values of bool elements in AppCommand, and to output 187 // error messages. |flag_expect| is a bit mask specifying the expected 188 // presence/absence of bool variables. 189 static void ValidateAppCommandFlags( 190 const ProductContext& ctx, 191 const AppCommand& app_cmd, 192 const std::set<base::string16>& flags_expected, 193 const base::string16& name, 194 bool* is_valid); 195 static void ValidateOnOsUpgradeCommand(const ProductContext& ctx, 196 const AppCommand& app_cmd, 197 bool* is_valid); 198 static void ValidateQueryEULAAcceptanceCommand(const ProductContext& ctx, 199 const AppCommand& app_cmd, 200 bool* is_valid); 201 static void ValidateQuickEnableApplicationHostCommand( 202 const ProductContext& ctx, 203 const AppCommand& app_cmd, 204 bool* is_valid); 205 206 static void ValidateAppCommandExpectations( 207 const ProductContext& ctx, 208 const CommandExpectations& expectations, 209 bool* is_valid); 210 static void ValidateBinariesCommands(const ProductContext& ctx, 211 bool* is_valid); 212 static void ValidateBinaries(const InstallationState& machine_state, 213 bool system_install, 214 const ProductState& binaries_state, 215 bool* is_valid); 216 static void ValidateSetupPath(const ProductContext& ctx, 217 const base::FilePath& setup_exe, 218 const base::string16& purpose, 219 bool* is_valid); 220 static void ValidateCommandExpectations(const ProductContext& ctx, 221 const base::CommandLine& command, 222 const SwitchExpectations& expected, 223 const base::string16& source, 224 bool* is_valid); 225 static void ValidateUninstallCommand(const ProductContext& ctx, 226 const base::CommandLine& command, 227 const base::string16& source, 228 bool* is_valid); 229 static void ValidateRenameCommand(const ProductContext& ctx, 230 bool* is_valid); 231 static void ValidateOldVersionValues(const ProductContext& ctx, 232 bool* is_valid); 233 static void ValidateMultiInstallProduct(const ProductContext& ctx, 234 bool* is_valid); 235 static void ValidateAppCommands(const ProductContext& ctx, 236 bool* is_valid); 237 static void ValidateUsageStats(const ProductContext& ctx, 238 bool* is_valid); 239 static void ValidateProduct(const InstallationState& machine_state, 240 bool system_install, 241 const ProductState& product_state, 242 const ProductRules& rules, 243 bool* is_valid); 244 245 // A collection of all valid installation types. 246 static const InstallationType kInstallationTypes[]; 247 248 private: 249 DISALLOW_IMPLICIT_CONSTRUCTORS(InstallationValidator); 250 }; 251 252 } // namespace installer 253 254 #endif // CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_ 255