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 
23 namespace rtc {
24 
25 class 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.
30 class AsyncClosure : public RefCountInterface {
31  public:
32   // Runs the asynchronous task, and triggers a callback to the calling
33   // thread if needed. Should be called from the target thread.
34   virtual void Execute() = 0;
35  protected:
~AsyncClosure()36   ~AsyncClosure() override {}
37 };
38 
39 // Simple closure that doesn't trigger a callback for the calling thread.
40 template <class FunctorT>
41 class FireAndForgetAsyncClosure : public AsyncClosure {
42  public:
FireAndForgetAsyncClosure(const FunctorT & functor)43   explicit FireAndForgetAsyncClosure(const FunctorT& functor)
44       : functor_(functor) {}
Execute()45   virtual void Execute() {
46     functor_();
47   }
48  private:
49   FunctorT functor_;
50 };
51 
52 // Base class for closures that may trigger a callback for the calling thread.
53 // Listens for the "destroyed" signals from the calling thread and the invoker,
54 // and cancels the callback to the calling thread if either is destroyed.
55 class NotifyingAsyncClosureBase : public AsyncClosure,
56                                   public sigslot::has_slots<> {
57  public:
58   ~NotifyingAsyncClosureBase() override;
59 
60  protected:
61   NotifyingAsyncClosureBase(AsyncInvoker* invoker, Thread* calling_thread);
62   void TriggerCallback();
SetCallback(const Callback0<void> & callback)63   void SetCallback(const Callback0<void>& callback) {
64     CritScope cs(&crit_);
65     callback_ = callback;
66   }
CallbackCanceled()67   bool CallbackCanceled() const { return calling_thread_ == NULL; }
68 
69  private:
70   Callback0<void> callback_;
71   CriticalSection crit_;
72   AsyncInvoker* invoker_;
73   Thread* calling_thread_;
74 
75   void CancelCallback();
76 };
77 
78 // Closures that have a non-void return value and require a callback.
79 template <class ReturnT, class FunctorT, class HostT>
80 class NotifyingAsyncClosure : public NotifyingAsyncClosureBase {
81  public:
NotifyingAsyncClosure(AsyncInvoker * invoker,Thread * calling_thread,const FunctorT & functor,void (HostT::* callback)(ReturnT),HostT * callback_host)82   NotifyingAsyncClosure(AsyncInvoker* invoker,
83                         Thread* calling_thread,
84                         const FunctorT& functor,
85                         void (HostT::*callback)(ReturnT),
86                         HostT* callback_host)
87       :  NotifyingAsyncClosureBase(invoker, calling_thread),
88          functor_(functor),
89          callback_(callback),
90          callback_host_(callback_host) {}
Execute()91   virtual void Execute() {
92     ReturnT result = functor_();
93     if (!CallbackCanceled()) {
94       SetCallback(Callback0<void>(Bind(callback_, callback_host_, result)));
95       TriggerCallback();
96     }
97   }
98 
99  private:
100   FunctorT functor_;
101   void (HostT::*callback_)(ReturnT);
102   HostT* callback_host_;
103 };
104 
105 // Closures that have a void return value and require a callback.
106 template <class FunctorT, class HostT>
107 class NotifyingAsyncClosure<void, FunctorT, HostT>
108     : public NotifyingAsyncClosureBase {
109  public:
NotifyingAsyncClosure(AsyncInvoker * invoker,Thread * calling_thread,const FunctorT & functor,void (HostT::* callback)(),HostT * callback_host)110   NotifyingAsyncClosure(AsyncInvoker* invoker,
111                         Thread* calling_thread,
112                         const FunctorT& functor,
113                         void (HostT::*callback)(),
114                         HostT* callback_host)
115       : NotifyingAsyncClosureBase(invoker, calling_thread),
116         functor_(functor) {
117     SetCallback(Callback0<void>(Bind(callback, callback_host)));
118   }
Execute()119   virtual void Execute() {
120     functor_();
121     TriggerCallback();
122   }
123 
124  private:
125   FunctorT functor_;
126 };
127 
128 }  // namespace rtc
129 
130 #endif  // WEBRTC_BASE_ASYNCINVOKER_INL_H_
131