Updating XTBs based on .GRDs from branch master
[chromium-blink-merge.git] / remoting / protocol / channel_dispatcher_base.cc
blobe7e0f5b033c325689a168885352cc470142e5044
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
3 // found in the LICENSE file.
5 #include "remoting/protocol/channel_dispatcher_base.h"
7 #include "base/bind.h"
8 #include "remoting/protocol/p2p_stream_socket.h"
9 #include "remoting/protocol/session.h"
10 #include "remoting/protocol/session_config.h"
11 #include "remoting/protocol/stream_channel_factory.h"
13 namespace remoting {
14 namespace protocol {
16 ChannelDispatcherBase::ChannelDispatcherBase(const char* channel_name)
17 : channel_name_(channel_name),
18 channel_factory_(nullptr),
19 event_handler_(nullptr) {
22 ChannelDispatcherBase::~ChannelDispatcherBase() {
23 if (channel_factory_)
24 channel_factory_->CancelChannelCreation(channel_name_);
27 void ChannelDispatcherBase::Init(Session* session,
28 const ChannelConfig& config,
29 EventHandler* event_handler) {
30 DCHECK(session);
31 switch (config.transport) {
32 case ChannelConfig::TRANSPORT_MUX_STREAM:
33 channel_factory_ = session->GetMultiplexedChannelFactory();
34 break;
36 case ChannelConfig::TRANSPORT_STREAM:
37 channel_factory_ = session->GetTransportChannelFactory();
38 break;
40 default:
41 LOG(FATAL) << "Unknown transport type: " << config.transport;
44 event_handler_ = event_handler;
46 channel_factory_->CreateChannel(channel_name_, base::Bind(
47 &ChannelDispatcherBase::OnChannelReady, base::Unretained(this)));
50 void ChannelDispatcherBase::OnChannelReady(
51 scoped_ptr<P2PStreamSocket> socket) {
52 if (!socket.get()) {
53 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR);
54 return;
57 channel_factory_ = nullptr;
58 channel_ = socket.Pass();
59 writer_.Init(
60 base::Bind(&P2PStreamSocket::Write, base::Unretained(channel_.get())),
61 base::Bind(&ChannelDispatcherBase::OnReadWriteFailed,
62 base::Unretained(this)));
63 reader_.StartReading(channel_.get(),
64 base::Bind(&ChannelDispatcherBase::OnReadWriteFailed,
65 base::Unretained(this)));
67 event_handler_->OnChannelInitialized(this);
70 void ChannelDispatcherBase::OnReadWriteFailed(int error) {
71 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR);
74 } // namespace protocol
75 } // namespace remoting