Upstream version 5.34.104.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/socket/client_socket_handle.h"
16 #include "url/gurl.h"
17
18 namespace net {
19 class HttpNetworkSession;
20 class NetLog;
21 }
22
23 namespace gcm {
24
25 class ConnectionHandlerImpl;
26
27 class GCM_EXPORT ConnectionFactoryImpl :
28     public ConnectionFactory,
29     public net::NetworkChangeNotifier::ConnectionTypeObserver,
30     public net::NetworkChangeNotifier::IPAddressObserver {
31  public:
32   ConnectionFactoryImpl(
33       const GURL& mcs_endpoint,
34       const net::BackoffEntry::Policy& backoff_policy,
35       scoped_refptr<net::HttpNetworkSession> network_session,
36       net::NetLog* net_log);
37   virtual ~ConnectionFactoryImpl();
38
39   // ConnectionFactory implementation.
40   virtual void Initialize(
41       const BuildLoginRequestCallback& request_builder,
42       const ConnectionHandler::ProtoReceivedCallback& read_callback,
43       const ConnectionHandler::ProtoSentCallback& write_callback) OVERRIDE;
44   virtual ConnectionHandler* GetConnectionHandler() const OVERRIDE;
45   virtual void Connect() OVERRIDE;
46   virtual bool IsEndpointReachable() const OVERRIDE;
47   virtual base::TimeTicks NextRetryAttempt() const OVERRIDE;
48   virtual void SignalConnectionReset(ConnectionResetReason reason) OVERRIDE;
49
50   // NetworkChangeNotifier observer implementations.
51   virtual void OnConnectionTypeChanged(
52       net::NetworkChangeNotifier::ConnectionType type) OVERRIDE;
53   virtual void OnIPAddressChanged() OVERRIDE;
54
55  protected:
56   // Implementation of Connect(..). If not in backoff, uses |login_request_|
57   // in attempting a connection/handshake. On connection/handshake failure, goes
58   // into backoff.
59   // Virtual for testing.
60   virtual void ConnectImpl();
61
62   // Helper method for initalizing the connection hander.
63   // Virtual for testing.
64   virtual void InitHandler();
65
66   // Helper method for creating a backoff entry.
67   // Virtual for testing.
68   virtual scoped_ptr<net::BackoffEntry> CreateBackoffEntry(
69       const net::BackoffEntry::Policy* const policy);
70
71   // Returns the current time in Ticks.
72   // Virtual for testing.
73   virtual base::TimeTicks NowTicks();
74
75   // Callback for Socket connection completion.
76   void OnConnectDone(int result);
77
78   // ConnectionHandler callback for connection issues.
79   void ConnectionHandlerCallback(int result);
80
81  private:
82   // The MCS endpoint to make connections to.
83   const GURL mcs_endpoint_;
84
85   // The backoff policy to use.
86   const net::BackoffEntry::Policy backoff_policy_;
87
88   // ---- net:: components for establishing connections. ----
89   // Network session for creating new connections.
90   const scoped_refptr<net::HttpNetworkSession> network_session_;
91   // Net log to use in connection attempts.
92   net::NetLog* const net_log_;
93   // The handle to the socket for the current connection, if one exists.
94   net::ClientSocketHandle socket_handle_;
95   // Current backoff entry.
96   scoped_ptr<net::BackoffEntry> backoff_entry_;
97   // Backoff entry from previous connection attempt. Updated on each login
98   // completion.
99   scoped_ptr<net::BackoffEntry> previous_backoff_;
100
101   // Whether a connection attempt is currently in progress or we're in backoff
102   // waiting until the next connection attempt. |!connecting_| denotes
103   // steady state with an active connection.
104   bool connecting_;
105
106   // Whether login successfully completed after the connection was established.
107   // If a connection reset happens while attempting to log in, the current
108   // backoff entry is reused (after incrementing with a new failure).
109   bool logging_in_;
110
111   // The time of the last login completion. Used for calculating whether to
112   // restore a previous backoff entry and for measuring uptime.
113   base::TimeTicks last_login_time_;
114
115   // The current connection handler, if one exists.
116   scoped_ptr<ConnectionHandlerImpl> connection_handler_;
117
118   // Builder for generating new login requests.
119   BuildLoginRequestCallback request_builder_;
120
121   base::WeakPtrFactory<ConnectionFactoryImpl> weak_ptr_factory_;
122
123   DISALLOW_COPY_AND_ASSIGN(ConnectionFactoryImpl);
124 };
125
126 }  // namespace gcm
127
128 #endif  // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_