OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_ | 5 #ifndef GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_ |
6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_ | 6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_ |
7 | 7 |
| 8 #include <string> |
| 9 |
8 #include "base/time/time.h" | 10 #include "base/time/time.h" |
9 #include "google_apis/gcm/base/gcm_export.h" | 11 #include "google_apis/gcm/base/gcm_export.h" |
10 #include "google_apis/gcm/engine/connection_handler.h" | 12 #include "google_apis/gcm/engine/connection_handler.h" |
11 | 13 |
| 14 class GURL; |
| 15 |
| 16 namespace net { |
| 17 class IPEndPoint; |
| 18 } |
| 19 |
12 namespace mcs_proto { | 20 namespace mcs_proto { |
13 class LoginRequest; | 21 class LoginRequest; |
14 } | 22 } |
15 | 23 |
16 namespace gcm { | 24 namespace gcm { |
17 | 25 |
18 // Factory for creating a ConnectionHandler and maintaining its connection. | 26 // Factory for creating a ConnectionHandler and maintaining its connection. |
19 // The factory retains ownership of the ConnectionHandler and will enforce | 27 // The factory retains ownership of the ConnectionHandler and will enforce |
20 // backoff policies when attempting connections. | 28 // backoff policies when attempting connections. |
21 class GCM_EXPORT ConnectionFactory { | 29 class GCM_EXPORT ConnectionFactory { |
22 public: | 30 public: |
23 typedef base::Callback<void(mcs_proto::LoginRequest* login_request)> | 31 typedef base::Callback<void(mcs_proto::LoginRequest* login_request)> |
24 BuildLoginRequestCallback; | 32 BuildLoginRequestCallback; |
25 | 33 |
26 // Reasons for triggering a connection reset. Note that these enums are | 34 // Reasons for triggering a connection reset. Note that these enums are |
27 // consumed by a histogram, so ordering should not be modified. | 35 // consumed by a histogram, so ordering should not be modified. |
28 enum ConnectionResetReason { | 36 enum ConnectionResetReason { |
29 LOGIN_FAILURE, // Login response included an error. | 37 LOGIN_FAILURE, // Login response included an error. |
30 CLOSE_COMMAND, // Received a close command. | 38 CLOSE_COMMAND, // Received a close command. |
31 HEARTBEAT_FAILURE, // Heartbeat was not acknowledged in time. | 39 HEARTBEAT_FAILURE, // Heartbeat was not acknowledged in time. |
32 SOCKET_FAILURE, // net::Socket error. | 40 SOCKET_FAILURE, // net::Socket error. |
33 NETWORK_CHANGE, // NetworkChangeNotifier notified of a network change. | 41 NETWORK_CHANGE, // NetworkChangeNotifier notified of a network change. |
34 // Count of total number of connection reset reasons. All new reset reasons | 42 // Count of total number of connection reset reasons. All new reset reasons |
35 // should be added above this line. | 43 // should be added above this line. |
36 CONNECTION_RESET_COUNT, | 44 CONNECTION_RESET_COUNT, |
37 }; | 45 }; |
38 | 46 |
| 47 // Listener interface to be notified of endpoint connection events. |
| 48 class GCM_EXPORT ConnectionListener { |
| 49 public: |
| 50 ConnectionListener(); |
| 51 virtual ~ConnectionListener(); |
| 52 |
| 53 // Notifies the listener that GCM has performed a handshake with and is now |
| 54 // actively connected to |current_server|. |ip_endpoint| is the resolved |
| 55 // ip address/port through which the connection is being made. |
| 56 virtual void OnConnected(const GURL& current_server, |
| 57 const net::IPEndPoint& ip_endpoint) = 0; |
| 58 |
| 59 // Notifies the listener that the connection has been interrupted. |
| 60 virtual void OnDisconnected() = 0; |
| 61 }; |
| 62 |
39 ConnectionFactory(); | 63 ConnectionFactory(); |
40 virtual ~ConnectionFactory(); | 64 virtual ~ConnectionFactory(); |
41 | 65 |
42 // Initialize the factory, creating a connection handler with a disconnected | 66 // Initialize the factory, creating a connection handler with a disconnected |
43 // socket. Should only be called once. | 67 // socket. Should only be called once. |
44 // Upon connection: | 68 // Upon connection: |
45 // |read_callback| will be invoked with the contents of any received protobuf | 69 // |read_callback| will be invoked with the contents of any received protobuf |
46 // message. | 70 // message. |
47 // |write_callback| will be invoked anytime a message has been successfully | 71 // |write_callback| will be invoked anytime a message has been successfully |
48 // sent. Note: this just means the data was sent to the wire, not that the | 72 // sent. Note: this just means the data was sent to the wire, not that the |
(...skipping 10 matching lines...) Expand all Loading... |
59 // Opens a new connection and initiates login handshake. Upon completion of | 83 // Opens a new connection and initiates login handshake. Upon completion of |
60 // the handshake, |read_callback| will be invoked with a valid | 84 // the handshake, |read_callback| will be invoked with a valid |
61 // mcs_proto::LoginResponse. | 85 // mcs_proto::LoginResponse. |
62 // Note: Initialize must have already been invoked. | 86 // Note: Initialize must have already been invoked. |
63 virtual void Connect() = 0; | 87 virtual void Connect() = 0; |
64 | 88 |
65 // Whether or not the MCS endpoint is currently reachable with an active | 89 // Whether or not the MCS endpoint is currently reachable with an active |
66 // connection. | 90 // connection. |
67 virtual bool IsEndpointReachable() const = 0; | 91 virtual bool IsEndpointReachable() const = 0; |
68 | 92 |
| 93 // Returns a debug string describing the connection state. |
| 94 virtual std::string GetConnectionStateString() const = 0; |
| 95 |
69 // If in backoff, the time at which the next retry will be made. Otherwise, | 96 // If in backoff, the time at which the next retry will be made. Otherwise, |
70 // a null time, indicating either no attempt to connect has been made or no | 97 // a null time, indicating either no attempt to connect has been made or no |
71 // backoff is in progress. | 98 // backoff is in progress. |
72 virtual base::TimeTicks NextRetryAttempt() const = 0; | 99 virtual base::TimeTicks NextRetryAttempt() const = 0; |
73 | 100 |
74 // Manually reset the connection. This can occur if an application specific | 101 // Manually reset the connection. This can occur if an application specific |
75 // event forced a reset (e.g. server sends a close connection response). | 102 // event forced a reset (e.g. server sends a close connection response). |
76 // If the last connection was made within kConnectionResetWindowSecs, the old | 103 // If the last connection was made within kConnectionResetWindowSecs, the old |
77 // backoff is restored, else a new backoff kicks off. | 104 // backoff is restored, else a new backoff kicks off. |
78 virtual void SignalConnectionReset(ConnectionResetReason reason) = 0; | 105 virtual void SignalConnectionReset(ConnectionResetReason reason) = 0; |
| 106 |
| 107 // Sets the current connection listener. Only one listener is supported at a |
| 108 // time, and the listener must either outlive the connection factory or |
| 109 // call SetConnectionListener(NULL) upon destruction. |
| 110 virtual void SetConnectionListener(ConnectionListener* listener) = 0; |
79 }; | 111 }; |
80 | 112 |
81 } // namespace gcm | 113 } // namespace gcm |
82 | 114 |
83 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_ | 115 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_ |
OLD | NEW |