| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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 GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_ | 5 #ifndef GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_ |
| 6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_ | 6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_ |
| 7 | 7 |
| 8 #include "google_apis/gcm/engine/connection_factory.h" | 8 #include "google_apis/gcm/engine/connection_factory.h" |
| 9 | 9 |
| 10 #include <stddef.h> | 10 #include <stddef.h> |
| 11 | 11 |
| 12 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "google_apis/gcm/engine/connection_event_tracker.h" |
| 15 #include "google_apis/gcm/engine/connection_handler.h" | 16 #include "google_apis/gcm/engine/connection_handler.h" |
| 16 #include "google_apis/gcm/protocol/mcs.pb.h" | 17 #include "google_apis/gcm/protocol/mcs.pb.h" |
| 17 #include "net/base/backoff_entry.h" | 18 #include "net/base/backoff_entry.h" |
| 18 #include "net/base/network_change_notifier.h" | 19 #include "net/base/network_change_notifier.h" |
| 19 #include "net/log/net_log_with_source.h" | 20 #include "net/log/net_log_with_source.h" |
| 20 #include "net/proxy/proxy_info.h" | 21 #include "net/proxy/proxy_info.h" |
| 21 #include "net/proxy/proxy_service.h" | 22 #include "net/proxy/proxy_service.h" |
| 22 #include "net/socket/client_socket_handle.h" | 23 #include "net/socket/client_socket_handle.h" |
| 23 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 24 | 25 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 72 // Returns the server to which the factory is currently connected, or if | 73 // Returns the server to which the factory is currently connected, or if |
| 73 // a connection is currently pending, the server to which the next connection | 74 // a connection is currently pending, the server to which the next connection |
| 74 // attempt will be made. | 75 // attempt will be made. |
| 75 GURL GetCurrentEndpoint() const; | 76 GURL GetCurrentEndpoint() const; |
| 76 | 77 |
| 77 // Returns the IPEndpoint to which the factory is currently connected. If no | 78 // Returns the IPEndpoint to which the factory is currently connected. If no |
| 78 // connection is active, returns an empty IPEndpoint. | 79 // connection is active, returns an empty IPEndpoint. |
| 79 net::IPEndPoint GetPeerIP(); | 80 net::IPEndPoint GetPeerIP(); |
| 80 | 81 |
| 81 protected: | 82 protected: |
| 82 // Implementation of Connect(..). If not in backoff, uses |login_request_| | 83 // Initiate the connection to the GCM server. |
| 83 // in attempting a connection/handshake. On connection/handshake failure, goes | |
| 84 // into backoff. | |
| 85 // Virtual for testing. | 84 // Virtual for testing. |
| 86 virtual void ConnectImpl(); | 85 virtual void StartConnection(); |
| 87 | 86 |
| 88 // Helper method for initalizing the connection hander. | 87 // Helper method for initalizing the connection hander. |
| 89 // Virtual for testing. | 88 // Virtual for testing. |
| 90 virtual void InitHandler(); | 89 virtual void InitHandler(); |
| 91 | 90 |
| 92 // Helper method for creating a backoff entry. | 91 // Helper method for creating a backoff entry. |
| 93 // Virtual for testing. | 92 // Virtual for testing. |
| 94 virtual std::unique_ptr<net::BackoffEntry> CreateBackoffEntry( | 93 virtual std::unique_ptr<net::BackoffEntry> CreateBackoffEntry( |
| 95 const net::BackoffEntry::Policy* const policy); | 94 const net::BackoffEntry::Policy* const policy); |
| 96 | 95 |
| 97 // Helper method for creating the connection handler. | 96 // Helper method for creating the connection handler. |
| 98 // Virtual for testing. | 97 // Virtual for testing. |
| 99 virtual std::unique_ptr<ConnectionHandler> CreateConnectionHandler( | 98 virtual std::unique_ptr<ConnectionHandler> CreateConnectionHandler( |
| 100 base::TimeDelta read_timeout, | 99 base::TimeDelta read_timeout, |
| 101 const ConnectionHandler::ProtoReceivedCallback& read_callback, | 100 const ConnectionHandler::ProtoReceivedCallback& read_callback, |
| 102 const ConnectionHandler::ProtoSentCallback& write_callback, | 101 const ConnectionHandler::ProtoSentCallback& write_callback, |
| 103 const ConnectionHandler::ConnectionChangedCallback& connection_callback); | 102 const ConnectionHandler::ConnectionChangedCallback& connection_callback); |
| 104 | 103 |
| 105 // Returns the current time in Ticks. | 104 // Returns the current time in Ticks. |
| 106 // Virtual for testing. | 105 // Virtual for testing. |
| 107 virtual base::TimeTicks NowTicks(); | 106 virtual base::TimeTicks NowTicks(); |
| 108 | 107 |
| 109 // Callback for Socket connection completion. | 108 // Callback for Socket connection completion. |
| 110 void OnConnectDone(int result); | 109 void OnConnectDone(int result); |
| 111 | 110 |
| 112 // ConnectionHandler callback for connection issues. | 111 // ConnectionHandler callback for connection issues. |
| 113 void ConnectionHandlerCallback(int result); | 112 void ConnectionHandlerCallback(int result); |
| 114 | 113 |
| 115 private: | 114 private: |
| 115 friend class ConnectionFactoryImplTest; |
| 116 |
| 117 ConnectionEventTracker* GetEventTrackerForTesting(); |
| 118 |
| 116 // Helper method for checking backoff and triggering a connection as | 119 // Helper method for checking backoff and triggering a connection as |
| 117 // necessary. | 120 // necessary. |
| 118 void ConnectWithBackoff(); | 121 void ConnectWithBackoff(); |
| 119 | 122 |
| 123 // Implementation of Connect(..). If not in backoff attempts a connection and |
| 124 // handshake. On connection/handshake failure, goes into backoff. |
| 125 void ConnectImpl(); |
| 126 |
| 120 // Proxy resolution and connection functions. | 127 // Proxy resolution and connection functions. |
| 121 void OnProxyResolveDone(int status); | 128 void OnProxyResolveDone(int status); |
| 122 void OnProxyConnectDone(int status); | 129 void OnProxyConnectDone(int status); |
| 123 int ReconsiderProxyAfterError(int error); | 130 int ReconsiderProxyAfterError(int error); |
| 124 void ReportSuccessfulProxyConnection(); | 131 void ReportSuccessfulProxyConnection(); |
| 125 | 132 |
| 126 // Closes the local socket if one is present, and resets connection handler. | 133 // Closes the local socket if one is present, and resets connection handler. |
| 127 void CloseSocket(); | 134 void CloseSocket(); |
| 128 | 135 |
| 129 // Updates the GCM Network Session's HttpAuthCache with the HTTP Network | 136 // Updates the GCM Network Session's HttpAuthCache with the HTTP Network |
| 130 // Session's cache, if available. | 137 // Session's cache, if available. |
| 131 void RebuildNetworkSessionAuthCache(); | 138 void RebuildNetworkSessionAuthCache(); |
| 132 | 139 |
| 140 // The tracker will maintain a list of all connection attempts with GCM, |
| 141 // whether they succeeded, and their duration. |
| 142 ConnectionEventTracker event_tracker_; |
| 143 |
| 133 // The MCS endpoints to make connections to, sorted in order of priority. | 144 // The MCS endpoints to make connections to, sorted in order of priority. |
| 134 const std::vector<GURL> mcs_endpoints_; | 145 const std::vector<GURL> mcs_endpoints_; |
| 135 // Index to the endpoint for which a connection should be attempted next. | 146 // Index to the endpoint for which a connection should be attempted next. |
| 136 size_t next_endpoint_; | 147 size_t next_endpoint_; |
| 137 // Index to the endpoint that was last successfully connected. | 148 // Index to the endpoint that was last successfully connected. |
| 138 size_t last_successful_endpoint_; | 149 size_t last_successful_endpoint_; |
| 139 | 150 |
| 140 // The backoff policy to use. | 151 // The backoff policy to use. |
| 141 const net::BackoffEntry::Policy backoff_policy_; | 152 const net::BackoffEntry::Policy backoff_policy_; |
| 142 | 153 |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 200 ConnectionListener* listener_; | 211 ConnectionListener* listener_; |
| 201 | 212 |
| 202 base::WeakPtrFactory<ConnectionFactoryImpl> weak_ptr_factory_; | 213 base::WeakPtrFactory<ConnectionFactoryImpl> weak_ptr_factory_; |
| 203 | 214 |
| 204 DISALLOW_COPY_AND_ASSIGN(ConnectionFactoryImpl); | 215 DISALLOW_COPY_AND_ASSIGN(ConnectionFactoryImpl); |
| 205 }; | 216 }; |
| 206 | 217 |
| 207 } // namespace gcm | 218 } // namespace gcm |
| 208 | 219 |
| 209 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_ | 220 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_ |
| OLD | NEW |