Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(344)

Side by Side Diff: remoting/jingle_glue/jingle_thread.cc

Issue 7302002: Make JingleThreadMessageLoop usable without JingleThread. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: - Created 9 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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 namespace {
19 public talk_base::MessageHandler { 19
20 class JingleMessagePump : public base::MessagePump,
21 public talk_base::MessageHandler {
20 public: 22 public:
21 JingleMessagePump(JingleThread* thread) : thread_(thread) { } 23 JingleMessagePump(talk_base::Thread* thread, MessageLoop* message_loop)
24 : thread_(thread), message_loop_(message_loop) {
25 }
22 26
23 virtual void Run(Delegate* delegate) { NOTIMPLEMENTED(); } 27 virtual void Run(Delegate* delegate) { NOTIMPLEMENTED(); }
24 virtual void Quit() { NOTIMPLEMENTED(); } 28 virtual void Quit() { NOTIMPLEMENTED(); }
25 virtual void ScheduleWork() { 29 virtual void ScheduleWork() {
26 thread_->Post(this, kRunTasksMessageId); 30 thread_->Post(this, kRunTasksMessageId);
27 } 31 }
28 virtual void ScheduleDelayedWork(const base::TimeTicks& time) { 32 virtual void ScheduleDelayedWork(const base::TimeTicks& time) {
29 delayed_work_time_ = time; 33 delayed_work_time_ = time;
30 ScheduleNextDelayedTask(); 34 ScheduleNextDelayedTask();
31 } 35 }
32 36
33 void OnMessage(talk_base::Message* msg) { 37 void OnMessage(talk_base::Message* msg) {
34 DCHECK(msg->message_id == kRunTasksMessageId); 38 DCHECK(msg->message_id == kRunTasksMessageId);
35 39
36 // Clear currently pending messages in case there were delayed tasks. 40 // Clear currently pending messages in case there were delayed tasks.
37 // Will schedule it again from ScheduleNextDelayedTask() if neccessary. 41 // Will schedule it again from ScheduleNextDelayedTask() if neccessary.
38 thread_->Clear(this, kRunTasksMessageId); 42 thread_->Clear(this, kRunTasksMessageId);
39 43
40 // This code is executed whenever we get new message in |message_loop_|. 44 // This code is executed whenever we get new message in |message_loop_|.
41 // JingleMessagePump posts new tasks in the jingle thread. 45 // JingleMessagePump posts new tasks in the jingle thread.
42 // TODO(sergeyu): Remove it when JingleThread moved on Chromium's 46 // TODO(sergeyu): Remove it when JingleThread moved on Chromium's
43 // base::Thread. 47 // base::Thread.
44 base::MessagePump::Delegate* delegate = thread_->message_loop(); 48 base::MessagePump::Delegate* delegate = message_loop_;
45 // Process all pending tasks. 49 // Process all pending tasks.
46 while (true) { 50 while (true) {
47 if (delegate->DoWork()) 51 if (delegate->DoWork())
48 continue; 52 continue;
49 if (delegate->DoDelayedWork(&delayed_work_time_)) 53 if (delegate->DoDelayedWork(&delayed_work_time_))
50 continue; 54 continue;
51 break; 55 break;
52 } 56 }
53 57
54 ScheduleNextDelayedTask(); 58 ScheduleNextDelayedTask();
55 } 59 }
56 60
57 private: 61 private:
58 void ScheduleNextDelayedTask() { 62 void ScheduleNextDelayedTask() {
59 DCHECK_EQ(thread_->message_loop(), MessageLoop::current()); 63 DCHECK_EQ(message_loop_, MessageLoop::current());
60 64
61 if (!delayed_work_time_.is_null()) { 65 if (!delayed_work_time_.is_null()) {
62 base::TimeTicks now = base::TimeTicks::Now(); 66 base::TimeTicks now = base::TimeTicks::Now();
63 int delay = static_cast<int>((delayed_work_time_ - now).InMilliseconds()); 67 int delay = static_cast<int>((delayed_work_time_ - now).InMilliseconds());
64 if (delay > 0) { 68 if (delay > 0) {
65 thread_->PostDelayed(delay, this, kRunTasksMessageId); 69 thread_->PostDelayed(delay, this, kRunTasksMessageId);
66 } else { 70 } else {
67 thread_->Post(this, kRunTasksMessageId); 71 thread_->Post(this, kRunTasksMessageId);
68 } 72 }
69 } 73 }
70 } 74 }
71 75
72 JingleThread* thread_; 76 talk_base::Thread* thread_;
77 MessageLoop* message_loop_;
Wez 2011/07/01 20:33:00 Normally the pump is created by the relevant Messa
Sergey Ulanov 2011/07/01 21:14:28 No, because nobody ever calls Run() on this Pump.
73 base::TimeTicks delayed_work_time_; 78 base::TimeTicks delayed_work_time_;
74 }; 79 };
75 80
76 class JingleThread::JingleMessageLoop : public MessageLoop { 81 } // namespace
77 public:
78 JingleMessageLoop(JingleThread* thread)
79 : MessageLoop(MessageLoop::TYPE_IO) {
80 pump_ = new JingleMessagePump(thread);
81 }
82 82
83 void Initialize() { 83 JingleThreadMessageLoop::JingleThreadMessageLoop(talk_base::Thread* thread)
84 jingle_message_loop_state_.reset(new AutoRunState(this)); 84 : MessageLoop(MessageLoop::TYPE_IO) {
85 } 85 pump_ = new JingleMessagePump(thread, this);
86 }
86 87
87 private: 88 JingleThreadMessageLoop::~JingleThreadMessageLoop() {
88 // AutoRunState sets |state_| for this message loop. It needs to be 89 }
89 // created here because we never call Run() or RunAllPending() for 90
90 // the thread. 91 void JingleThreadMessageLoop::Initialize() {
91 scoped_ptr<AutoRunState> jingle_message_loop_state_; 92 jingle_message_loop_state_.reset(new AutoRunState(this));
92 }; 93 }
93 94
94 TaskPump::TaskPump() { 95 TaskPump::TaskPump() {
95 } 96 }
96 97
97 void TaskPump::WakeTasks() { 98 void TaskPump::WakeTasks() {
98 talk_base::Thread::Current()->Post(this); 99 talk_base::Thread::Current()->Post(this);
99 } 100 }
100 101
101 int64 TaskPump::CurrentTime() { 102 int64 TaskPump::CurrentTime() {
102 return static_cast<int64>(talk_base::Time()); 103 return static_cast<int64>(talk_base::Time());
103 } 104 }
104 105
105 void TaskPump::OnMessage(talk_base::Message* pmsg) { 106 void TaskPump::OnMessage(talk_base::Message* pmsg) {
106 RunTasks(); 107 RunTasks();
107 } 108 }
Wez 2011/07/01 20:33:00 Sorry, I'm not clear on what TaskPump is actually
Sergey Ulanov 2011/07/01 21:14:28 It's an implementation of talk_base::TaskRunner th
Wez 2011/07/01 21:49:28 Yes, what I was really asking is that, given it's
Sergey Ulanov 2011/07/01 22:40:54 There is no default implementation of TaskRunner i
108 109
109 JingleThread::JingleThread() 110 JingleThread::JingleThread()
110 : task_pump_(NULL), 111 : task_pump_(NULL),
111 started_event_(true, false), 112 started_event_(true, false),
112 stopped_event_(true, false), 113 stopped_event_(true, false),
113 message_loop_(NULL) { 114 message_loop_(NULL) {
114 } 115 }
115 116
116 JingleThread::~JingleThread() { } 117 JingleThread::~JingleThread() { }
117 118
118 void JingleThread::Start() { 119 void JingleThread::Start() {
119 Thread::Start(); 120 Thread::Start();
120 started_event_.Wait(); 121 started_event_.Wait();
121 } 122 }
122 123
123 void JingleThread::Run() { 124 void JingleThread::Run() {
124 JingleMessageLoop message_loop(this); 125 JingleThreadMessageLoop message_loop(this);
125 message_loop.Initialize(); 126 message_loop.Initialize();
126 message_loop_ = &message_loop; 127 message_loop_ = &message_loop;
127 128
128 TaskPump task_pump; 129 TaskPump task_pump;
129 task_pump_ = &task_pump; 130 task_pump_ = &task_pump;
130 131
131 // Signal after we've initialized |message_loop_| and |task_pump_|. 132 // Signal after we've initialized |message_loop_| and |task_pump_|.
132 started_event_.Signal(); 133 started_event_.Signal();
133 134
134 Thread::Run(); 135 Thread::Run();
(...skipping 11 matching lines...) Expand all
146 stopped_event_.Wait(); 147 stopped_event_.Wait();
147 148
148 // This will wait until the thread is actually finished. 149 // This will wait until the thread is actually finished.
149 Thread::Stop(); 150 Thread::Stop();
150 } 151 }
151 152
152 MessageLoop* JingleThread::message_loop() { 153 MessageLoop* JingleThread::message_loop() {
153 return message_loop_; 154 return message_loop_;
154 } 155 }
155 156
156 // Returns task pump if the thread is running, otherwise NULL is returned. 157 // Returns task pump if the thread is running, otherwise NULL is returned.
Wez 2011/07/01 21:49:28 nit: This comment is already present in the header
Sergey Ulanov 2011/07/01 22:40:54 Done.
157 TaskPump* JingleThread::task_pump() { 158 TaskPump* JingleThread::task_pump() {
Wez 2011/07/01 21:49:28 nit: Presumably this is only callable from the Jin
Sergey Ulanov 2011/07/01 22:40:54 No, XmppSignalStrategy calls it.
158 return task_pump_; 159 return task_pump_;
159 } 160 }
160 161
161 void JingleThread::OnMessage(talk_base::Message* msg) { 162 void JingleThread::OnMessage(talk_base::Message* msg) {
162 DCHECK(msg->message_id == kStopMessageId); 163 DCHECK(msg->message_id == kStopMessageId);
163 164
164 // Stop the thread only if there are no more messages left in the queue, 165 // Stop the thread only if there are no more messages left in the queue,
165 // otherwise post another task to try again later. 166 // otherwise post another task to try again later.
166 if (!msgq_.empty() || fPeekKeep_) { 167 if (!msgq_.empty() || fPeekKeep_) {
167 Post(this, kStopMessageId); 168 Post(this, kStopMessageId);
168 } else { 169 } else {
169 MessageQueue::Quit(); 170 MessageQueue::Quit();
170 } 171 }
171 } 172 }
172 173
173 } // namespace remoting 174 } // namespace remoting
OLDNEW
« remoting/jingle_glue/jingle_thread.h ('K') | « remoting/jingle_glue/jingle_thread.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698