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 "google_apis/gcm/protocol/mcs.pb.h"
12 #include "net/base/backoff_entry.h"
13 #include "net/base/network_change_notifier.h"
14 #include "net/socket/client_socket_handle.h"
18 class HttpNetworkSession
;
24 class ConnectionHandlerImpl
;
26 class GCM_EXPORT ConnectionFactoryImpl
:
27 public ConnectionFactory
,
28 public net::NetworkChangeNotifier::ConnectionTypeObserver
,
29 public net::NetworkChangeNotifier::IPAddressObserver
{
31 ConnectionFactoryImpl(
32 const GURL
& mcs_endpoint
,
33 scoped_refptr
<net::HttpNetworkSession
> network_session
,
34 net::NetLog
* net_log
);
35 virtual ~ConnectionFactoryImpl();
37 // ConnectionFactory implementation.
38 virtual void Initialize(
39 const BuildLoginRequestCallback
& request_builder
,
40 const ConnectionHandler::ProtoReceivedCallback
& read_callback
,
41 const ConnectionHandler::ProtoSentCallback
& write_callback
) OVERRIDE
;
42 virtual ConnectionHandler
* GetConnectionHandler() const OVERRIDE
;
43 virtual void Connect() OVERRIDE
;
44 virtual bool IsEndpointReachable() const OVERRIDE
;
45 virtual base::TimeTicks
NextRetryAttempt() const OVERRIDE
;
46 virtual void SignalConnectionReset() OVERRIDE
;
48 // NetworkChangeNotifier observer implementations.
49 virtual void OnConnectionTypeChanged(
50 net::NetworkChangeNotifier::ConnectionType type
) OVERRIDE
;
51 virtual void OnIPAddressChanged() OVERRIDE
;
54 // Implementation of Connect(..). If not in backoff, uses |login_request_|
55 // in attempting a connection/handshake. On connection/handshake failure, goes
57 // Virtual for testing.
58 virtual void ConnectImpl();
60 // Helper method for initalizing the connection hander.
61 // Virtual for testing.
62 virtual void InitHandler();
64 // Helper method for creating a backoff entry.
65 // Virtual for testing.
66 virtual scoped_ptr
<net::BackoffEntry
> CreateBackoffEntry(
67 const net::BackoffEntry::Policy
* const policy
);
69 // Returns the current time in Ticks.
70 // Virtual for testing.
71 virtual base::TimeTicks
NowTicks();
73 // Callback for Socket connection completion.
74 void OnConnectDone(int result
);
76 // ConnectionHandler callback for connection issues.
77 void ConnectionHandlerCallback(int result
);
80 // The MCS endpoint to make connections to.
81 const GURL mcs_endpoint_
;
83 // ---- net:: components for establishing connections. ----
84 // Network session for creating new connections.
85 const scoped_refptr
<net::HttpNetworkSession
> network_session_
;
86 // Net log to use in connection attempts.
87 net::NetLog
* const net_log_
;
88 // The handle to the socket for the current connection, if one exists.
89 net::ClientSocketHandle socket_handle_
;
90 // Connection attempt backoff policy.
91 scoped_ptr
<net::BackoffEntry
> backoff_entry_
;
92 // Backoff policy from previous backoff attempt.
93 scoped_ptr
<net::BackoffEntry
> previous_backoff_
;
94 base::TimeTicks backoff_reset_time_
;
96 // Whether a connection attempt is currently in progress or we're in backoff
97 // waiting until the next connection attempt. |!connecting_| denotes
98 // steady state with an active connection.
101 // The current connection handler, if one exists.
102 scoped_ptr
<ConnectionHandlerImpl
> connection_handler_
;
104 // Builder for generating new login requests.
105 BuildLoginRequestCallback request_builder_
;
107 base::WeakPtrFactory
<ConnectionFactoryImpl
> weak_ptr_factory_
;
109 DISALLOW_COPY_AND_ASSIGN(ConnectionFactoryImpl
);
114 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_