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