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/socket/client_socket_handle.h"
19 class HttpNetworkSession;
25 class ConnectionHandlerImpl;
27 class GCM_EXPORT ConnectionFactoryImpl :
28 public ConnectionFactory,
29 public net::NetworkChangeNotifier::ConnectionTypeObserver,
30 public net::NetworkChangeNotifier::IPAddressObserver {
32 ConnectionFactoryImpl(
33 const GURL& mcs_endpoint,
34 const net::BackoffEntry::Policy& backoff_policy,
35 scoped_refptr<net::HttpNetworkSession> network_session,
36 net::NetLog* net_log);
37 virtual ~ConnectionFactoryImpl();
39 // ConnectionFactory implementation.
40 virtual void Initialize(
41 const BuildLoginRequestCallback& request_builder,
42 const ConnectionHandler::ProtoReceivedCallback& read_callback,
43 const ConnectionHandler::ProtoSentCallback& write_callback) OVERRIDE;
44 virtual ConnectionHandler* GetConnectionHandler() const OVERRIDE;
45 virtual void Connect() OVERRIDE;
46 virtual bool IsEndpointReachable() const OVERRIDE;
47 virtual base::TimeTicks NextRetryAttempt() const OVERRIDE;
48 virtual void SignalConnectionReset(ConnectionResetReason reason) OVERRIDE;
50 // NetworkChangeNotifier observer implementations.
51 virtual void OnConnectionTypeChanged(
52 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
53 virtual void OnIPAddressChanged() OVERRIDE;
56 // Implementation of Connect(..). If not in backoff, uses |login_request_|
57 // in attempting a connection/handshake. On connection/handshake failure, goes
59 // Virtual for testing.
60 virtual void ConnectImpl();
62 // Helper method for initalizing the connection hander.
63 // Virtual for testing.
64 virtual void InitHandler();
66 // Helper method for creating a backoff entry.
67 // Virtual for testing.
68 virtual scoped_ptr<net::BackoffEntry> CreateBackoffEntry(
69 const net::BackoffEntry::Policy* const policy);
71 // Returns the current time in Ticks.
72 // Virtual for testing.
73 virtual base::TimeTicks NowTicks();
75 // Callback for Socket connection completion.
76 void OnConnectDone(int result);
78 // ConnectionHandler callback for connection issues.
79 void ConnectionHandlerCallback(int result);
82 // The MCS endpoint to make connections to.
83 const GURL mcs_endpoint_;
85 // The backoff policy to use.
86 const net::BackoffEntry::Policy backoff_policy_;
88 // ---- net:: components for establishing connections. ----
89 // Network session for creating new connections.
90 const scoped_refptr<net::HttpNetworkSession> network_session_;
91 // Net log to use in connection attempts.
92 net::NetLog* const net_log_;
93 // The handle to the socket for the current connection, if one exists.
94 net::ClientSocketHandle socket_handle_;
95 // Current backoff entry.
96 scoped_ptr<net::BackoffEntry> backoff_entry_;
97 // Backoff entry from previous connection attempt. Updated on each login
99 scoped_ptr<net::BackoffEntry> previous_backoff_;
101 // Whether a connection attempt is currently in progress or we're in backoff
102 // waiting until the next connection attempt. |!connecting_| denotes
103 // steady state with an active connection.
106 // Whether login successfully completed after the connection was established.
107 // If a connection reset happens while attempting to log in, the current
108 // backoff entry is reused (after incrementing with a new failure).
111 // The time of the last login completion. Used for calculating whether to
112 // restore a previous backoff entry and for measuring uptime.
113 base::TimeTicks last_login_time_;
115 // The current connection handler, if one exists.
116 scoped_ptr<ConnectionHandlerImpl> connection_handler_;
118 // Builder for generating new login requests.
119 BuildLoginRequestCallback request_builder_;
121 base::WeakPtrFactory<ConnectionFactoryImpl> weak_ptr_factory_;
123 DISALLOW_COPY_AND_ASSIGN(ConnectionFactoryImpl);
128 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_