1 // Copyright (c) 2012 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_CALLBACK_WORK_ITEM_H_ 6 #define CHROME_INSTALLER_UTIL_CALLBACK_WORK_ITEM_H_ 7 8 #include "base/callback.h" 9 #include "base/compiler_specific.h" 10 #include "base/gtest_prod_util.h" 11 #include "chrome/installer/util/work_item.h" 12 13 // A work item that invokes a callback on Do() and Rollback(). In the following 14 // example, the function SomeWorkItemCallback() will be invoked when an item 15 // list is processed. 16 // 17 // // A callback invoked to do some work. 18 // bool SomeWorkItemCallback(const CallbackWorkItem& item) { 19 // if (item.IsRollback()) { 20 // // Rollback work goes here. The return value is ignored in this case. 21 // return true; 22 // } 23 // 24 // // Roll forward work goes here. The return value indicates success/failure 25 // // of the item. 26 // return true; 27 // } 28 // 29 // void SomeFunctionThatAddsItemsToAList(WorkItemList* item_list) { 30 // ... 31 // item_list->AddCallbackWorkItem(base::Bind(&SomeWorkItemCallback)); 32 // ... 33 // } 34 class CallbackWorkItem : public WorkItem { 35 public: 36 virtual ~CallbackWorkItem(); 37 38 virtual bool Do() OVERRIDE; 39 virtual void Rollback() OVERRIDE; 40 41 bool IsRollback() const; 42 43 private: 44 friend class WorkItem; 45 46 enum RollState { 47 RS_UNDEFINED, 48 RS_FORWARD, 49 RS_BACKWARD, 50 }; 51 52 CallbackWorkItem(base::Callback<bool(const CallbackWorkItem&)> callback); 53 54 base::Callback<bool(const CallbackWorkItem&)> callback_; 55 RollState roll_state_; 56 57 FRIEND_TEST_ALL_PREFIXES(CallbackWorkItemTest, TestFailure); 58 FRIEND_TEST_ALL_PREFIXES(CallbackWorkItemTest, TestForwardBackward); 59 }; 60 61 #endif // CHROME_INSTALLER_UTIL_CALLBACK_WORK_ITEM_H_ 62