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
3 // found in the LICENSE file.
5 #ifndef GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_
6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_
8 #include "google_apis/gcm/engine/connection_factory.h"
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "google_apis/gcm/engine/connection_handler.h"
13 #include "google_apis/gcm/protocol/mcs.pb.h"
14 #include "net/base/backoff_entry.h"
15 #include "net/base/network_change_notifier.h"
16 #include "net/proxy/proxy_info.h"
17 #include "net/proxy/proxy_service.h"
18 #include "net/socket/client_socket_handle.h"
22 class HttpNetworkSession
;
28 class ConnectionHandlerImpl
;
29 class GCMStatsRecorder
;
31 class GCM_EXPORT ConnectionFactoryImpl
:
32 public ConnectionFactory
,
33 public net::NetworkChangeNotifier::NetworkChangeObserver
{
35 // |http_network_session| is an optional network session to use as a source
36 // for proxy auth credentials (via its HttpAuthCache). |gcm_network_session|
37 // is the network session through which GCM connections should be made, and
38 // must not be the same as |http_network_session|.
39 ConnectionFactoryImpl(
40 const std::vector
<GURL
>& mcs_endpoints
,
41 const net::BackoffEntry::Policy
& backoff_policy
,
42 const scoped_refptr
<net::HttpNetworkSession
>& gcm_network_session
,
43 const scoped_refptr
<net::HttpNetworkSession
>& http_network_session
,
45 GCMStatsRecorder
* recorder
);
46 ~ConnectionFactoryImpl() override
;
48 // ConnectionFactory implementation.
50 const BuildLoginRequestCallback
& request_builder
,
51 const ConnectionHandler::ProtoReceivedCallback
& read_callback
,
52 const ConnectionHandler::ProtoSentCallback
& write_callback
) override
;
53 ConnectionHandler
* GetConnectionHandler() const override
;
54 void Connect() override
;
55 bool IsEndpointReachable() const override
;
56 std::string
GetConnectionStateString() const override
;
57 base::TimeTicks
NextRetryAttempt() const override
;
58 void SignalConnectionReset(ConnectionResetReason reason
) override
;
59 void SetConnectionListener(ConnectionListener
* listener
) override
;
61 // NetworkChangeObserver implementation.
62 void OnNetworkChanged(
63 net::NetworkChangeNotifier::ConnectionType type
) override
;
65 // Returns the server to which the factory is currently connected, or if
66 // a connection is currently pending, the server to which the next connection
67 // attempt will be made.
68 GURL
GetCurrentEndpoint() const;
70 // Returns the IPEndpoint to which the factory is currently connected. If no
71 // connection is active, returns an empty IPEndpoint.
72 net::IPEndPoint
GetPeerIP();
75 // Implementation of Connect(..). If not in backoff, uses |login_request_|
76 // in attempting a connection/handshake. On connection/handshake failure, goes
78 // Virtual for testing.
79 virtual void ConnectImpl();
81 // Helper method for initalizing the connection hander.
82 // Virtual for testing.
83 virtual void InitHandler();
85 // Helper method for creating a backoff entry.
86 // Virtual for testing.
87 virtual scoped_ptr
<net::BackoffEntry
> CreateBackoffEntry(
88 const net::BackoffEntry::Policy
* const policy
);
90 // Helper method for creating the connection handler.
91 // Virtual for testing.
92 virtual scoped_ptr
<ConnectionHandler
> CreateConnectionHandler(
93 base::TimeDelta read_timeout
,
94 const ConnectionHandler::ProtoReceivedCallback
& read_callback
,
95 const ConnectionHandler::ProtoSentCallback
& write_callback
,
96 const ConnectionHandler::ConnectionChangedCallback
& connection_callback
);
98 // Returns the current time in Ticks.
99 // Virtual for testing.
100 virtual base::TimeTicks
NowTicks();
102 // Callback for Socket connection completion.
103 void OnConnectDone(int result
);
105 // ConnectionHandler callback for connection issues.
106 void ConnectionHandlerCallback(int result
);
109 // Helper method for checking backoff and triggering a connection as
111 void ConnectWithBackoff();
113 // Proxy resolution and connection functions.
114 void OnProxyResolveDone(int status
);
115 void OnProxyConnectDone(int status
);
116 int ReconsiderProxyAfterError(int error
);
117 void ReportSuccessfulProxyConnection();
119 // Closes the local socket if one is present, and resets connection handler.
122 // Updates the GCM Network Session's HttpAuthCache with the HTTP Network
123 // Session's cache, if available.
124 void RebuildNetworkSessionAuthCache();
126 // The MCS endpoints to make connections to, sorted in order of priority.
127 const std::vector
<GURL
> mcs_endpoints_
;
128 // Index to the endpoint for which a connection should be attempted next.
129 size_t next_endpoint_
;
130 // Index to the endpoint that was last successfully connected.
131 size_t last_successful_endpoint_
;
133 // The backoff policy to use.
134 const net::BackoffEntry::Policy backoff_policy_
;
136 // ---- net:: components for establishing connections. ----
137 // Network session for creating new GCM connections.
138 const scoped_refptr
<net::HttpNetworkSession
> gcm_network_session_
;
139 // HTTP Network session. If set, is used for extracting proxy auth
140 // credentials. If not set, is ignored.
141 const scoped_refptr
<net::HttpNetworkSession
> http_network_session_
;
142 // Net log to use in connection attempts.
143 net::BoundNetLog bound_net_log_
;
144 // The current PAC request, if one exists. Owned by the proxy service.
145 net::ProxyService::PacRequest
* pac_request_
;
146 // The current proxy info.
147 net::ProxyInfo proxy_info_
;
148 // The handle to the socket for the current connection, if one exists.
149 net::ClientSocketHandle socket_handle_
;
150 // Current backoff entry.
151 scoped_ptr
<net::BackoffEntry
> backoff_entry_
;
152 // Backoff entry from previous connection attempt. Updated on each login
154 scoped_ptr
<net::BackoffEntry
> previous_backoff_
;
156 // Whether a connection attempt is currently actively in progress.
159 // Whether the client is waiting for backoff to finish before attempting to
160 // connect. Canary jobs are able to preempt connections pending backoff
162 bool waiting_for_backoff_
;
164 // Whether the NetworkChangeNotifier has informed the client that there is
165 // no current connection. No connection attempts will be made until the
166 // client is informed of a valid connection type.
167 bool waiting_for_network_online_
;
169 // Whether login successfully completed after the connection was established.
170 // If a connection reset happens while attempting to log in, the current
171 // backoff entry is reused (after incrementing with a new failure).
174 // The time of the last login completion. Used for calculating whether to
175 // restore a previous backoff entry and for measuring uptime.
176 base::TimeTicks last_login_time_
;
178 // Cached callbacks. Set at |Initialize| time, consumed at first |Connect|
180 ConnectionHandler::ProtoReceivedCallback read_callback_
;
181 ConnectionHandler::ProtoSentCallback write_callback_
;
183 // The current connection handler, if one exists.
184 scoped_ptr
<ConnectionHandler
> connection_handler_
;
186 // Builder for generating new login requests.
187 BuildLoginRequestCallback request_builder_
;
189 // Recorder that records GCM activities for debugging purpose. Not owned.
190 GCMStatsRecorder
* recorder_
;
192 // Listener for connection change events.
193 ConnectionListener
* listener_
;
195 base::WeakPtrFactory
<ConnectionFactoryImpl
> weak_ptr_factory_
;
197 DISALLOW_COPY_AND_ASSIGN(ConnectionFactoryImpl
);
202 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_