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_authentication_handler.h" | |
| 6 | |
| 7 #include "base/callback_helpers.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "base/timer/timer.h" | |
| 10 #include "blimp/common/proto/blimp_message.pb.h" | |
| 11 #include "blimp/net/blimp_connection.h" | |
| 12 #include "blimp/net/blimp_message_processor.h" | |
| 13 #include "blimp/net/blimp_transport.h" | |
| 14 #include "blimp/net/connection_error_observer.h" | |
| 15 #include "net/base/completion_callback.h" | |
| 16 #include "net/base/net_errors.h" | |
| 17 | |
| 18 namespace blimp { | |
| 19 | |
| 20 namespace { | |
| 21 // Expect authentication to be done in 10 seconds. | |
| 22 const double kAuthTimeoutDurationInSeconds = 10; | |
| 23 | |
| 24 // Authenticates one connection. It deletes itself when | |
| 25 // * the connection is authenticated and passed to |connection_handler|. | |
| 26 // * the connection gets into error state. | |
| 27 // * timeout on waiting for auth message. | |
| 28 class Authenticator : public ConnectionErrorObserver, | |
| 29 public BlimpMessageProcessor { | |
| 30 public: | |
| 31 explicit Authenticator(scoped_ptr<BlimpConnection> connection, | |
| 32 base::WeakPtr<ConnectionHandler> connection_handler); | |
| 33 ~Authenticator() override; | |
| 34 | |
| 35 private: | |
| 36 // Processes authentication result and delete this object. | |
| 37 void OnConnectionAuthenticated(bool authenticated); | |
| 38 | |
| 39 // Invoked on timeout while waiting for auth message. | |
| 40 void OnAuthenticationTimeout(); | |
| 41 | |
| 42 // ConnectionErrorObserver implementation. | |
| 43 void OnConnectionError(int error) override; | |
| 44 | |
| 45 // BlimpMessageProcessor implementation. | |
| 46 void ProcessMessage(scoped_ptr<BlimpMessage> message, | |
| 47 const net::CompletionCallback& callback) override; | |
| 48 | |
| 49 // The connection to be authenticated. | |
| 50 scoped_ptr<BlimpConnection> connection_; | |
| 51 | |
| 52 // Handler for authenticated connections. | |
| 53 base::WeakPtr<ConnectionHandler> connection_handler_; | |
|
Kevin M
2015/12/04 19:12:05
This isn't used anymore; remove it and all referen
haibinlu
2015/12/07 21:54:50
It is used to take a connection when it is authent
| |
| 54 | |
| 55 // A timer to fail authentication on timeout. | |
| 56 base::OneShotTimer timer_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(Authenticator); | |
| 59 }; | |
| 60 | |
| 61 Authenticator::Authenticator( | |
| 62 scoped_ptr<BlimpConnection> connection, | |
| 63 base::WeakPtr<ConnectionHandler> connection_handler) | |
| 64 : connection_(std::move(connection)), | |
| 65 connection_handler_(connection_handler) { | |
| 66 connection_->SetConnectionErrorObserver(this); | |
| 67 connection_->SetIncomingMessageProcessor(this); | |
| 68 timer_.Start(FROM_HERE, | |
|
Kevin M
2015/12/04 19:12:05
Constructors shouldn't be responsible for doing ac
haibinlu
2015/12/07 21:54:50
Per wez's previous comment, Start method is remove
| |
| 69 base::TimeDelta::FromSecondsD(kAuthTimeoutDurationInSeconds), | |
| 70 this, &Authenticator::OnAuthenticationTimeout); | |
| 71 } | |
| 72 | |
| 73 Authenticator::~Authenticator() {} | |
| 74 | |
| 75 void Authenticator::OnConnectionAuthenticated(bool authenticated) { | |
| 76 timer_.Stop(); | |
| 77 connection_->SetIncomingMessageProcessor(nullptr); | |
| 78 connection_->SetConnectionErrorObserver(nullptr); | |
| 79 | |
| 80 if (authenticated) { | |
| 81 connection_handler_->HandleConnection(std::move(connection_)); | |
| 82 } | |
| 83 | |
| 84 delete this; | |
| 85 } | |
| 86 | |
| 87 void Authenticator::OnAuthenticationTimeout() { | |
| 88 DVLOG(1) << "Connection authentication timeout"; | |
| 89 OnConnectionAuthenticated(false); | |
| 90 } | |
| 91 | |
| 92 void Authenticator::OnConnectionError(int error) { | |
| 93 DVLOG(1) << "Connection error before authenticated"; | |
| 94 OnConnectionAuthenticated(false); | |
| 95 } | |
| 96 | |
| 97 void Authenticator::ProcessMessage(scoped_ptr<BlimpMessage> message, | |
| 98 const net::CompletionCallback& callback) { | |
| 99 if (message->type() == BlimpMessage::PROTOCOL_CONTROL) { | |
| 100 // TODO(haibinlu): check client token. | |
| 101 OnConnectionAuthenticated(true); | |
| 102 } else { | |
| 103 OnConnectionAuthenticated(false); | |
| 104 } | |
| 105 | |
| 106 callback.Run(net::OK); | |
| 107 } | |
| 108 | |
| 109 } // namespace | |
| 110 | |
| 111 EngineAuthenticationHandler::EngineAuthenticationHandler( | |
| 112 ConnectionHandler* connection_handler) | |
| 113 : connection_handler_weak_factory_(connection_handler) {} | |
| 114 | |
| 115 EngineAuthenticationHandler::~EngineAuthenticationHandler() {} | |
| 116 | |
| 117 void EngineAuthenticationHandler::HandleConnection( | |
| 118 scoped_ptr<BlimpConnection> connection) { | |
| 119 // Authenticator manages its own lifetime. | |
| 120 new Authenticator(std::move(connection), | |
| 121 connection_handler_weak_factory_.GetWeakPtr()); | |
| 122 } | |
| 123 | |
| 124 } // namespace blimp | |
| OLD | NEW |