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 #include "jingle/glue/thread_wrapper.h" |
| 6 |
| 7 namespace jingle_glue { |
| 8 |
| 9 JingleThreadWrapper::JingleThreadWrapper(MessageLoop* message_loop) |
| 10 : message_loop_(message_loop) { |
| 11 DCHECK_EQ(message_loop_, MessageLoop::current()); |
| 12 |
| 13 talk_base::ThreadManager::SetCurrent(this); |
| 14 message_loop_->AddDestructionObserver(this); |
| 15 } |
| 16 |
| 17 JingleThreadWrapper::~JingleThreadWrapper() { |
| 18 } |
| 19 |
| 20 void JingleThreadWrapper::WillDestroyCurrentMessageLoop() { |
| 21 talk_base::ThreadManager::SetCurrent(NULL); |
| 22 message_loop_->RemoveDestructionObserver(this); |
| 23 delete this; |
| 24 } |
| 25 |
| 26 void JingleThreadWrapper::Post( |
| 27 talk_base::MessageHandler* handler, uint32 message_id, |
| 28 talk_base::MessageData* data, bool time_sensitive) { |
| 29 PostTaskInternal(0, handler, message_id, data); |
| 30 } |
| 31 |
| 32 void JingleThreadWrapper::PostDelayed( |
| 33 int delay_ms, talk_base::MessageHandler* handler, |
| 34 uint32 message_id, talk_base::MessageData* data) { |
| 35 PostTaskInternal(delay_ms, handler, message_id, data); |
| 36 } |
| 37 |
| 38 void JingleThreadWrapper::Clear(talk_base::MessageHandler* handler, uint32 id, |
| 39 talk_base::MessageList* removed) { |
| 40 base::AutoLock auto_lock(lock_); |
| 41 |
| 42 for (MessagesQueue::iterator it = messages_.begin(); |
| 43 it != messages_.end();) { |
| 44 if (it->second.Match(handler, id)) { |
| 45 if (removed) { |
| 46 removed->push_back(it->second); |
| 47 } else { |
| 48 delete it->second.pdata; |
| 49 } |
| 50 MessagesQueue::iterator next = it; |
| 51 ++next; |
| 52 messages_.erase(it); |
| 53 it = next; |
| 54 } else { |
| 55 ++it; |
| 56 } |
| 57 } |
| 58 } |
| 59 |
| 60 void JingleThreadWrapper::PostTaskInternal( |
| 61 int delay_ms, talk_base::MessageHandler* handler, |
| 62 uint32 message_id, talk_base::MessageData* data) { |
| 63 int task_id; |
| 64 talk_base::Message message; |
| 65 message.phandler = handler; |
| 66 message.message_id = message_id; |
| 67 message.pdata = data; |
| 68 { |
| 69 base::AutoLock auto_lock(lock_); |
| 70 task_id = ++last_task_id_; |
| 71 messages_.insert(std::pair<int, talk_base::Message>(task_id, message)); |
| 72 } |
| 73 |
| 74 if (delay_ms <= 0) { |
| 75 message_loop_->PostTask(FROM_HERE, NewRunnableMethod( |
| 76 this, &JingleThreadWrapper::RunTask, task_id)); |
| 77 } else { |
| 78 message_loop_->PostDelayedTask( |
| 79 FROM_HERE, |
| 80 NewRunnableMethod(this, &JingleThreadWrapper::RunTask, task_id), |
| 81 delay_ms); |
| 82 } |
| 83 } |
| 84 |
| 85 void JingleThreadWrapper::RunTask(int task_id) { |
| 86 bool have_message = false; |
| 87 talk_base::Message message; |
| 88 { |
| 89 base::AutoLock auto_lock(lock_); |
| 90 MessagesQueue::iterator it = messages_.find(task_id); |
| 91 if (it != messages_.end()) { |
| 92 have_message = true; |
| 93 message = it->second; |
| 94 messages_.erase(it); |
| 95 } |
| 96 } |
| 97 |
| 98 if (have_message) { |
| 99 message.phandler->OnMessage(&message); |
| 100 delete message.pdata; |
| 101 } |
| 102 } |
| 103 |
| 104 // All methods below are marked as not reached. See comments in the |
| 105 // header for more details. |
| 106 void JingleThreadWrapper::Quit() { |
| 107 NOTREACHED(); |
| 108 } |
| 109 |
| 110 bool JingleThreadWrapper::IsQuitting() { |
| 111 NOTREACHED(); |
| 112 return false; |
| 113 } |
| 114 |
| 115 void JingleThreadWrapper::Restart() { |
| 116 NOTREACHED(); |
| 117 } |
| 118 |
| 119 bool JingleThreadWrapper::Get(talk_base::Message*, int, bool) { |
| 120 NOTREACHED(); |
| 121 return false; |
| 122 } |
| 123 |
| 124 bool JingleThreadWrapper::Peek(talk_base::Message*, int) { |
| 125 NOTREACHED(); |
| 126 return false; |
| 127 } |
| 128 |
| 129 void JingleThreadWrapper::PostAt(uint32, talk_base::MessageHandler*, |
| 130 uint32, talk_base::MessageData*) { |
| 131 NOTREACHED(); |
| 132 } |
| 133 |
| 134 void JingleThreadWrapper::Dispatch(talk_base::Message* msg) { |
| 135 NOTREACHED(); |
| 136 } |
| 137 |
| 138 void JingleThreadWrapper::ReceiveSends() { |
| 139 NOTREACHED(); |
| 140 } |
| 141 |
| 142 int JingleThreadWrapper::GetDelay() { |
| 143 NOTREACHED(); |
| 144 return 0; |
| 145 } |
| 146 |
| 147 void JingleThreadWrapper::Stop() { |
| 148 NOTREACHED(); |
| 149 } |
| 150 |
| 151 void JingleThreadWrapper::Run() { |
| 152 NOTREACHED(); |
| 153 } |
| 154 |
| 155 } // namespace jingle_glue |
OLD | NEW |