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"
8 #include "net/socket/stream_socket.h"
9 #include "remoting/protocol/authenticator.h"
10 #include "remoting/protocol/channel_authenticator.h"
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(
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
);
45 channel_authenticators_
.erase(it
);
49 void SecureChannelFactory::OnBaseChannelCreated(
50 const std::string
& name
,
51 const ChannelCreatedCallback
& callback
,
52 scoped_ptr
<net::StreamSocket
> socket
) {
54 callback
.Run(nullptr);
58 ChannelAuthenticator
* channel_authenticator
=
59 authenticator_
->CreateChannelAuthenticator().release();
60 channel_authenticators_
[name
] = channel_authenticator
;
61 channel_authenticator
->SecureAndAuthenticate(
63 base::Bind(&SecureChannelFactory::OnSecureChannelCreated
,
64 base::Unretained(this), name
, callback
));
67 void SecureChannelFactory::OnSecureChannelCreated(
68 const std::string
& name
,
69 const ChannelCreatedCallback
& callback
,
71 scoped_ptr
<net::StreamSocket
> 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());
77 channel_authenticators_
.erase(it
);
79 callback
.Run(socket
.Pass());
82 } // namespace protocol
83 } // namespace remoting