Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / installer / util / work_item.h
bloba8c53769b9726ab77d1fa4d9f3b9da019efe6fc0
1 // Copyright (c) 2011 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 // Base class for managing an action of a sequence of actions to be carried
6 // out during install/update/uninstall. Supports rollback of actions if this
7 // process fails.
9 #ifndef CHROME_INSTALLER_UTIL_WORK_ITEM_H_
10 #define CHROME_INSTALLER_UTIL_WORK_ITEM_H_
12 #include <windows.h>
14 #include <string>
15 #include <vector>
17 #include "base/basictypes.h"
18 #include "base/callback_forward.h"
20 class CallbackWorkItem;
21 class CopyTreeWorkItem;
22 class CreateDirWorkItem;
23 class CreateRegKeyWorkItem;
24 class DeleteTreeWorkItem;
25 class DeleteRegKeyWorkItem;
26 class DeleteRegValueWorkItem;
27 class MoveTreeWorkItem;
28 class SelfRegWorkItem;
29 class SetRegValueWorkItem;
30 class WorkItemList;
32 namespace base {
33 class FilePath;
36 // A base class that defines APIs to perform/rollback an action or a
37 // sequence of actions during install/update/uninstall.
38 class WorkItem {
39 public:
40 // A callback that returns the desired value based on the |existing_value|.
41 // |existing_value| will be empty if the value didn't previously exist or
42 // existed under a non-string type.
43 using GetValueFromExistingCallback =
44 base::Callback<std::wstring(const std::wstring& existing_value)>;
46 // All registry operations can be instructed to operate on a specific view
47 // of the registry by specifying a REGSAM value to the wow64_access parameter.
48 // The wow64_access parameter can be one of:
49 // KEY_WOW64_32KEY - Operate on the 32-bit view.
50 // KEY_WOW64_64KEY - Operate on the 64-bit view.
51 // kWow64Default - Operate on the default view (e.g. 32-bit on 32-bit
52 // systems, and 64-bit on 64-bit systems).
53 // See http://msdn.microsoft.com/en-us/library/windows/desktop/aa384129.aspx
54 static const REGSAM kWow64Default = 0;
55 // Possible states
56 enum CopyOverWriteOption {
57 ALWAYS, // Always overwrite regardless of what existed before.
58 NEVER, // Not used currently.
59 IF_DIFFERENT, // Overwrite if different. Currently only applies to file.
60 IF_NOT_PRESENT, // Copy only if file/directory do not exist already.
61 NEW_NAME_IF_IN_USE // Copy to a new path if dest is in use(only files).
64 // Options for the MoveTree work item.
65 enum MoveTreeOption {
66 ALWAYS_MOVE, // Always attempt to do a move operation.
67 CHECK_DUPLICATES // Only move if the move target is different.
70 // Abstract base class for the conditions used by ConditionWorkItemList.
71 // TODO(robertshield): Move this out of WorkItem.
72 class Condition {
73 public:
74 virtual ~Condition() {}
75 virtual bool ShouldRun() const = 0;
78 virtual ~WorkItem();
80 // Create a CallbackWorkItem that invokes a callback.
81 static CallbackWorkItem* CreateCallbackWorkItem(
82 base::Callback<bool(const CallbackWorkItem&)> callback);
84 // Create a CopyTreeWorkItem that recursively copies a file system hierarchy
85 // from source path to destination path.
86 // * If overwrite_option is ALWAYS, the created CopyTreeWorkItem always
87 // overwrites files.
88 // * If overwrite_option is NEW_NAME_IF_IN_USE, file is copied with an
89 // alternate name specified by alternative_path.
90 static CopyTreeWorkItem* CreateCopyTreeWorkItem(
91 const base::FilePath& source_path,
92 const base::FilePath& dest_path,
93 const base::FilePath& temp_dir,
94 CopyOverWriteOption overwrite_option,
95 const base::FilePath& alternative_path);
97 // Create a CreateDirWorkItem that creates a directory at the given path.
98 static CreateDirWorkItem* CreateCreateDirWorkItem(const base::FilePath& path);
100 // Create a CreateRegKeyWorkItem that creates a registry key at the given
101 // path.
102 static CreateRegKeyWorkItem* CreateCreateRegKeyWorkItem(
103 HKEY predefined_root,
104 const std::wstring& path,
105 REGSAM wow64_access);
107 // Create a DeleteRegKeyWorkItem that deletes a registry key at the given
108 // path.
109 static DeleteRegKeyWorkItem* CreateDeleteRegKeyWorkItem(
110 HKEY predefined_root,
111 const std::wstring& path,
112 REGSAM wow64_access);
114 // Create a DeleteRegValueWorkItem that deletes a registry value
115 static DeleteRegValueWorkItem* CreateDeleteRegValueWorkItem(
116 HKEY predefined_root,
117 const std::wstring& key_path,
118 REGSAM wow64_access,
119 const std::wstring& value_name);
121 // Create a DeleteTreeWorkItem that recursively deletes a file system
122 // hierarchy at the given root path. A key file can be optionally specified
123 // by key_path.
124 static DeleteTreeWorkItem* CreateDeleteTreeWorkItem(
125 const base::FilePath& root_path,
126 const base::FilePath& temp_path,
127 const std::vector<base::FilePath>& key_paths);
129 // Create a MoveTreeWorkItem that recursively moves a file system hierarchy
130 // from source path to destination path.
131 static MoveTreeWorkItem* CreateMoveTreeWorkItem(
132 const base::FilePath& source_path,
133 const base::FilePath& dest_path,
134 const base::FilePath& temp_dir,
135 MoveTreeOption duplicate_option);
137 // Create a SetRegValueWorkItem that sets a registry value with REG_SZ type
138 // at the key with specified path.
139 static SetRegValueWorkItem* CreateSetRegValueWorkItem(
140 HKEY predefined_root,
141 const std::wstring& key_path,
142 REGSAM wow64_access,
143 const std::wstring& value_name,
144 const std::wstring& value_data,
145 bool overwrite);
147 // Create a SetRegValueWorkItem that sets a registry value with REG_DWORD type
148 // at the key with specified path.
149 static SetRegValueWorkItem* CreateSetRegValueWorkItem(
150 HKEY predefined_root,
151 const std::wstring& key_path,
152 REGSAM wow64_access,
153 const std::wstring& value_name,
154 DWORD value_data,
155 bool overwrite);
157 // Create a SetRegValueWorkItem that sets a registry value with REG_QWORD type
158 // at the key with specified path.
159 static SetRegValueWorkItem* CreateSetRegValueWorkItem(
160 HKEY predefined_root,
161 const std::wstring& key_path,
162 REGSAM wow64_access,
163 const std::wstring& value_name,
164 int64 value_data,
165 bool overwrite);
167 // Create a SetRegValueWorkItem that sets a registry value based on the value
168 // provided by |get_value_callback| given the existing value under
169 // |key_path\value_name|.
170 static SetRegValueWorkItem* CreateSetRegValueWorkItem(
171 HKEY predefined_root,
172 const std::wstring& key_path,
173 REGSAM wow64_access,
174 const std::wstring& value_name,
175 const GetValueFromExistingCallback& get_value_callback);
177 // Add a SelfRegWorkItem that registers or unregisters a DLL at the
178 // specified path.
179 static SelfRegWorkItem* CreateSelfRegWorkItem(const std::wstring& dll_path,
180 bool do_register,
181 bool user_level_registration);
183 // Create an empty WorkItemList. A WorkItemList can recursively contains
184 // a list of WorkItems.
185 static WorkItemList* CreateWorkItemList();
187 // Create an empty WorkItemList that cannot be rolled back.
188 // Such a work item list executes all items on a best effort basis and does
189 // not abort execution if an item in the list fails.
190 static WorkItemList* CreateNoRollbackWorkItemList();
192 // Create a conditional work item list that will execute only if
193 // condition->ShouldRun() returns true. The WorkItemList instance
194 // assumes ownership of condition.
195 static WorkItemList* CreateConditionalWorkItemList(Condition* condition);
197 // Perform the actions of WorkItem. Returns true if success, returns false
198 // otherwise.
199 // If the WorkItem is transactional, then Do() is done as a transaction.
200 // If it returns false, there will be no change on the system.
201 virtual bool Do() = 0;
203 // Rollback any actions previously carried out by this WorkItem. If the
204 // WorkItem is transactional, then the previous actions can be fully
205 // rolled back. If the WorkItem is non-transactional, the rollback is a
206 // best effort.
207 virtual void Rollback() = 0;
209 // If called with true, this WorkItem may return true from its Do() method
210 // even on failure and Rollback will have no effect.
211 void set_ignore_failure(bool ignore_failure) {
212 ignore_failure_ = ignore_failure;
215 // Returns true if this WorkItem should ignore failures.
216 bool ignore_failure() const {
217 return ignore_failure_;
220 // Sets an optional log message that a work item may use to print additional
221 // instance-specific information.
222 void set_log_message(const std::string& log_message) {
223 log_message_ = log_message;
226 // Retrieves the optional log message. The retrieved string may be empty.
227 const std::string& log_message() const { return log_message_; }
229 protected:
230 WorkItem();
232 // Specifies whether this work item my fail to complete and yet still
233 // return true from Do().
234 bool ignore_failure_;
236 std::string log_message_;
239 #endif // CHROME_INSTALLER_UTIL_WORK_ITEM_H_