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