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 20 namespace base { 21 class FilePath; 22 class SequencedTaskRunner; 23 } 24 25 namespace extensions { 26 class ExtensionGCMAppHandlerTest; 27 } 28 29 namespace net { 30 class URLRequestContextGetter; 31 } 32 33 namespace gcm { 34 35 class GCMAppHandler; 36 class GCMClientFactory; 37 38 // GCMDriver implementation for desktop and Chrome OS, using GCMClient. 39 class 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 59 // GCMDriver implementation: 60 virtual void Enable() OVERRIDE; 61 virtual void Disable() OVERRIDE; 62 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE; 63 virtual bool IsStarted() const OVERRIDE; 64 virtual bool IsConnected() const OVERRIDE; 65 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback, 66 bool clear_logs) OVERRIDE; 67 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback, 68 bool recording) OVERRIDE; 69 70 protected: 71 // GCMDriver implementation: 72 virtual GCMClient::Result EnsureStarted() OVERRIDE; 73 virtual void RegisterImpl( 74 const std::string& app_id, 75 const std::vector<std::string>& sender_ids) OVERRIDE; 76 virtual void UnregisterImpl(const std::string& app_id) OVERRIDE; 77 virtual void SendImpl(const std::string& app_id, 78 const std::string& receiver_id, 79 const GCMClient::OutgoingMessage& message) OVERRIDE; 80 81 private: 82 class DelayedTaskController; 83 class IOWorker; 84 85 // Stops the GCM service. It can be restarted by calling EnsureStarted again. 86 void Stop(); 87 88 // Remove cached data when GCM service is stopped. 89 void RemoveCachedData(); 90 91 void DoRegister(const std::string& app_id, 92 const std::vector<std::string>& sender_ids); 93 void DoUnregister(const std::string& app_id); 94 void DoSend(const std::string& app_id, 95 const std::string& receiver_id, 96 const GCMClient::OutgoingMessage& message); 97 98 // Callbacks posted from IO thread to UI thread. 99 void MessageReceived(const std::string& app_id, 100 const GCMClient::IncomingMessage& message); 101 void MessagesDeleted(const std::string& app_id); 102 void MessageSendError(const std::string& app_id, 103 const GCMClient::SendErrorDetails& send_error_details); 104 void GCMClientReady(); 105 void OnConnected(const net::IPEndPoint& ip_endpoint); 106 void OnDisconnected(); 107 108 void GetGCMStatisticsFinished(const GCMClient::GCMStatistics& stats); 109 110 // Flag to indicate whether the user is signed in to a GAIA account. 111 // TODO(jianli): To be removed when sign-in enforcement is dropped. 112 bool signed_in_; 113 114 // Flag to indicate if GCM is started. 115 bool gcm_started_; 116 117 // Flag to indicate if GCM is enabled. 118 bool gcm_enabled_; 119 120 // Flag to indicate the last known state of the GCM client. Because this 121 // flag lives on the UI thread, while the GCM client lives on the IO thread, 122 // it may be out of date while connection changes are happening. 123 bool connected_; 124 125 scoped_refptr<base::SequencedTaskRunner> ui_thread_; 126 scoped_refptr<base::SequencedTaskRunner> io_thread_; 127 128 scoped_ptr<DelayedTaskController> delayed_task_controller_; 129 130 // For all the work occurring on the IO thread. Must be destroyed on the IO 131 // thread. 132 scoped_ptr<IOWorker> io_worker_; 133 134 // Callback for GetGCMStatistics. 135 GetGCMStatisticsCallback request_gcm_statistics_callback_; 136 137 // Used to pass a weak pointer to the IO worker. 138 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_; 139 140 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop); 141 }; 142 143 } // namespace gcm 144 145 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ 146