Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / google_apis / gcm / engine / connection_factory_impl.h
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.
4
5 #ifndef GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_
6 #define GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_
7
8 #include "google_apis/gcm/engine/connection_factory.h"
9
10 #include "base/memory/weak_ptr.h"
11 #include "base/time/time.h"
12 #include "google_apis/gcm/protocol/mcs.pb.h"
13 #include "net/base/backoff_entry.h"
14 #include "net/base/network_change_notifier.h"
15 #include "net/proxy/proxy_info.h"
16 #include "net/proxy/proxy_service.h"
17 #include "net/socket/client_socket_handle.h"
18 #include "url/gurl.h"
19
20 namespace net {
21 class HttpNetworkSession;
22 class NetLog;
23 }
24
25 namespace gcm {
26
27 class ConnectionHandlerImpl;
28 class GCMStatsRecorder;
29
30 class GCM_EXPORT ConnectionFactoryImpl :
31     public ConnectionFactory,
32     public net::NetworkChangeNotifier::ConnectionTypeObserver,
33     public net::NetworkChangeNotifier::IPAddressObserver {
34  public:
35   ConnectionFactoryImpl(
36       const std::vector<GURL>& mcs_endpoints,
37       const net::BackoffEntry::Policy& backoff_policy,
38       scoped_refptr<net::HttpNetworkSession> network_session,
39       net::NetLog* net_log,
40       GCMStatsRecorder* recorder);
41   virtual ~ConnectionFactoryImpl();
42
43   // ConnectionFactory implementation.
44   virtual void Initialize(
45       const BuildLoginRequestCallback& request_builder,
46       const ConnectionHandler::ProtoReceivedCallback& read_callback,
47       const ConnectionHandler::ProtoSentCallback& write_callback) OVERRIDE;
48   virtual ConnectionHandler* GetConnectionHandler() const OVERRIDE;
49   virtual void Connect() OVERRIDE;
50   virtual bool IsEndpointReachable() const OVERRIDE;
51   virtual base::TimeTicks NextRetryAttempt() const OVERRIDE;
52   virtual void SignalConnectionReset(ConnectionResetReason reason) OVERRIDE;
53
54   // NetworkChangeNotifier observer implementations.
55   virtual void OnConnectionTypeChanged(
56       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
57   virtual void OnIPAddressChanged() OVERRIDE;
58
59   // Returns the server to which the factory is currently connected, or if
60   // a connection is currently pending, the server to which the next connection
61   // attempt will be made.
62   GURL GetCurrentEndpoint() const;
63
64  protected:
65   // Implementation of Connect(..). If not in backoff, uses |login_request_|
66   // in attempting a connection/handshake. On connection/handshake failure, goes
67   // into backoff.
68   // Virtual for testing.
69   virtual void ConnectImpl();
70
71   // Helper method for initalizing the connection hander.
72   // Virtual for testing.
73   virtual void InitHandler();
74
75   // Helper method for creating a backoff entry.
76   // Virtual for testing.
77   virtual scoped_ptr<net::BackoffEntry> CreateBackoffEntry(
78       const net::BackoffEntry::Policy* const policy);
79
80   // Helper method for creating the connection handler.
81   // Virtual for testing.
82   virtual scoped_ptr<ConnectionHandler> CreateConnectionHandler(
83       base::TimeDelta read_timeout,
84       const ConnectionHandler::ProtoReceivedCallback& read_callback,
85       const ConnectionHandler::ProtoSentCallback& write_callback,
86       const ConnectionHandler::ConnectionChangedCallback& connection_callback);
87
88   // Returns the current time in Ticks.
89   // Virtual for testing.
90   virtual base::TimeTicks NowTicks();
91
92   // Callback for Socket connection completion.
93   void OnConnectDone(int result);
94
95   // ConnectionHandler callback for connection issues.
96   void ConnectionHandlerCallback(int result);
97
98  private:
99   // Helper method for checking backoff and triggering a connection as
100   // necessary.
101   void ConnectWithBackoff();
102
103   // Proxy resolution and connection functions.
104   void OnProxyResolveDone(int status);
105   void OnProxyConnectDone(int status);
106   int ReconsiderProxyAfterError(int error);
107   void ReportSuccessfulProxyConnection();
108
109   void CloseSocket();
110
111   // The MCS endpoints to make connections to, sorted in order of priority.
112   const std::vector<GURL> mcs_endpoints_;
113   // Index to the endpoint for which a connection should be attempted next.
114   size_t next_endpoint_;
115   // Index to the endpoint that was last successfully connected.
116   size_t last_successful_endpoint_;
117
118   // The backoff policy to use.
119   const net::BackoffEntry::Policy backoff_policy_;
120
121   // ---- net:: components for establishing connections. ----
122   // Network session for creating new connections.
123   const scoped_refptr<net::HttpNetworkSession> network_session_;
124   // Net log to use in connection attempts.
125   net::BoundNetLog bound_net_log_;
126   // The current PAC request, if one exists. Owned by the proxy service.
127   net::ProxyService::PacRequest* pac_request_;
128   // The current proxy info.
129   net::ProxyInfo proxy_info_;
130   // The handle to the socket for the current connection, if one exists.
131   net::ClientSocketHandle socket_handle_;
132   // Current backoff entry.
133   scoped_ptr<net::BackoffEntry> backoff_entry_;
134   // Backoff entry from previous connection attempt. Updated on each login
135   // completion.
136   scoped_ptr<net::BackoffEntry> previous_backoff_;
137
138   // Whether a connection attempt is currently actively in progress.
139   bool connecting_;
140
141   // Whether the client is waiting for backoff to finish before attempting to
142   // connect. Canary jobs are able to preempt connections pending backoff
143   // expiration.
144   bool waiting_for_backoff_;
145
146   // Whether login successfully completed after the connection was established.
147   // If a connection reset happens while attempting to log in, the current
148   // backoff entry is reused (after incrementing with a new failure).
149   bool logging_in_;
150
151   // The time of the last login completion. Used for calculating whether to
152   // restore a previous backoff entry and for measuring uptime.
153   base::TimeTicks last_login_time_;
154
155   // The current connection handler, if one exists.
156   scoped_ptr<ConnectionHandler> connection_handler_;
157
158   // Builder for generating new login requests.
159   BuildLoginRequestCallback request_builder_;
160
161   // Recorder that records GCM activities for debugging purpose. Not owned.
162   GCMStatsRecorder* recorder_;
163
164   base::WeakPtrFactory<ConnectionFactoryImpl> weak_ptr_factory_;
165
166   DISALLOW_COPY_AND_ASSIGN(ConnectionFactoryImpl);
167 };
168
169 }  // namespace gcm
170
171 #endif  // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_