work_item_list.h revision cedac228d2dd51db4b79ea1e72c7f249408ee061
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#ifndef CHROME_INSTALLER_UTIL_WORK_ITEM_LIST_H_
6#define CHROME_INSTALLER_UTIL_WORK_ITEM_LIST_H_
7
8#include <windows.h>
9
10#include <list>
11#include <string>
12#include <vector>
13
14#include "base/callback_forward.h"
15#include "base/memory/scoped_ptr.h"
16#include "chrome/installer/util/work_item.h"
17
18namespace base {
19class FilePath;
20}
21
22// A WorkItem subclass that recursively contains a list of WorkItems. Thus it
23// provides functionalities to carry out or roll back the sequence of actions
24// defined by the list of WorkItems it contains.
25// The WorkItems are executed in the same order as they are added to the list.
26class WorkItemList : public WorkItem {
27 public:
28  virtual ~WorkItemList();
29
30  // Execute the WorkItems in the same order as they are added to the list.
31  // It aborts as soon as one WorkItem fails.
32  virtual bool Do();
33
34  // Rollback the WorkItems in the reverse order as they are executed.
35  virtual void Rollback();
36
37  // Add a WorkItem to the list.
38  // A WorkItem can only be added to the list before the list's DO() is called.
39  // Once a WorkItem is added to the list. The list owns the WorkItem.
40  virtual void AddWorkItem(WorkItem* work_item);
41
42  // Add a CallbackWorkItem that invokes a callback.
43  virtual WorkItem* AddCallbackWorkItem(
44      base::Callback<bool(const CallbackWorkItem&)> callback);
45
46  // Add a CopyTreeWorkItem to the list of work items.
47  // See the NOTE in the documentation for the CopyTreeWorkItem class for
48  // special considerations regarding |temp_dir|.
49  virtual WorkItem* AddCopyTreeWorkItem(
50      const std::wstring& source_path,
51      const std::wstring& dest_path,
52      const std::wstring& temp_dir,
53      CopyOverWriteOption overwrite_option,
54      const std::wstring& alternative_path = L"");
55
56  // Add a CreateDirWorkItem that creates a directory at the given path.
57  virtual WorkItem* AddCreateDirWorkItem(const base::FilePath& path);
58
59  // Add a CreateRegKeyWorkItem that creates a registry key at the given
60  // path.
61  virtual WorkItem* AddCreateRegKeyWorkItem(HKEY predefined_root,
62                                            const std::wstring& path,
63                                            REGSAM wow64_access);
64
65  // Add a DeleteRegKeyWorkItem that deletes a registry key from the given
66  // path.
67  virtual WorkItem* AddDeleteRegKeyWorkItem(HKEY predefined_root,
68                                            const std::wstring& path,
69                                            REGSAM wow64_access);
70
71  // Add a DeleteRegValueWorkItem that deletes registry value of type REG_SZ
72  // or REG_DWORD.
73  virtual WorkItem* AddDeleteRegValueWorkItem(HKEY predefined_root,
74                                              const std::wstring& key_path,
75                                              REGSAM wow64_access,
76                                              const std::wstring& value_name);
77
78  // Add a DeleteTreeWorkItem that recursively deletes a file system
79  // hierarchy at the given root path. A key file can be optionally specified
80  // by key_path.
81  virtual WorkItem* AddDeleteTreeWorkItem(
82      const base::FilePath& root_path,
83      const base::FilePath& temp_path,
84      const std::vector<base::FilePath>& key_paths);
85
86  // Same as above but without support for key files.
87  virtual WorkItem* AddDeleteTreeWorkItem(const base::FilePath& root_path,
88                                          const base::FilePath& temp_path);
89
90  // Add a MoveTreeWorkItem to the list of work items.
91  virtual WorkItem* AddMoveTreeWorkItem(const std::wstring& source_path,
92                                        const std::wstring& dest_path,
93                                        const std::wstring& temp_dir,
94                                        MoveTreeOption duplicate_option);
95
96  // Add a SetRegValueWorkItem that sets a registry value with REG_SZ type
97  // at the key with specified path.
98  virtual WorkItem* AddSetRegValueWorkItem(HKEY predefined_root,
99                                           const std::wstring& key_path,
100                                           REGSAM wow64_access,
101                                           const std::wstring& value_name,
102                                           const std::wstring& value_data,
103                                           bool overwrite);
104
105  // Add a SetRegValueWorkItem that sets a registry value with REG_DWORD type
106  // at the key with specified path.
107  virtual WorkItem* AddSetRegValueWorkItem(HKEY predefined_root,
108                                           const std::wstring& key_path,
109                                           REGSAM wow64_access,
110                                           const std::wstring& value_name,
111                                           DWORD value_data,
112                                           bool overwrite);
113
114  // Add a SetRegValueWorkItem that sets a registry value with REG_QWORD type
115  // at the key with specified path.
116  virtual WorkItem* AddSetRegValueWorkItem(HKEY predefined_root,
117                                           const std::wstring& key_path,
118                                           REGSAM wow64_access,
119                                           const std::wstring& value_name,
120                                           int64 value_data,
121                                           bool overwrite);
122
123  // Add a SelfRegWorkItem that registers or unregisters a DLL at the
124  // specified path. If user_level_registration is true, then alternate
125  // registration and unregistration entry point names will be used.
126  virtual WorkItem* AddSelfRegWorkItem(const std::wstring& dll_path,
127                                       bool do_register,
128                                       bool user_level_registration);
129
130 protected:
131  friend class WorkItem;
132
133  typedef std::list<WorkItem*> WorkItems;
134  typedef WorkItems::iterator WorkItemIterator;
135
136  enum ListStatus {
137    // List has not been executed. Ok to add new WorkItem.
138    ADD_ITEM,
139    // List has been executed. Can not add new WorkItem.
140    LIST_EXECUTED,
141    // List has been executed and rolled back. No further action is acceptable.
142    LIST_ROLLED_BACK
143  };
144
145  WorkItemList();
146
147  ListStatus status_;
148
149  // The list of WorkItems, in the order of them being added.
150  WorkItems list_;
151
152  // The list of executed WorkItems, in the reverse order of them being
153  // executed.
154  WorkItems executed_list_;
155};
156
157// A specialization of WorkItemList that executes items in the list on a
158// best-effort basis.  Failure of individual items to execute does not prevent
159// subsequent items from being executed.
160// Also, as the class name suggests, Rollback is not possible.
161class NoRollbackWorkItemList : public WorkItemList {
162 public:
163  virtual ~NoRollbackWorkItemList();
164
165  // Execute the WorkItems in the same order as they are added to the list.
166  // If a WorkItem fails, the function will return failure but all other
167  // WorkItems will still be executed.
168  virtual bool Do();
169
170  // No-op.
171  virtual void Rollback();
172};
173
174#endif  // CHROME_INSTALLER_UTIL_WORK_ITEM_LIST_H_
175