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" |
11 #include "third_party/libjingle/source/talk/base/ssladapter.h" | 11 #include "third_party/libjingle/source/talk/base/ssladapter.h" |
12 | 12 |
13 namespace remoting { | 13 namespace remoting { |
14 | 14 |
15 const uint32 kRunTasksMessageId = 1; | 15 const uint32 kRunTasksMessageId = 1; |
16 const uint32 kStopMessageId = 2; | 16 const uint32 kStopMessageId = 2; |
17 | 17 |
18 class JingleThread::JingleMessagePump : public base::MessagePump { | 18 class JingleThread::JingleMessagePump : public base::MessagePump, |
| 19 public talk_base::MessageHandler { |
19 public: | 20 public: |
20 JingleMessagePump(JingleThread* thread) : thread_(thread) { } | 21 JingleMessagePump(JingleThread* thread) : thread_(thread) { } |
21 | 22 |
22 virtual void Run(Delegate* delegate) { NOTIMPLEMENTED() ;} | 23 virtual void Run(Delegate* delegate) { NOTIMPLEMENTED(); } |
23 virtual void Quit() { NOTIMPLEMENTED(); } | 24 virtual void Quit() { NOTIMPLEMENTED(); } |
24 virtual void ScheduleWork() { | 25 virtual void ScheduleWork() { |
25 thread_->Post(thread_, kRunTasksMessageId); | 26 thread_->Post(this, kRunTasksMessageId); |
26 } | 27 } |
27 virtual void ScheduleDelayedWork(const base::Time& time) { | 28 virtual void ScheduleDelayedWork(const base::Time& time) { |
28 NOTIMPLEMENTED(); | 29 delayed_work_time_ = time; |
| 30 ScheduleNextDelayedTask(); |
| 31 } |
| 32 |
| 33 void OnMessage(talk_base::Message* msg) { |
| 34 DCHECK(msg->message_id == kRunTasksMessageId); |
| 35 |
| 36 // This code is executed whenever we get new message in |message_loop_|. |
| 37 // JingleMessagePump posts new tasks in the jingle thread. |
| 38 // TODO(sergeyu): Remove it when JingleThread moved on Chromium's |
| 39 // base::Thread. |
| 40 base::MessagePump::Delegate* delegate = thread_->message_loop(); |
| 41 // Loop until we run out of work. |
| 42 while (true) { |
| 43 if (!delegate->DoWork()) |
| 44 break; |
| 45 } |
| 46 |
| 47 // Process all pending tasks. |
| 48 while (true) { |
| 49 if (delegate->DoWork()) |
| 50 continue; |
| 51 if (delegate->DoDelayedWork(&delayed_work_time_)) |
| 52 continue; |
| 53 break; |
| 54 } |
| 55 |
| 56 ScheduleNextDelayedTask(); |
29 } | 57 } |
30 | 58 |
31 private: | 59 private: |
| 60 |
| 61 void ScheduleNextDelayedTask() { |
| 62 DCHECK_EQ(thread_->message_loop(), MessageLoop::current()); |
| 63 |
| 64 thread_->Clear(this, kRunTasksMessageId); |
| 65 if (!delayed_work_time_.is_null()) { |
| 66 base::Time now = base::Time::Now(); |
| 67 int delay = static_cast<int>((delayed_work_time_ - now).InMilliseconds()); |
| 68 if (delay > 0) { |
| 69 thread_->PostDelayed(delay, this, kRunTasksMessageId); |
| 70 } else { |
| 71 thread_->Post(this, kRunTasksMessageId); |
| 72 } |
| 73 } |
| 74 } |
| 75 |
32 JingleThread* thread_; | 76 JingleThread* thread_; |
| 77 base::Time delayed_work_time_; |
33 }; | 78 }; |
34 | 79 |
35 class JingleThread::JingleMessageLoop : public MessageLoop { | 80 class JingleThread::JingleMessageLoop : public MessageLoop { |
36 public: | 81 public: |
37 JingleMessageLoop(JingleThread* thread) | 82 JingleMessageLoop(JingleThread* thread) |
38 : MessageLoop(MessageLoop::TYPE_IO) { | 83 : MessageLoop(MessageLoop::TYPE_IO) { |
39 pump_ = new JingleMessagePump(thread); | 84 pump_ = new JingleMessagePump(thread); |
40 } | 85 } |
41 }; | 86 }; |
42 | 87 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 MessageLoop* JingleThread::message_loop() { | 145 MessageLoop* JingleThread::message_loop() { |
101 return message_loop_; | 146 return message_loop_; |
102 } | 147 } |
103 | 148 |
104 // Returns task pump if the thread is running, otherwise NULL is returned. | 149 // Returns task pump if the thread is running, otherwise NULL is returned. |
105 TaskPump* JingleThread::task_pump() { | 150 TaskPump* JingleThread::task_pump() { |
106 return task_pump_; | 151 return task_pump_; |
107 } | 152 } |
108 | 153 |
109 void JingleThread::OnMessage(talk_base::Message* msg) { | 154 void JingleThread::OnMessage(talk_base::Message* msg) { |
110 if (msg->message_id == kRunTasksMessageId) { | 155 DCHECK(msg->message_id == kStopMessageId); |
111 // This code is executed whenever we get new message in |message_loop_|. | 156 |
112 // JingleMessagePump posts new tasks in the jingle thread. | 157 // Stop the thread only if there are no more messages left in the queue, |
113 // TODO(sergeyu): Remove it when JingleThread moved on Chromium's | 158 // otherwise post another task to try again later. |
114 // base::Thread. | 159 if (msgq_.size() > 0 || fPeekKeep_) { |
115 base::MessagePump::Delegate* delegate = message_loop_; | 160 Post(this, kStopMessageId); |
116 // Loop until we run out of work. | 161 } else { |
117 while (true) { | 162 MessageQueue::Quit(); |
118 if (!delegate->DoWork()) | |
119 break; | |
120 } | |
121 } else if (msg->message_id == kStopMessageId) { | |
122 // Stop the thread only if there are no more messages left in the queue, | |
123 // otherwise post another task to try again later. | |
124 if (msgq_.size() > 0 || fPeekKeep_) { | |
125 Post(this, kStopMessageId); | |
126 } else { | |
127 MessageQueue::Quit(); | |
128 } | |
129 } | 163 } |
130 } | 164 } |
131 | 165 |
132 } // namespace remoting | 166 } // namespace remoting |
OLD | NEW |