#ifndef SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_
#define SYNC_NOTIFIER_GCM_NETWORK_CHANNEL_H_
#include <string>
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/threading/non_thread_safe.h"
#include "net/base/backoff_entry.h"
#include "net/base/network_change_notifier.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "sync/base/sync_export.h"
#include "sync/notifier/gcm_network_channel_delegate.h"
#include "sync/notifier/sync_system_resources.h"
#include "url/gurl.h"
class GoogleServiceAuthError;
namespace syncer {
class GCMNetworkChannel;
struct GCMNetworkChannelDiagnostic {
  explicit GCMNetworkChannelDiagnostic(GCMNetworkChannel* parent);
  
  scoped_ptr<base::DictionaryValue> CollectDebugData() const;
  
  std::string GCMClientResultToString(
      const gcm::GCMClient::Result result) const;
  GCMNetworkChannel* parent_;
  bool last_message_empty_echo_token_;
  base::Time last_message_received_time_;
  int last_post_response_code_;
  std::string registration_id_;
  gcm::GCMClient::Result registration_result_;
  int sent_messages_count_;
};
class SYNC_EXPORT_PRIVATE GCMNetworkChannel
    : public SyncNetworkChannel,
      public net::URLFetcherDelegate,
      public net::NetworkChangeNotifier::NetworkChangeObserver,
      public base::NonThreadSafe {
 public:
  GCMNetworkChannel(
      scoped_refptr<net::URLRequestContextGetter> request_context_getter,
      scoped_ptr<GCMNetworkChannelDelegate> delegate);
  virtual ~GCMNetworkChannel();
  
  virtual void SendMessage(const std::string& message) OVERRIDE;
  virtual void SetMessageReceiver(
      invalidation::MessageCallback* incoming_receiver) OVERRIDE;
  
  virtual void UpdateCredentials(const std::string& email,
                                 const std::string& token) OVERRIDE;
  virtual void RequestDetailedStatus(
      base::Callback<void(const base::DictionaryValue&)> callback) OVERRIDE;
  
  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
  
  virtual void OnNetworkChanged(
      net::NetworkChangeNotifier::ConnectionType connection_type) OVERRIDE;
 protected:
  void ResetRegisterBackoffEntryForTest(
      const net::BackoffEntry::Policy* policy);
  virtual GURL BuildUrl(const std::string& registration_id);
 private:
  friend class GCMNetworkChannelTest;
  void Register();
  void OnRegisterComplete(const std::string& registration_id,
                          gcm::GCMClient::Result result);
  void RequestAccessToken();
  void OnGetTokenComplete(const GoogleServiceAuthError& error,
                          const std::string& token);
  void OnIncomingMessage(const std::string& message,
                         const std::string& echo_token);
  
  
  static void Base64EncodeURLSafe(const std::string& input,
                                  std::string* output);
  static bool Base64DecodeURLSafe(const std::string& input,
                                  std::string* output);
  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
  scoped_ptr<GCMNetworkChannelDelegate> delegate_;
  
  
  std::string cached_message_;
  
  
  std::string access_token_;
  
  
  std::string registration_id_;
  scoped_ptr<net::BackoffEntry> register_backoff_entry_;
  scoped_ptr<net::URLFetcher> fetcher_;
  
  
  std::string echo_token_;
  GCMNetworkChannelDiagnostic diagnostic_info_;
  base::WeakPtrFactory<GCMNetworkChannel> weak_factory_;
  DISALLOW_COPY_AND_ASSIGN(GCMNetworkChannel);
};
}  
#endif