work_item.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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.
8
9#ifndef CHROME_INSTALLER_UTIL_WORK_ITEM_H_
10#define CHROME_INSTALLER_UTIL_WORK_ITEM_H_
11
12#include <windows.h>
13
14#include <string>
15#include <vector>
16
17#include "base/basictypes.h"
18#include "base/callback_forward.h"
19
20class CallbackWorkItem;
21class CopyRegKeyWorkItem;
22class CopyTreeWorkItem;
23class CreateDirWorkItem;
24class CreateRegKeyWorkItem;
25class DeleteTreeWorkItem;
26class DeleteRegKeyWorkItem;
27class DeleteRegValueWorkItem;
28class FilePath;
29class MoveTreeWorkItem;
30class SelfRegWorkItem;
31class SetRegValueWorkItem;
32class WorkItemList;
33
34// A base class that defines APIs to perform/rollback an action or a
35// sequence of actions during install/update/uninstall.
36class WorkItem {
37 public:
38  // Possible states
39  enum CopyOverWriteOption {
40    ALWAYS,  // Always overwrite regardless of what existed before.
41    NEVER,  // Not used currently.
42    IF_DIFFERENT,  // Overwrite if different. Currently only applies to file.
43    IF_NOT_PRESENT,  // Copy only if file/directory do not exist already.
44    NEW_NAME_IF_IN_USE  // Copy to a new path if dest is in use(only files).
45  };
46
47  // Options for the MoveTree work item.
48  enum MoveTreeOption {
49    ALWAYS_MOVE,  // Always attempt to do a move operation.
50    CHECK_DUPLICATES  // Only move if the move target is different.
51  };
52
53  // Abstract base class for the conditions used by ConditionWorkItemList.
54  // TODO(robertshield): Move this out of WorkItem.
55  class Condition {
56   public:
57    virtual ~Condition() {}
58    virtual bool ShouldRun() const = 0;
59  };
60
61  virtual ~WorkItem();
62
63  // Create a CallbackWorkItem that invokes a callback.
64  static CallbackWorkItem* CreateCallbackWorkItem(
65      base::Callback<bool(const CallbackWorkItem&)> callback);
66
67  // Create a CopyRegKeyWorkItem that recursively copies a given registry key.
68  static CopyRegKeyWorkItem* CreateCopyRegKeyWorkItem(
69      HKEY predefined_root,
70      const std::wstring& source_key_path,
71      const std::wstring& dest_key_path,
72      CopyOverWriteOption overwrite_option);
73
74  // Create a CopyTreeWorkItem that recursively copies a file system hierarchy
75  // from source path to destination path.
76  // * If overwrite_option is ALWAYS, the created CopyTreeWorkItem always
77  //   overwrites files.
78  // * If overwrite_option is NEW_NAME_IF_IN_USE, file is copied with an
79  //   alternate name specified by alternative_path.
80  static CopyTreeWorkItem* CreateCopyTreeWorkItem(
81      const FilePath& source_path,
82      const FilePath& dest_path,
83      const FilePath& temp_dir,
84      CopyOverWriteOption overwrite_option,
85      const FilePath& alternative_path);
86
87  // Create a CreateDirWorkItem that creates a directory at the given path.
88  static CreateDirWorkItem* CreateCreateDirWorkItem(const FilePath& path);
89
90  // Create a CreateRegKeyWorkItem that creates a registry key at the given
91  // path.
92  static CreateRegKeyWorkItem* CreateCreateRegKeyWorkItem(
93      HKEY predefined_root, const std::wstring& path);
94
95  // Create a DeleteRegKeyWorkItem that deletes a registry key at the given
96  // path.
97  static DeleteRegKeyWorkItem* CreateDeleteRegKeyWorkItem(
98      HKEY predefined_root, const std::wstring& path);
99
100  // Create a DeleteRegValueWorkItem that deletes a registry value
101  static DeleteRegValueWorkItem* CreateDeleteRegValueWorkItem(
102      HKEY predefined_root,
103      const std::wstring& key_path,
104      const std::wstring& value_name);
105
106  // Create a DeleteTreeWorkItem that recursively deletes a file system
107  // hierarchy at the given root path. A key file can be optionally specified
108  // by key_path.
109  static DeleteTreeWorkItem* CreateDeleteTreeWorkItem(
110      const FilePath& root_path,
111      const FilePath& temp_path,
112      const std::vector<FilePath>& key_paths);
113
114  // Create a MoveTreeWorkItem that recursively moves a file system hierarchy
115  // from source path to destination path.
116  static MoveTreeWorkItem* CreateMoveTreeWorkItem(
117      const FilePath& source_path,
118      const FilePath& dest_path,
119      const FilePath& temp_dir,
120      MoveTreeOption duplicate_option);
121
122  // Create a SetRegValueWorkItem that sets a registry value with REG_SZ type
123  // at the key with specified path.
124  static SetRegValueWorkItem* CreateSetRegValueWorkItem(
125      HKEY predefined_root,
126      const std::wstring& key_path,
127      const std::wstring& value_name,
128      const std::wstring& value_data,
129      bool overwrite);
130
131  // Create a SetRegValueWorkItem that sets a registry value with REG_DWORD type
132  // at the key with specified path.
133  static SetRegValueWorkItem* CreateSetRegValueWorkItem(
134      HKEY predefined_root,
135      const std::wstring& key_path,
136      const std::wstring& value_name,
137      DWORD value_data, bool overwrite);
138
139  // Create a SetRegValueWorkItem that sets a registry value with REG_QWORD type
140  // at the key with specified path.
141  static SetRegValueWorkItem* CreateSetRegValueWorkItem(
142      HKEY predefined_root,
143      const std::wstring& key_path,
144      const std::wstring& value_name,
145      int64 value_data, bool overwrite);
146
147  // Add a SelfRegWorkItem that registers or unregisters a DLL at the
148  // specified path.
149  static SelfRegWorkItem* CreateSelfRegWorkItem(const std::wstring& dll_path,
150                                                bool do_register,
151                                                bool user_level_registration);
152
153  // Create an empty WorkItemList. A WorkItemList can recursively contains
154  // a list of WorkItems.
155  static WorkItemList* CreateWorkItemList();
156
157  // Create an empty WorkItemList that cannot be rolled back.
158  // Such a work item list executes all items on a best effort basis and does
159  // not abort execution if an item in the list fails.
160  static WorkItemList* CreateNoRollbackWorkItemList();
161
162  // Create a conditional work item list that will execute only if
163  // condition->ShouldRun() returns true. The WorkItemList instance
164  // assumes ownership of condition.
165  static WorkItemList* CreateConditionalWorkItemList(Condition* condition);
166
167  // Perform the actions of WorkItem. Returns true if success, returns false
168  // otherwise.
169  // If the WorkItem is transactional, then Do() is done as a transaction.
170  // If it returns false, there will be no change on the system.
171  virtual bool Do() = 0;
172
173  // Rollback any actions previously carried out by this WorkItem. If the
174  // WorkItem is transactional, then the previous actions can be fully
175  // rolled back. If the WorkItem is non-transactional, the rollback is a
176  // best effort.
177  virtual void Rollback() = 0;
178
179  // If called with true, this WorkItem may return true from its Do() method
180  // even on failure and Rollback will have no effect.
181  void set_ignore_failure(bool ignore_failure) {
182    ignore_failure_ = ignore_failure;
183  }
184
185  // Returns true if this WorkItem should ignore failures.
186  bool ignore_failure() const {
187    return ignore_failure_;
188  }
189
190  // Sets an optional log message that a work item may use to print additional
191  // instance-specific information.
192  void set_log_message(const std::string& log_message) {
193    log_message_ = log_message;
194  }
195
196  // Retrieves the optional log message. The retrieved string may be empty.
197  const std::string& log_message() const { return log_message_; }
198
199 protected:
200  WorkItem();
201
202  // Specifies whether this work item my fail to complete and yet still
203  // return true from Do().
204  bool ignore_failure_;
205
206  std::string log_message_;
207};
208
209#endif  // CHROME_INSTALLER_UTIL_WORK_ITEM_H_
210