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 // Creates JingleThreadWrapper for |task_runner| that runs tasks on the
40 static scoped_ptr
<JingleThreadWrapper
> WrapTaskRunner(
41 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
43 // Returns thread wrapper for the current thread or nullptr if it doesn't
45 static JingleThreadWrapper
* current();
47 ~JingleThreadWrapper() override
;
49 // Sets whether the thread can be used to send messages
50 // synchronously to another thread using Send() method. Set to false
51 // by default to avoid potential jankiness when Send() used on
52 // renderer thread. It should be set explicitly for threads that
53 // need to call Send() for other threads.
54 void set_send_allowed(bool allowed
) { send_allowed_
= allowed
; }
56 // MessageLoop::DestructionObserver implementation.
57 void WillDestroyCurrentMessageLoop() override
;
59 // rtc::MessageQueue overrides.
60 void Post(rtc::MessageHandler
* phandler
,
62 rtc::MessageData
* pdata
,
63 bool time_sensitive
) override
;
64 void PostDelayed(int delay_ms
,
65 rtc::MessageHandler
* handler
,
67 rtc::MessageData
* data
) override
;
68 void Clear(rtc::MessageHandler
* handler
,
70 rtc::MessageList
* removed
) override
;
71 void Send(rtc::MessageHandler
* handler
,
73 rtc::MessageData
* data
) override
;
75 // Following methods are not supported.They are overriden just to
76 // ensure that they are not called (each of them contain NOTREACHED
77 // in the body). Some of this methods can be implemented if it
78 // becomes neccessary to use libjingle code that calls them.
80 bool IsQuitting() override
;
81 void Restart() override
;
82 bool Get(rtc::Message
* message
, int delay_ms
, bool process_io
) override
;
83 bool Peek(rtc::Message
* message
, int delay_ms
) override
;
84 void PostAt(uint32 timestamp
,
85 rtc::MessageHandler
* handler
,
87 rtc::MessageData
* data
) override
;
88 void Dispatch(rtc::Message
* message
) override
;
89 void ReceiveSends() override
;
90 int GetDelay() override
;
92 // rtc::Thread overrides.
97 typedef std::map
<int, rtc::Message
> MessagesQueue
;
100 explicit JingleThreadWrapper(
101 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner
);
103 void PostTaskInternal(
104 int delay_ms
, rtc::MessageHandler
* handler
,
105 uint32 message_id
, rtc::MessageData
* data
);
106 void RunTask(int task_id
);
107 void ProcessPendingSends();
109 // Task runner used to execute messages posted on this thread.
110 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
114 // |lock_| must be locked when accessing |messages_|.
117 MessagesQueue messages_
;
118 std::list
<PendingSend
*> pending_send_messages_
;
119 base::WaitableEvent pending_send_event_
;
121 base::WeakPtr
<JingleThreadWrapper
> weak_ptr_
;
122 base::WeakPtrFactory
<JingleThreadWrapper
> weak_ptr_factory_
;
124 DISALLOW_COPY_AND_ASSIGN(JingleThreadWrapper
);
127 } // namespace jingle_glue
129 #endif // JINGLE_GLUE_THREAD_WRAPPER_H_