1 // Copyright 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_H_
6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_
10 #include "base/time/time.h"
11 #include "google_apis/gcm/base/gcm_export.h"
12 #include "google_apis/gcm/engine/connection_handler.h"
26 // Factory for creating a ConnectionHandler and maintaining its connection.
27 // The factory retains ownership of the ConnectionHandler and will enforce
28 // backoff policies when attempting connections.
29 class GCM_EXPORT ConnectionFactory
{
31 typedef base::Callback
<void(mcs_proto::LoginRequest
* login_request
)>
32 BuildLoginRequestCallback
;
34 // Reasons for triggering a connection reset. Note that these enums are
35 // consumed by a histogram, so ordering should not be modified.
36 enum ConnectionResetReason
{
37 LOGIN_FAILURE
, // Login response included an error.
38 CLOSE_COMMAND
, // Received a close command.
39 HEARTBEAT_FAILURE
, // Heartbeat was not acknowledged in time.
40 SOCKET_FAILURE
, // net::Socket error.
41 NETWORK_CHANGE
, // NetworkChangeNotifier notified of a network
43 NEW_HEARTBEAT_INTERVAL
, // New heartbeat interval was set.
44 // Count of total number of connection reset reasons. All new reset reasons
45 // should be added above this line.
46 CONNECTION_RESET_COUNT
,
49 // Listener interface to be notified of endpoint connection events.
50 class GCM_EXPORT ConnectionListener
{
53 virtual ~ConnectionListener();
55 // Notifies the listener that GCM has performed a handshake with and is now
56 // actively connected to |current_server|. |ip_endpoint| is the resolved
57 // ip address/port through which the connection is being made.
58 virtual void OnConnected(const GURL
& current_server
,
59 const net::IPEndPoint
& ip_endpoint
) = 0;
61 // Notifies the listener that the connection has been interrupted.
62 virtual void OnDisconnected() = 0;
66 virtual ~ConnectionFactory();
68 // Initialize the factory, creating a connection handler with a disconnected
69 // socket. Should only be called once.
71 // |read_callback| will be invoked with the contents of any received protobuf
73 // |write_callback| will be invoked anytime a message has been successfully
74 // sent. Note: this just means the data was sent to the wire, not that the
75 // other end received it.
76 virtual void Initialize(
77 const BuildLoginRequestCallback
& request_builder
,
78 const ConnectionHandler::ProtoReceivedCallback
& read_callback
,
79 const ConnectionHandler::ProtoSentCallback
& write_callback
) = 0;
81 // Get the connection handler for this factory. Initialize(..) must have
83 virtual ConnectionHandler
* GetConnectionHandler() const = 0;
85 // Opens a new connection and initiates login handshake. Upon completion of
86 // the handshake, |read_callback| will be invoked with a valid
87 // mcs_proto::LoginResponse.
88 // Note: Initialize must have already been invoked.
89 virtual void Connect() = 0;
91 // Whether or not the MCS endpoint is currently reachable with an active
93 virtual bool IsEndpointReachable() const = 0;
95 // Returns a debug string describing the connection state.
96 virtual std::string
GetConnectionStateString() const = 0;
98 // If in backoff, the time at which the next retry will be made. Otherwise,
99 // a null time, indicating either no attempt to connect has been made or no
100 // backoff is in progress.
101 virtual base::TimeTicks
NextRetryAttempt() const = 0;
103 // Manually reset the connection. This can occur if an application specific
104 // event forced a reset (e.g. server sends a close connection response).
105 // If the last connection was made within kConnectionResetWindowSecs, the old
106 // backoff is restored, else a new backoff kicks off.
107 virtual void SignalConnectionReset(ConnectionResetReason reason
) = 0;
109 // Sets the current connection listener. Only one listener is supported at a
110 // time, and the listener must either outlive the connection factory or
111 // call SetConnectionListener(NULL) upon destruction.
112 virtual void SetConnectionListener(ConnectionListener
* listener
) = 0;
117 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_