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 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.
| |
| 18 } // namespace | |
| 19 | |
| 20 EngineAuthHandler::Authenticator::Authenticator( | |
|
Kevin M
2015/12/03 19:11:36
Move Authenticator impl below EngineAuthHandler?
haibinlu
2015/12/03 22:28:35
Done.
| |
| 21 scoped_ptr<BlimpConnection> connection) | |
| 22 : connection_(std::move(connection)) { | |
| 23 DCHECK(connection_); | |
| 24 } | |
| 25 | |
| 26 EngineAuthHandler::Authenticator::~Authenticator() {} | |
| 27 | |
| 28 void EngineAuthHandler::Authenticator::Start( | |
| 29 const AuthCallback& auth_callback) { | |
| 30 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.
| |
| 31 auth_callback_ = auth_callback; | |
| 32 connection_->SetConnectionErrorObserver(this); | |
| 33 connection_->SetIncomingMessageProcessor(this); | |
| 34 | |
| 35 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
| |
| 36 timer_->Start(FROM_HERE, | |
| 37 base::TimeDelta::FromSeconds(kAuthTimeoutDurationInSeconds), | |
|
Kevin M
2015/12/03 19:11:36
FromSecondsD?
haibinlu
2015/12/03 22:28:35
Done.
| |
| 38 this, &EngineAuthHandler::Authenticator::FailAuthetication); | |
| 39 } | |
| 40 | |
| 41 void EngineAuthHandler::Authenticator::OnConnectionError(int error) { | |
| 42 DCHECK(connection_); | |
| 43 InvokeAuthCallback(false); | |
| 44 } | |
| 45 | |
| 46 void EngineAuthHandler::Authenticator::ProcessMessage( | |
| 47 scoped_ptr<BlimpMessage> message, | |
| 48 const net::CompletionCallback& callback) { | |
| 49 DCHECK(connection_); | |
| 50 if (message->type() == BlimpMessage::PROTOCOL_CONTROL) { | |
| 51 // TODO(haibinlu): check client token. | |
| 52 InvokeAuthCallback(true); | |
| 53 callback.Run(net::OK); | |
| 54 } else { | |
| 55 InvokeAuthCallback(false); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 void EngineAuthHandler::Authenticator::InvokeAuthCallback( | |
| 60 bool isAuthenticated) { | |
| 61 timer_->Stop(); | |
| 62 | |
| 63 if (auth_callback_.is_null()) { | |
| 64 return; | |
| 65 } | |
| 66 | |
| 67 base::ResetAndReturn(&auth_callback_) | |
| 68 .Run(std::move(connection_), isAuthenticated); | |
| 69 } | |
| 70 | |
| 71 void EngineAuthHandler::Authenticator::FailAuthetication() { | |
| 72 InvokeAuthCallback(false); | |
| 73 } | |
| 74 | |
| 75 EngineAuthHandler::EngineAuthHandler(ConnectionHandler* connection_handler) | |
| 76 : connection_handler_(connection_handler) { | |
| 77 DCHECK(connection_handler_); | |
| 78 } | |
| 79 | |
| 80 EngineAuthHandler::~EngineAuthHandler() {} | |
| 81 | |
| 82 void EngineAuthHandler::HandleConnection( | |
| 83 scoped_ptr<BlimpConnection> connection) { | |
| 84 DCHECK(connection); | |
| 85 scoped_ptr<Authenticator> authenticator( | |
| 86 new Authenticator(std::move(connection))); | |
| 87 authenticator->Start(base::Bind(&EngineAuthHandler::OnAuthResult, | |
| 88 base::Unretained(this), | |
| 89 base::Unretained(authenticator.get()))); | |
| 90 pending_auths_.push_back(std::move(authenticator)); | |
| 91 } | |
| 92 | |
| 93 void EngineAuthHandler::OnAuthResult(Authenticator* authenticator, | |
| 94 scoped_ptr<BlimpConnection> connection, | |
| 95 bool isAuthenticated) { | |
| 96 if (isAuthenticated) { | |
| 97 connection_handler_->HandleConnection(std::move(connection)); | |
| 98 } | |
| 99 | |
| 100 RemovePendingAuthenticator(authenticator); | |
| 101 } | |
| 102 | |
| 103 void EngineAuthHandler::RemovePendingAuthenticator( | |
| 104 Authenticator* authenticator) { | |
| 105 auto it = pending_auths_.begin(); | |
| 106 for (; it != pending_auths_.end(); ++it) { | |
| 107 if (it->get() == authenticator) | |
| 108 break; | |
| 109 } | |
| 110 DCHECK(it != pending_auths_.end()); | |
| 111 pending_auths_.erase(it); | |
| 112 } | |
| 113 | |
| 114 } // namespace blimp | |
| OLD | NEW |