summaryrefslogtreecommitdiffstats
path: root/chrome/installer/util/installation_validator.h
blob: 519718e68e8b76f89061a2690e689acfc8f0a879 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_
#define CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_
#pragma once

#include <map>
#include <string>
#include <utility>
#include <vector>

#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "chrome/installer/util/browser_distribution.h"

class CommandLine;
class FilePath;

namespace installer {

class InstallationState;
class AppCommand;
class ProductState;

// A class that validates the state of an installation.  Violations are logged
// via LOG(ERROR).
class InstallationValidator {
 public:
  class ProductBits {
   public:
    // Bits that form the components of an installation type.
    enum {
      CHROME_SINGLE           = 0x01,
      CHROME_MULTI            = 0x02,
      CHROME_FRAME_SINGLE     = 0x04,
      CHROME_FRAME_MULTI      = 0x08,
      CHROME_FRAME_READY_MODE = 0x10,
    };
  };  // class ProductBits

  // Identifiers of all valid installation types.
  enum InstallationType {
    NO_PRODUCTS = 0,
    CHROME_SINGLE =
        ProductBits::CHROME_SINGLE,
    CHROME_MULTI =
        ProductBits::CHROME_MULTI,
    CHROME_FRAME_SINGLE =
        ProductBits::CHROME_FRAME_SINGLE,
    CHROME_FRAME_SINGLE_CHROME_SINGLE =
        ProductBits::CHROME_FRAME_SINGLE | ProductBits::CHROME_SINGLE,
    CHROME_FRAME_SINGLE_CHROME_MULTI =
        ProductBits::CHROME_FRAME_SINGLE | ProductBits::CHROME_MULTI,
    CHROME_FRAME_MULTI =
        ProductBits::CHROME_FRAME_MULTI,
    CHROME_FRAME_MULTI_CHROME_MULTI =
        ProductBits::CHROME_FRAME_MULTI | ProductBits::CHROME_MULTI,
    CHROME_FRAME_READY_MODE_CHROME_MULTI =
        ProductBits::CHROME_FRAME_READY_MODE | ProductBits::CHROME_MULTI,
  };

  // Validates |machine_state| at user or system level, returning true if valid.
  // |type| is populated in either case, although it is a best-guess when the
  // method returns false.
  static bool ValidateInstallationTypeForState(
      const InstallationState& machine_state,
      bool system_level,
      InstallationType* type);

  // Validates the machine's current installation at user or system level,
  // returning true and populating |type| if valid.
  static bool ValidateInstallationType(bool system_level,
                                       InstallationType* type);

 protected:
  struct ProductContext;
  typedef std::vector<std::pair<std::string, bool> > SwitchExpectations;
  typedef void (*CommandValidatorFn)(const ProductContext& ctx,
                                     const AppCommand& command,
                                     bool* is_valid);
  typedef std::map<std::wstring, CommandValidatorFn> CommandExpectations;

  // An interface to product-specific validation rules.
  class ProductRules {
   public:
    virtual ~ProductRules() { }
    virtual BrowserDistribution::Type distribution_type() const = 0;
    virtual void AddUninstallSwitchExpectations(
        const InstallationState& machine_state,
        bool system_install,
        const ProductState& product_state,
        SwitchExpectations* expectations) const = 0;
    virtual void AddRenameSwitchExpectations(
        const InstallationState& machine_state,
        bool system_install,
        const ProductState& product_state,
        SwitchExpectations* expectations) const = 0;
    // Return true if the rules allow usagestats setting.
    virtual bool UsageStatsAllowed(const ProductState& product_state) const = 0;
  };

  // Validation rules for the Chrome browser.
  class ChromeRules : public ProductRules {
   public:
    virtual BrowserDistribution::Type distribution_type() const OVERRIDE;
    virtual void AddUninstallSwitchExpectations(
        const InstallationState& machine_state,
        bool system_install,
        const ProductState& product_state,
        SwitchExpectations* expectations) const OVERRIDE;
    virtual void AddRenameSwitchExpectations(
        const InstallationState& machine_state,
        bool system_install,
        const ProductState& product_state,
        SwitchExpectations* expectations) const OVERRIDE;
    virtual bool UsageStatsAllowed(
        const ProductState& product_state) const OVERRIDE;
  };

  // Validation rules for Chrome Frame.
  class ChromeFrameRules : public ProductRules {
   public:
    virtual BrowserDistribution::Type distribution_type() const OVERRIDE;
    virtual void AddUninstallSwitchExpectations(
        const InstallationState& machine_state,
        bool system_install,
        const ProductState& product_state,
        SwitchExpectations* expectations) const OVERRIDE;
    virtual void AddRenameSwitchExpectations(
        const InstallationState& machine_state,
        bool system_install,
        const ProductState& product_state,
        SwitchExpectations* expectations) const OVERRIDE;
    virtual bool UsageStatsAllowed(
        const ProductState& product_state) const OVERRIDE;
  };

  // Validation rules for the multi-install Chrome binaries.
  class ChromeBinariesRules : public ProductRules {
   public:
    virtual BrowserDistribution::Type distribution_type() const OVERRIDE;
    virtual void AddUninstallSwitchExpectations(
        const InstallationState& machine_state,
        bool system_install,
        const ProductState& product_state,
        SwitchExpectations* expectations) const OVERRIDE;
    virtual void AddRenameSwitchExpectations(
        const InstallationState& machine_state,
        bool system_install,
        const ProductState& product_state,
        SwitchExpectations* expectations) const OVERRIDE;
    virtual bool UsageStatsAllowed(
        const ProductState& product_state) const OVERRIDE;
  };

  struct ProductContext {
    const InstallationState& machine_state;
    bool system_install;
    BrowserDistribution* dist;
    const ProductState& state;
    const ProductRules& rules;
  };

  static void ValidateQuickEnableCfCommand(const ProductContext& ctx,
                                           const AppCommand& command,
                                           bool* is_valid);
  static void ValidateAppCommandExpectations(
      const ProductContext& ctx,
      const CommandExpectations& expectations,
      bool* is_valid);
  static void ValidateBinariesCommands(const ProductContext& ctx,
                                       bool* is_valid);
  static void ValidateBinaries(const InstallationState& machine_state,
                               bool system_install,
                               const ProductState& binaries_state,
                               bool* is_valid);
  static void ValidateSetupPath(const ProductContext& ctx,
                                const FilePath& setup_exe,
                                const char* purpose,
                                bool* is_valid);
  static void ValidateCommandExpectations(const ProductContext& ctx,
                                          const CommandLine& command,
                                          const SwitchExpectations& expected,
                                          const char* source,
                                          bool* is_valid);
  static void ValidateUninstallCommand(const ProductContext& ctx,
                                       const CommandLine& command,
                                       const char* source,
                                       bool* is_valid);
  static void ValidateRenameCommand(const ProductContext& ctx,
                                    bool* is_valid);
  static void ValidateOldVersionValues(const ProductContext& ctx,
                                       bool* is_valid);
  static void ValidateMultiInstallProduct(const ProductContext& ctx,
                                          bool* is_valid);
  static void ValidateAppCommands(const ProductContext& ctx,
                                  bool* is_valid);
  static void ValidateUsageStats(const ProductContext& ctx,
                                 bool* is_valid);
  static void ValidateProduct(const InstallationState& machine_state,
                              bool system_install,
                              const ProductState& product_state,
                              const ProductRules& rules,
                              bool* is_valid);

  // A collection of all valid installation types.
  static const InstallationType kInstallationTypes[];

 private:
  DISALLOW_IMPLICIT_CONSTRUCTORS(InstallationValidator);
};

}  // namespace installer

#endif  // CHROME_INSTALLER_UTIL_INSTALLATION_VALIDATOR_H_