| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BLIMP_NET_CLIENT_CONNECTION_MANAGER_H_ | 5 #ifndef BLIMP_NET_CLIENT_CONNECTION_MANAGER_H_ |
| 6 #define BLIMP_NET_CLIENT_CONNECTION_MANAGER_H_ | 6 #define BLIMP_NET_CLIENT_CONNECTION_MANAGER_H_ |
| 7 | 7 |
| 8 #include <string> |
| 8 #include <vector> | 9 #include <vector> |
| 9 | 10 |
| 10 #include "base/macros.h" | 11 #include "base/macros.h" |
| 11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 12 #include "blimp/net/blimp_net_export.h" | 14 #include "blimp/net/blimp_net_export.h" |
| 15 #include "blimp/net/connection_error_observer.h" |
| 13 | 16 |
| 14 namespace blimp { | 17 namespace blimp { |
| 15 | 18 |
| 16 class BlimpConnection; | 19 class BlimpConnection; |
| 17 class BlimpTransport; | 20 class BlimpTransport; |
| 18 class ConnectionHandler; | 21 class ConnectionHandler; |
| 19 | 22 |
| 20 // Coordinates the channel creation and authentication workflows for | 23 // Coordinates the channel creation and authentication workflows for |
| 21 // outgoing (Client) network connections. | 24 // outgoing (Client) network connections. |
| 22 // | 25 // |
| 23 // TODO(haibinlu): cope with network changes that may potentially affect the | 26 // TODO(haibinlu): cope with network changes that may potentially affect the |
| 24 // endpoint that we're trying to connect to. | 27 // endpoint that we're trying to connect to. |
| 25 class BLIMP_NET_EXPORT ClientConnectionManager { | 28 class BLIMP_NET_EXPORT ClientConnectionManager |
| 29 : public ConnectionErrorObserver { |
| 26 public: | 30 public: |
| 27 // Caller is responsible for ensuring that |connection_handler| | 31 // Caller is responsible for ensuring that |connection_handler| |
| 28 // outlives |this|. | 32 // outlives |this|. |
| 29 explicit ClientConnectionManager(ConnectionHandler* connection_handler); | 33 explicit ClientConnectionManager(ConnectionHandler* connection_handler); |
| 30 | 34 |
| 31 ~ClientConnectionManager(); | 35 ~ClientConnectionManager() override; |
| 32 | 36 |
| 33 // Adds a transport. All transports are expected to be added before invoking | 37 // Adds a transport. All transports are expected to be added before invoking |
| 34 // |Connect|. | 38 // |Connect|. |
| 35 void AddTransport(scoped_ptr<BlimpTransport> transport); | 39 void AddTransport(scoped_ptr<BlimpTransport> transport); |
| 36 | 40 |
| 37 // Attempts to create a connection using any of the BlimpTransports in | 41 // Attempts to create a connection using any of the BlimpTransports in |
| 38 // |transports_|. | 42 // |transports_|. |
| 39 // This will result in the handler being called back at-most-once. | 43 // This will result in the handler being called back at-most-once. |
| 40 // | 44 // |
| 41 // This is also a placeholder for automatic reconnection logic for common | 45 // This is also a placeholder for automatic reconnection logic for common |
| 42 // cases such as network switches, online/offline changes. | 46 // cases such as network switches, online/offline changes. |
| 43 void Connect(); | 47 void Connect(); |
| 44 | 48 |
| 49 // Sets the client token to use in the authentication message. |
| 50 void set_client_token(const std::string& client_token) { |
| 51 client_token_ = client_token; |
| 52 } |
| 53 |
| 45 private: | 54 private: |
| 46 // Tries to connect using the BlimpTransport specified at |transport_index|. | 55 // Tries to connect using the BlimpTransport specified at |transport_index|. |
| 47 void Connect(int transport_index); | 56 void Connect(int transport_index); |
| 48 | 57 |
| 49 // Callback invoked by transports_[transport_index] to indicate that it has a | 58 // Callback invoked by transports_[transport_index] to indicate that it has a |
| 50 // connection ready to be authenticated or there is an error. | 59 // connection ready to be authenticated or there is an error. |
| 51 void OnConnectResult(int transport_index, int result); | 60 void OnConnectResult(int transport_index, int result); |
| 52 | 61 |
| 53 // Sends authentication message to the engine via |connection|. | 62 // Sends authentication message to the engine via |connection|. |
| 54 void SendAuthenticationMessage(BlimpConnection* connection); | 63 void SendAuthenticationMessage(scoped_ptr<BlimpConnection> connection); |
| 55 | 64 |
| 65 // Invoked after the authentication message is sent to |connection|. |
| 66 // The result of the write operation is passed via |result|. |
| 67 void AuthenticationMessageSent(scoped_ptr<BlimpConnection> connection, |
| 68 int result); |
| 69 |
| 70 // ConnectionErrorObserver implementation. |
| 71 void OnConnectionError(int error) override; |
| 72 |
| 73 std::string client_token_; |
| 56 ConnectionHandler* connection_handler_; | 74 ConnectionHandler* connection_handler_; |
| 57 std::vector<scoped_ptr<BlimpTransport>> transports_; | 75 std::vector<scoped_ptr<BlimpTransport>> transports_; |
| 76 base::WeakPtrFactory<ClientConnectionManager> weak_factory_; |
| 58 | 77 |
| 59 DISALLOW_COPY_AND_ASSIGN(ClientConnectionManager); | 78 DISALLOW_COPY_AND_ASSIGN(ClientConnectionManager); |
| 60 }; | 79 }; |
| 61 | 80 |
| 62 } // namespace blimp | 81 } // namespace blimp |
| 63 | 82 |
| 64 #endif // BLIMP_NET_CLIENT_CONNECTION_MANAGER_H_ | 83 #endif // BLIMP_NET_CLIENT_CONNECTION_MANAGER_H_ |
| OLD | NEW |