| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "remoting/protocol/connection_to_client.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/location.h" | |
| 9 #include "net/base/io_buffer.h" | |
| 10 #include "remoting/protocol/clipboard_stub.h" | |
| 11 #include "remoting/protocol/host_control_dispatcher.h" | |
| 12 #include "remoting/protocol/host_event_dispatcher.h" | |
| 13 #include "remoting/protocol/host_stub.h" | |
| 14 #include "remoting/protocol/host_video_dispatcher.h" | |
| 15 #include "remoting/protocol/input_stub.h" | |
| 16 | |
| 17 namespace remoting { | |
| 18 namespace protocol { | |
| 19 | |
| 20 ConnectionToClient::ConnectionToClient(protocol::Session* session) | |
| 21 : handler_(nullptr), | |
| 22 session_(session) { | |
| 23 session_->SetEventHandler(this); | |
| 24 } | |
| 25 | |
| 26 ConnectionToClient::~ConnectionToClient() { | |
| 27 } | |
| 28 | |
| 29 void ConnectionToClient::SetEventHandler(EventHandler* event_handler) { | |
| 30 DCHECK(CalledOnValidThread()); | |
| 31 handler_ = event_handler; | |
| 32 } | |
| 33 | |
| 34 protocol::Session* ConnectionToClient::session() { | |
| 35 DCHECK(CalledOnValidThread()); | |
| 36 return session_.get(); | |
| 37 } | |
| 38 | |
| 39 void ConnectionToClient::Disconnect(ErrorCode error) { | |
| 40 DCHECK(CalledOnValidThread()); | |
| 41 | |
| 42 CloseChannels(); | |
| 43 | |
| 44 // This should trigger OnConnectionClosed() event and this object | |
| 45 // may be destroyed as the result. | |
| 46 session_->Close(error); | |
| 47 } | |
| 48 | |
| 49 void ConnectionToClient::OnInputEventReceived(int64_t timestamp) { | |
| 50 DCHECK(CalledOnValidThread()); | |
| 51 handler_->OnInputEventReceived(this, timestamp); | |
| 52 } | |
| 53 | |
| 54 VideoStub* ConnectionToClient::video_stub() { | |
| 55 DCHECK(CalledOnValidThread()); | |
| 56 return video_dispatcher_.get(); | |
| 57 } | |
| 58 | |
| 59 AudioStub* ConnectionToClient::audio_stub() { | |
| 60 DCHECK(CalledOnValidThread()); | |
| 61 return audio_writer_.get(); | |
| 62 } | |
| 63 | |
| 64 // Return pointer to ClientStub. | |
| 65 ClientStub* ConnectionToClient::client_stub() { | |
| 66 DCHECK(CalledOnValidThread()); | |
| 67 return control_dispatcher_.get(); | |
| 68 } | |
| 69 | |
| 70 void ConnectionToClient::set_clipboard_stub( | |
| 71 protocol::ClipboardStub* clipboard_stub) { | |
| 72 DCHECK(CalledOnValidThread()); | |
| 73 control_dispatcher_->set_clipboard_stub(clipboard_stub); | |
| 74 } | |
| 75 | |
| 76 void ConnectionToClient::set_host_stub(protocol::HostStub* host_stub) { | |
| 77 DCHECK(CalledOnValidThread()); | |
| 78 control_dispatcher_->set_host_stub(host_stub); | |
| 79 } | |
| 80 | |
| 81 void ConnectionToClient::set_input_stub(protocol::InputStub* input_stub) { | |
| 82 DCHECK(CalledOnValidThread()); | |
| 83 event_dispatcher_->set_input_stub(input_stub); | |
| 84 } | |
| 85 | |
| 86 void ConnectionToClient::set_video_feedback_stub( | |
| 87 VideoFeedbackStub* video_feedback_stub) { | |
| 88 DCHECK(CalledOnValidThread()); | |
| 89 video_dispatcher_->set_video_feedback_stub(video_feedback_stub); | |
| 90 } | |
| 91 | |
| 92 void ConnectionToClient::OnSessionStateChange(Session::State state) { | |
| 93 DCHECK(CalledOnValidThread()); | |
| 94 | |
| 95 DCHECK(handler_); | |
| 96 switch(state) { | |
| 97 case Session::INITIALIZING: | |
| 98 case Session::CONNECTING: | |
| 99 case Session::ACCEPTING: | |
| 100 case Session::CONNECTED: | |
| 101 // Don't care about these events. | |
| 102 break; | |
| 103 case Session::AUTHENTICATING: | |
| 104 handler_->OnConnectionAuthenticating(this); | |
| 105 break; | |
| 106 case Session::AUTHENTICATED: | |
| 107 // Initialize channels. | |
| 108 control_dispatcher_.reset(new HostControlDispatcher()); | |
| 109 control_dispatcher_->Init(session_.get(), | |
| 110 session_->config().control_config(), this); | |
| 111 | |
| 112 event_dispatcher_.reset(new HostEventDispatcher()); | |
| 113 event_dispatcher_->Init(session_.get(), session_->config().event_config(), | |
| 114 this); | |
| 115 event_dispatcher_->set_on_input_event_callback(base::Bind( | |
| 116 &ConnectionToClient::OnInputEventReceived, base::Unretained(this))); | |
| 117 | |
| 118 video_dispatcher_.reset(new HostVideoDispatcher()); | |
| 119 video_dispatcher_->Init(session_.get(), session_->config().video_config(), | |
| 120 this); | |
| 121 | |
| 122 audio_writer_ = AudioWriter::Create(session_->config()); | |
| 123 if (audio_writer_.get()) { | |
| 124 audio_writer_->Init(session_.get(), session_->config().audio_config(), | |
| 125 this); | |
| 126 } | |
| 127 | |
| 128 // Notify the handler after initializing the channels, so that | |
| 129 // ClientSession can get a client clipboard stub. | |
| 130 handler_->OnConnectionAuthenticated(this); | |
| 131 break; | |
| 132 | |
| 133 case Session::CLOSED: | |
| 134 Close(OK); | |
| 135 break; | |
| 136 | |
| 137 case Session::FAILED: | |
| 138 Close(session_->error()); | |
| 139 break; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 void ConnectionToClient::OnSessionRouteChange( | |
| 144 const std::string& channel_name, | |
| 145 const TransportRoute& route) { | |
| 146 handler_->OnRouteChange(this, channel_name, route); | |
| 147 } | |
| 148 | |
| 149 void ConnectionToClient::OnChannelInitialized( | |
| 150 ChannelDispatcherBase* channel_dispatcher) { | |
| 151 DCHECK(CalledOnValidThread()); | |
| 152 | |
| 153 NotifyIfChannelsReady(); | |
| 154 } | |
| 155 | |
| 156 void ConnectionToClient::OnChannelError( | |
| 157 ChannelDispatcherBase* channel_dispatcher, | |
| 158 ErrorCode error) { | |
| 159 DCHECK(CalledOnValidThread()); | |
| 160 | |
| 161 LOG(ERROR) << "Failed to connect channel " | |
| 162 << channel_dispatcher->channel_name(); | |
| 163 Close(CHANNEL_CONNECTION_ERROR); | |
| 164 } | |
| 165 | |
| 166 void ConnectionToClient::NotifyIfChannelsReady() { | |
| 167 DCHECK(CalledOnValidThread()); | |
| 168 | |
| 169 if (!control_dispatcher_ || !control_dispatcher_->is_connected()) | |
| 170 return; | |
| 171 if (!event_dispatcher_ || !event_dispatcher_->is_connected()) | |
| 172 return; | |
| 173 if (!video_dispatcher_ || !video_dispatcher_->is_connected()) | |
| 174 return; | |
| 175 if ((!audio_writer_ || !audio_writer_->is_connected()) && | |
| 176 session_->config().is_audio_enabled()) { | |
| 177 return; | |
| 178 } | |
| 179 handler_->OnConnectionChannelsConnected(this); | |
| 180 } | |
| 181 | |
| 182 void ConnectionToClient::Close(ErrorCode error) { | |
| 183 CloseChannels(); | |
| 184 handler_->OnConnectionClosed(this, error); | |
| 185 } | |
| 186 | |
| 187 void ConnectionToClient::CloseChannels() { | |
| 188 control_dispatcher_.reset(); | |
| 189 event_dispatcher_.reset(); | |
| 190 video_dispatcher_.reset(); | |
| 191 audio_writer_.reset(); | |
| 192 } | |
| 193 | |
| 194 } // namespace protocol | |
| 195 } // namespace remoting | |
| OLD | NEW |