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