MD Downloads: prevent search text from overlapping with the cancel search (X)
[chromium-blink-merge.git] / remoting / protocol / channel_dispatcher_base.cc
blob3d54269bb24f624666e5897ca12295a2607470f5
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_QUIC_STREAM:
37 channel_factory_ = session->GetQuicChannelFactory();
38 break;
40 case ChannelConfig::TRANSPORT_STREAM:
41 channel_factory_ = session->GetTransportChannelFactory();
42 break;
44 default:
45 LOG(FATAL) << "Unknown transport type: " << config.transport;
48 event_handler_ = event_handler;
50 channel_factory_->CreateChannel(channel_name_, base::Bind(
51 &ChannelDispatcherBase::OnChannelReady, base::Unretained(this)));
54 void ChannelDispatcherBase::OnChannelReady(
55 scoped_ptr<P2PStreamSocket> socket) {
56 if (!socket.get()) {
57 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR);
58 return;
61 channel_factory_ = nullptr;
62 channel_ = socket.Pass();
63 writer_.Init(
64 base::Bind(&P2PStreamSocket::Write, base::Unretained(channel_.get())),
65 base::Bind(&ChannelDispatcherBase::OnReadWriteFailed,
66 base::Unretained(this)));
67 reader_.StartReading(channel_.get(),
68 base::Bind(&ChannelDispatcherBase::OnReadWriteFailed,
69 base::Unretained(this)));
71 event_handler_->OnChannelInitialized(this);
74 void ChannelDispatcherBase::OnReadWriteFailed(int error) {
75 event_handler_->OnChannelError(this, CHANNEL_CONNECTION_ERROR);
78 } // namespace protocol
79 } // namespace remoting