OLD | NEW |
(Empty) | |
| 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 "google_apis/gcm/protocol/mcs.pb.h" |
| 12 #include "net/base/backoff_entry.h" |
| 13 #include "net/base/network_change_notifier.h" |
| 14 #include "net/socket/client_socket_handle.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 namespace net { |
| 18 class HttpNetworkSession; |
| 19 class NetLog; |
| 20 } |
| 21 |
| 22 namespace gcm { |
| 23 |
| 24 class ConnectionHandlerImpl; |
| 25 |
| 26 class GCM_EXPORT ConnectionFactoryImpl : |
| 27 public ConnectionFactory, |
| 28 public net::NetworkChangeNotifier::ConnectionTypeObserver, |
| 29 public net::NetworkChangeNotifier::IPAddressObserver { |
| 30 public: |
| 31 ConnectionFactoryImpl( |
| 32 const GURL& mcs_endpoint, |
| 33 scoped_refptr<net::HttpNetworkSession> network_session, |
| 34 net::NetLog* net_log); |
| 35 virtual ~ConnectionFactoryImpl(); |
| 36 |
| 37 // ConnectionFactory implementation. |
| 38 virtual ConnectionHandler* BuildConnectionHandler( |
| 39 const ConnectionHandler::ProtoReceivedCallback& read_callback, |
| 40 const ConnectionHandler::ProtoSentCallback& write_callback) OVERRIDE; |
| 41 virtual void Connect(const mcs_proto::LoginRequest& login_request) OVERRIDE; |
| 42 virtual bool IsEndpointReachable() const OVERRIDE; |
| 43 virtual base::TimeTicks NextRetryAttempt() const OVERRIDE; |
| 44 |
| 45 // NetworkChangeNotifier observer implementations. |
| 46 virtual void OnConnectionTypeChanged( |
| 47 net::NetworkChangeNotifier::ConnectionType type) OVERRIDE; |
| 48 virtual void OnIPAddressChanged() OVERRIDE; |
| 49 |
| 50 protected: |
| 51 // Implementation of Connect(..). If not in backoff, uses |login_request_| |
| 52 // in attempting a connection/handshake. On connection/handshake failure, goes |
| 53 // into backoff. |
| 54 // Virtual for testing. |
| 55 virtual void ConnectImpl(); |
| 56 |
| 57 // Helper method for initalizing the connection hander. |
| 58 // Virtual for testing. |
| 59 virtual void InitHandler(); |
| 60 |
| 61 // Helper method for creating a backoff entry. |
| 62 // Virtual for testing. |
| 63 virtual scoped_ptr<net::BackoffEntry> CreateBackoffEntry( |
| 64 const net::BackoffEntry::Policy* const policy); |
| 65 |
| 66 // Callback for Socket connection completion. |
| 67 void OnConnectDone(int result); |
| 68 |
| 69 private: |
| 70 // ConnectionHandler callback for connection issues. |
| 71 void ConnectionHandlerCallback(int result); |
| 72 |
| 73 // The MCS endpoint to make connections to. |
| 74 const GURL mcs_endpoint_; |
| 75 |
| 76 // ---- net:: components for establishing connections. ---- |
| 77 // Network session for creating new connections. |
| 78 const scoped_refptr<net::HttpNetworkSession> network_session_; |
| 79 // Net log to use in connection attempts. |
| 80 net::NetLog* const net_log_; |
| 81 // The handle to the socket for the current connection, if one exists. |
| 82 net::ClientSocketHandle socket_handle_; |
| 83 // Connection attempt backoff policy. |
| 84 scoped_ptr<net::BackoffEntry> backoff_entry_; |
| 85 |
| 86 // The current connection handler, if one exists. |
| 87 scoped_ptr<ConnectionHandlerImpl> connection_handler_; |
| 88 |
| 89 // The current login request if a connection attempt is in progress/pending. |
| 90 mcs_proto::LoginRequest login_request_; |
| 91 |
| 92 base::WeakPtrFactory<ConnectionFactoryImpl> weak_ptr_factory_; |
| 93 |
| 94 DISALLOW_COPY_AND_ASSIGN(ConnectionFactoryImpl); |
| 95 }; |
| 96 |
| 97 } // namespace gcm |
| 98 |
| 99 #endif // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_IMPL_H_ |
OLD | NEW |