Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 #include "remoting/jingle_glue/jingle_thread.h" | 5 #include "remoting/jingle_glue/jingle_thread.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/message_pump.h" | 9 #include "base/message_pump.h" |
| 10 #include "base/time.h" | 10 #include "base/time.h" |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 35 | 35 |
| 36 // Clear currently pending messages in case there were delayed tasks. | 36 // Clear currently pending messages in case there were delayed tasks. |
| 37 // Will schedule it again from ScheduleNextDelayedTask() if neccessary. | 37 // Will schedule it again from ScheduleNextDelayedTask() if neccessary. |
| 38 thread_->Clear(this, kRunTasksMessageId); | 38 thread_->Clear(this, kRunTasksMessageId); |
| 39 | 39 |
| 40 // This code is executed whenever we get new message in |message_loop_|. | 40 // This code is executed whenever we get new message in |message_loop_|. |
| 41 // JingleMessagePump posts new tasks in the jingle thread. | 41 // JingleMessagePump posts new tasks in the jingle thread. |
| 42 // TODO(sergeyu): Remove it when JingleThread moved on Chromium's | 42 // TODO(sergeyu): Remove it when JingleThread moved on Chromium's |
| 43 // base::Thread. | 43 // base::Thread. |
| 44 base::MessagePump::Delegate* delegate = thread_->message_loop(); | 44 base::MessagePump::Delegate* delegate = thread_->message_loop(); |
| 45 // Loop until we run out of work. | |
| 46 while (true) { | |
| 47 if (!delegate->DoWork()) | |
| 48 break; | |
| 49 } | |
| 50 | |
| 51 // Process all pending tasks. | 45 // Process all pending tasks. |
| 52 while (true) { | 46 while (true) { |
| 53 if (delegate->DoWork()) | 47 if (delegate->DoWork()) |
| 54 continue; | 48 continue; |
| 55 if (delegate->DoDelayedWork(&delayed_work_time_)) | 49 if (delegate->DoDelayedWork(&delayed_work_time_)) |
| 56 continue; | 50 continue; |
| 57 break; | 51 break; |
| 58 } | 52 } |
| 59 | 53 |
| 60 ScheduleNextDelayedTask(); | 54 ScheduleNextDelayedTask(); |
| 61 } | 55 } |
| 62 | 56 |
| 63 private: | 57 private: |
| 64 | |
| 65 void ScheduleNextDelayedTask() { | 58 void ScheduleNextDelayedTask() { |
| 66 DCHECK_EQ(thread_->message_loop(), MessageLoop::current()); | 59 DCHECK_EQ(thread_->message_loop(), MessageLoop::current()); |
| 67 | 60 |
| 68 if (!delayed_work_time_.is_null()) { | 61 if (!delayed_work_time_.is_null()) { |
| 69 base::TimeTicks now = base::TimeTicks::Now(); | 62 base::TimeTicks now = base::TimeTicks::Now(); |
| 70 int delay = static_cast<int>((delayed_work_time_ - now).InMilliseconds()); | 63 int delay = static_cast<int>((delayed_work_time_ - now).InMilliseconds()); |
| 71 if (delay > 0) { | 64 if (delay > 0) { |
| 72 thread_->PostDelayed(delay, this, kRunTasksMessageId); | 65 thread_->PostDelayed(delay, this, kRunTasksMessageId); |
| 73 } else { | 66 } else { |
| 74 thread_->Post(this, kRunTasksMessageId); | 67 thread_->Post(this, kRunTasksMessageId); |
| 75 } | 68 } |
| 76 } | 69 } |
| 77 } | 70 } |
| 78 | 71 |
| 79 JingleThread* thread_; | 72 JingleThread* thread_; |
| 80 base::TimeTicks delayed_work_time_; | 73 base::TimeTicks delayed_work_time_; |
| 81 }; | 74 }; |
| 82 | 75 |
| 83 class JingleThread::JingleMessageLoop : public MessageLoop { | 76 class JingleThread::JingleMessageLoop : public MessageLoop { |
| 84 public: | 77 public: |
| 85 JingleMessageLoop(JingleThread* thread) | 78 JingleMessageLoop(JingleThread* thread) |
| 86 : MessageLoop(MessageLoop::TYPE_IO) { | 79 : MessageLoop(MessageLoop::TYPE_IO), |
| 80 ALLOW_THIS_IN_INITIALIZER_LIST(jingle_message_loop_state_(this)) { | |
|
awong
2010/12/23 01:16:44
I'd actually throw this out of the constructor. T
Sergey Ulanov
2010/12/23 01:22:50
Yes. Good catch.
| |
| 87 pump_ = new JingleMessagePump(thread); | 81 pump_ = new JingleMessagePump(thread); |
| 88 } | 82 } |
| 83 | |
| 84 private: | |
| 85 // AutoRunState sets |state_| for this message loop. It needs to be | |
| 86 // created here because we never call Run() or RunAllPending() for | |
| 87 // the thread. | |
| 88 AutoRunState jingle_message_loop_state_; | |
| 89 }; | 89 }; |
| 90 | 90 |
| 91 TaskPump::TaskPump() { | 91 TaskPump::TaskPump() { |
| 92 } | 92 } |
| 93 | 93 |
| 94 void TaskPump::WakeTasks() { | 94 void TaskPump::WakeTasks() { |
| 95 talk_base::Thread::Current()->Post(this); | 95 talk_base::Thread::Current()->Post(this); |
| 96 } | 96 } |
| 97 | 97 |
| 98 int64 TaskPump::CurrentTime() { | 98 int64 TaskPump::CurrentTime() { |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 160 // Stop the thread only if there are no more messages left in the queue, | 160 // Stop the thread only if there are no more messages left in the queue, |
| 161 // otherwise post another task to try again later. | 161 // otherwise post another task to try again later. |
| 162 if (msgq_.size() > 0 || fPeekKeep_) { | 162 if (msgq_.size() > 0 || fPeekKeep_) { |
| 163 Post(this, kStopMessageId); | 163 Post(this, kStopMessageId); |
| 164 } else { | 164 } else { |
| 165 MessageQueue::Quit(); | 165 MessageQueue::Quit(); |
| 166 } | 166 } |
| 167 } | 167 } |
| 168 | 168 |
| 169 } // namespace remoting | 169 } // namespace remoting |
| OLD | NEW |