1/*
2 *  Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_ASYNCINVOKER_INL_H_
12#define WEBRTC_BASE_ASYNCINVOKER_INL_H_
13
14#include "webrtc/base/bind.h"
15#include "webrtc/base/callback.h"
16#include "webrtc/base/criticalsection.h"
17#include "webrtc/base/messagehandler.h"
18#include "webrtc/base/refcount.h"
19#include "webrtc/base/scoped_ref_ptr.h"
20#include "webrtc/base/sigslot.h"
21#include "webrtc/base/thread.h"
22
23namespace rtc {
24
25class AsyncInvoker;
26
27// Helper class for AsyncInvoker. Runs a task and triggers a callback
28// on the calling thread if necessary. Instances are ref-counted so their
29// lifetime can be independent of AsyncInvoker.
30class AsyncClosure : public RefCountInterface {
31 public:
32  virtual ~AsyncClosure() {}
33  // Runs the asynchronous task, and triggers a callback to the calling
34  // thread if needed. Should be called from the target thread.
35  virtual void Execute() = 0;
36};
37
38// Simple closure that doesn't trigger a callback for the calling thread.
39template <class FunctorT>
40class FireAndForgetAsyncClosure : public AsyncClosure {
41 public:
42  explicit FireAndForgetAsyncClosure(const FunctorT& functor)
43      : functor_(functor) {}
44  virtual void Execute() {
45    functor_();
46  }
47 private:
48  FunctorT functor_;
49};
50
51// Base class for closures that may trigger a callback for the calling thread.
52// Listens for the "destroyed" signals from the calling thread and the invoker,
53// and cancels the callback to the calling thread if either is destroyed.
54class NotifyingAsyncClosureBase : public AsyncClosure,
55                                  public sigslot::has_slots<> {
56 public:
57  virtual ~NotifyingAsyncClosureBase() { disconnect_all(); }
58
59 protected:
60  NotifyingAsyncClosureBase(AsyncInvoker* invoker, Thread* calling_thread);
61  void TriggerCallback();
62  void SetCallback(const Callback0<void>& callback) {
63    CritScope cs(&crit_);
64    callback_ = callback;
65  }
66  bool CallbackCanceled() const { return calling_thread_ == NULL; }
67
68 private:
69  Callback0<void> callback_;
70  CriticalSection crit_;
71  AsyncInvoker* invoker_;
72  Thread* calling_thread_;
73
74  void CancelCallback();
75};
76
77// Closures that have a non-void return value and require a callback.
78template <class ReturnT, class FunctorT, class HostT>
79class NotifyingAsyncClosure : public NotifyingAsyncClosureBase {
80 public:
81  NotifyingAsyncClosure(AsyncInvoker* invoker,
82                        Thread* calling_thread,
83                        const FunctorT& functor,
84                        void (HostT::*callback)(ReturnT),
85                        HostT* callback_host)
86      :  NotifyingAsyncClosureBase(invoker, calling_thread),
87         functor_(functor),
88         callback_(callback),
89         callback_host_(callback_host) {}
90  virtual void Execute() {
91    ReturnT result = functor_();
92    if (!CallbackCanceled()) {
93      SetCallback(Callback0<void>(Bind(callback_, callback_host_, result)));
94      TriggerCallback();
95    }
96  }
97
98 private:
99  FunctorT functor_;
100  void (HostT::*callback_)(ReturnT);
101  HostT* callback_host_;
102};
103
104// Closures that have a void return value and require a callback.
105template <class FunctorT, class HostT>
106class NotifyingAsyncClosure<void, FunctorT, HostT>
107    : public NotifyingAsyncClosureBase {
108 public:
109  NotifyingAsyncClosure(AsyncInvoker* invoker,
110                        Thread* calling_thread,
111                        const FunctorT& functor,
112                        void (HostT::*callback)(),
113                        HostT* callback_host)
114      : NotifyingAsyncClosureBase(invoker, calling_thread),
115        functor_(functor) {
116    SetCallback(Callback0<void>(Bind(callback, callback_host)));
117  }
118  virtual void Execute() {
119    functor_();
120    TriggerCallback();
121  }
122
123 private:
124  FunctorT functor_;
125};
126
127}  // namespace rtc
128
129#endif  // WEBRTC_BASE_ASYNCINVOKER_INL_H_
130