Android: Get rid of extra dup()s on launching child processes
[chromium-blink-merge.git] / chrome / installer / util / installer_state.h
blobaa056c13c88186f9f4ff9c74b07431a0dfcd8df6
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_INSTALLER_STATE_H_
6 #define CHROME_INSTALLER_UTIL_INSTALLER_STATE_H_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/files/file_path.h"
14 #include "base/logging.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/version.h"
18 #include "chrome/installer/util/browser_distribution.h"
19 #include "chrome/installer/util/product.h"
20 #include "chrome/installer/util/util_constants.h"
22 #if defined(OS_WIN)
23 #include <windows.h> // NOLINT
24 #endif
26 namespace base {
27 class CommandLine;
30 namespace installer {
32 class ChannelInfo;
33 class InstallationState;
34 class MasterPreferences;
36 class ProductState;
38 typedef std::vector<Product*> Products;
40 // Encapsulates the state of the current installation operation. Only valid
41 // for installs and upgrades (not for uninstalls or non-install commands).
42 // This class interprets the command-line arguments and master preferences and
43 // determines the operations to be performed. For example, the Chrome Binaries
44 // are automatically added if required in multi-install mode.
45 // TODO(erikwright): This is now used a fair bit during uninstall, and
46 // InstallerState::Initialize() contains a lot of code for uninstall. The class
47 // comment should probably be updated.
48 // TODO(grt): Rename to InstallerEngine/Conductor or somesuch?
49 class InstallerState {
50 public:
51 enum Level {
52 UNKNOWN_LEVEL,
53 USER_LEVEL,
54 SYSTEM_LEVEL
57 enum PackageType {
58 UNKNOWN_PACKAGE_TYPE,
59 SINGLE_PACKAGE,
60 MULTI_PACKAGE
63 enum Operation {
64 UNINITIALIZED,
65 SINGLE_INSTALL_OR_UPDATE,
66 MULTI_INSTALL,
67 MULTI_UPDATE,
68 UNINSTALL
71 // Constructs an uninitialized instance; see Initialize().
72 InstallerState();
74 // Constructs an initialized but empty instance.
75 explicit InstallerState(Level level);
77 // Initializes this object based on the current operation.
78 void Initialize(const base::CommandLine& command_line,
79 const MasterPreferences& prefs,
80 const InstallationState& machine_state);
82 // Adds a product constructed on the basis of |state|, setting this object's
83 // msi flag if |state| is msi-installed. Returns the product that was added,
84 // or NULL if |state| is incompatible with this object. Ownership is not
85 // passed to the caller.
86 Product* AddProductFromState(BrowserDistribution::Type type,
87 const ProductState& state);
89 // Returns the product that was added, or NULL if |product| is incompatible
90 // with this object. Ownership of |product| is taken by this object, while
91 // ownership of the return value is not passed to the caller.
92 Product* AddProduct(scoped_ptr<Product>* product);
94 // Removes |product| from the set of products to be operated on. The object
95 // pointed to by |product| is freed. Returns false if |product| is not
96 // present in the set.
97 bool RemoveProduct(const Product* product);
99 // The level (user or system) of this operation.
100 Level level() const { return level_; }
102 // The package type (single or multi) of this operation.
103 PackageType package_type() const { return package_type_; }
105 // An identifier of this operation.
106 Operation operation() const { return operation_; }
108 // A convenience method returning level() == SYSTEM_LEVEL.
109 // TODO(grt): Eradicate the bool in favor of the enum.
110 bool system_install() const;
112 // A convenience method returning package_type() == MULTI_PACKAGE.
113 // TODO(grt): Eradicate the bool in favor of the enum.
114 bool is_multi_install() const;
116 // The full path to the place where the operand resides.
117 const base::FilePath& target_path() const { return target_path_; }
119 // True if the "msi" preference is set or if a product with the "msi" state
120 // flag is set is to be operated on.
121 bool is_msi() const { return msi_; }
123 // True if the --verbose-logging command-line flag is set or if the
124 // verbose_logging master preferences option is true.
125 bool verbose_logging() const { return verbose_logging_; }
127 #if defined(OS_WIN)
128 HKEY root_key() const { return root_key_; }
129 #endif
131 // The ClientState key by which we interact with Google Update.
132 const std::wstring& state_key() const { return state_key_; }
134 // Convenience method to return the type of the BrowserDistribution associated
135 // with the ClientState key we will be interacting with.
136 BrowserDistribution::Type state_type() const { return state_type_; }
138 // Returns the BrowserDistribution instance corresponding to the binaries for
139 // this run if we're operating on a multi-package product.
140 BrowserDistribution* multi_package_binaries_distribution() const {
141 DCHECK(package_type_ == MULTI_PACKAGE);
142 DCHECK(multi_package_distribution_ != NULL);
143 return multi_package_distribution_;
146 const Products& products() const { return products_.get(); }
148 // Returns the product of the desired type, or NULL if none found.
149 const Product* FindProduct(BrowserDistribution::Type distribution_type) const;
151 // Returns the currently installed version in |target_path|, or NULL if no
152 // products are installed. Ownership is passed to the caller.
153 base::Version* GetCurrentVersion(
154 const InstallationState& machine_state) const;
156 // Returns the critical update version if all of the following are true:
157 // * --critical-update-version=CUV was specified on the command-line.
158 // * current_version == NULL or current_version < CUV.
159 // * new_version >= CUV.
160 // Otherwise, returns an invalid version.
161 base::Version DetermineCriticalVersion(
162 const base::Version* current_version,
163 const base::Version& new_version) const;
165 // Returns whether or not there is currently a Chrome Frame instance running.
166 // Note that there isn't a mechanism to lock Chrome Frame in place, so Chrome
167 // Frame may either exit or start up after this is called.
168 bool IsChromeFrameRunning(const InstallationState& machine_state) const;
170 // Returns true if any of the binaries from a multi-install Chrome Frame that
171 // has been migrated to single-install are still in use.
172 bool AreBinariesInUse(const InstallationState& machine_state) const;
174 // Returns the path to the installer under Chrome version folder
175 // (for example <target_path>\Google\Chrome\Application\<Version>\Installer)
176 base::FilePath GetInstallerDirectory(const base::Version& version) const;
178 // Try to delete all directories under |temp_path| whose versions are less
179 // than |new_version| and not equal to |existing_version|. |existing_version|
180 // may be NULL.
181 void RemoveOldVersionDirectories(const base::Version& new_version,
182 base::Version* existing_version,
183 const base::FilePath& temp_path) const;
185 // Adds to |com_dll_list| the list of COM DLLs that are to be registered
186 // and/or unregistered. The list may be empty.
187 void AddComDllList(std::vector<base::FilePath>* com_dll_list) const;
189 // See InstallUtil::UpdateInstallerStage.
190 void UpdateStage(installer::InstallerStage stage) const;
192 // For a MULTI_INSTALL or MULTI_UPDATE operation, updates the Google Update
193 // "ap" values for all products being operated on.
194 void UpdateChannels() const;
196 // Sets installer result information in the registry for consumption by Google
197 // Update. The InstallerResult value is set to 0 (SUCCESS) or 1
198 // (FAILED_CUSTOM_ERROR) depending on whether |status| maps to success or not.
199 // |status| itself is written to the InstallerError value.
200 // |string_resource_id|, if non-zero, identifies a localized string written to
201 // the InstallerResultUIString value. |launch_cmd|, if non-NULL and
202 // non-empty, is written to the InstallerSuccessLaunchCmdLine value.
203 void WriteInstallerResult(InstallStatus status,
204 int string_resource_id,
205 const std::wstring* launch_cmd) const;
207 // Returns true if this install needs to register an Active Setup command.
208 bool RequiresActiveSetup() const;
210 protected:
211 // Bits for the |file_bits| argument of AnyExistsAndIsInUse.
212 enum {
213 CHROME_DLL = 1 << 0,
214 CHROME_FRAME_DLL = 1 << 1,
215 CHROME_FRAME_HELPER_DLL = 1 << 2,
216 CHROME_FRAME_HELPER_EXE = 1 << 3,
217 NUM_BINARIES = 4
220 // Returns true if |file| exists and cannot be opened for exclusive write
221 // access.
222 static bool IsFileInUse(const base::FilePath& file);
224 // Clears the instance to an uninitialized state.
225 void Clear();
227 // Returns true if any file corresponding to a bit in |file_bits| (from the
228 // enum above) for the currently installed version exists and is in use.
229 bool AnyExistsAndIsInUse(const InstallationState& machine_state,
230 uint32 file_bits) const;
231 base::FilePath GetDefaultProductInstallPath(BrowserDistribution* dist) const;
232 bool CanAddProduct(const Product& product,
233 const base::FilePath* product_dir) const;
234 Product* AddProductInDirectory(const base::FilePath* product_dir,
235 scoped_ptr<Product>* product);
236 Product* AddProductFromPreferences(
237 BrowserDistribution::Type distribution_type,
238 const MasterPreferences& prefs,
239 const InstallationState& machine_state);
240 bool IsMultiInstallUpdate(const MasterPreferences& prefs,
241 const InstallationState& machine_state);
243 // Enumerates all files named one of
244 // [chrome.exe, old_chrome.exe, new_chrome.exe] in target_path_ and
245 // returns their version numbers in a set.
246 void GetExistingExeVersions(std::set<std::string>* existing_versions) const;
248 // Sets this object's level and updates the root_key_ accordingly.
249 void set_level(Level level);
251 // Sets this object's package type and updates the multi_package_distribution_
252 // accordingly.
253 void set_package_type(PackageType type);
255 Operation operation_;
256 base::FilePath target_path_;
257 std::wstring state_key_;
258 BrowserDistribution::Type state_type_;
259 ScopedVector<Product> products_;
260 BrowserDistribution* multi_package_distribution_;
261 base::Version critical_update_version_;
262 Level level_;
263 PackageType package_type_;
264 #if defined(OS_WIN)
265 HKEY root_key_;
266 #endif
267 bool msi_;
268 bool verbose_logging_;
270 private:
271 DISALLOW_COPY_AND_ASSIGN(InstallerState);
272 }; // class InstallerState
274 } // namespace installer
276 #endif // CHROME_INSTALLER_UTIL_INSTALLER_STATE_H_