| 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 CHROME_BROWSER_SERVICES_GCM_GCM_DRIVER_H_ | 5 #ifndef CHROME_BROWSER_SERVICES_GCM_GCM_DRIVER_H_ |
| 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_DRIVER_H_ | 6 #define CHROME_BROWSER_SERVICES_GCM_GCM_DRIVER_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" | 12 #include "base/basictypes.h" |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/files/file_path.h" | |
| 16 #include "base/memory/ref_counted.h" | |
| 17 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 19 #include "components/gcm_driver/default_gcm_app_handler.h" | 17 #include "components/gcm_driver/default_gcm_app_handler.h" |
| 20 #include "google_apis/gaia/identity_provider.h" | 18 #include "google_apis/gaia/identity_provider.h" |
| 21 #include "google_apis/gcm/gcm_client.h" | 19 #include "google_apis/gcm/gcm_client.h" |
| 22 | 20 |
| 21 namespace base { |
| 22 class FilePath; |
| 23 } |
| 24 |
| 23 namespace extensions { | 25 namespace extensions { |
| 24 class ExtensionGCMAppHandlerTest; | 26 class ExtensionGCMAppHandlerTest; |
| 25 } | 27 } |
| 26 | 28 |
| 27 namespace net { | 29 namespace net { |
| 28 class URLRequestContextGetter; | 30 class URLRequestContextGetter; |
| 29 } | 31 } |
| 30 | 32 |
| 31 namespace gcm { | 33 namespace gcm { |
| 32 | 34 |
| 33 class GCMAppHandler; | 35 class GCMAppHandler; |
| 34 class GCMClientFactory; | 36 class GCMClientFactory; |
| 35 | 37 |
| 36 // A bridge between the GCM users in Chrome and the GCMClient layer. | 38 // A bridge between the GCM users in Chrome and the GCMClient layer. |
| 37 class GCMDriver : public IdentityProvider::Observer { | 39 class GCMDriver : public IdentityProvider::Observer { |
| 38 public: | 40 public: |
| 39 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; | 41 typedef std::map<std::string, GCMAppHandler*> GCMAppHandlerMap; |
| 40 typedef base::Callback<void(const std::string& registration_id, | 42 typedef base::Callback<void(const std::string& registration_id, |
| 41 GCMClient::Result result)> RegisterCallback; | 43 GCMClient::Result result)> RegisterCallback; |
| 42 typedef base::Callback<void(const std::string& message_id, | 44 typedef base::Callback<void(const std::string& message_id, |
| 43 GCMClient::Result result)> SendCallback; | 45 GCMClient::Result result)> SendCallback; |
| 44 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback; | 46 typedef base::Callback<void(GCMClient::Result result)> UnregisterCallback; |
| 45 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)> | 47 typedef base::Callback<void(const GCMClient::GCMStatistics& stats)> |
| 46 GetGCMStatisticsCallback; | 48 GetGCMStatisticsCallback; |
| 47 | 49 |
| 48 explicit GCMDriver(scoped_ptr<IdentityProvider> identity_provider); | 50 GCMDriver(scoped_ptr<GCMClientFactory> gcm_client_factory, |
| 51 scoped_ptr<IdentityProvider> identity_provider, |
| 52 const base::FilePath& store_path, |
| 53 const scoped_refptr<net::URLRequestContextGetter>& request_context); |
| 49 virtual ~GCMDriver(); | 54 virtual ~GCMDriver(); |
| 50 | 55 |
| 51 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory); | 56 // Enables/disables GCM service. |
| 52 | 57 void Enable(); |
| 53 void Start(); | 58 void Disable(); |
| 54 | |
| 55 void Stop(); | |
| 56 | 59 |
| 57 // This method must be called before destroying the GCMDriver. Once it has | 60 // This method must be called before destroying the GCMDriver. Once it has |
| 58 // been called, no other GCMDriver methods may be used. | 61 // been called, no other GCMDriver methods may be used. |
| 59 void ShutdownService(); | 62 void Shutdown(); |
| 60 | 63 |
| 61 // Adds a handler for a given app. | 64 // Adds a handler for a given app. |
| 62 void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); | 65 void AddAppHandler(const std::string& app_id, GCMAppHandler* handler); |
| 63 | 66 |
| 64 // Remove the handler for a given app. | 67 // Remove the handler for a given app. |
| 65 void RemoveAppHandler(const std::string& app_id); | 68 void RemoveAppHandler(const std::string& app_id); |
| 66 | 69 |
| 67 // Registers |sender_id| for an app. A registration ID will be returned by | 70 // Registers |sender_id| for an app. A registration ID will be returned by |
| 68 // the GCM server. | 71 // the GCM server. |
| 69 // |app_id|: application ID. | 72 // |app_id|: application ID. |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 // Get GCM client internal states and statistics. | 106 // Get GCM client internal states and statistics. |
| 104 // If clear_logs is true then activity logs will be cleared before the stats | 107 // If clear_logs is true then activity logs will be cleared before the stats |
| 105 // are returned. | 108 // are returned. |
| 106 void GetGCMStatistics(const GetGCMStatisticsCallback& callback, | 109 void GetGCMStatistics(const GetGCMStatisticsCallback& callback, |
| 107 bool clear_logs); | 110 bool clear_logs); |
| 108 | 111 |
| 109 // Enables/disables GCM activity recording, and then returns the stats. | 112 // Enables/disables GCM activity recording, and then returns the stats. |
| 110 void SetGCMRecording(const GetGCMStatisticsCallback& callback, | 113 void SetGCMRecording(const GetGCMStatisticsCallback& callback, |
| 111 bool recording); | 114 bool recording); |
| 112 | 115 |
| 116 // Returns the user name if the profile is signed in. |
| 117 std::string SignedInUserName() const; |
| 118 |
| 113 // IdentityProvider::Observer: | 119 // IdentityProvider::Observer: |
| 114 virtual void OnActiveAccountLogin() OVERRIDE; | 120 virtual void OnActiveAccountLogin() OVERRIDE; |
| 115 virtual void OnActiveAccountLogout() OVERRIDE; | 121 virtual void OnActiveAccountLogout() OVERRIDE; |
| 116 | 122 |
| 117 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; } | 123 const GCMAppHandlerMap& app_handlers() const { return app_handlers_; } |
| 118 | 124 |
| 119 protected: | 125 protected: |
| 120 virtual bool ShouldStartAutomatically() const = 0; | 126 // Used for constructing fake GCMDriver for testing purpose. |
| 121 | 127 GCMDriver(); |
| 122 virtual base::FilePath GetStorePath() const = 0; | |
| 123 | |
| 124 virtual scoped_refptr<net::URLRequestContextGetter> | |
| 125 GetURLRequestContextGetter() const = 0; | |
| 126 | |
| 127 scoped_ptr<IdentityProvider> identity_provider_; | |
| 128 | 128 |
| 129 private: | 129 private: |
| 130 class DelayedTaskController; | 130 class DelayedTaskController; |
| 131 class IOWorker; | 131 class IOWorker; |
| 132 | 132 |
| 133 // Ensures that the GCMClient is started and the GCM check-in is done if the | 133 // Ensures that the GCMClient is started and the GCM check-in is done if the |
| 134 // |identity_provider_| is able to supply an account ID. | 134 // |identity_provider_| is able to supply an account ID. |
| 135 void EnsureStarted(); | 135 void EnsureStarted(); |
| 136 | 136 |
| 137 // Stops the GCM service. It can be restarted by calling EnsureStarted again. |
| 138 void Stop(); |
| 139 |
| 137 // Remove cached data when GCM service is stopped. | 140 // Remove cached data when GCM service is stopped. |
| 138 void RemoveCachedData(); | 141 void RemoveCachedData(); |
| 139 | 142 |
| 140 // Checks out of GCM and erases any cached and persisted data. | 143 // Checks out of GCM and erases any cached and persisted data. |
| 141 void CheckOut(); | 144 void CheckOut(); |
| 142 | 145 |
| 143 // Ensures that the app is ready for GCM functions and events. | 146 // Ensures that the app is ready for GCM functions and events. |
| 144 GCMClient::Result EnsureAppReady(const std::string& app_id); | 147 GCMClient::Result EnsureAppReady(const std::string& app_id); |
| 145 | 148 |
| 146 // Should be called when an app with |app_id| is trying to un/register. | 149 // Should be called when an app with |app_id| is trying to un/register. |
| (...skipping 20 matching lines...) Expand all Loading... |
| 167 void MessagesDeleted(const std::string& app_id); | 170 void MessagesDeleted(const std::string& app_id); |
| 168 void MessageSendError(const std::string& app_id, | 171 void MessageSendError(const std::string& app_id, |
| 169 const GCMClient::SendErrorDetails& send_error_details); | 172 const GCMClient::SendErrorDetails& send_error_details); |
| 170 void GCMClientReady(); | 173 void GCMClientReady(); |
| 171 | 174 |
| 172 // Returns the handler for the given app. | 175 // Returns the handler for the given app. |
| 173 GCMAppHandler* GetAppHandler(const std::string& app_id); | 176 GCMAppHandler* GetAppHandler(const std::string& app_id); |
| 174 | 177 |
| 175 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); | 178 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); |
| 176 | 179 |
| 180 // Flag to indicate if GCM is enabled. |
| 181 bool gcm_enabled_; |
| 182 |
| 177 // Flag to indicate if GCMClient is ready. | 183 // Flag to indicate if GCMClient is ready. |
| 178 bool gcm_client_ready_; | 184 bool gcm_client_ready_; |
| 179 | 185 |
| 180 // The account ID that this service is responsible for. Empty when the service | 186 // The account ID that this service is responsible for. Empty when the service |
| 181 // is not running. | 187 // is not running. |
| 182 std::string account_id_; | 188 std::string account_id_; |
| 183 | 189 |
| 190 scoped_ptr<IdentityProvider> identity_provider_; |
| 191 |
| 184 scoped_ptr<DelayedTaskController> delayed_task_controller_; | 192 scoped_ptr<DelayedTaskController> delayed_task_controller_; |
| 185 | 193 |
| 186 // For all the work occurring on the IO thread. Must be destroyed on the IO | 194 // For all the work occurring on the IO thread. Must be destroyed on the IO |
| 187 // thread. | 195 // thread. |
| 188 scoped_ptr<IOWorker> io_worker_; | 196 scoped_ptr<IOWorker> io_worker_; |
| 189 | 197 |
| 190 // App handler map (from app_id to handler pointer). | 198 // App handler map (from app_id to handler pointer). |
| 191 // The handler is not owned. | 199 // The handler is not owned. |
| 192 GCMAppHandlerMap app_handlers_; | 200 GCMAppHandlerMap app_handlers_; |
| 193 | 201 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 208 | 216 |
| 209 // Used to pass a weak pointer to the IO worker. | 217 // Used to pass a weak pointer to the IO worker. |
| 210 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_; | 218 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_; |
| 211 | 219 |
| 212 DISALLOW_COPY_AND_ASSIGN(GCMDriver); | 220 DISALLOW_COPY_AND_ASSIGN(GCMDriver); |
| 213 }; | 221 }; |
| 214 | 222 |
| 215 } // namespace gcm | 223 } // namespace gcm |
| 216 | 224 |
| 217 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_DRIVER_H_ | 225 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_DRIVER_H_ |
| OLD | NEW |