| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/location.h" | 9 #include "base/location.h" |
| 10 #include "base/message_loop_proxy.h" | |
| 11 #include "remoting/base/constants.h" | 10 #include "remoting/base/constants.h" |
| 12 #include "remoting/jingle_glue/javascript_signal_strategy.h" | 11 #include "remoting/jingle_glue/javascript_signal_strategy.h" |
| 13 #include "remoting/jingle_glue/xmpp_signal_strategy.h" | 12 #include "remoting/jingle_glue/xmpp_signal_strategy.h" |
| 14 #include "remoting/protocol/auth_util.h" | 13 #include "remoting/protocol/auth_util.h" |
| 15 #include "remoting/protocol/authenticator.h" | 14 #include "remoting/protocol/authenticator.h" |
| 16 #include "remoting/protocol/client_control_dispatcher.h" | 15 #include "remoting/protocol/client_control_dispatcher.h" |
| 17 #include "remoting/protocol/client_event_dispatcher.h" | 16 #include "remoting/protocol/client_event_dispatcher.h" |
| 18 #include "remoting/protocol/client_stub.h" | 17 #include "remoting/protocol/client_stub.h" |
| 19 #include "remoting/protocol/clipboard_stub.h" | 18 #include "remoting/protocol/clipboard_stub.h" |
| 20 #include "remoting/protocol/errors.h" | 19 #include "remoting/protocol/errors.h" |
| 21 #include "remoting/protocol/jingle_session_manager.h" | 20 #include "remoting/protocol/jingle_session_manager.h" |
| 22 #include "remoting/protocol/pepper_transport_factory.h" | 21 #include "remoting/protocol/pepper_transport_factory.h" |
| 23 #include "remoting/protocol/video_reader.h" | 22 #include "remoting/protocol/video_reader.h" |
| 24 #include "remoting/protocol/video_stub.h" | 23 #include "remoting/protocol/video_stub.h" |
| 25 #include "remoting/protocol/util.h" | 24 #include "remoting/protocol/util.h" |
| 26 | 25 |
| 27 namespace remoting { | 26 namespace remoting { |
| 28 namespace protocol { | 27 namespace protocol { |
| 29 | 28 |
| 30 ConnectionToHost::ConnectionToHost( | 29 ConnectionToHost::ConnectionToHost( |
| 31 base::MessageLoopProxy* message_loop, | |
| 32 bool allow_nat_traversal) | 30 bool allow_nat_traversal) |
| 33 : message_loop_(message_loop), | 31 : allow_nat_traversal_(allow_nat_traversal), |
| 34 allow_nat_traversal_(allow_nat_traversal), | |
| 35 event_callback_(NULL), | 32 event_callback_(NULL), |
| 36 client_stub_(NULL), | 33 client_stub_(NULL), |
| 37 clipboard_stub_(NULL), | 34 clipboard_stub_(NULL), |
| 38 video_stub_(NULL), | 35 video_stub_(NULL), |
| 39 state_(CONNECTING), | 36 state_(CONNECTING), |
| 40 error_(OK) { | 37 error_(OK) { |
| 41 } | 38 } |
| 42 | 39 |
| 43 ConnectionToHost::~ConnectionToHost() { | 40 ConnectionToHost::~ConnectionToHost() { |
| 44 } | 41 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 82 signal_strategy_.reset(strategy); | 79 signal_strategy_.reset(strategy); |
| 83 signal_strategy_->AddListener(this); | 80 signal_strategy_->AddListener(this); |
| 84 signal_strategy_->Connect(); | 81 signal_strategy_->Connect(); |
| 85 | 82 |
| 86 session_manager_.reset(new JingleSessionManager( | 83 session_manager_.reset(new JingleSessionManager( |
| 87 transport_factory.Pass(), allow_nat_traversal_)); | 84 transport_factory.Pass(), allow_nat_traversal_)); |
| 88 session_manager_->Init(signal_strategy_.get(), this); | 85 session_manager_->Init(signal_strategy_.get(), this); |
| 89 } | 86 } |
| 90 | 87 |
| 91 void ConnectionToHost::Disconnect(const base::Closure& shutdown_task) { | 88 void ConnectionToHost::Disconnect(const base::Closure& shutdown_task) { |
| 92 if (!message_loop_->BelongsToCurrentThread()) { | 89 DCHECK(CalledOnValidThread()); |
| 93 message_loop_->PostTask( | |
| 94 FROM_HERE, base::Bind(&ConnectionToHost::Disconnect, | |
| 95 base::Unretained(this), shutdown_task)); | |
| 96 return; | |
| 97 } | |
| 98 | 90 |
| 99 CloseChannels(); | 91 CloseChannels(); |
| 100 | 92 |
| 101 if (session_.get()) | 93 if (session_.get()) |
| 102 session_.reset(); | 94 session_.reset(); |
| 103 | 95 |
| 104 if (session_manager_.get()) | 96 if (session_manager_.get()) |
| 105 session_manager_.reset(); | 97 session_manager_.reset(); |
| 106 | 98 |
| 107 if (signal_strategy_.get()) { | 99 if (signal_strategy_.get()) { |
| 108 signal_strategy_->RemoveListener(this); | 100 signal_strategy_->RemoveListener(this); |
| 109 signal_strategy_.reset(); | 101 signal_strategy_.reset(); |
| 110 } | 102 } |
| 111 | 103 |
| 112 shutdown_task.Run(); | 104 shutdown_task.Run(); |
| 113 } | 105 } |
| 114 | 106 |
| 115 const SessionConfig& ConnectionToHost::config() { | 107 const SessionConfig& ConnectionToHost::config() { |
| 116 return session_->config(); | 108 return session_->config(); |
| 117 } | 109 } |
| 118 | 110 |
| 119 void ConnectionToHost::OnSignalStrategyStateChange( | 111 void ConnectionToHost::OnSignalStrategyStateChange( |
| 120 SignalStrategy::State state) { | 112 SignalStrategy::State state) { |
| 121 DCHECK(message_loop_->BelongsToCurrentThread()); | 113 DCHECK(CalledOnValidThread()); |
| 122 DCHECK(event_callback_); | 114 DCHECK(event_callback_); |
| 123 | 115 |
| 124 if (state == SignalStrategy::CONNECTED) { | 116 if (state == SignalStrategy::CONNECTED) { |
| 125 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid(); | 117 VLOG(1) << "Connected as: " << signal_strategy_->GetLocalJid(); |
| 126 } else if (state == SignalStrategy::DISCONNECTED) { | 118 } else if (state == SignalStrategy::DISCONNECTED) { |
| 127 VLOG(1) << "Connection closed."; | 119 VLOG(1) << "Connection closed."; |
| 128 CloseOnError(SIGNALING_ERROR); | 120 CloseOnError(SIGNALING_ERROR); |
| 129 } | 121 } |
| 130 } | 122 } |
| 131 | 123 |
| 132 void ConnectionToHost::OnSessionManagerReady() { | 124 void ConnectionToHost::OnSessionManagerReady() { |
| 133 DCHECK(message_loop_->BelongsToCurrentThread()); | 125 DCHECK(CalledOnValidThread()); |
| 134 | 126 |
| 135 // After SessionManager is initialized we can try to connect to the host. | 127 // After SessionManager is initialized we can try to connect to the host. |
| 136 scoped_ptr<CandidateSessionConfig> candidate_config = | 128 scoped_ptr<CandidateSessionConfig> candidate_config = |
| 137 CandidateSessionConfig::CreateDefault(); | 129 CandidateSessionConfig::CreateDefault(); |
| 138 session_ = session_manager_->Connect( | 130 session_ = session_manager_->Connect( |
| 139 host_jid_, authenticator_.Pass(), candidate_config.Pass(), | 131 host_jid_, authenticator_.Pass(), candidate_config.Pass(), |
| 140 base::Bind(&ConnectionToHost::OnSessionStateChange, | 132 base::Bind(&ConnectionToHost::OnSessionStateChange, |
| 141 base::Unretained(this))); | 133 base::Unretained(this))); |
| 142 } | 134 } |
| 143 | 135 |
| 144 void ConnectionToHost::OnIncomingSession( | 136 void ConnectionToHost::OnIncomingSession( |
| 145 Session* session, | 137 Session* session, |
| 146 SessionManager::IncomingSessionResponse* response) { | 138 SessionManager::IncomingSessionResponse* response) { |
| 147 DCHECK(message_loop_->BelongsToCurrentThread()); | 139 DCHECK(CalledOnValidThread()); |
| 148 // Client always rejects incoming sessions. | 140 // Client always rejects incoming sessions. |
| 149 *response = SessionManager::DECLINE; | 141 *response = SessionManager::DECLINE; |
| 150 } | 142 } |
| 151 | 143 |
| 152 ConnectionToHost::State ConnectionToHost::state() const { | 144 ConnectionToHost::State ConnectionToHost::state() const { |
| 153 return state_; | 145 return state_; |
| 154 } | 146 } |
| 155 | 147 |
| 156 void ConnectionToHost::OnSessionStateChange( | 148 void ConnectionToHost::OnSessionStateChange( |
| 157 Session::State state) { | 149 Session::State state) { |
| 158 DCHECK(message_loop_->BelongsToCurrentThread()); | 150 DCHECK(CalledOnValidThread()); |
| 159 DCHECK(event_callback_); | 151 DCHECK(event_callback_); |
| 160 | 152 |
| 161 switch (state) { | 153 switch (state) { |
| 162 case Session::INITIALIZING: | 154 case Session::INITIALIZING: |
| 163 case Session::CONNECTING: | 155 case Session::CONNECTING: |
| 164 case Session::CONNECTED: | 156 case Session::CONNECTED: |
| 165 // Don't care about these events. | 157 // Don't care about these events. |
| 166 break; | 158 break; |
| 167 | 159 |
| 168 case Session::AUTHENTICATED: | 160 case Session::AUTHENTICATED: |
| 169 video_reader_.reset(VideoReader::Create( | 161 video_reader_.reset(VideoReader::Create(session_->config())); |
| 170 message_loop_, session_->config())); | |
| 171 video_reader_->Init(session_.get(), video_stub_, base::Bind( | 162 video_reader_->Init(session_.get(), video_stub_, base::Bind( |
| 172 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); | 163 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); |
| 173 | 164 |
| 174 control_dispatcher_.reset(new ClientControlDispatcher()); | 165 control_dispatcher_.reset(new ClientControlDispatcher()); |
| 175 control_dispatcher_->Init(session_.get(), base::Bind( | 166 control_dispatcher_->Init(session_.get(), base::Bind( |
| 176 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); | 167 &ConnectionToHost::OnChannelInitialized, base::Unretained(this))); |
| 177 control_dispatcher_->set_client_stub(client_stub_); | 168 control_dispatcher_->set_client_stub(client_stub_); |
| 178 control_dispatcher_->set_clipboard_stub(clipboard_stub_); | 169 control_dispatcher_->set_clipboard_stub(clipboard_stub_); |
| 179 | 170 |
| 180 event_dispatcher_.reset(new ClientEventDispatcher()); | 171 event_dispatcher_.reset(new ClientEventDispatcher()); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 | 226 |
| 236 void ConnectionToHost::CloseChannels() { | 227 void ConnectionToHost::CloseChannels() { |
| 237 control_dispatcher_.reset(); | 228 control_dispatcher_.reset(); |
| 238 event_dispatcher_.reset(); | 229 event_dispatcher_.reset(); |
| 239 clipboard_forwarder_.set_clipboard_stub(NULL); | 230 clipboard_forwarder_.set_clipboard_stub(NULL); |
| 240 event_forwarder_.set_input_stub(NULL); | 231 event_forwarder_.set_input_stub(NULL); |
| 241 video_reader_.reset(); | 232 video_reader_.reset(); |
| 242 } | 233 } |
| 243 | 234 |
| 244 void ConnectionToHost::SetState(State state, ErrorCode error) { | 235 void ConnectionToHost::SetState(State state, ErrorCode error) { |
| 245 DCHECK(message_loop_->BelongsToCurrentThread()); | 236 DCHECK(CalledOnValidThread()); |
| 246 // |error| should be specified only when |state| is set to FAILED. | 237 // |error| should be specified only when |state| is set to FAILED. |
| 247 DCHECK(state == FAILED || error == OK); | 238 DCHECK(state == FAILED || error == OK); |
| 248 | 239 |
| 249 if (state != state_) { | 240 if (state != state_) { |
| 250 state_ = state; | 241 state_ = state; |
| 251 error_ = error; | 242 error_ = error; |
| 252 event_callback_->OnConnectionState(state_, error_); | 243 event_callback_->OnConnectionState(state_, error_); |
| 253 } | 244 } |
| 254 } | 245 } |
| 255 | 246 |
| 256 } // namespace protocol | 247 } // namespace protocol |
| 257 } // namespace remoting | 248 } // namespace remoting |
| OLD | NEW |