Home | History | Annotate | Download | only in util
      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 
     18 namespace base {
     19 class 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.
     26 class 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 CopyRegKeyWorkItem that recursively copies a given registry key.
     47   virtual WorkItem* AddCopyRegKeyWorkItem(HKEY predefined_root,
     48                                           const std::wstring& source_key_path,
     49                                           const std::wstring& dest_key_path,
     50                                           CopyOverWriteOption overwrite_option);
     51 
     52   // Add a CopyTreeWorkItem to the list of work items.
     53   // See the NOTE in the documentation for the CopyTreeWorkItem class for
     54   // special considerations regarding |temp_dir|.
     55   virtual WorkItem* AddCopyTreeWorkItem(
     56       const std::wstring& source_path,
     57       const std::wstring& dest_path,
     58       const std::wstring& temp_dir,
     59       CopyOverWriteOption overwrite_option,
     60       const std::wstring& alternative_path = L"");
     61 
     62   // Add a CreateDirWorkItem that creates a directory at the given path.
     63   virtual WorkItem* AddCreateDirWorkItem(const base::FilePath& path);
     64 
     65   // Add a CreateRegKeyWorkItem that creates a registry key at the given
     66   // path.
     67   virtual WorkItem* AddCreateRegKeyWorkItem(HKEY predefined_root,
     68                                             const std::wstring& path);
     69 
     70   // Add a DeleteRegKeyWorkItem that deletes a registry key from the given
     71   // path.
     72   virtual WorkItem* AddDeleteRegKeyWorkItem(HKEY predefined_root,
     73                                             const std::wstring& path);
     74 
     75   // Add a DeleteRegValueWorkItem that deletes registry value of type REG_SZ
     76   // or REG_DWORD.
     77   virtual WorkItem* AddDeleteRegValueWorkItem(HKEY predefined_root,
     78                                               const std::wstring& key_path,
     79                                               const std::wstring& value_name);
     80 
     81   // Add a DeleteTreeWorkItem that recursively deletes a file system
     82   // hierarchy at the given root path. A key file can be optionally specified
     83   // by key_path.
     84   virtual WorkItem* AddDeleteTreeWorkItem(
     85       const base::FilePath& root_path,
     86       const base::FilePath& temp_path,
     87       const std::vector<base::FilePath>& key_paths);
     88 
     89   // Same as above but without support for key files.
     90   virtual WorkItem* AddDeleteTreeWorkItem(const base::FilePath& root_path,
     91                                           const base::FilePath& temp_path);
     92 
     93   // Add a MoveTreeWorkItem to the list of work items.
     94   virtual WorkItem* AddMoveTreeWorkItem(const std::wstring& source_path,
     95                                         const std::wstring& dest_path,
     96                                         const std::wstring& temp_dir,
     97                                         MoveTreeOption duplicate_option);
     98 
     99   // Add a SetRegValueWorkItem that sets a registry value with REG_SZ type
    100   // at the key with specified path.
    101   virtual WorkItem* AddSetRegValueWorkItem(HKEY predefined_root,
    102                                            const std::wstring& key_path,
    103                                            const std::wstring& value_name,
    104                                            const std::wstring& value_data,
    105                                            bool overwrite);
    106 
    107   // Add a SetRegValueWorkItem that sets a registry value with REG_DWORD type
    108   // at the key with specified path.
    109   virtual WorkItem* AddSetRegValueWorkItem(HKEY predefined_root,
    110                                            const std::wstring& key_path,
    111                                            const std::wstring& value_name,
    112                                            DWORD value_data,
    113                                            bool overwrite);
    114 
    115   // Add a SetRegValueWorkItem that sets a registry value with REG_QWORD type
    116   // at the key with specified path.
    117   virtual WorkItem* AddSetRegValueWorkItem(HKEY predefined_root,
    118                                            const std::wstring& key_path,
    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.
    161 class 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