Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / libjingle / source / talk / base / asyncinvoker-inl.h
1 /*
2  * libjingle
3  * Copyright 2014 Google Inc.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  *  1. Redistributions of source code must retain the above copyright notice,
9  *     this list of conditions and the following disclaimer.
10  *  2. Redistributions in binary form must reproduce the above copyright notice,
11  *     this list of conditions and the following disclaimer in the documentation
12  *     and/or other materials provided with the distribution.
13  *  3. The name of the author may not be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 #ifndef TALK_BASE_ASYNCINVOKER_INL_H_
29 #define TALK_BASE_ASYNCINVOKER_INL_H_
30
31 #include "talk/base/bind.h"
32 #include "talk/base/callback.h"
33 #include "talk/base/criticalsection.h"
34 #include "talk/base/messagehandler.h"
35 #include "talk/base/refcount.h"
36 #include "talk/base/scoped_ref_ptr.h"
37 #include "talk/base/sigslot.h"
38 #include "talk/base/thread.h"
39
40 namespace talk_base {
41
42 class AsyncInvoker;
43
44 // Helper class for AsyncInvoker. Runs a task and triggers a callback
45 // on the calling thread if necessary. Instances are ref-counted so their
46 // lifetime can be independent of AsyncInvoker.
47 class AsyncClosure : public RefCountInterface {
48  public:
49   virtual ~AsyncClosure() {}
50   // Runs the asynchronous task, and triggers a callback to the calling
51   // thread if needed. Should be called from the target thread.
52   virtual void Execute() = 0;
53 };
54
55 // Simple closure that doesn't trigger a callback for the calling thread.
56 template <class FunctorT>
57 class FireAndForgetAsyncClosure : public AsyncClosure {
58  public:
59   explicit FireAndForgetAsyncClosure(const FunctorT& functor)
60       : functor_(functor) {}
61   virtual void Execute() {
62     functor_();
63   }
64  private:
65   FunctorT functor_;
66 };
67
68 // Base class for closures that may trigger a callback for the calling thread.
69 // Listens for the "destroyed" signals from the calling thread and the invoker,
70 // and cancels the callback to the calling thread if either is destroyed.
71 class NotifyingAsyncClosureBase : public AsyncClosure,
72                                   public sigslot::has_slots<> {
73  public:
74   virtual ~NotifyingAsyncClosureBase() { disconnect_all(); }
75
76  protected:
77   NotifyingAsyncClosureBase(AsyncInvoker* invoker, Thread* calling_thread);
78   void TriggerCallback();
79   void SetCallback(const Callback0<void>& callback) {
80     CritScope cs(&crit_);
81     callback_ = callback;
82   }
83   bool CallbackCanceled() const { return calling_thread_ == NULL; }
84
85  private:
86   Callback0<void> callback_;
87   CriticalSection crit_;
88   AsyncInvoker* invoker_;
89   Thread* calling_thread_;
90
91   void CancelCallback();
92 };
93
94 // Closures that have a non-void return value and require a callback.
95 template <class ReturnT, class FunctorT, class HostT>
96 class NotifyingAsyncClosure : public NotifyingAsyncClosureBase {
97  public:
98   NotifyingAsyncClosure(AsyncInvoker* invoker,
99                         Thread* calling_thread,
100                         const FunctorT& functor,
101                         void (HostT::*callback)(ReturnT),
102                         HostT* callback_host)
103       :  NotifyingAsyncClosureBase(invoker, calling_thread),
104          functor_(functor),
105          callback_(callback),
106          callback_host_(callback_host) {}
107   virtual void Execute() {
108     ReturnT result = functor_();
109     if (!CallbackCanceled()) {
110       SetCallback(Callback0<void>(Bind(callback_, callback_host_, result)));
111       TriggerCallback();
112     }
113   }
114
115  private:
116   FunctorT functor_;
117   void (HostT::*callback_)(ReturnT);
118   HostT* callback_host_;
119 };
120
121 // Closures that have a void return value and require a callback.
122 template <class FunctorT, class HostT>
123 class NotifyingAsyncClosure<void, FunctorT, HostT>
124     : public NotifyingAsyncClosureBase {
125  public:
126   NotifyingAsyncClosure(AsyncInvoker* invoker,
127                         Thread* calling_thread,
128                         const FunctorT& functor,
129                         void (HostT::*callback)(),
130                         HostT* callback_host)
131       : NotifyingAsyncClosureBase(invoker, calling_thread),
132         functor_(functor) {
133     SetCallback(Callback0<void>(Bind(callback, callback_host)));
134   }
135   virtual void Execute() {
136     functor_();
137     TriggerCallback();
138   }
139
140  private:
141   FunctorT functor_;
142 };
143
144 }  // namespace talk_base
145
146 #endif  // TALK_BASE_ASYNCINVOKER_INL_H_