MD Downloads: prevent search text from overlapping with the cancel search (X)
[chromium-blink-merge.git] / remoting / protocol / secure_channel_factory.cc
blobabfd178343034b617b5112ab66543fbe7ffdb235
1 // Copyright 2014 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/secure_channel_factory.h"
7 #include "base/bind.h"
8 #include "remoting/protocol/authenticator.h"
9 #include "remoting/protocol/channel_authenticator.h"
10 #include "remoting/protocol/p2p_stream_socket.h"
12 namespace remoting {
13 namespace protocol {
15 SecureChannelFactory::SecureChannelFactory(
16 StreamChannelFactory* channel_factory,
17 Authenticator* authenticator)
18 : channel_factory_(channel_factory),
19 authenticator_(authenticator) {
20 DCHECK_EQ(authenticator_->state(), Authenticator::ACCEPTED);
23 SecureChannelFactory::~SecureChannelFactory() {
24 // CancelChannelCreation() is expected to be called before destruction.
25 DCHECK(channel_authenticators_.empty());
28 void SecureChannelFactory::CreateChannel(
29 const std::string& name,
30 const ChannelCreatedCallback& callback) {
31 DCHECK(!callback.is_null());
32 channel_factory_->CreateChannel(
33 name,
34 base::Bind(&SecureChannelFactory::OnBaseChannelCreated,
35 base::Unretained(this), name, callback));
38 void SecureChannelFactory::CancelChannelCreation(
39 const std::string& name) {
40 AuthenticatorMap::iterator it = channel_authenticators_.find(name);
41 if (it == channel_authenticators_.end()) {
42 channel_factory_->CancelChannelCreation(name);
43 } else {
44 delete it->second;
45 channel_authenticators_.erase(it);
49 void SecureChannelFactory::OnBaseChannelCreated(
50 const std::string& name,
51 const ChannelCreatedCallback& callback,
52 scoped_ptr<P2PStreamSocket> socket) {
53 if (!socket) {
54 callback.Run(nullptr);
55 return;
58 ChannelAuthenticator* channel_authenticator =
59 authenticator_->CreateChannelAuthenticator().release();
60 channel_authenticators_[name] = channel_authenticator;
61 channel_authenticator->SecureAndAuthenticate(
62 socket.Pass(),
63 base::Bind(&SecureChannelFactory::OnSecureChannelCreated,
64 base::Unretained(this), name, callback));
67 void SecureChannelFactory::OnSecureChannelCreated(
68 const std::string& name,
69 const ChannelCreatedCallback& callback,
70 int error,
71 scoped_ptr<P2PStreamSocket> socket) {
72 DCHECK((socket && error == net::OK) || (!socket && error != net::OK));
74 AuthenticatorMap::iterator it = channel_authenticators_.find(name);
75 DCHECK(it != channel_authenticators_.end());
76 delete it->second;
77 channel_authenticators_.erase(it);
79 callback.Run(socket.Pass());
82 } // namespace protocol
83 } // namespace remoting