Home | History | Annotate | Download | only in engine
      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 "base/time/time.h"
      9 #include "google_apis/gcm/base/gcm_export.h"
     10 #include "google_apis/gcm/engine/connection_handler.h"
     11 
     12 namespace mcs_proto {
     13 class LoginRequest;
     14 }
     15 
     16 namespace gcm {
     17 
     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;
     25 
     26   ConnectionFactory();
     27   virtual ~ConnectionFactory();
     28 
     29   // Initialize the factory, creating a connection handler with a disconnected
     30   // socket. Should only be called once.
     31   // Upon connection:
     32   // |read_callback| will be invoked with the contents of any received protobuf
     33   // message.
     34   // |write_callback| will be invoked anytime a message has been successfully
     35   // sent. Note: this just means the data was sent to the wire, not that the
     36   // other end received it.
     37   virtual void Initialize(
     38       const BuildLoginRequestCallback& request_builder,
     39       const ConnectionHandler::ProtoReceivedCallback& read_callback,
     40       const ConnectionHandler::ProtoSentCallback& write_callback) = 0;
     41 
     42   // Get the connection handler for this factory. Initialize(..) must have
     43   // been called.
     44   virtual ConnectionHandler* GetConnectionHandler() const = 0;
     45 
     46   // Opens a new connection and initiates login handshake. Upon completion of
     47   // the handshake, |read_callback| will be invoked with a valid
     48   // mcs_proto::LoginResponse.
     49   // Note: Initialize must have already been invoked.
     50   virtual void Connect() = 0;
     51 
     52   // Whether or not the MCS endpoint is currently reachable with an active
     53   // connection.
     54   virtual bool IsEndpointReachable() const = 0;
     55 
     56   // If in backoff, the time at which the next retry will be made. Otherwise,
     57   // a null time, indicating either no attempt to connect has been made or no
     58   // backoff is in progress.
     59   virtual base::TimeTicks NextRetryAttempt() const = 0;
     60 };
     61 
     62 }  // namespace gcm
     63 
     64 #endif  // GOOGLE_APIS_GCM_ENGINE_CONNECTION_FACTORY_H_
     65