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

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

Issue 7312013: Minor cleanups in JingleSession and JingleSessionManager. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address comments. 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) 2011 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 namespace { 18 namespace {
19 19
20 class JingleMessagePump : public base::MessagePump, 20 class JingleMessagePump : public base::MessagePump,
21 public talk_base::MessageHandler { 21 public talk_base::MessageHandler {
22 public: 22 public:
23 JingleMessagePump(talk_base::Thread* thread) 23 JingleMessagePump(talk_base::Thread* thread)
24 : thread_(thread), delegate_(NULL) { 24 : thread_(thread), delegate_(NULL), stopping_(false) {
25 } 25 }
26 26
27 virtual void Run(Delegate* delegate) { 27 virtual void Run(Delegate* delegate) {
28 delegate_ = delegate; 28 delegate_ = delegate;
29 29
30 talk_base::Thread::Current()->Thread::Run(); 30 thread_->Thread::Run();
31 // Call Restart() so that we can run again. 31 // Call Restart() so that we can run again.
32 talk_base::Thread::Current()->Restart(); 32 thread_->Restart();
33 33
34 delegate_ = NULL; 34 delegate_ = NULL;
35 } 35 }
36 36
37 virtual void Quit() { 37 virtual void Quit() {
38 talk_base::Thread::Current()->Quit(); 38 if (!stopping_) {
39 stopping_ = true;
40
41 // Shutdown gracefully: make sure that we excute all messages
42 // left in the queue before exiting. Thread::Quit() would not do
43 // that.
44 thread_->Post(this, kStopMessageId);
45 }
Wez 2011/07/06 23:26:55 Aren't these changes from CL 7227017?
39 } 46 }
40 47
41 virtual void ScheduleWork() { 48 virtual void ScheduleWork() {
42 thread_->Post(this, kRunTasksMessageId); 49 thread_->Post(this, kRunTasksMessageId);
43 } 50 }
44 51
45 virtual void ScheduleDelayedWork(const base::TimeTicks& time) { 52 virtual void ScheduleDelayedWork(const base::TimeTicks& time) {
46 delayed_work_time_ = time; 53 delayed_work_time_ = time;
47 ScheduleNextDelayedTask(); 54 ScheduleNextDelayedTask();
48 } 55 }
49 56
50 void OnMessage(talk_base::Message* msg) { 57 void OnMessage(talk_base::Message* msg) {
51 DCHECK(msg->message_id == kRunTasksMessageId); 58 if (msg->message_id == kRunTasksMessageId) {
52 DCHECK(delegate_); 59 DCHECK(delegate_);
53 60
54 // Clear currently pending messages in case there were delayed tasks. 61 // Clear currently pending messages in case there were delayed tasks.
55 // Will schedule it again from ScheduleNextDelayedTask() if neccessary. 62 // Will schedule it again from ScheduleNextDelayedTask() if neccessary.
56 thread_->Clear(this, kRunTasksMessageId); 63 thread_->Clear(this, kRunTasksMessageId);
57 64
58 // Process all pending tasks. 65 // Process all pending tasks.
59 while (true) { 66 while (true) {
60 if (delegate_->DoWork()) 67 if (delegate_->DoWork())
61 continue; 68 continue;
62 if (delegate_->DoDelayedWork(&delayed_work_time_)) 69 if (delegate_->DoDelayedWork(&delayed_work_time_))
63 continue; 70 continue;
64 if (delegate_->DoIdleWork()) 71 if (delegate_->DoIdleWork())
65 continue; 72 continue;
66 break; 73 break;
74 }
75
76 ScheduleNextDelayedTask();
77 } else if (msg->message_id == kStopMessageId) {
78 DCHECK(stopping_);
79 // Stop the thread only if there are no more non-delayed
80 // messages left in the queue, otherwise post another task to
81 // try again later.
82 int delay = thread_->GetDelay();
83 if (delay > 0 || delay == talk_base::kForever) {
84 stopping_ = false;
85 thread_->Quit();
86 } else {
87 thread_->Post(this, kStopMessageId);
88 }
89 } else {
90 NOTREACHED();
67 } 91 }
92 }
68 93
69 ScheduleNextDelayedTask();
70 }
71 94
72 private: 95 private:
73 void ScheduleNextDelayedTask() { 96 void ScheduleNextDelayedTask() {
74 if (!delayed_work_time_.is_null()) { 97 if (!delayed_work_time_.is_null()) {
75 base::TimeTicks now = base::TimeTicks::Now(); 98 base::TimeTicks now = base::TimeTicks::Now();
76 int delay = static_cast<int>((delayed_work_time_ - now).InMilliseconds()); 99 int delay = static_cast<int>((delayed_work_time_ - now).InMilliseconds());
77 if (delay > 0) { 100 if (delay > 0) {
78 thread_->PostDelayed(delay, this, kRunTasksMessageId); 101 thread_->PostDelayed(delay, this, kRunTasksMessageId);
79 } else { 102 } else {
80 thread_->Post(this, kRunTasksMessageId); 103 thread_->Post(this, kRunTasksMessageId);
81 } 104 }
82 } 105 }
83 } 106 }
84 107
85 talk_base::Thread* thread_; 108 talk_base::Thread* thread_;
86 Delegate* delegate_; 109 Delegate* delegate_;
87 base::TimeTicks delayed_work_time_; 110 base::TimeTicks delayed_work_time_;
111 bool stopping_;
88 }; 112 };
89 113
90 } // namespace 114 } // namespace
91 115
92 JingleThreadMessageLoop::JingleThreadMessageLoop(talk_base::Thread* thread) 116 JingleThreadMessageLoop::JingleThreadMessageLoop(talk_base::Thread* thread)
93 : MessageLoop(MessageLoop::TYPE_IO) { 117 : MessageLoop(MessageLoop::TYPE_IO) {
94 pump_ = new JingleMessagePump(thread); 118 pump_ = new JingleMessagePump(thread);
95 } 119 }
96 120
97 JingleThreadMessageLoop::~JingleThreadMessageLoop() { 121 JingleThreadMessageLoop::~JingleThreadMessageLoop() {
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
138 162
139 message_loop.Run(); 163 message_loop.Run();
140 164
141 stopped_event_.Signal(); 165 stopped_event_.Signal();
142 166
143 task_pump_ = NULL; 167 task_pump_ = NULL;
144 message_loop_ = NULL; 168 message_loop_ = NULL;
145 } 169 }
146 170
147 void JingleThread::Stop() { 171 void JingleThread::Stop() {
148 // Shutdown gracefully: make sure that we excute all messages left in the 172 message_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
149 // queue before exiting. Thread::Stop() would not do that.
150 Post(this, kStopMessageId);
151 stopped_event_.Wait(); 173 stopped_event_.Wait();
152 174
153 // This will wait until the thread is actually finished. 175 // This will wait until the thread is actually finished.
154 Thread::Stop(); 176 Thread::Stop();
155 } 177 }
156 178
157 MessageLoop* JingleThread::message_loop() { 179 MessageLoop* JingleThread::message_loop() {
158 return message_loop_; 180 return message_loop_;
159 } 181 }
160 182
161 TaskPump* JingleThread::task_pump() { 183 TaskPump* JingleThread::task_pump() {
162 return task_pump_; 184 return task_pump_;
163 } 185 }
164 186
165 void JingleThread::OnMessage(talk_base::Message* msg) {
166 DCHECK(msg->message_id == kStopMessageId);
167
168 // Stop the thread only if there are no more messages left in the queue,
169 // otherwise post another task to try again later.
170 if (!msgq_.empty() || fPeekKeep_) {
171 Post(this, kStopMessageId);
172 } else {
173 MessageQueue::Quit();
174 }
175 }
176
177 } // namespace remoting 187 } // namespace remoting
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698