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.
4
5#ifndef GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_
6#define GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_
7
8#include <string>
9
10#include "base/time/time.h"
11#include "google_apis/gcm/base/gcm_export.h"
12#include "google_apis/gcm/engine/connection_handler.h"
13
14class GURL;
15
16namespace net {
17class IPEndPoint;
18}
19
20namespace mcs_proto {
21class LoginRequest;
22}
23
24namespace gcm {
25
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.
29class GCM_EXPORT ConnectionFactory {
30 public:
31  typedef base::Callback<void(mcs_proto::LoginRequest* login_request)>
32      BuildLoginRequestCallback;
33
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 change.
42    // Count of total number of connection reset reasons. All new reset reasons
43    // should be added above this line.
44    CONNECTION_RESET_COUNT,
45  };
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
63  ConnectionFactory();
64  virtual ~ConnectionFactory();
65
66  // Initialize the factory, creating a connection handler with a disconnected
67  // socket. Should only be called once.
68  // Upon connection:
69  // |read_callback| will be invoked with the contents of any received protobuf
70  // message.
71  // |write_callback| will be invoked anytime a message has been successfully
72  // sent. Note: this just means the data was sent to the wire, not that the
73  // other end received it.
74  virtual void Initialize(
75      const BuildLoginRequestCallback& request_builder,
76      const ConnectionHandler::ProtoReceivedCallback& read_callback,
77      const ConnectionHandler::ProtoSentCallback& write_callback) = 0;
78
79  // Get the connection handler for this factory. Initialize(..) must have
80  // been called.
81  virtual ConnectionHandler* GetConnectionHandler() const = 0;
82
83  // Opens a new connection and initiates login handshake. Upon completion of
84  // the handshake, |read_callback| will be invoked with a valid
85  // mcs_proto::LoginResponse.
86  // Note: Initialize must have already been invoked.
87  virtual void Connect() = 0;
88
89  // Whether or not the MCS endpoint is currently reachable with an active
90  // connection.
91  virtual bool IsEndpointReachable() const = 0;
92
93  // Returns a debug string describing the connection state.
94  virtual std::string GetConnectionStateString() const = 0;
95
96  // If in backoff, the time at which the next retry will be made. Otherwise,
97  // a null time, indicating either no attempt to connect has been made or no
98  // backoff is in progress.
99  virtual base::TimeTicks NextRetryAttempt() const = 0;
100
101  // Manually reset the connection. This can occur if an application specific
102  // event forced a reset (e.g. server sends a close connection response).
103  // If the last connection was made within kConnectionResetWindowSecs, the old
104  // backoff is restored, else a new backoff kicks off.
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;
111};
112
113}  // namespace gcm
114
115#endif  // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_
116