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