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. | |
Nicolas Zea
2014/05/19 21:04:06
nit: comment what it does if the profile is not si
jianli
2014/05/19 23:03:48
Done.
| |
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 GCM service starts when all of the following conditions |
134 // |identity_provider_| is able to supply an account ID. | 134 // satisfy: |
135 void EnsureStarted(); | 135 // 1) GCM is enabled. |
136 // 2) The identity provider is able to supply an account ID. | |
137 GCMClient::Result EnsureStarted(); | |
138 | |
139 // Stops the GCM service. It can be restarted by calling EnsureStarted again. | |
140 void Stop(); | |
136 | 141 |
137 // Remove cached data when GCM service is stopped. | 142 // Remove cached data when GCM service is stopped. |
138 void RemoveCachedData(); | 143 void RemoveCachedData(); |
139 | 144 |
140 // Checks out of GCM and erases any cached and persisted data. | 145 // Checks out of GCM and erases any cached and persisted data. |
141 void CheckOut(); | 146 void CheckOut(); |
142 | 147 |
143 // Ensures that the app is ready for GCM functions and events. | |
144 GCMClient::Result EnsureAppReady(const std::string& app_id); | |
145 | |
146 // Should be called when an app with |app_id| is trying to un/register. | 148 // Should be called when an app with |app_id| is trying to un/register. |
147 // Checks whether another un/registration is in progress. | 149 // Checks whether another un/registration is in progress. |
148 bool IsAsyncOperationPending(const std::string& app_id) const; | 150 bool IsAsyncOperationPending(const std::string& app_id) const; |
149 | 151 |
150 void DoRegister(const std::string& app_id, | 152 void DoRegister(const std::string& app_id, |
151 const std::vector<std::string>& sender_ids); | 153 const std::vector<std::string>& sender_ids); |
152 void DoUnregister(const std::string& app_id); | 154 void DoUnregister(const std::string& app_id); |
153 void DoSend(const std::string& app_id, | 155 void DoSend(const std::string& app_id, |
154 const std::string& receiver_id, | 156 const std::string& receiver_id, |
155 const GCMClient::OutgoingMessage& message); | 157 const GCMClient::OutgoingMessage& message); |
(...skipping 11 matching lines...) Expand all Loading... | |
167 void MessagesDeleted(const std::string& app_id); | 169 void MessagesDeleted(const std::string& app_id); |
168 void MessageSendError(const std::string& app_id, | 170 void MessageSendError(const std::string& app_id, |
169 const GCMClient::SendErrorDetails& send_error_details); | 171 const GCMClient::SendErrorDetails& send_error_details); |
170 void GCMClientReady(); | 172 void GCMClientReady(); |
171 | 173 |
172 // Returns the handler for the given app. | 174 // Returns the handler for the given app. |
173 GCMAppHandler* GetAppHandler(const std::string& app_id); | 175 GCMAppHandler* GetAppHandler(const std::string& app_id); |
174 | 176 |
175 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); | 177 void GetGCMStatisticsFinished(GCMClient::GCMStatistics stats); |
176 | 178 |
179 // Flag to indicate if GCM is enabled. | |
180 bool gcm_enabled_; | |
181 | |
177 // Flag to indicate if GCMClient is ready. | 182 // Flag to indicate if GCMClient is ready. |
178 bool gcm_client_ready_; | 183 bool gcm_client_ready_; |
179 | 184 |
180 // The account ID that this service is responsible for. Empty when the service | 185 // The account ID that this service is responsible for. Empty when the service |
181 // is not running. | 186 // is not running. |
182 std::string account_id_; | 187 std::string account_id_; |
183 | 188 |
189 scoped_ptr<IdentityProvider> identity_provider_; | |
190 | |
184 scoped_ptr<DelayedTaskController> delayed_task_controller_; | 191 scoped_ptr<DelayedTaskController> delayed_task_controller_; |
185 | 192 |
186 // For all the work occurring on the IO thread. Must be destroyed on the IO | 193 // For all the work occurring on the IO thread. Must be destroyed on the IO |
187 // thread. | 194 // thread. |
188 scoped_ptr<IOWorker> io_worker_; | 195 scoped_ptr<IOWorker> io_worker_; |
189 | 196 |
190 // App handler map (from app_id to handler pointer). | 197 // App handler map (from app_id to handler pointer). |
191 // The handler is not owned. | 198 // The handler is not owned. |
192 GCMAppHandlerMap app_handlers_; | 199 GCMAppHandlerMap app_handlers_; |
193 | 200 |
(...skipping 14 matching lines...) Expand all Loading... | |
208 | 215 |
209 // Used to pass a weak pointer to the IO worker. | 216 // Used to pass a weak pointer to the IO worker. |
210 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_; | 217 base::WeakPtrFactory<GCMDriver> weak_ptr_factory_; |
211 | 218 |
212 DISALLOW_COPY_AND_ASSIGN(GCMDriver); | 219 DISALLOW_COPY_AND_ASSIGN(GCMDriver); |
213 }; | 220 }; |
214 | 221 |
215 } // namespace gcm | 222 } // namespace gcm |
216 | 223 |
217 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_DRIVER_H_ | 224 #endif // CHROME_BROWSER_SERVICES_GCM_GCM_DRIVER_H_ |
OLD | NEW |