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/host/chromoting_host.h" | 5 #include "remoting/host/chromoting_host.h" |
6 | 6 |
7 #include "base/stl_util-inl.h" | 7 #include "base/stl_util-inl.h" |
8 #include "base/task.h" | 8 #include "base/task.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "remoting/base/constants.h" | 10 #include "remoting/base/constants.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 capturer_(capturer), | 29 capturer_(capturer), |
30 encoder_(encoder), | 30 encoder_(encoder), |
31 executor_(executor), | 31 executor_(executor), |
32 state_(kInitial) { | 32 state_(kInitial) { |
33 } | 33 } |
34 | 34 |
35 ChromotingHost::~ChromotingHost() { | 35 ChromotingHost::~ChromotingHost() { |
36 } | 36 } |
37 | 37 |
38 void ChromotingHost::Start(Task* shutdown_task) { | 38 void ChromotingHost::Start(Task* shutdown_task) { |
| 39 if (MessageLoop::current() != context_->main_message_loop()) { |
| 40 context_->main_message_loop()->PostTask( |
| 41 FROM_HERE, |
| 42 NewRunnableMethod(this, &ChromotingHost::Start, shutdown_task)); |
| 43 return; |
| 44 } |
| 45 |
| 46 DCHECK(!jingle_client_); |
| 47 DCHECK(shutdown_task); |
| 48 |
| 49 // Make sure this object is not started. |
| 50 { |
| 51 AutoLock auto_lock(lock_); |
| 52 if (state_ != kInitial) |
| 53 return; |
| 54 state_ = kStarted; |
| 55 } |
| 56 |
39 // Get capturer to set up it's initial configuration. | 57 // Get capturer to set up it's initial configuration. |
40 capturer_->ScreenConfigurationChanged(); | 58 capturer_->ScreenConfigurationChanged(); |
41 | 59 |
42 // Submit a task to perform host registration. We'll also start | 60 // Save the shutdown task. |
43 // listening to connection if registration is done. | 61 shutdown_task_.reset(shutdown_task); |
44 context_->main_message_loop()->PostTask( | 62 |
45 FROM_HERE, | 63 std::string xmpp_login; |
46 NewRunnableMethod(this, &ChromotingHost::DoStart, shutdown_task)); | 64 std::string xmpp_auth_token; |
| 65 if (!config_->GetString(kXmppLoginConfigPath, &xmpp_login) || |
| 66 !config_->GetString(kXmppAuthTokenConfigPath, &xmpp_auth_token)) { |
| 67 LOG(ERROR) << "XMPP credentials are not defined in config."; |
| 68 return; |
| 69 } |
| 70 |
| 71 access_verifier_.Init(config_); |
| 72 |
| 73 // Connect to the talk network with a JingleClient. |
| 74 jingle_client_ = new JingleClient(context_->jingle_thread()); |
| 75 jingle_client_->Init(xmpp_login, xmpp_auth_token, |
| 76 kChromotingTokenServiceName, this); |
| 77 |
| 78 heartbeat_sender_ = new HeartbeatSender(); |
| 79 if (!heartbeat_sender_->Init(config_, jingle_client_.get())) { |
| 80 LOG(ERROR) << "Failed to initialize HeartbeatSender."; |
| 81 return; |
| 82 } |
47 } | 83 } |
48 | 84 |
49 // This method is called when we need to destroy the host process. | 85 // This method is called when we need to destroy the host process. |
50 void ChromotingHost::Shutdown() { | 86 void ChromotingHost::Shutdown() { |
51 context_->main_message_loop()->PostTask( | 87 if (MessageLoop::current() != context_->main_message_loop()) { |
52 FROM_HERE, | 88 context_->main_message_loop()->PostTask( |
53 NewRunnableMethod(this, &ChromotingHost::DoShutdown)); | 89 FROM_HERE, |
| 90 NewRunnableMethod(this, &ChromotingHost::Shutdown)); |
| 91 return; |
| 92 } |
| 93 |
| 94 // No-op if this object is not started yet. |
| 95 { |
| 96 AutoLock auto_lock(lock_); |
| 97 if (state_ != kStarted) |
| 98 return; |
| 99 state_ = kStopped; |
| 100 } |
| 101 |
| 102 // Tell the session to pause and then disconnect all clients. |
| 103 if (session_.get()) { |
| 104 session_->Pause(); |
| 105 session_->RemoveAllClients(); |
| 106 } |
| 107 |
| 108 // Disconnect all clients. |
| 109 if (client_) { |
| 110 client_->Disconnect(); |
| 111 } |
| 112 |
| 113 // Disconnect from the talk network. |
| 114 if (jingle_client_) { |
| 115 jingle_client_->Close(); |
| 116 } |
| 117 |
| 118 // Stop the heartbeat sender. |
| 119 if (heartbeat_sender_) { |
| 120 heartbeat_sender_->Stop(); |
| 121 } |
| 122 |
| 123 // Lastly call the shutdown task. |
| 124 if (shutdown_task_.get()) { |
| 125 shutdown_task_->Run(); |
| 126 } |
54 } | 127 } |
55 | 128 |
56 // This method is called if a client is connected to this object. | 129 // This method is called if a client is connected to this object. |
57 void ChromotingHost::OnClientConnected(ClientConnection* client) { | 130 void ChromotingHost::OnClientConnected(ClientConnection* client) { |
58 DCHECK_EQ(context_->main_message_loop(), MessageLoop::current()); | 131 DCHECK_EQ(context_->main_message_loop(), MessageLoop::current()); |
59 | 132 |
60 // Create a new RecordSession if there was none. | 133 // Create a new RecordSession if there was none. |
61 if (!session_.get()) { | 134 if (!session_.get()) { |
62 // Then we create a SessionManager passing the message loops that | 135 // Then we create a SessionManager passing the message loops that |
63 // it should run on. | 136 // it should run on. |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 // Start heartbeating after we've connected. | 213 // Start heartbeating after we've connected. |
141 heartbeat_sender_->Start(); | 214 heartbeat_sender_->Start(); |
142 } else if (state == JingleClient::CLOSED) { | 215 } else if (state == JingleClient::CLOSED) { |
143 LOG(INFO) << "Host disconnected from talk network." << std::endl; | 216 LOG(INFO) << "Host disconnected from talk network." << std::endl; |
144 | 217 |
145 // Stop heartbeating. | 218 // Stop heartbeating. |
146 heartbeat_sender_->Stop(); | 219 heartbeat_sender_->Stop(); |
147 | 220 |
148 // TODO(sergeyu): We should try reconnecting here instead of terminating | 221 // TODO(sergeyu): We should try reconnecting here instead of terminating |
149 // the host. | 222 // the host. |
150 // Post a shutdown task to properly shutdown the chromoting host. | 223 Shutdown(); |
151 context_->main_message_loop()->PostTask( | |
152 FROM_HERE, NewRunnableMethod(this, &ChromotingHost::DoShutdown)); | |
153 } | 224 } |
154 } | 225 } |
155 | 226 |
156 bool ChromotingHost::OnAcceptConnection( | 227 bool ChromotingHost::OnAcceptConnection( |
157 JingleClient* jingle_client, const std::string& jid, | 228 JingleClient* jingle_client, const std::string& jid, |
158 JingleChannel::Callback** channel_callback) { | 229 JingleChannel::Callback** channel_callback) { |
159 AutoLock auto_lock(lock_); | 230 AutoLock auto_lock(lock_); |
160 if (state_ != kStarted) | 231 if (state_ != kStarted) |
161 return false; | 232 return false; |
162 | 233 |
(...skipping 24 matching lines...) Expand all Loading... |
187 return; | 258 return; |
188 | 259 |
189 DCHECK_EQ(jingle_client_.get(), jingle_client); | 260 DCHECK_EQ(jingle_client_.get(), jingle_client); |
190 | 261 |
191 // Since the session manager has not started, it is still safe to access | 262 // Since the session manager has not started, it is still safe to access |
192 // the client directly. Note that we give the ownership of the channel | 263 // the client directly. Note that we give the ownership of the channel |
193 // to the client. | 264 // to the client. |
194 client_->set_jingle_channel(channel); | 265 client_->set_jingle_channel(channel); |
195 } | 266 } |
196 | 267 |
197 void ChromotingHost::DoStart(Task* shutdown_task) { | |
198 DCHECK_EQ(context_->main_message_loop(), MessageLoop::current()); | |
199 DCHECK(!jingle_client_); | |
200 DCHECK(shutdown_task); | |
201 | |
202 // Make sure this object is not started. | |
203 { | |
204 AutoLock auto_lock(lock_); | |
205 if (state_ != kInitial) | |
206 return; | |
207 state_ = kStarted; | |
208 } | |
209 | |
210 // Save the shutdown task. | |
211 shutdown_task_.reset(shutdown_task); | |
212 | |
213 std::string xmpp_login; | |
214 std::string xmpp_auth_token; | |
215 if (!config_->GetString(kXmppLoginConfigPath, &xmpp_login) || | |
216 !config_->GetString(kXmppAuthTokenConfigPath, &xmpp_auth_token)) { | |
217 LOG(ERROR) << "XMPP credentials are not defined in the config."; | |
218 return; | |
219 } | |
220 | |
221 if (!access_verifier_.Init(config_)) | |
222 return; | |
223 | |
224 // Connect to the talk network with a JingleClient. | |
225 jingle_client_ = new JingleClient(context_->jingle_thread()); | |
226 jingle_client_->Init(xmpp_login, xmpp_auth_token, | |
227 kChromotingTokenServiceName, this); | |
228 | |
229 heartbeat_sender_ = new HeartbeatSender(); | |
230 if (!heartbeat_sender_->Init(config_, jingle_client_.get())) { | |
231 LOG(ERROR) << "Failed to initialize HeartbeatSender."; | |
232 return; | |
233 } | |
234 } | |
235 | |
236 void ChromotingHost::DoShutdown() { | |
237 DCHECK_EQ(context_->main_message_loop(), MessageLoop::current()); | |
238 | |
239 // No-op if this object is not started yet. | |
240 { | |
241 AutoLock auto_lock(lock_); | |
242 if (state_ != kStarted) | |
243 return; | |
244 state_ = kStopped; | |
245 } | |
246 | |
247 // Tell the session to pause and then disconnect all clients. | |
248 if (session_.get()) { | |
249 session_->Pause(); | |
250 session_->RemoveAllClients(); | |
251 } | |
252 | |
253 // Disconnect all clients. | |
254 if (client_) { | |
255 client_->Disconnect(); | |
256 } | |
257 | |
258 // Disconnect from the talk network. | |
259 if (jingle_client_) { | |
260 jingle_client_->Close(); | |
261 } | |
262 | |
263 // Stop the heartbeat sender. | |
264 if (heartbeat_sender_) { | |
265 heartbeat_sender_->Stop(); | |
266 } | |
267 | |
268 // Lastly call the shutdown task. | |
269 if (shutdown_task_.get()) { | |
270 shutdown_task_->Run(); | |
271 } | |
272 } | |
273 | |
274 } // namespace remoting | 268 } // namespace remoting |
OLD | NEW |