| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_ | 5 #ifndef COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ |
| 6 #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_ | 6 #define COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/basictypes.h" | |
| 13 #include "base/callback.h" | |
| 14 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 16 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 18 #include "components/gcm_driver/default_gcm_app_handler.h" | 17 #include "components/gcm_driver/gcm_driver.h" |
| 19 #include "google_apis/gaia/identity_provider.h" | 18 #include "google_apis/gaia/identity_provider.h" |
| 20 #include "google_apis/gcm/gcm_client.h" | 19 #include "google_apis/gcm/gcm_client.h" |
| 21 | 20 |
| 22 namespace base { | 21 namespace base { |
| 23 class FilePath; | 22 class FilePath; |
| 24 class SequencedTaskRunner; | 23 class SequencedTaskRunner; |
| 25 } | 24 } |
| 26 | 25 |
| 27 namespace extensions { | 26 namespace extensions { |
| 28 class ExtensionGCMAppHandlerTest; | 27 class ExtensionGCMAppHandlerTest; |
| 29 } | 28 } |
| 30 | 29 |
| 31 namespace net { | 30 namespace net { |
| 32 class URLRequestContextGetter; | 31 class URLRequestContextGetter; |
| 33 } | 32 } |
| 34 | 33 |
| 35 namespace gcm { | 34 namespace gcm { |
| 36 | 35 |
| 37 class GCMAppHandler; | 36 class GCMAppHandler; |
| 38 class GCMClientFactory; | 37 class GCMClientFactory; |
| 39 | 38 |
| 40 // A bridge between the GCM users in Chrome and the GCMClient layer. | 39 // GCMDriver implementation for desktop and Chrome OS, using GCMClient. |
| 41 class GCMDriver : public IdentityProvider::Observer { | 40 class GCMDriverDesktop : public GCMDriver, public IdentityProvider::Observer { |
| 42 public: | 41 public: |
| 43 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; | 42 GCMDriverDesktop( |
| 44 typedef base::Callback<void(const std::string& registration_id, | |
| 45 GCMClient::Result result)> RegisterCallback; | |
| 46 typedef base::Callback<void(const std::string& message_id, | |
| 47 GCMClient::Result result)> SendCallback; | |
| 48 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback; | |
| 49 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)> | |
| 50 GetGCMStatisticsCallback; | |
| 51 | |
| 52 GCMDriver( | |
| 53 scoped_ptr<GCMClientFactory> gcm_client_factory, | 43 scoped_ptr<GCMClientFactory> gcm_client_factory, |
| 54 scoped_ptr<IdentityProvider> identity_provider, | 44 scoped_ptr<IdentityProvider> identity_provider, |
| 55 const GCMClient::ChromeBuildInfo& chrome_build_info, | 45 const GCMClient::ChromeBuildInfo& chrome_build_info, |
| 56 const base::FilePath& store_path, | 46 const base::FilePath& store_path, |
| 57 const scoped_refptr<net::URLRequestContextGetter>& request_context, | 47 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 58 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, | 48 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 59 const scoped_refptr<base::SequencedTaskRunner>& io_thread, | 49 const scoped_refptr<base::SequencedTaskRunner>& io_thread, |
| 60 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); | 50 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner); |
| 61 virtual ~GCMDriver(); | 51 virtual ~GCMDriverDesktop(); |
| 62 | 52 |
| 63 // Enables/disables GCM service. | 53 // GCMDriver implementation: |
| 64 void Enable(); | 54 virtual void Enable() OVERRIDE; |
| 65 void Disable(); | 55 virtual void Disable() OVERRIDE; |
| 66 | 56 virtual void Shutdown() OVERRIDE; |
| 67 // This method must be called before destroying the GCMDriver. Once it has | 57 virtual void AddAppHandler(const std::string& app_id, |
| 68 // been called, no other GCMDriver methods may be used. | 58 GCMAppHandler* handler) OVERRIDE; |
| 69 virtual void Shutdown(); | 59 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE; |
| 70 | |
| 71 // Adds a handler for a given app. | |
| 72 virtual void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); | |
| 73 | |
| 74 // Remove the handler for a given app. | |
| 75 virtual void RemoveAppHandler(const std::string& app_id); | |
| 76 | |
| 77 // Registers |sender_id| for an app. A registration ID will be returned by | |
| 78 // the GCM server. | |
| 79 // |app_id|: application ID. | |
| 80 // |sender_ids|: list of IDs of the servers that are allowed to send the | |
| 81 // messages to the application. These IDs are assigned by the | |
| 82 // Google API Console. | |
| 83 // |callback|: to be called once the asynchronous operation is done. | |
| 84 virtual void Register(const std::string& app_id, | 60 virtual void Register(const std::string& app_id, |
| 85 const std::vector<std::string>& sender_ids, | 61 const std::vector<std::string>& sender_ids, |
| 86 const RegisterCallback& callback); | 62 const RegisterCallback& callback) OVERRIDE; |
| 87 | |
| 88 // Unregisters an app from using GCM. | |
| 89 // |app_id|: application ID. | |
| 90 // |callback|: to be called once the asynchronous operation is done. | |
| 91 virtual void Unregister(const std::string& app_id, | 63 virtual void Unregister(const std::string& app_id, |
| 92 const UnregisterCallback& callback); | 64 const UnregisterCallback& callback) OVERRIDE; |
| 93 | |
| 94 // Sends a message to a given receiver. | |
| 95 // |app_id|: application ID. | |
| 96 // |receiver_id|: registration ID of the receiver party. | |
| 97 // |message|: message to be sent. | |
| 98 // |callback|: to be called once the asynchronous operation is done. | |
| 99 virtual void Send(const std::string& app_id, | 65 virtual void Send(const std::string& app_id, |
| 100 const std::string& receiver_id, | 66 const std::string& receiver_id, |
| 101 const GCMClient::OutgoingMessage& message, | 67 const GCMClient::OutgoingMessage& message, |
| 102 const SendCallback& callback); | 68 const SendCallback& callback) OVERRIDE; |
| 69 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE; |
| 70 virtual bool IsStarted() const OVERRIDE; |
| 71 virtual bool IsGCMClientReady() const OVERRIDE; |
| 72 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback, |
| 73 bool clear_logs) OVERRIDE; |
| 74 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback, |
| 75 bool recording) OVERRIDE; |
| 76 virtual std::string SignedInUserName() const OVERRIDE; |
| 103 | 77 |
| 104 // For testing purpose. | 78 // IdentityProvider::Observer implementation: |
| 105 GCMClient* GetGCMClientForTesting() const; | |
| 106 | |
| 107 // Returns true if the service was started. | |
| 108 bool IsStarted() const; | |
| 109 | |
| 110 // Returns true if the gcm client is ready. | |
| 111 bool IsGCMClientReady() const; | |
| 112 | |
| 113 // Get GCM client internal states and statistics. | |
| 114 // If clear_logs is true then activity logs will be cleared before the stats | |
| 115 // are returned. | |
| 116 void GetGCMStatistics(const GetGCMStatisticsCallback& callback, | |
| 117 bool clear_logs); | |
| 118 | |
| 119 // Enables/disables GCM activity recording, and then returns the stats. | |
| 120 void SetGCMRecording(const GetGCMStatisticsCallback& callback, | |
| 121 bool recording); | |
| 122 | |
| 123 // Returns the user name if the profile is signed in. Empty string otherwise. | |
| 124 std::string SignedInUserName() const; | |
| 125 | |
| 126 // IdentityProvider::Observer: | |
| 127 virtual void OnActiveAccountLogin() OVERRIDE; | 79 virtual void OnActiveAccountLogin() OVERRIDE; |
| 128 virtual void OnActiveAccountLogout() OVERRIDE; | 80 virtual void OnActiveAccountLogout() OVERRIDE; |
| 129 | 81 |
| 130 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; } | |
| 131 | |
| 132 protected: | |
| 133 // Used for constructing fake GCMDriver for testing purpose. | |
| 134 GCMDriver(); | |
| 135 | |
| 136 private: | 82 private: |
| 137 class DelayedTaskController; | 83 class DelayedTaskController; |
| 138 class IOWorker; | 84 class IOWorker; |
| 139 | 85 |
| 140 // Ensures that the GCM service starts when all of the following conditions | 86 // Ensures that the GCM service starts when all of the following conditions |
| 141 // satisfy: | 87 // satisfy: |
| 142 // 1) GCM is enabled. | 88 // 1) GCM is enabled. |
| 143 // 2) The identity provider is able to supply an account ID. | 89 // 2) The identity provider is able to supply an account ID. |
| 144 GCMClient::Result EnsureStarted(); | 90 GCMClient::Result EnsureStarted(); |
| 145 | 91 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 171 void SendFinished(const std::string& app_id, | 117 void SendFinished(const std::string& app_id, |
| 172 const std::string& message_id, | 118 const std::string& message_id, |
| 173 GCMClient::Result result); | 119 GCMClient::Result result); |
| 174 void MessageReceived(const std::string& app_id, | 120 void MessageReceived(const std::string& app_id, |
| 175 GCMClient::IncomingMessage message); | 121 GCMClient::IncomingMessage message); |
| 176 void MessagesDeleted(const std::string& app_id); | 122 void MessagesDeleted(const std::string& app_id); |
| 177 void MessageSendError(const std::string& app_id, | 123 void MessageSendError(const std::string& app_id, |
| 178 const GCMClient::SendErrorDetails& send_error_details); | 124 const GCMClient::SendErrorDetails& send_error_details); |
| 179 void GCMClientReady(); | 125 void GCMClientReady(); |
| 180 | 126 |
| 181 // Returns the handler for the given app. | |
| 182 GCMAppHandler* GetAppHandler(const std::string& app_id); | |
| 183 | |
| 184 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); | 127 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); |
| 185 | 128 |
| 186 // Flag to indicate if GCM is enabled. | 129 // Flag to indicate if GCM is enabled. |
| 187 bool gcm_enabled_; | 130 bool gcm_enabled_; |
| 188 | 131 |
| 189 // Flag to indicate if GCMClient is ready. | 132 // Flag to indicate if GCMClient is ready. |
| 190 bool gcm_client_ready_; | 133 bool gcm_client_ready_; |
| 191 | 134 |
| 192 // The account ID that this service is responsible for. Empty when the service | 135 // The account ID that this service is responsible for. Empty when the service |
| 193 // is not running. | 136 // is not running. |
| 194 std::string account_id_; | 137 std::string account_id_; |
| 195 | 138 |
| 196 scoped_ptr<IdentityProvider> identity_provider_; | 139 scoped_ptr<IdentityProvider> identity_provider_; |
| 197 scoped_refptr<base::SequencedTaskRunner> ui_thread_; | 140 scoped_refptr<base::SequencedTaskRunner> ui_thread_; |
| 198 scoped_refptr<base::SequencedTaskRunner> io_thread_; | 141 scoped_refptr<base::SequencedTaskRunner> io_thread_; |
| 199 | 142 |
| 200 scoped_ptr<DelayedTaskController> delayed_task_controller_; | 143 scoped_ptr<DelayedTaskController> delayed_task_controller_; |
| 201 | 144 |
| 202 // For all the work occurring on the IO thread. Must be destroyed on the IO | 145 // For all the work occurring on the IO thread. Must be destroyed on the IO |
| 203 // thread. | 146 // thread. |
| 204 scoped_ptr<IOWorker> io_worker_; | 147 scoped_ptr<IOWorker> io_worker_; |
| 205 | 148 |
| 206 // App handler map (from app_id to handler pointer). | |
| 207 // The handler is not owned. | |
| 208 GCMAppHandlerMap app_handlers_; | |
| 209 | |
| 210 // The default handler when no app handler can be found in the map. | |
| 211 DefaultGCMAppHandler default_app_handler_; | |
| 212 | |
| 213 // Callback map (from app_id to callback) for Register. | 149 // Callback map (from app_id to callback) for Register. |
| 214 std::map<std::string, RegisterCallback> register_callbacks_; | 150 std::map<std::string, RegisterCallback> register_callbacks_; |
| 215 | 151 |
| 216 // Callback map (from app_id to callback) for Unregister. | 152 // Callback map (from app_id to callback) for Unregister. |
| 217 std::map<std::string, UnregisterCallback> unregister_callbacks_; | 153 std::map<std::string, UnregisterCallback> unregister_callbacks_; |
| 218 | 154 |
| 219 // Callback map (from <app_id, message_id> to callback) for Send. | 155 // Callback map (from <app_id, message_id> to callback) for Send. |
| 220 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; | 156 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; |
| 221 | 157 |
| 222 // Callback for GetGCMStatistics. | 158 // Callback for GetGCMStatistics. |
| 223 GetGCMStatisticsCallback request_gcm_statistics_callback_; | 159 GetGCMStatisticsCallback request_gcm_statistics_callback_; |
| 224 | 160 |
| 225 // Used to pass a weak pointer to the IO worker. | 161 // Used to pass a weak pointer to the IO worker. |
| 226 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_; | 162 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_; |
| 227 | 163 |
| 228 DISALLOW_COPY_AND_ASSIGN(GCMDriver); | 164 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop); |
| 229 }; | 165 }; |
| 230 | 166 |
| 231 } // namespace gcm | 167 } // namespace gcm |
| 232 | 168 |
| 233 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_ | 169 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ |
| OLD | NEW |