| 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 overrides: |
| 64 void Enable(); | 54 virtual void Shutdown() OVERRIDE; |
| 65 void Disable(); | 55 virtual void AddAppHandler(const std::string& app_id, |
| 56 GCMAppHandler* handler) OVERRIDE; |
| 57 virtual void RemoveAppHandler(const std::string& app_id) OVERRIDE; |
| 66 | 58 |
| 67 // This method must be called before destroying the GCMDriver. Once it has | 59 // GCMDriver implementation: |
| 68 // been called, no other GCMDriver methods may be used. | 60 virtual void Enable() OVERRIDE; |
| 69 virtual void Shutdown(); | 61 virtual void Disable() 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, | 62 virtual void Register(const std::string& app_id, |
| 85 const std::vector<std::string>& sender_ids, | 63 const std::vector<std::string>& sender_ids, |
| 86 const RegisterCallback& callback); | 64 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, | 65 virtual void Unregister(const std::string& app_id, |
| 92 const UnregisterCallback& callback); | 66 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, | 67 virtual void Send(const std::string& app_id, |
| 100 const std::string& receiver_id, | 68 const std::string& receiver_id, |
| 101 const GCMClient::OutgoingMessage& message, | 69 const GCMClient::OutgoingMessage& message, |
| 102 const SendCallback& callback); | 70 const SendCallback& callback) OVERRIDE; |
| 71 virtual GCMClient* GetGCMClientForTesting() const OVERRIDE; |
| 72 virtual bool IsStarted() const OVERRIDE; |
| 73 virtual bool IsGCMClientReady() const OVERRIDE; |
| 74 virtual void GetGCMStatistics(const GetGCMStatisticsCallback& callback, |
| 75 bool clear_logs) OVERRIDE; |
| 76 virtual void SetGCMRecording(const GetGCMStatisticsCallback& callback, |
| 77 bool recording) OVERRIDE; |
| 78 virtual std::string SignedInUserName() const OVERRIDE; |
| 103 | 79 |
| 104 // For testing purpose. | 80 // 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; | 81 virtual void OnActiveAccountLogin() OVERRIDE; |
| 128 virtual void OnActiveAccountLogout() OVERRIDE; | 82 virtual void OnActiveAccountLogout() OVERRIDE; |
| 129 | 83 |
| 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: | 84 private: |
| 137 class DelayedTaskController; | 85 class DelayedTaskController; |
| 138 class IOWorker; | 86 class IOWorker; |
| 139 | 87 |
| 140 // Ensures that the GCM service starts when all of the following conditions | 88 // Ensures that the GCM service starts when all of the following conditions |
| 141 // satisfy: | 89 // satisfy: |
| 142 // 1) GCM is enabled. | 90 // 1) GCM is enabled. |
| 143 // 2) The identity provider is able to supply an account ID. | 91 // 2) The identity provider is able to supply an account ID. |
| 144 GCMClient::Result EnsureStarted(); | 92 GCMClient::Result EnsureStarted(); |
| 145 | 93 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 171 void SendFinished(const std::string& app_id, | 119 void SendFinished(const std::string& app_id, |
| 172 const std::string& message_id, | 120 const std::string& message_id, |
| 173 GCMClient::Result result); | 121 GCMClient::Result result); |
| 174 void MessageReceived(const std::string& app_id, | 122 void MessageReceived(const std::string& app_id, |
| 175 GCMClient::IncomingMessage message); | 123 GCMClient::IncomingMessage message); |
| 176 void MessagesDeleted(const std::string& app_id); | 124 void MessagesDeleted(const std::string& app_id); |
| 177 void MessageSendError(const std::string& app_id, | 125 void MessageSendError(const std::string& app_id, |
| 178 const GCMClient::SendErrorDetails& send_error_details); | 126 const GCMClient::SendErrorDetails& send_error_details); |
| 179 void GCMClientReady(); | 127 void GCMClientReady(); |
| 180 | 128 |
| 181 // Returns the handler for the given app. | |
| 182 GCMAppHandler* GetAppHandler(const std::string& app_id); | |
| 183 | |
| 184 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); | 129 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); |
| 185 | 130 |
| 186 // Flag to indicate if GCM is enabled. | 131 // Flag to indicate if GCM is enabled. |
| 187 bool gcm_enabled_; | 132 bool gcm_enabled_; |
| 188 | 133 |
| 189 // Flag to indicate if GCMClient is ready. | 134 // Flag to indicate if GCMClient is ready. |
| 190 bool gcm_client_ready_; | 135 bool gcm_client_ready_; |
| 191 | 136 |
| 192 // The account ID that this service is responsible for. Empty when the service | 137 // The account ID that this service is responsible for. Empty when the service |
| 193 // is not running. | 138 // is not running. |
| 194 std::string account_id_; | 139 std::string account_id_; |
| 195 | 140 |
| 196 scoped_ptr<IdentityProvider> identity_provider_; | 141 scoped_ptr<IdentityProvider> identity_provider_; |
| 197 scoped_refptr<base::SequencedTaskRunner> ui_thread_; | 142 scoped_refptr<base::SequencedTaskRunner> ui_thread_; |
| 198 scoped_refptr<base::SequencedTaskRunner> io_thread_; | 143 scoped_refptr<base::SequencedTaskRunner> io_thread_; |
| 199 | 144 |
| 200 scoped_ptr<DelayedTaskController> delayed_task_controller_; | 145 scoped_ptr<DelayedTaskController> delayed_task_controller_; |
| 201 | 146 |
| 202 // For all the work occurring on the IO thread. Must be destroyed on the IO | 147 // For all the work occurring on the IO thread. Must be destroyed on the IO |
| 203 // thread. | 148 // thread. |
| 204 scoped_ptr<IOWorker> io_worker_; | 149 scoped_ptr<IOWorker> io_worker_; |
| 205 | 150 |
| 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. | 151 // Callback map (from app_id to callback) for Register. |
| 214 std::map<std::string, RegisterCallback> register_callbacks_; | 152 std::map<std::string, RegisterCallback> register_callbacks_; |
| 215 | 153 |
| 216 // Callback map (from app_id to callback) for Unregister. | 154 // Callback map (from app_id to callback) for Unregister. |
| 217 std::map<std::string, UnregisterCallback> unregister_callbacks_; | 155 std::map<std::string, UnregisterCallback> unregister_callbacks_; |
| 218 | 156 |
| 219 // Callback map (from <app_id, message_id> to callback) for Send. | 157 // Callback map (from <app_id, message_id> to callback) for Send. |
| 220 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; | 158 std::map<std::pair<std::string, std::string>, SendCallback> send_callbacks_; |
| 221 | 159 |
| 222 // Callback for GetGCMStatistics. | 160 // Callback for GetGCMStatistics. |
| 223 GetGCMStatisticsCallback request_gcm_statistics_callback_; | 161 GetGCMStatisticsCallback request_gcm_statistics_callback_; |
| 224 | 162 |
| 225 // Used to pass a weak pointer to the IO worker. | 163 // Used to pass a weak pointer to the IO worker. |
| 226 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_; | 164 base::WeakPtrFactory<GCMDriverDesktop> weak_ptr_factory_; |
| 227 | 165 |
| 228 DISALLOW_COPY_AND_ASSIGN(GCMDriver); | 166 DISALLOW_COPY_AND_ASSIGN(GCMDriverDesktop); |
| 229 }; | 167 }; |
| 230 | 168 |
| 231 } // namespace gcm | 169 } // namespace gcm |
| 232 | 170 |
| 233 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_H_ | 171 #endif // COMPONENTS_GCM_DRIVER_GCM_DRIVER_DESKTOP_H_ |
| OLD | NEW |