gcm_driver_desktop.h revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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_driver.h" 18#include "google_apis/gaia/identity_provider.h" 19#include "google_apis/gcm/gcm_client.h" 20 21namespace base { 22class FilePath; 23class SequencedTaskRunner; 24} 25 26namespace extensions { 27class ExtensionGCMAppHandlerTest; 28} 29 30namespace net { 31class URLRequestContextGetter; 32} 33 34namespace gcm { 35 36class GCMAppHandler; 37class GCMClientFactory; 38 39// GCMDriver implementation for desktop and Chrome OS, using GCMClient. 40class GCMDriverDesktop : public GCMDriver, public IdentityProvider::Observer { 41 public: 42 GCMDriverDesktop( 43 scoped_ptr<GCMClientFactory> gcm_client_factory, 44 scoped_ptr<IdentityProvider> identity_provider, 45 const GCMClient::ChromeBuildInfo& chrome_build_info, 46 const base::FilePath& store_path, 47 const scoped_refptr<net::URLRequestContextGetter>& request_context, 48 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, 49 const scoped_refptr<base::SequencedTaskRunner>& io_thread, 50 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); 51 virtual ~GCMDriverDesktop(); 52 53 // IdentityProvider::Observer implementation: 54 virtual void OnActiveAccountLogin() OVERRIDE; 55 virtual void OnActiveAccountLogout() OVERRIDE; 56 57 // GCMDriver overrides: 58 virtual void Shutdown() OVERRIDE; 59 virtual void AddAppHandler(const std::string& app_id, 60 GCMAppHandler* handler) OVERRIDE; 61 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE; 62 63 // GCMDriver implementation: 64 virtual void Enable() OVERRIDE; 65 virtual void Disable() OVERRIDE; 66 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE; 67 virtual bool IsStarted() const OVERRIDE; 68 virtual bool IsGCMClientReady() const OVERRIDE; 69 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback, 70 bool clear_logs) OVERRIDE; 71 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback, 72 bool recording) OVERRIDE; 73 virtual std::string SignedInUserName() const OVERRIDE; 74 75 protected: 76 // GCMDriver implementation: 77 virtual GCMClient::Result EnsureStarted() OVERRIDE; 78 virtual void RegisterImpl( 79 const std::string& app_id, 80 const std::vector<std::string>& sender_ids) OVERRIDE; 81 virtual void UnregisterImpl(const std::string& app_id) OVERRIDE; 82 virtual void SendImpl(const std::string& app_id, 83 const std::string& receiver_id, 84 const GCMClient::OutgoingMessage& message) OVERRIDE; 85 86 private: 87 class DelayedTaskController; 88 class IOWorker; 89 90 // Stops the GCM service. It can be restarted by calling EnsureStarted again. 91 void Stop(); 92 93 // Remove cached data when GCM service is stopped. 94 void RemoveCachedData(); 95 96 // Checks out of GCM and erases any cached and persisted data. 97 void CheckOut(); 98 99 void DoRegister(const std::string& app_id, 100 const std::vector<std::string>& sender_ids); 101 void DoUnregister(const std::string& app_id); 102 void DoSend(const std::string& app_id, 103 const std::string& receiver_id, 104 const GCMClient::OutgoingMessage& message); 105 106 // Callbacks posted from IO thread to UI thread. 107 void MessageReceived(const std::string& app_id, 108 const GCMClient::IncomingMessage& message); 109 void MessagesDeleted(const std::string& app_id); 110 void MessageSendError(const std::string& app_id, 111 const GCMClient::SendErrorDetails& send_error_details); 112 void GCMClientReady(); 113 114 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats); 115 116 // Flag to indicate if GCM is enabled. 117 bool gcm_enabled_; 118 119 // Flag to indicate if GCMClient is ready. 120 bool gcm_client_ready_; 121 122 // The account ID that this service is responsible for. Empty when the service 123 // is not running. 124 std::string account_id_; 125 126 scoped_ptr<IdentityProvider> identity_provider_; 127 scoped_refptr<base::SequencedTaskRunner> ui_thread_; 128 scoped_refptr<base::SequencedTaskRunner> io_thread_; 129 130 scoped_ptr<DelayedTaskController> delayed_task_controller_; 131 132 // For all the work occurring on the IO thread. Must be destroyed on the IO 133 // thread. 134 scoped_ptr<IOWorker> io_worker_; 135 136 // Callback for GetGCMStatistics. 137 GetGCMStatisticsCallback request_gcm_statistics_callback_; 138 139 // Used to pass a weak pointer to the IO worker. 140 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_; 141 142 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop); 143}; 144 145} // namespace gcm 146 147#endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ 148