[Metrics] Make MetricsStateManager take a callback param to check if UMA is enabled.
[chromium-blink-merge.git] / google_apis / gcm / engine / connection_factory.h
blob393d6a9ca247d32030abd1baa9170f3913f9c8b4
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_
8 #include "base/time/time.h"
9 #include "google_apis/gcm/base/gcm_export.h"
10 #include "google_apis/gcm/engine/connection_handler.h"
12 namespace mcs_proto {
13 class LoginRequest;
16 namespace gcm {
18 // Factory for creating a ConnectionHandler and maintaining its connection.
19 // The factory retains ownership of the ConnectionHandler and will enforce
20 // backoff policies when attempting connections.
21 class GCM_EXPORT ConnectionFactory {
22 public:
23 typedef base::Callback<void(mcs_proto::LoginRequest* login_request)>
24 BuildLoginRequestCallback;
26 // Reasons for triggering a connection reset. Note that these enums are
27 // consumed by a histogram, so ordering should not be modified.
28 enum ConnectionResetReason {
29 LOGIN_FAILURE, // Login response included an error.
30 CLOSE_COMMAND, // Received a close command.
31 HEARTBEAT_FAILURE, // Heartbeat was not acknowledged in time.
32 SOCKET_FAILURE, // net::Socket error.
33 NETWORK_CHANGE, // NetworkChangeNotifier notified of a network change.
34 // Count of total number of connection reset reasons. All new reset reasons
35 // should be added above this line.
36 CONNECTION_RESET_COUNT,
39 ConnectionFactory();
40 virtual ~ConnectionFactory();
42 // Initialize the factory, creating a connection handler with a disconnected
43 // socket. Should only be called once.
44 // Upon connection:
45 // |read_callback| will be invoked with the contents of any received protobuf
46 // message.
47 // |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
49 // other end received it.
50 virtual void Initialize(
51 const BuildLoginRequestCallback& request_builder,
52 const ConnectionHandler::ProtoReceivedCallback& read_callback,
53 const ConnectionHandler::ProtoSentCallback& write_callback) = 0;
55 // Get the connection handler for this factory. Initialize(..) must have
56 // been called.
57 virtual ConnectionHandler* GetConnectionHandler() const = 0;
59 // Opens a new connection and initiates login handshake. Upon completion of
60 // the handshake, |read_callback| will be invoked with a valid
61 // mcs_proto::LoginResponse.
62 // Note: Initialize must have already been invoked.
63 virtual void Connect() = 0;
65 // Whether or not the MCS endpoint is currently reachable with an active
66 // connection.
67 virtual bool IsEndpointReachable() const = 0;
69 // 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
71 // backoff is in progress.
72 virtual base::TimeTicks NextRetryAttempt() const = 0;
74 // Manually reset the connection. This can occur if an application specific
75 // event forced a reset (e.g. server sends a close connection response).
76 // If the last connection was made within kConnectionResetWindowSecs, the old
77 // backoff is restored, else a new backoff kicks off.
78 virtual void SignalConnectionReset(ConnectionResetReason reason) = 0;
81 } // namespace gcm
83 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_