summaryrefslogtreecommitdiff
path: root/chromium/components/payments/content/initialization_task.h
blob: 16c094ca0dd7aa7d9a5b75ecc35adc22c7157a8a (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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2019 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 COMPONENTS_PAYMENTS_CONTENT_INITIALIZATION_TASK_H_
#define COMPONENTS_PAYMENTS_CONTENT_INITIALIZATION_TASK_H_

#include "base/macros.h"
#include "base/observer_list.h"
#include "base/observer_list_types.h"

namespace payments {

// An interface for a task that takes time to initialize. Useful for monitoring
// initialization of several asynchronous tasks.
//
// Sample usage:
//
//   class Foo : public InitializationTask {
//    public:
//     Foo() {}
//
//     ~Foo() override {}
//
//     // InitializationTask:
//     bool IsInitialized() override {
//       return is_initialized_;
//     }
//
//     void SomeAction() {
//       is_initialized_ = true;
//       NotifyInitialized();
//     }
//
//    private:
//     bool is_initialized_ = false;
//   };
class InitializationTask {
 public:
  // An interface for an observer of an initialization task.
  //
  // Sample usage:
  //
  //   class Bar : public InitializationTask::Observer {
  //    public:
  //     explicit Bar(Foo* foo) : foo_(foo) {
  //       if (foo_->IsInitialized()) {
  //         UseFoo();
  //       } else {
  //         foo_->AddInitializationObserver(this);
  //       }
  //     }
  //
  //     ~Bar() override {}
  //
  //     // InitializationTask::Observer:
  //     void OnInitialized(InitializationTask* initialization_task) override {
  //       initialization_task->RemoveInitializationObserver(this);
  //       UseFoo();
  //     }
  //
  //     void UseFoo() {
  //       foo_->DoSomethingInteresting();
  //     }
  //
  //     private:
  //       // Not owned. Must outlive Bar.
  //       Foo* foo_;
  //   };
  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override;

    // Called when the observed task has initialized.
    virtual void OnInitialized(InitializationTask* initialization_task) = 0;
  };

  InitializationTask();
  virtual ~InitializationTask();

  // Add the |observer| to be notified of initialization.
  void AddInitializationObserver(Observer* observer);

  // Remove the |observer| of initialization.
  void RemoveInitializationObserver(Observer* observer);

  // Notify all observers of initialization. Should be called at most once.
  void NotifyInitialized();

  // Whether the task has initialized.
  virtual bool IsInitialized() const = 0;

 private:
  // The list of observers for this initialization task.
  base::ObserverList<Observer> observers_;

  // Whether NotifyInitialized() has been called.
  bool has_notified_ = false;

  DISALLOW_COPY_AND_ASSIGN(InitializationTask);
};

}  // namespace payments

#endif  // COMPONENTS_PAYMENTS_CONTENT_INITIALIZATION_TASK_H_