summaryrefslogtreecommitdiffstats
path: root/chrome/installer/util/callback_work_item.h
blob: f843de62fd861ec5663f2fad77842b6e9167736b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_INSTALLER_UTIL_CALLBACK_WORK_ITEM_H_
#define CHROME_INSTALLER_UTIL_CALLBACK_WORK_ITEM_H_

#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/gtest_prod_util.h"
#include "chrome/installer/util/work_item.h"

// A work item that invokes a callback on Do() and Rollback().  In the following
// example, the function SomeWorkItemCallback() will be invoked when an item
// list is processed.
//
// // A callback invoked to do some work.
// bool SomeWorkItemCallback(const CallbackWorkItem& item) {
//   if (item.IsRollback()) {
//     // Rollback work goes here.  The return value is ignored in this case.
//     return true;
//   }
//
//   // Roll forward work goes here.  The return value indicates success/failure
//   // of the item.
//   return true;
// }
//
// void SomeFunctionThatAddsItemsToAList(WorkItemList* item_list) {
//   ...
//   item_list->AddCallbackWorkItem(base::Bind(&SomeWorkItemCallback));
//   ...
// }
class CallbackWorkItem : public WorkItem {
 public:
  virtual ~CallbackWorkItem();

  virtual bool Do() OVERRIDE;
  virtual void Rollback() OVERRIDE;

  bool IsRollback() const;

 private:
  friend class WorkItem;

  enum RollState {
    RS_UNDEFINED,
    RS_FORWARD,
    RS_BACKWARD,
  };

  CallbackWorkItem(base::Callback<bool(const CallbackWorkItem&)> callback);

  base::Callback<bool(const CallbackWorkItem&)> callback_;
  RollState roll_state_;

  FRIEND_TEST_ALL_PREFIXES(CallbackWorkItemTest, TestFailure);
  FRIEND_TEST_ALL_PREFIXES(CallbackWorkItemTest, TestForwardBackward);
};

#endif  // CHROME_INSTALLER_UTIL_CALLBACK_WORK_ITEM_H_