Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "blimp/net/engine_auth_handler.h" | |
| 6 | |
| 7 #include "base/callback_helpers.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "blimp/common/proto/blimp_message.pb.h" | |
| 10 #include "blimp/net/blimp_connection.h" | |
| 11 #include "blimp/net/blimp_transport.h" | |
| 12 #include "net/base/net_errors.h" | |
| 13 | |
| 14 namespace blimp { | |
| 15 | |
| 16 namespace { | |
| 17 // Expect authentication to be done in 10 seconds. | |
| 18 const double kAuthTimeoutDurationInSeconds = 10; | |
| 19 } // namespace | |
| 20 | |
| 21 EngineAuthHandler::EngineAuthHandler(ConnectionHandler* connection_handler) | |
| 22 : connection_handler_(connection_handler) { | |
| 23 DCHECK(connection_handler_); | |
| 24 } | |
| 25 | |
| 26 EngineAuthHandler::~EngineAuthHandler() {} | |
| 27 | |
| 28 void EngineAuthHandler::HandleConnection( | |
| 29 scoped_ptr<BlimpConnection> connection) { | |
| 30 scoped_ptr<Authenticator> authenticator( | |
| 31 new Authenticator(std::move(connection))); | |
| 32 authenticator->Start(base::Bind(&EngineAuthHandler::OnAuthResult, | |
| 33 base::Unretained(this), | |
| 34 base::Unretained(authenticator.get()))); | |
|
Wez
2015/12/04 01:35:38
Why do we have a separate Start() call, rather tha
haibinlu
2015/12/04 02:50:40
removed
| |
| 35 pending_auths_.push_back(std::move(authenticator)); | |
| 36 } | |
| 37 | |
| 38 void EngineAuthHandler::OnAuthResult(Authenticator* authenticator, | |
| 39 scoped_ptr<BlimpConnection> connection, | |
| 40 bool authenticated) { | |
| 41 if (authenticated) { | |
| 42 connection_handler_->HandleConnection(std::move(connection)); | |
| 43 } | |
| 44 | |
| 45 RemovePendingAuthenticator(authenticator); | |
| 46 } | |
| 47 | |
| 48 void EngineAuthHandler::RemovePendingAuthenticator( | |
| 49 Authenticator* authenticator) { | |
| 50 auto it = pending_auths_.begin(); | |
| 51 for (; it != pending_auths_.end(); ++it) { | |
| 52 if (it->get() == authenticator) | |
| 53 break; | |
| 54 } | |
| 55 DCHECK(it != pending_auths_.end()); | |
| 56 pending_auths_.erase(it); | |
| 57 } | |
| 58 | |
| 59 EngineAuthHandler::Authenticator::Authenticator( | |
| 60 scoped_ptr<BlimpConnection> connection) | |
| 61 : connection_(std::move(connection)) { | |
| 62 DCHECK(connection_); | |
| 63 } | |
| 64 | |
| 65 EngineAuthHandler::Authenticator::~Authenticator() {} | |
| 66 | |
| 67 void EngineAuthHandler::Authenticator::Start( | |
| 68 const AuthCallback& auth_callback) { | |
| 69 auth_callback_ = auth_callback; | |
| 70 connection_->SetConnectionErrorObserver(this); | |
| 71 connection_->SetIncomingMessageProcessor(this); | |
| 72 timer_.Start(FROM_HERE, | |
| 73 base::TimeDelta::FromSecondsD(kAuthTimeoutDurationInSeconds), | |
| 74 this, &EngineAuthHandler::Authenticator::FailAuthetication); | |
| 75 } | |
| 76 | |
| 77 void EngineAuthHandler::Authenticator::OnConnectionError(int error) { | |
| 78 DVLOG(1) << "Connection error before authenticated"; | |
| 79 InvokeAuthCallback(false); | |
| 80 } | |
| 81 | |
| 82 void EngineAuthHandler::Authenticator::ProcessMessage( | |
| 83 scoped_ptr<BlimpMessage> message, | |
| 84 const net::CompletionCallback& callback) { | |
| 85 if (message->type() == BlimpMessage::PROTOCOL_CONTROL) { | |
|
Wez
2015/12/04 01:35:38
If you called SetIncomingMessageProcessor(null) on
haibinlu
2015/12/04 02:50:40
Added, though need to restore the ability to SetIn
| |
| 86 // TODO(haibinlu): check client token. | |
| 87 InvokeAuthCallback(true); | |
| 88 callback.Run(net::OK); | |
|
Wez
2015/12/04 01:35:38
Why are you only acknowledging the message in case
haibinlu
2015/12/04 02:50:40
Done.
| |
| 89 } else { | |
| 90 InvokeAuthCallback(false); | |
| 91 } | |
| 92 } | |
| 93 | |
| 94 void EngineAuthHandler::Authenticator::InvokeAuthCallback(bool authenticated) { | |
| 95 DCHECK(!auth_callback_.is_null()); | |
| 96 timer_.Stop(); | |
| 97 base::ResetAndReturn(&auth_callback_) | |
| 98 .Run(std::move(connection_), authenticated); | |
| 99 } | |
| 100 | |
| 101 void EngineAuthHandler::Authenticator::FailAuthetication() { | |
| 102 DVLOG(1) << "Connection authentication timeout"; | |
|
Wez
2015/12/04 01:35:38
If this is specific to timeout, it should be calle
haibinlu
2015/12/04 02:50:40
On 2015/12/04 01:35:38, Wez wrote:
> If this is sp
| |
| 103 InvokeAuthCallback(false); | |
| 104 } | |
| 105 | |
| 106 } // namespace blimp | |
| OLD | NEW |