Chromium Code Reviews| Index: blimp/net/engine_auth_handler.h |
| diff --git a/blimp/net/engine_auth_handler.h b/blimp/net/engine_auth_handler.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..25b359d5369f87693ec97d1d7320c3839efd4e39 |
| --- /dev/null |
| +++ b/blimp/net/engine_auth_handler.h |
| @@ -0,0 +1,99 @@ |
| +// 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. |
| + |
| +#ifndef BLIMP_NET_ENGINE_AUTH_HANDLER_H_ |
| +#define BLIMP_NET_ENGINE_AUTH_HANDLER_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/macros.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/timer/timer.h" |
| +#include "blimp/net/blimp_message_processor.h" |
| +#include "blimp/net/blimp_net_export.h" |
| +#include "blimp/net/connection_error_observer.h" |
| +#include "blimp/net/connection_handler.h" |
| +#include "net/base/completion_callback.h" |
| + |
| +namespace blimp { |
| + |
| +class BlimpConnection; |
| +class BlimpMessage; |
| + |
| +// Authenticates connections and hand them over to |connection_handler| |
| +// if it is authenticated. |
| +class BLIMP_NET_EXPORT EngineAuthHandler : public ConnectionHandler { |
| + public: |
| + // Caller is responsible for ensuring that |connection_handler| outlives |
| + // |this|. |
| + explicit EngineAuthHandler(ConnectionHandler* connection_handler); |
| + |
| + ~EngineAuthHandler() override; |
| + |
| + // ConnectionHandler implementation. |
| + void HandleConnection(scoped_ptr<BlimpConnection> connection) override; |
| + |
| + private: |
| + using AuthCallback = |
| + base::Callback<void(scoped_ptr<BlimpConnection> connection, |
| + bool authenticated)>; |
| + |
| + // Authenticates one connection. |auth_callback| is invoked when |
| + // * the connection is authenticated. |
| + // * the connection gets into error state. |
| + // * timeout on waiting for auth message. |
| + class Authenticator : public ConnectionErrorObserver, |
| + public BlimpMessageProcessor { |
| + public: |
| + explicit Authenticator(scoped_ptr<BlimpConnection> connection); |
| + ~Authenticator() override; |
| + |
| + void Start(const AuthCallback& auth_callback); |
| + |
| + private: |
| + void InvokeAuthCallback(bool authenticated); |
| + |
| + // Invoked on timeout while waiting for auth message. |
| + void FailAuthetication(); |
| + |
| + // ConnectionErrorObserver implementation. |
| + // Used to implement reconnection logic on unexpected disconnections. |
| + void OnConnectionError(int error) override; |
| + |
| + // BlimpMessageProcessor implementation. |
| + void ProcessMessage(scoped_ptr<BlimpMessage> message, |
| + const net::CompletionCallback& callback) override; |
| + |
| + // The connection to be authenticated. |
| + scoped_ptr<BlimpConnection> connection_; |
|
Kevin M
2015/12/03 23:12:05
Can you make this a const?
haibinlu
2015/12/03 23:42:21
can not. autheticator will give the ownership of t
|
| + |
| + // Callback for authentication result; |
| + AuthCallback auth_callback_; |
| + |
| + // A timer to fail authentication on timeout. |
| + base::OneShotTimer timer_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(Authenticator); |
| + }; |
| + |
| + // Callback when |authenticator| has the authentication result. |
| + void OnAuthResult(Authenticator* authenticator, |
| + scoped_ptr<BlimpConnection> connection, |
| + bool authenticated); |
| + |
| + void RemovePendingAuthenticator(Authenticator* authenticator); |
| + |
| + // Handler for authenticated connections. |
| + ConnectionHandler* connection_handler_; |
| + |
| + // Pending authentications. An authenticator is removed from this list once |
| + // it has the authentication result. |
| + std::vector<scoped_ptr<Authenticator>> pending_auths_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(EngineAuthHandler); |
| +}; |
| + |
| +} // namespace blimp |
| + |
| +#endif // BLIMP_NET_ENGINE_AUTH_HANDLER_H_ |