Chromium Code Reviews| Index: blimp/net/engine_auth_handler.cc |
| diff --git a/blimp/net/engine_auth_handler.cc b/blimp/net/engine_auth_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ec397b0b15895bb59b15e5b2f26878211fd39d74 |
| --- /dev/null |
| +++ b/blimp/net/engine_auth_handler.cc |
| @@ -0,0 +1,111 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "blimp/net/engine_auth_handler.h" |
| + |
| +#include "base/callback_helpers.h" |
| +#include "base/logging.h" |
| +#include "blimp/common/proto/blimp_message.pb.h" |
| +#include "blimp/net/blimp_connection.h" |
| +#include "blimp/net/blimp_transport.h" |
| +#include "net/base/net_errors.h" |
| + |
| +namespace blimp { |
| + |
| +namespace { |
| +// Expect authentication to be done in 10 seconds. |
| +const double kAuthTimeoutDurationInSeconds = 10; |
| +} // namespace |
| + |
| +EngineAuthHandler::EngineAuthHandler(ConnectionHandler* connection_handler) |
| + : connection_handler_(connection_handler) { |
| + DCHECK(connection_handler_); |
| +} |
| + |
| +EngineAuthHandler::~EngineAuthHandler() {} |
| + |
| +void EngineAuthHandler::HandleConnection( |
| + scoped_ptr<BlimpConnection> connection) { |
| + scoped_ptr<Authenticator> authenticator( |
| + new Authenticator(std::move(connection))); |
| + authenticator->Start(base::Bind(&EngineAuthHandler::OnAuthResult, |
| + base::Unretained(this), |
| + base::Unretained(authenticator.get()))); |
| + pending_auths_.push_back(std::move(authenticator)); |
| +} |
| + |
| +void EngineAuthHandler::OnAuthResult(Authenticator* authenticator, |
| + scoped_ptr<BlimpConnection> connection, |
| + bool authenticated) { |
| + if (authenticated) { |
| + connection_handler_->HandleConnection(std::move(connection)); |
| + } |
| + |
| + RemovePendingAuthenticator(authenticator); |
| +} |
| + |
| +void EngineAuthHandler::RemovePendingAuthenticator( |
| + Authenticator* authenticator) { |
| + auto it = pending_auths_.begin(); |
| + for (; it != pending_auths_.end(); ++it) { |
| + if (it->get() == authenticator) |
| + break; |
| + } |
| + DCHECK(it != pending_auths_.end()); |
| + pending_auths_.erase(it); |
| +} |
| + |
| +EngineAuthHandler::Authenticator::Authenticator( |
| + scoped_ptr<BlimpConnection> connection) |
| + : connection_(std::move(connection)) { |
| + DCHECK(connection_); |
| +} |
| + |
| +EngineAuthHandler::Authenticator::~Authenticator() {} |
| + |
| +void EngineAuthHandler::Authenticator::Start( |
| + const AuthCallback& auth_callback) { |
| + DCHECK(connection_); |
|
Kevin M
2015/12/03 23:12:05
Not needed if connection_ is const
haibinlu
2015/12/03 23:42:21
Acknowledged.
|
| + auth_callback_ = auth_callback; |
| + connection_->SetConnectionErrorObserver(this); |
| + connection_->SetIncomingMessageProcessor(this); |
| + timer_.Start(FROM_HERE, |
| + base::TimeDelta::FromSecondsD(kAuthTimeoutDurationInSeconds), |
| + this, &EngineAuthHandler::Authenticator::FailAuthetication); |
| +} |
| + |
| +void EngineAuthHandler::Authenticator::OnConnectionError(int error) { |
| + DCHECK(connection_); |
|
Kevin M
2015/12/03 23:12:05
Not needed
haibinlu
2015/12/03 23:42:21
Done.
|
| + InvokeAuthCallback(false); |
|
Kevin M
2015/12/03 23:12:05
Log |error|? Anything we can do to interpret it or
haibinlu
2015/12/03 23:42:21
added log. but I donot think we need to propagate
|
| +} |
| + |
| +void EngineAuthHandler::Authenticator::ProcessMessage( |
| + scoped_ptr<BlimpMessage> message, |
| + const net::CompletionCallback& callback) { |
| + DCHECK(connection_); |
|
Kevin M
2015/12/03 23:12:05
Not needed
haibinlu
2015/12/03 23:42:21
Done.
|
| + if (message->type() == BlimpMessage::PROTOCOL_CONTROL) { |
| + // TODO(haibinlu): check client token. |
| + InvokeAuthCallback(true); |
| + callback.Run(net::OK); |
| + } else { |
| + InvokeAuthCallback(false); |
| + } |
| +} |
| + |
| +void EngineAuthHandler::Authenticator::InvokeAuthCallback(bool authenticated) { |
| + timer_.Stop(); |
| + |
| + if (auth_callback_.is_null()) { |
|
Kevin M
2015/12/03 23:12:05
DCHECK for this case, there aren't any cases where
haibinlu
2015/12/03 23:42:21
Done.
|
| + return; |
| + } |
| + |
| + base::ResetAndReturn(&auth_callback_) |
| + .Run(std::move(connection_), authenticated); |
| +} |
| + |
| +void EngineAuthHandler::Authenticator::FailAuthetication() { |
| + InvokeAuthCallback(false); |
| +} |
| + |
| +} // namespace blimp |