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
3 // found in the LICENSE file.
5 #ifndef JINGLE_GLUE_THREAD_WRAPPER_H_
6 #define JINGLE_GLUE_THREAD_WRAPPER_H_
11 #include "base/compiler_specific.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/synchronization/lock.h"
14 #include "base/synchronization/waitable_event.h"
15 #include "third_party/webrtc/base/thread.h"
17 namespace jingle_glue
{
19 // JingleThreadWrapper implements rtc::Thread interface on top of
20 // Chromium's SingleThreadTaskRunner interface. Currently only the bare minimum
21 // that is used by P2P part of libjingle is implemented. There are two ways to
22 // create this object:
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
30 class JingleThreadWrapper
: public base::MessageLoop::DestructionObserver
,
33 // Create JingleThreadWrapper for the current thread if it hasn't been created
34 // yet. The thread wrapper is destroyed automatically when the current
35 // MessageLoop is destroyed.
36 static void EnsureForCurrentMessageLoop();
38 // Returns thread wrapper for the current thread. NULL is returned
39 // if EnsureForCurrentMessageLoop() has never been called for this
41 static JingleThreadWrapper
* current();
43 explicit JingleThreadWrapper(
44 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
45 ~JingleThreadWrapper() override
;
47 // Sets whether the thread can be used to send messages
48 // synchronously to another thread using Send() method. Set to false
49 // by default to avoid potential jankiness when Send() used on
50 // renderer thread. It should be set explicitly for threads that
51 // need to call Send() for other threads.
52 void set_send_allowed(bool allowed
) { send_allowed_
= allowed
; }
54 // MessageLoop::DestructionObserver implementation.
55 void WillDestroyCurrentMessageLoop() override
;
57 // rtc::MessageQueue overrides.
58 void Post(rtc::MessageHandler
* phandler
,
60 rtc::MessageData
* pdata
,
61 bool time_sensitive
) override
;
62 void PostDelayed(int delay_ms
,
63 rtc::MessageHandler
* handler
,
65 rtc::MessageData
* data
) override
;
66 void Clear(rtc::MessageHandler
* handler
,
68 rtc::MessageList
* removed
) override
;
69 void Send(rtc::MessageHandler
* handler
,
71 rtc::MessageData
* data
) override
;
73 // Following methods are not supported.They are overriden just to
74 // ensure that they are not called (each of them contain NOTREACHED
75 // in the body). Some of this methods can be implemented if it
76 // becomes neccessary to use libjingle code that calls them.
78 bool IsQuitting() override
;
79 void Restart() override
;
80 bool Get(rtc::Message
* message
, int delay_ms
, bool process_io
) override
;
81 bool Peek(rtc::Message
* message
, int delay_ms
) override
;
82 void PostAt(uint32 timestamp
,
83 rtc::MessageHandler
* handler
,
85 rtc::MessageData
* data
) override
;
86 void Dispatch(rtc::Message
* message
) override
;
87 void ReceiveSends() override
;
88 int GetDelay() override
;
90 // rtc::Thread overrides.
95 typedef std::map
<int, rtc::Message
> MessagesQueue
;
98 void PostTaskInternal(
99 int delay_ms
, rtc::MessageHandler
* handler
,
100 uint32 message_id
, rtc::MessageData
* data
);
101 void RunTask(int task_id
);
102 void ProcessPendingSends();
104 // Task runner used to execute messages posted on this thread.
105 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
109 // |lock_| must be locked when accessing |messages_|.
112 MessagesQueue messages_
;
113 std::list
<PendingSend
*> pending_send_messages_
;
114 base::WaitableEvent pending_send_event_
;
116 base::WeakPtr
<JingleThreadWrapper
> weak_ptr_
;
117 base::WeakPtrFactory
<JingleThreadWrapper
> weak_ptr_factory_
;
119 DISALLOW_COPY_AND_ASSIGN(JingleThreadWrapper
);
122 } // namespace jingle_glue
124 #endif // JINGLE_GLUE_THREAD_WRAPPER_H_