OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #ifndef JINGLE_GLUE_THREAD_WRAPPER_H_ |
| 6 #define JINGLE_GLUE_THREAD_WRAPPER_H_ |
| 7 |
| 8 #include <map> |
| 9 |
| 10 #include "base/message_loop.h" |
| 11 #include "base/synchronization/lock.h" |
| 12 #include "third_party/libjingle/source/talk/base/thread.h" |
| 13 |
| 14 class MessageLoop; |
| 15 |
| 16 namespace jingle_glue { |
| 17 |
| 18 // JingleThreadWrapper wraps Chromium threads using talk_base::Thread |
| 19 // interface. The object must be created on a thread it belongs |
| 20 // to. Each JingleThreadWrapper deletes itself when MessageLoop is |
| 21 // destroyed. Currently only the bare minimum that is used by P2P |
| 22 // part of libjingle is implemented. |
| 23 class JingleThreadWrapper |
| 24 : public MessageLoop::DestructionObserver, |
| 25 public talk_base::Thread { |
| 26 public: |
| 27 JingleThreadWrapper(MessageLoop* message_loop); |
| 28 |
| 29 // MessageLoop::DestructionObserver implementation. |
| 30 virtual void WillDestroyCurrentMessageLoop() OVERRIDE; |
| 31 |
| 32 // talk_base::MessageQueue overrides. |
| 33 virtual void Post(talk_base::MessageHandler *phandler, uint32 id = 0, |
| 34 talk_base::MessageData *pdata = NULL, |
| 35 bool time_sensitive = false) OVERRIDE; |
| 36 virtual void PostDelayed( |
| 37 int delay_ms, talk_base::MessageHandler* handler, uint32 id = 0, |
| 38 talk_base::MessageData* data = NULL) OVERRIDE; |
| 39 virtual void Clear(talk_base::MessageHandler* handler, |
| 40 uint32 id = talk_base::MQID_ANY, |
| 41 talk_base::MessageList* removed = NULL) OVERRIDE; |
| 42 |
| 43 // Following methods are not supported.They are overriden just to |
| 44 // ensure that they are not called (each of them contain NOTREACHED |
| 45 // in the body). Some of this methods can be implemented if it |
| 46 // becomes neccessary to use libjingle code that calls them. |
| 47 virtual void Quit() OVERRIDE; |
| 48 virtual bool IsQuitting() OVERRIDE; |
| 49 virtual void Restart() OVERRIDE; |
| 50 virtual bool Get(talk_base::Message* msg, int delay_ms = talk_base::kForever, |
| 51 bool process_io = true) OVERRIDE; |
| 52 virtual bool Peek(talk_base::Message* msg, int delay_ms = 0) OVERRIDE; |
| 53 virtual void PostAt( |
| 54 uint32 timestamp, talk_base::MessageHandler* handler, |
| 55 uint32 id = 0, talk_base::MessageData* data = NULL) OVERRIDE; |
| 56 virtual void Dispatch(talk_base::Message* msg) OVERRIDE; |
| 57 virtual void ReceiveSends() OVERRIDE; |
| 58 virtual int GetDelay() OVERRIDE; |
| 59 |
| 60 // talk_base::Thread overrides. |
| 61 virtual void Stop() OVERRIDE; |
| 62 virtual void Run() OVERRIDE; |
| 63 |
| 64 private: |
| 65 typedef std::map<int, talk_base::Message> MessagesQueue; |
| 66 |
| 67 virtual ~JingleThreadWrapper(); |
| 68 |
| 69 void PostTaskInternal( |
| 70 int delay_ms, talk_base::MessageHandler* handler, |
| 71 uint32 message_id, talk_base::MessageData* data); |
| 72 void RunTask(int task_id); |
| 73 |
| 74 // Chromium thread used to execute messages posted on this thread. |
| 75 MessageLoop* message_loop_; |
| 76 |
| 77 // |lock_| must be locked when accessing |messages_|. |
| 78 base::Lock lock_; |
| 79 int last_task_id_; |
| 80 MessagesQueue messages_; |
| 81 }; |
| 82 |
| 83 } |
| 84 |
| 85 // Safe to disable refcounting because JingleThreadWrapper deletes |
| 86 // itself with the thread. |
| 87 DISABLE_RUNNABLE_METHOD_REFCOUNT(jingle_glue::JingleThreadWrapper); |
| 88 |
| 89 #endif // JINGLE_GLUE_THREAD_WRAPPER_H_ |
OLD | NEW |