OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef JINGLE_GLUE_THREAD_WRAPPER_H_ | 5 #ifndef JINGLE_GLUE_THREAD_WRAPPER_H_ |
6 #define JINGLE_GLUE_THREAD_WRAPPER_H_ | 6 #define JINGLE_GLUE_THREAD_WRAPPER_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
12 #include "base/message_loop.h" | 12 #include "base/message_loop.h" |
13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
14 #include "base/synchronization/waitable_event.h" | 14 #include "base/synchronization/waitable_event.h" |
15 #include "third_party/libjingle/source/talk/base/thread.h" | 15 #include "third_party/libjingle/source/talk/base/thread.h" |
16 | 16 |
17 namespace jingle_glue { | 17 namespace jingle_glue { |
18 | 18 |
19 // JingleThreadWrapper wraps Chromium threads using talk_base::Thread | 19 // JingleThreadWrapper implements talk_base::Thread interface on top of |
20 // interface. The object must be created by calling | 20 // Chromium's SingleThreadTaskRunner interface. Currently only the bare minimum |
21 // EnsureForCurrentThread(). Each JingleThreadWrapper deletes itself | 21 // that is used by P2P part of libjingle is implemented. There are two ways to |
22 // when MessageLoop is destroyed. Currently only the bare minimum that | 22 // create this object: |
23 // is used by P2P part of libjingle is implemented. | 23 // |
| 24 // - Call EnsureForCurrentMessageLoop(). This approach works only on threads |
| 25 // that have MessageLoop In this case JingleThreadWrapper deletes itself |
| 26 // automatically when MessageLoop is destroyed. |
| 27 // - Using JingleThreadWrapper() constructor. In this case the creating code |
| 28 // must pass a valid task runner for the current thread and also delete the |
| 29 // wrapper later. |
24 class JingleThreadWrapper | 30 class JingleThreadWrapper |
25 : public MessageLoop::DestructionObserver, | 31 : public MessageLoop::DestructionObserver, |
26 public talk_base::Thread { | 32 public talk_base::Thread { |
27 public: | 33 public: |
28 // Create JingleThreadWrapper for the current thread if it hasn't | 34 // Create JingleThreadWrapper for the current thread if it hasn't been created |
29 // been created yet. | 35 // yet. The thread wrapper is destroyed automatically when the current |
30 static void EnsureForCurrentThread(); | 36 // MessageLoop is destroyed. |
| 37 static void EnsureForCurrentMessageLoop(); |
31 | 38 |
32 // Returns thread wrapper for the current thread. NULL is returned | 39 // Returns thread wrapper for the current thread. NULL is returned |
33 // if EnsureForCurrentThread() has never been called for this | 40 // if EnsureForCurrentMessageLoop() has never been called for this |
34 // thread. | 41 // thread. |
35 static JingleThreadWrapper* current(); | 42 static JingleThreadWrapper* current(); |
36 | 43 |
37 JingleThreadWrapper(MessageLoop* message_loop); | 44 explicit JingleThreadWrapper( |
| 45 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 46 virtual ~JingleThreadWrapper(); |
38 | 47 |
39 // Sets whether the thread can be used to send messages | 48 // Sets whether the thread can be used to send messages |
40 // synchronously to another thread using Send() method. Set to false | 49 // synchronously to another thread using Send() method. Set to false |
41 // by default to avoid potential jankiness when Send() used on | 50 // by default to avoid potential jankiness when Send() used on |
42 // renderer thread. It should be set explicitly for threads that | 51 // renderer thread. It should be set explicitly for threads that |
43 // need to call Send() for other threads. | 52 // need to call Send() for other threads. |
44 void set_send_allowed(bool allowed) { send_allowed_ = allowed; } | 53 void set_send_allowed(bool allowed) { send_allowed_ = allowed; } |
45 | 54 |
46 // MessageLoop::DestructionObserver implementation. | 55 // MessageLoop::DestructionObserver implementation. |
47 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; | 56 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 virtual int GetDelay() OVERRIDE; | 92 virtual int GetDelay() OVERRIDE; |
84 | 93 |
85 // talk_base::Thread overrides. | 94 // talk_base::Thread overrides. |
86 virtual void Stop() OVERRIDE; | 95 virtual void Stop() OVERRIDE; |
87 virtual void Run() OVERRIDE; | 96 virtual void Run() OVERRIDE; |
88 | 97 |
89 private: | 98 private: |
90 typedef std::map<int, talk_base::Message> MessagesQueue; | 99 typedef std::map<int, talk_base::Message> MessagesQueue; |
91 struct PendingSend; | 100 struct PendingSend; |
92 | 101 |
93 virtual ~JingleThreadWrapper(); | |
94 | |
95 void PostTaskInternal( | 102 void PostTaskInternal( |
96 int delay_ms, talk_base::MessageHandler* handler, | 103 int delay_ms, talk_base::MessageHandler* handler, |
97 uint32 message_id, talk_base::MessageData* data); | 104 uint32 message_id, talk_base::MessageData* data); |
98 void RunTask(int task_id); | 105 void RunTask(int task_id); |
99 void ProcessPendingSends(); | 106 void ProcessPendingSends(); |
100 | 107 |
101 // Chromium thread used to execute messages posted on this thread. | 108 // Task runner used to execute messages posted on this thread. |
102 MessageLoop* message_loop_; | 109 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
103 | 110 |
104 bool send_allowed_; | 111 bool send_allowed_; |
105 | 112 |
106 // |lock_| must be locked when accessing |messages_|. | 113 // |lock_| must be locked when accessing |messages_|. |
107 base::Lock lock_; | 114 base::Lock lock_; |
108 int last_task_id_; | 115 int last_task_id_; |
109 MessagesQueue messages_; | 116 MessagesQueue messages_; |
110 std::list<PendingSend*> pending_send_messages_; | 117 std::list<PendingSend*> pending_send_messages_; |
111 base::WaitableEvent pending_send_event_; | 118 base::WaitableEvent pending_send_event_; |
| 119 |
| 120 base::WeakPtrFactory<JingleThreadWrapper> weak_ptr_factory_; |
| 121 base::WeakPtr<JingleThreadWrapper> weak_ptr_; |
| 122 |
| 123 DISALLOW_COPY_AND_ASSIGN(JingleThreadWrapper); |
112 }; | 124 }; |
113 | 125 |
114 } | 126 } // namespace jingle_glue |
115 | 127 |
116 #endif // JINGLE_GLUE_THREAD_WRAPPER_H_ | 128 #endif // JINGLE_GLUE_THREAD_WRAPPER_H_ |
OLD | NEW |