• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "remoting/protocol/channel_dispatcher_base.h"
6 
7 #include "base/bind.h"
8 #include "net/socket/stream_socket.h"
9 #include "remoting/protocol/channel_factory.h"
10 #include "remoting/protocol/session.h"
11 #include "remoting/protocol/session_config.h"
12 
13 namespace remoting {
14 namespace protocol {
15 
ChannelDispatcherBase(const char * channel_name)16 ChannelDispatcherBase::ChannelDispatcherBase(const char* channel_name)
17     : channel_name_(channel_name),
18       channel_factory_(NULL) {
19 }
20 
~ChannelDispatcherBase()21 ChannelDispatcherBase::~ChannelDispatcherBase() {
22   if (channel_factory_)
23     channel_factory_->CancelChannelCreation(channel_name_);
24 }
25 
Init(Session * session,const ChannelConfig & config,const InitializedCallback & callback)26 void ChannelDispatcherBase::Init(Session* session,
27                                  const ChannelConfig& config,
28                                  const InitializedCallback& callback) {
29   DCHECK(session);
30   switch (config.transport) {
31     case ChannelConfig::TRANSPORT_MUX_STREAM:
32       channel_factory_ = session->GetMultiplexedChannelFactory();
33       break;
34 
35     case ChannelConfig::TRANSPORT_STREAM:
36       channel_factory_ = session->GetTransportChannelFactory();
37       break;
38 
39     default:
40       NOTREACHED();
41       callback.Run(false);
42       return;
43   }
44 
45   initialized_callback_ = callback;
46 
47   channel_factory_->CreateStreamChannel(channel_name_, base::Bind(
48       &ChannelDispatcherBase::OnChannelReady, base::Unretained(this)));
49 }
50 
OnChannelReady(scoped_ptr<net::StreamSocket> socket)51 void ChannelDispatcherBase::OnChannelReady(
52     scoped_ptr<net::StreamSocket> socket) {
53   if (!socket.get()) {
54     initialized_callback_.Run(false);
55     return;
56   }
57 
58   channel_ = socket.Pass();
59 
60   OnInitialized();
61 
62   initialized_callback_.Run(true);
63 }
64 
65 }  // namespace protocol
66 }  // namespace remoting
67