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..640e9915bcc29facc43cc4a1472fd3a2bffbd5f1 |
| --- /dev/null |
| +++ b/blimp/net/engine_auth_handler.cc |
| @@ -0,0 +1,114 @@ |
| +// 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 { |
| +const int kAuthTimeoutDurationInSeconds = 2; |
|
Kevin M
2015/12/03 19:11:36
Add a comment
Kevin M
2015/12/03 19:11:36
This timeout is far too short to deal with bufferb
haibinlu
2015/12/03 22:28:35
Done.
haibinlu
2015/12/03 22:28:35
Done.
|
| +} // namespace |
| + |
| +EngineAuthHandler::Authenticator::Authenticator( |
|
Kevin M
2015/12/03 19:11:36
Move Authenticator impl below EngineAuthHandler?
haibinlu
2015/12/03 22:28:35
Done.
|
| + 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 19:11:36
Unnecessary b/c only EngineAuthHandler can call it
haibinlu
2015/12/03 22:28:35
Done.
|
| + auth_callback_ = auth_callback; |
| + connection_->SetConnectionErrorObserver(this); |
| + connection_->SetIncomingMessageProcessor(this); |
| + |
| + timer_.reset(new base::OneShotTimer()); |
|
Kevin M
2015/12/03 19:11:36
Nit: no parans needed
haibinlu
2015/12/03 22:28:35
removed
|
| + timer_->Start(FROM_HERE, |
| + base::TimeDelta::FromSeconds(kAuthTimeoutDurationInSeconds), |
|
Kevin M
2015/12/03 19:11:36
FromSecondsD?
haibinlu
2015/12/03 22:28:35
Done.
|
| + this, &EngineAuthHandler::Authenticator::FailAuthetication); |
| +} |
| + |
| +void EngineAuthHandler::Authenticator::OnConnectionError(int error) { |
| + DCHECK(connection_); |
| + InvokeAuthCallback(false); |
| +} |
| + |
| +void EngineAuthHandler::Authenticator::ProcessMessage( |
| + scoped_ptr<BlimpMessage> message, |
| + const net::CompletionCallback& callback) { |
| + DCHECK(connection_); |
| + 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 isAuthenticated) { |
| + timer_->Stop(); |
| + |
| + if (auth_callback_.is_null()) { |
| + return; |
| + } |
| + |
| + base::ResetAndReturn(&auth_callback_) |
| + .Run(std::move(connection_), isAuthenticated); |
| +} |
| + |
| +void EngineAuthHandler::Authenticator::FailAuthetication() { |
| + InvokeAuthCallback(false); |
| +} |
| + |
| +EngineAuthHandler::EngineAuthHandler(ConnectionHandler* connection_handler) |
| + : connection_handler_(connection_handler) { |
| + DCHECK(connection_handler_); |
| +} |
| + |
| +EngineAuthHandler::~EngineAuthHandler() {} |
| + |
| +void EngineAuthHandler::HandleConnection( |
| + scoped_ptr<BlimpConnection> connection) { |
| + DCHECK(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 isAuthenticated) { |
| + if (isAuthenticated) { |
| + 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); |
| +} |
| + |
| +} // namespace blimp |