gcm_driver_desktop.h revision 03b57e008b61dfcb1fbad3aea950ae0e001748b0
1// Copyright 2014 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 COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ 6#define COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ 7 8#include <map> 9#include <string> 10#include <vector> 11 12#include "base/compiler_specific.h" 13#include "base/macros.h" 14#include "base/memory/ref_counted.h" 15#include "base/memory/scoped_ptr.h" 16#include "base/memory/weak_ptr.h" 17#include "components/gcm_driver/gcm_client.h" 18#include "components/gcm_driver/gcm_driver.h" 19 20namespace base { 21class FilePath; 22class SequencedTaskRunner; 23} 24 25namespace extensions { 26class ExtensionGCMAppHandlerTest; 27} 28 29namespace net { 30class URLRequestContextGetter; 31} 32 33namespace gcm { 34 35class GCMAppHandler; 36class GCMClientFactory; 37 38// GCMDriver implementation for desktop and Chrome OS, using GCMClient. 39class GCMDriverDesktop : public GCMDriver { 40 public: 41 GCMDriverDesktop( 42 scoped_ptr<GCMClientFactory> gcm_client_factory, 43 const GCMClient::ChromeBuildInfo& chrome_build_info, 44 const base::FilePath& store_path, 45 const scoped_refptr<net::URLRequestContextGetter>& request_context, 46 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, 47 const scoped_refptr<base::SequencedTaskRunner>& io_thread, 48 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); 49 virtual ~GCMDriverDesktop(); 50 51 // GCMDriver overrides: 52 virtual void Shutdown() OVERRIDE; 53 virtual void OnSignedIn() OVERRIDE; 54 virtual void Purge() OVERRIDE; 55 virtual void AddAppHandler(const std::string& app_id, 56 GCMAppHandler* handler) OVERRIDE; 57 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE; 58 virtual void Enable() OVERRIDE; 59 virtual void Disable() OVERRIDE; 60 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE; 61 virtual bool IsStarted() const OVERRIDE; 62 virtual bool IsConnected() const OVERRIDE; 63 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback, 64 bool clear_logs) OVERRIDE; 65 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback, 66 bool recording) OVERRIDE; 67 virtual void UpdateAccountMapping( 68 const AccountMapping& account_mapping) OVERRIDE; 69 virtual void RemoveAccountMapping(const std::string& account_id) OVERRIDE; 70 71 // GCMDriverDesktop specific implementation. 72 // Sets a list of accounts with OAuth2 tokens for the next checkin. 73 // |account_tokens| maps email addresses to OAuth2 access tokens. 74 // |account_removed| indicates that an account has been removed since the 75 // last time the callback was called, which triggers an immediate checkin, 76 // to ensure that association between device and account is removed. 77 void SetAccountsForCheckin( 78 const std::map<std::string, std::string>& account_tokens); 79 80 protected: 81 // GCMDriver implementation: 82 virtual GCMClient::Result EnsureStarted() OVERRIDE; 83 virtual void RegisterImpl( 84 const std::string& app_id, 85 const std::vector<std::string>& sender_ids) OVERRIDE; 86 virtual void UnregisterImpl(const std::string& app_id) OVERRIDE; 87 virtual void SendImpl(const std::string& app_id, 88 const std::string& receiver_id, 89 const GCMClient::OutgoingMessage& message) OVERRIDE; 90 91 private: 92 class DelayedTaskController; 93 class IOWorker; 94 95 // Stops the GCM service. It can be restarted by calling EnsureStarted again. 96 void Stop(); 97 98 // Remove cached data when GCM service is stopped. 99 void RemoveCachedData(); 100 101 void DoRegister(const std::string& app_id, 102 const std::vector<std::string>& sender_ids); 103 void DoUnregister(const std::string& app_id); 104 void DoSend(const std::string& app_id, 105 const std::string& receiver_id, 106 const GCMClient::OutgoingMessage& message); 107 108 // Callbacks posted from IO thread to UI thread. 109 void MessageReceived(const std::string& app_id, 110 const GCMClient::IncomingMessage& message); 111 void MessagesDeleted(const std::string& app_id); 112 void MessageSendError(const std::string& app_id, 113 const GCMClient::SendErrorDetails& send_error_details); 114 void SendAcknowledged(const std::string& app_id, 115 const std::string& message_id); 116 void GCMClientReady(); 117 void OnConnected(const net::IPEndPoint& ip_endpoint); 118 void OnDisconnected(); 119 120 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats); 121 122 // Flag to indicate whether the user is signed in to a GAIA account. 123 // TODO(jianli): To be removed when sign-in enforcement is dropped. 124 bool signed_in_; 125 126 // Flag to indicate if GCM is started. 127 bool gcm_started_; 128 129 // Flag to indicate if GCM is enabled. 130 bool gcm_enabled_; 131 132 // Flag to indicate the last known state of the GCM client. Because this 133 // flag lives on the UI thread, while the GCM client lives on the IO thread, 134 // it may be out of date while connection changes are happening. 135 bool connected_; 136 137 scoped_refptr<base::SequencedTaskRunner> ui_thread_; 138 scoped_refptr<base::SequencedTaskRunner> io_thread_; 139 140 scoped_ptr<DelayedTaskController> delayed_task_controller_; 141 142 // For all the work occurring on the IO thread. Must be destroyed on the IO 143 // thread. 144 scoped_ptr<IOWorker> io_worker_; 145 146 // Callback for GetGCMStatistics. 147 GetGCMStatisticsCallback request_gcm_statistics_callback_; 148 149 // Used to pass a weak pointer to the IO worker. 150 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_; 151 152 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop); 153}; 154 155} // namespace gcm 156 157#endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ 158