OLD | NEW |
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/protocol/connection_to_host.h" | 5 #include "remoting/protocol/connection_to_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/callback.h" | 8 #include "base/callback.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop_proxy.h" |
10 #include "remoting/base/constants.h" | 10 #include "remoting/base/constants.h" |
11 #include "remoting/jingle_glue/host_resolver.h" | 11 #include "remoting/jingle_glue/host_resolver.h" |
12 #include "remoting/jingle_glue/http_port_allocator.h" | 12 #include "remoting/jingle_glue/http_port_allocator.h" |
13 #include "remoting/jingle_glue/javascript_signal_strategy.h" | 13 #include "remoting/jingle_glue/javascript_signal_strategy.h" |
14 #include "remoting/jingle_glue/xmpp_signal_strategy.h" | 14 #include "remoting/jingle_glue/xmpp_signal_strategy.h" |
15 #include "remoting/protocol/auth_token_utils.h" | 15 #include "remoting/protocol/auth_token_utils.h" |
16 #include "remoting/protocol/client_message_dispatcher.h" | 16 #include "remoting/protocol/client_message_dispatcher.h" |
17 #include "remoting/protocol/client_stub.h" | 17 #include "remoting/protocol/client_stub.h" |
18 #include "remoting/protocol/host_control_sender.h" | 18 #include "remoting/protocol/host_control_sender.h" |
19 #include "remoting/protocol/input_sender.h" | 19 #include "remoting/protocol/input_sender.h" |
20 #include "remoting/protocol/jingle_session_manager.h" | 20 #include "remoting/protocol/jingle_session_manager.h" |
21 #include "remoting/protocol/video_reader.h" | 21 #include "remoting/protocol/video_reader.h" |
22 #include "remoting/protocol/video_stub.h" | 22 #include "remoting/protocol/video_stub.h" |
23 #include "remoting/protocol/util.h" | 23 #include "remoting/protocol/util.h" |
24 | 24 |
25 namespace remoting { | 25 namespace remoting { |
26 namespace protocol { | 26 namespace protocol { |
27 | 27 |
28 ConnectionToHost::ConnectionToHost( | 28 ConnectionToHost::ConnectionToHost( |
29 MessageLoop* message_loop, | 29 base::MessageLoopProxy* message_loop, |
30 talk_base::NetworkManager* network_manager, | 30 talk_base::NetworkManager* network_manager, |
31 talk_base::PacketSocketFactory* socket_factory, | 31 talk_base::PacketSocketFactory* socket_factory, |
32 HostResolverFactory* host_resolver_factory, | 32 HostResolverFactory* host_resolver_factory, |
33 PortAllocatorSessionFactory* session_factory, | 33 PortAllocatorSessionFactory* session_factory, |
34 bool allow_nat_traversal) | 34 bool allow_nat_traversal) |
35 : message_loop_(message_loop), | 35 : message_loop_(message_loop), |
36 network_manager_(network_manager), | 36 network_manager_(network_manager), |
37 socket_factory_(socket_factory), | 37 socket_factory_(socket_factory), |
38 host_resolver_factory_(host_resolver_factory), | 38 host_resolver_factory_(host_resolver_factory), |
39 port_allocator_session_factory_(session_factory), | 39 port_allocator_session_factory_(session_factory), |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 host_public_key_ = host_public_key; | 77 host_public_key_ = host_public_key; |
78 | 78 |
79 // Initialize |signal_strategy_|. | 79 // Initialize |signal_strategy_|. |
80 JavascriptSignalStrategy* strategy = new JavascriptSignalStrategy(your_jid); | 80 JavascriptSignalStrategy* strategy = new JavascriptSignalStrategy(your_jid); |
81 strategy->AttachXmppProxy(xmpp_proxy); | 81 strategy->AttachXmppProxy(xmpp_proxy); |
82 signal_strategy_.reset(strategy); | 82 signal_strategy_.reset(strategy); |
83 signal_strategy_->Init(this); | 83 signal_strategy_->Init(this); |
84 } | 84 } |
85 | 85 |
86 void ConnectionToHost::Disconnect(const base::Closure& shutdown_task) { | 86 void ConnectionToHost::Disconnect(const base::Closure& shutdown_task) { |
87 if (MessageLoop::current() != message_loop_) { | 87 if (!message_loop_->BelongsToCurrentThread()) { |
88 message_loop_->PostTask( | 88 message_loop_->PostTask( |
89 FROM_HERE, base::Bind(&ConnectionToHost::Disconnect, | 89 FROM_HERE, base::Bind(&ConnectionToHost::Disconnect, |
90 base::Unretained(this), shutdown_task)); | 90 base::Unretained(this), shutdown_task)); |
91 return; | 91 return; |
92 } | 92 } |
93 | 93 |
94 CloseChannels(); | 94 CloseChannels(); |
95 | 95 |
96 if (session_.get()) | 96 if (session_.get()) |
97 session_.reset(); | 97 session_.reset(); |
98 | 98 |
99 if (session_manager_.get()) | 99 if (session_manager_.get()) |
100 session_manager_.reset(); | 100 session_manager_.reset(); |
101 | 101 |
102 if (signal_strategy_.get()) | 102 if (signal_strategy_.get()) |
103 signal_strategy_.reset(); | 103 signal_strategy_.reset(); |
104 | 104 |
105 shutdown_task.Run(); | 105 shutdown_task.Run(); |
106 } | 106 } |
107 | 107 |
108 void ConnectionToHost::InitSession() { | 108 void ConnectionToHost::InitSession() { |
109 DCHECK_EQ(message_loop_, MessageLoop::current()); | 109 DCHECK(message_loop_->BelongsToCurrentThread()); |
110 | 110 |
111 // Initialize chromotocol |session_manager_|. | 111 // Initialize chromotocol |session_manager_|. |
112 JingleSessionManager* session_manager = | 112 JingleSessionManager* session_manager = |
113 JingleSessionManager::CreateSandboxed( | 113 JingleSessionManager::CreateSandboxed( |
114 network_manager_.release(), socket_factory_.release(), | 114 message_loop_, network_manager_.release(), socket_factory_.release(), |
115 host_resolver_factory_.release(), | 115 host_resolver_factory_.release(), |
116 port_allocator_session_factory_.release()); | 116 port_allocator_session_factory_.release()); |
117 | 117 |
118 // TODO(ajwong): Make this a command switch when we're more stable. | 118 // TODO(ajwong): Make this a command switch when we're more stable. |
119 session_manager->set_allow_local_ips(true); | 119 session_manager->set_allow_local_ips(true); |
120 | 120 |
121 session_manager_.reset(session_manager); | 121 session_manager_.reset(session_manager); |
122 session_manager_->Init( | 122 session_manager_->Init( |
123 local_jid_, signal_strategy_.get(), this, NULL, "", allow_nat_traversal_); | 123 local_jid_, signal_strategy_.get(), this, NULL, "", allow_nat_traversal_); |
124 } | 124 } |
125 | 125 |
126 const SessionConfig* ConnectionToHost::config() { | 126 const SessionConfig* ConnectionToHost::config() { |
127 return session_->config(); | 127 return session_->config(); |
128 } | 128 } |
129 | 129 |
130 void ConnectionToHost::OnStateChange( | 130 void ConnectionToHost::OnStateChange( |
131 SignalStrategy::StatusObserver::State state) { | 131 SignalStrategy::StatusObserver::State state) { |
132 DCHECK_EQ(message_loop_, MessageLoop::current()); | 132 DCHECK(message_loop_->BelongsToCurrentThread()); |
133 DCHECK(event_callback_); | 133 DCHECK(event_callback_); |
134 | 134 |
135 if (state == SignalStrategy::StatusObserver::CONNECTED) { | 135 if (state == SignalStrategy::StatusObserver::CONNECTED) { |
136 VLOG(1) << "Connected as: " << local_jid_; | 136 VLOG(1) << "Connected as: " << local_jid_; |
137 InitSession(); | 137 InitSession(); |
138 } else if (state == SignalStrategy::StatusObserver::CLOSED) { | 138 } else if (state == SignalStrategy::StatusObserver::CLOSED) { |
139 VLOG(1) << "Connection closed."; | 139 VLOG(1) << "Connection closed."; |
140 event_callback_->OnConnectionClosed(this); | 140 event_callback_->OnConnectionClosed(this); |
141 } | 141 } |
142 } | 142 } |
143 | 143 |
144 void ConnectionToHost::OnJidChange(const std::string& full_jid) { | 144 void ConnectionToHost::OnJidChange(const std::string& full_jid) { |
145 DCHECK_EQ(message_loop_, MessageLoop::current()); | 145 DCHECK(message_loop_->BelongsToCurrentThread()); |
146 local_jid_ = full_jid; | 146 local_jid_ = full_jid; |
147 } | 147 } |
148 | 148 |
149 void ConnectionToHost::OnSessionManagerInitialized() { | 149 void ConnectionToHost::OnSessionManagerInitialized() { |
150 DCHECK_EQ(message_loop_, MessageLoop::current()); | 150 DCHECK(message_loop_->BelongsToCurrentThread()); |
151 | 151 |
152 // After SessionManager is initialized we can try to connect to the host. | 152 // After SessionManager is initialized we can try to connect to the host. |
153 CandidateSessionConfig* candidate_config = | 153 CandidateSessionConfig* candidate_config = |
154 CandidateSessionConfig::CreateDefault(); | 154 CandidateSessionConfig::CreateDefault(); |
155 std::string client_token = | 155 std::string client_token = |
156 protocol::GenerateSupportAuthToken(local_jid_, access_code_); | 156 protocol::GenerateSupportAuthToken(local_jid_, access_code_); |
157 session_.reset(session_manager_->Connect( | 157 session_.reset(session_manager_->Connect( |
158 host_jid_, host_public_key_, client_token, candidate_config, | 158 host_jid_, host_public_key_, client_token, candidate_config, |
159 NewCallback(this, &ConnectionToHost::OnSessionStateChange))); | 159 NewCallback(this, &ConnectionToHost::OnSessionStateChange))); |
160 | 160 |
161 // Set the shared-secret for securing SSL channels. | 161 // Set the shared-secret for securing SSL channels. |
162 session_->set_shared_secret(access_code_); | 162 session_->set_shared_secret(access_code_); |
163 } | 163 } |
164 | 164 |
165 void ConnectionToHost::OnIncomingSession( | 165 void ConnectionToHost::OnIncomingSession( |
166 Session* session, | 166 Session* session, |
167 SessionManager::IncomingSessionResponse* response) { | 167 SessionManager::IncomingSessionResponse* response) { |
168 DCHECK_EQ(message_loop_, MessageLoop::current()); | 168 DCHECK(message_loop_->BelongsToCurrentThread()); |
169 // Client always rejects incoming sessions. | 169 // Client always rejects incoming sessions. |
170 *response = SessionManager::DECLINE; | 170 *response = SessionManager::DECLINE; |
171 } | 171 } |
172 | 172 |
173 void ConnectionToHost::OnSessionStateChange( | 173 void ConnectionToHost::OnSessionStateChange( |
174 Session::State state) { | 174 Session::State state) { |
175 DCHECK_EQ(message_loop_, MessageLoop::current()); | 175 DCHECK(message_loop_->BelongsToCurrentThread()); |
176 DCHECK(event_callback_); | 176 DCHECK(event_callback_); |
177 | 177 |
178 switch (state) { | 178 switch (state) { |
179 case Session::FAILED: | 179 case Session::FAILED: |
180 state_ = STATE_FAILED; | 180 state_ = STATE_FAILED; |
181 CloseOnError(); | 181 CloseOnError(); |
182 break; | 182 break; |
183 | 183 |
184 case Session::CLOSED: | 184 case Session::CLOSED: |
185 state_ = STATE_CLOSED; | 185 state_ = STATE_CLOSED; |
186 CloseChannels(); | 186 CloseChannels(); |
187 event_callback_->OnConnectionClosed(this); | 187 event_callback_->OnConnectionClosed(this); |
188 break; | 188 break; |
189 | 189 |
190 case Session::CONNECTED: | 190 case Session::CONNECTED: |
191 // Initialize reader and writer. | 191 // Initialize reader and writer. |
192 video_reader_.reset(VideoReader::Create(session_->config())); | 192 video_reader_.reset( |
| 193 VideoReader::Create(message_loop_, session_->config())); |
193 video_reader_->Init( | 194 video_reader_->Init( |
194 session_.get(), video_stub_, | 195 session_.get(), video_stub_, |
195 base::Bind(&ConnectionToHost::OnVideoChannelInitialized, | 196 base::Bind(&ConnectionToHost::OnVideoChannelInitialized, |
196 base::Unretained(this))); | 197 base::Unretained(this))); |
197 break; | 198 break; |
198 | 199 |
199 case Session::CONNECTED_CHANNELS: | 200 case Session::CONNECTED_CHANNELS: |
200 state_ = STATE_CONNECTED; | 201 state_ = STATE_CONNECTED; |
201 host_control_sender_.reset( | 202 host_control_sender_.reset( |
202 new HostControlSender(session_->control_channel())); | 203 new HostControlSender(message_loop_, session_->control_channel())); |
203 dispatcher_.reset(new ClientMessageDispatcher()); | 204 dispatcher_.reset(new ClientMessageDispatcher()); |
204 dispatcher_->Initialize(session_.get(), client_stub_); | 205 dispatcher_->Initialize(session_.get(), client_stub_); |
205 | 206 |
206 control_connected_ = true; | 207 control_connected_ = true; |
207 input_connected_ = true; | 208 input_connected_ = true; |
208 NotifyIfChannelsReady(); | 209 NotifyIfChannelsReady(); |
209 break; | 210 break; |
210 | 211 |
211 default: | 212 default: |
212 // Ignore the other states by default. | 213 // Ignore the other states by default. |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
245 | 246 |
246 video_reader_.reset(); | 247 video_reader_.reset(); |
247 } | 248 } |
248 | 249 |
249 void ConnectionToHost::OnClientAuthenticated() { | 250 void ConnectionToHost::OnClientAuthenticated() { |
250 // TODO(hclam): Don't send anything except authentication request if it is | 251 // TODO(hclam): Don't send anything except authentication request if it is |
251 // not authenticated. | 252 // not authenticated. |
252 state_ = STATE_AUTHENTICATED; | 253 state_ = STATE_AUTHENTICATED; |
253 | 254 |
254 // Create and enable the input stub now that we're authenticated. | 255 // Create and enable the input stub now that we're authenticated. |
255 input_sender_.reset(new InputSender(session_->event_channel())); | 256 input_sender_.reset( |
| 257 new InputSender(message_loop_, session_->event_channel())); |
256 } | 258 } |
257 | 259 |
258 ConnectionToHost::State ConnectionToHost::state() const { | 260 ConnectionToHost::State ConnectionToHost::state() const { |
259 return state_; | 261 return state_; |
260 } | 262 } |
261 | 263 |
262 } // namespace protocol | 264 } // namespace protocol |
263 } // namespace remoting | 265 } // namespace remoting |
OLD | NEW |