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/protocol/mcs.pb.h"
13 #include "net/base/backoff_entry.h"
14 #include "net/base/network_change_notifier.h"
15 #include "net/proxy/proxy_info.h"
16 #include "net/proxy/proxy_service.h"
17 #include "net/socket/client_socket_handle.h"
21 class HttpNetworkSession
;
27 class ConnectionHandlerImpl
;
28 class GCMStatsRecorder
;
30 class GCM_EXPORT ConnectionFactoryImpl
:
31 public ConnectionFactory
,
32 public net::NetworkChangeNotifier::ConnectionTypeObserver
,
33 public net::NetworkChangeNotifier::IPAddressObserver
{
35 ConnectionFactoryImpl(
36 const std::vector
<GURL
>& mcs_endpoints
,
37 const net::BackoffEntry::Policy
& backoff_policy
,
38 scoped_refptr
<net::HttpNetworkSession
> network_session
,
40 GCMStatsRecorder
* recorder
);
41 virtual ~ConnectionFactoryImpl();
43 // ConnectionFactory implementation.
44 virtual void Initialize(
45 const BuildLoginRequestCallback
& request_builder
,
46 const ConnectionHandler::ProtoReceivedCallback
& read_callback
,
47 const ConnectionHandler::ProtoSentCallback
& write_callback
) OVERRIDE
;
48 virtual ConnectionHandler
* GetConnectionHandler() const OVERRIDE
;
49 virtual void Connect() OVERRIDE
;
50 virtual bool IsEndpointReachable() const OVERRIDE
;
51 virtual base::TimeTicks
NextRetryAttempt() const OVERRIDE
;
52 virtual void SignalConnectionReset(ConnectionResetReason reason
) OVERRIDE
;
54 // NetworkChangeNotifier observer implementations.
55 virtual void OnConnectionTypeChanged(
56 net::NetworkChangeNotifier::ConnectionType type
) OVERRIDE
;
57 virtual void OnIPAddressChanged() OVERRIDE
;
59 // Returns the server to which the factory is currently connected, or if
60 // a connection is currently pending, the server to which the next connection
61 // attempt will be made.
62 GURL
GetCurrentEndpoint() const;
65 // Implementation of Connect(..). If not in backoff, uses |login_request_|
66 // in attempting a connection/handshake. On connection/handshake failure, goes
68 // Virtual for testing.
69 virtual void ConnectImpl();
71 // Helper method for initalizing the connection hander.
72 // Virtual for testing.
73 virtual void InitHandler();
75 // Helper method for creating a backoff entry.
76 // Virtual for testing.
77 virtual scoped_ptr
<net::BackoffEntry
> CreateBackoffEntry(
78 const net::BackoffEntry::Policy
* const policy
);
80 // Helper method for creating the connection handler.
81 // Virtual for testing.
82 virtual scoped_ptr
<ConnectionHandler
> CreateConnectionHandler(
83 base::TimeDelta read_timeout
,
84 const ConnectionHandler::ProtoReceivedCallback
& read_callback
,
85 const ConnectionHandler::ProtoSentCallback
& write_callback
,
86 const ConnectionHandler::ConnectionChangedCallback
& connection_callback
);
88 // Returns the current time in Ticks.
89 // Virtual for testing.
90 virtual base::TimeTicks
NowTicks();
92 // Callback for Socket connection completion.
93 void OnConnectDone(int result
);
95 // ConnectionHandler callback for connection issues.
96 void ConnectionHandlerCallback(int result
);
99 // Helper method for checking backoff and triggering a connection as
101 void ConnectWithBackoff();
103 // Proxy resolution and connection functions.
104 void OnProxyResolveDone(int status
);
105 void OnProxyConnectDone(int status
);
106 int ReconsiderProxyAfterError(int error
);
107 void ReportSuccessfulProxyConnection();
111 // The MCS endpoints to make connections to, sorted in order of priority.
112 const std::vector
<GURL
> mcs_endpoints_
;
113 // Index to the endpoint for which a connection should be attempted next.
114 size_t next_endpoint_
;
115 // Index to the endpoint that was last successfully connected.
116 size_t last_successful_endpoint_
;
118 // The backoff policy to use.
119 const net::BackoffEntry::Policy backoff_policy_
;
121 // ---- net:: components for establishing connections. ----
122 // Network session for creating new connections.
123 const scoped_refptr
<net::HttpNetworkSession
> network_session_
;
124 // Net log to use in connection attempts.
125 net::BoundNetLog bound_net_log_
;
126 // The current PAC request, if one exists. Owned by the proxy service.
127 net::ProxyService::PacRequest
* pac_request_
;
128 // The current proxy info.
129 net::ProxyInfo proxy_info_
;
130 // The handle to the socket for the current connection, if one exists.
131 net::ClientSocketHandle socket_handle_
;
132 // Current backoff entry.
133 scoped_ptr
<net::BackoffEntry
> backoff_entry_
;
134 // Backoff entry from previous connection attempt. Updated on each login
136 scoped_ptr
<net::BackoffEntry
> previous_backoff_
;
138 // Whether a connection attempt is currently actively in progress.
141 // Whether the client is waiting for backoff to finish before attempting to
142 // connect. Canary jobs are able to preempt connections pending backoff
144 bool waiting_for_backoff_
;
146 // Whether login successfully completed after the connection was established.
147 // If a connection reset happens while attempting to log in, the current
148 // backoff entry is reused (after incrementing with a new failure).
151 // The time of the last login completion. Used for calculating whether to
152 // restore a previous backoff entry and for measuring uptime.
153 base::TimeTicks last_login_time_
;
155 // The current connection handler, if one exists.
156 scoped_ptr
<ConnectionHandler
> connection_handler_
;
158 // Builder for generating new login requests.
159 BuildLoginRequestCallback request_builder_
;
161 // Recorder that records GCM activities for debugging purpose. Not owned.
162 GCMStatsRecorder
* recorder_
;
164 base::WeakPtrFactory
<ConnectionFactoryImpl
> weak_ptr_factory_
;
166 DISALLOW_COPY_AND_ASSIGN(ConnectionFactoryImpl
);
171 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_