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 #include "chrome/browser/services/gcm/gcm_driver.h" | 5 #include "chrome/browser/services/gcm/gcm_driver.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/sequenced_task_runner.h" | 14 #include "base/sequenced_task_runner.h" |
15 #include "base/threading/sequenced_worker_pool.h" | 15 #include "base/threading/sequenced_worker_pool.h" |
16 #include "chrome/common/chrome_version_info.h" | 16 #include "chrome/common/chrome_version_info.h" |
17 #include "components/gcm_driver/gcm_app_handler.h" | 17 #include "components/gcm_driver/gcm_app_handler.h" |
18 #include "components/gcm_driver/gcm_client_factory.h" | 18 #include "components/gcm_driver/gcm_client_factory.h" |
19 #include "components/gcm_driver/system_encryptor.h" | 19 #include "components/gcm_driver/system_encryptor.h" |
20 #include "content/public/browser/browser_thread.h" | |
21 #include "google_apis/gaia/oauth2_token_service.h" | 20 #include "google_apis/gaia/oauth2_token_service.h" |
22 #include "google_apis/gcm/protocol/android_checkin.pb.h" | 21 #include "google_apis/gcm/protocol/android_checkin.pb.h" |
23 #include "net/url_request/url_request_context_getter.h" | 22 #include "net/url_request/url_request_context_getter.h" |
24 | 23 |
25 namespace gcm { | 24 namespace gcm { |
26 | 25 |
27 namespace { | 26 namespace { |
28 | 27 |
29 checkin_proto::ChromeBuildProto_Platform GetPlatform() { | 28 checkin_proto::ChromeBuildProto_Platform GetPlatform() { |
30 #if defined(OS_WIN) | 29 #if defined(OS_WIN) |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 DCHECK(ready_); | 118 DCHECK(ready_); |
120 | 119 |
121 for (size_t i = 0; i < delayed_tasks_.size(); ++i) | 120 for (size_t i = 0; i < delayed_tasks_.size(); ++i) |
122 delayed_tasks_[i].Run(); | 121 delayed_tasks_[i].Run(); |
123 delayed_tasks_.clear(); | 122 delayed_tasks_.clear(); |
124 } | 123 } |
125 | 124 |
126 class GCMDriver::IOWorker : public GCMClient::Delegate { | 125 class GCMDriver::IOWorker : public GCMClient::Delegate { |
127 public: | 126 public: |
128 // Called on UI thread. | 127 // Called on UI thread. |
129 IOWorker(); | 128 IOWorker(const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 129 const scoped_refptr<base::SequencedTaskRunner>& io_thread); |
130 virtual ~IOWorker(); | 130 virtual ~IOWorker(); |
131 | 131 |
132 // Overridden from GCMClient::Delegate: | 132 // Overridden from GCMClient::Delegate: |
133 // Called on IO thread. | 133 // Called on IO thread. |
134 virtual void OnRegisterFinished(const std::string& app_id, | 134 virtual void OnRegisterFinished(const std::string& app_id, |
135 const std::string& registration_id, | 135 const std::string& registration_id, |
136 GCMClient::Result result) OVERRIDE; | 136 GCMClient::Result result) OVERRIDE; |
137 virtual void OnUnregisterFinished(const std::string& app_id, | 137 virtual void OnUnregisterFinished(const std::string& app_id, |
138 GCMClient::Result result) OVERRIDE; | 138 GCMClient::Result result) OVERRIDE; |
139 virtual void OnSendFinished(const std::string& app_id, | 139 virtual void OnSendFinished(const std::string& app_id, |
140 const std::string& message_id, | 140 const std::string& message_id, |
141 GCMClient::Result result) OVERRIDE; | 141 GCMClient::Result result) OVERRIDE; |
142 virtual void OnMessageReceived( | 142 virtual void OnMessageReceived( |
143 const std::string& app_id, | 143 const std::string& app_id, |
144 const GCMClient::IncomingMessage& message) OVERRIDE; | 144 const GCMClient::IncomingMessage& message) OVERRIDE; |
145 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | 145 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
146 virtual void OnMessageSendError( | 146 virtual void OnMessageSendError( |
147 const std::string& app_id, | 147 const std::string& app_id, |
148 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; | 148 const GCMClient::SendErrorDetails& send_error_details) OVERRIDE; |
149 virtual void OnGCMReady() OVERRIDE; | 149 virtual void OnGCMReady() OVERRIDE; |
150 virtual void OnActivityRecorded() OVERRIDE; | 150 virtual void OnActivityRecorded() OVERRIDE; |
151 | 151 |
152 // Called on IO thread. | 152 // Called on IO thread. |
153 void Initialize(scoped_ptr<GCMClientFactory> gcm_client_factory, | 153 void Initialize( |
154 const base::FilePath& store_path, | 154 scoped_ptr<GCMClientFactory> gcm_client_factory, |
155 const std::vector<std::string>& account_ids, | 155 const base::FilePath& store_path, |
156 const scoped_refptr<net::URLRequestContextGetter>& | 156 const std::vector<std::string>& account_ids, |
157 url_request_context_getter); | 157 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 158 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner); |
158 void Start(const base::WeakPtr<GCMDriver>& service); | 159 void Start(const base::WeakPtr<GCMDriver>& service); |
159 void Stop(); | 160 void Stop(); |
160 void CheckOut(); | 161 void CheckOut(); |
161 void Register(const std::string& app_id, | 162 void Register(const std::string& app_id, |
162 const std::vector<std::string>& sender_ids); | 163 const std::vector<std::string>& sender_ids); |
163 void Unregister(const std::string& app_id); | 164 void Unregister(const std::string& app_id); |
164 void Send(const std::string& app_id, | 165 void Send(const std::string& app_id, |
165 const std::string& receiver_id, | 166 const std::string& receiver_id, |
166 const GCMClient::OutgoingMessage& message); | 167 const GCMClient::OutgoingMessage& message); |
167 void GetGCMStatistics(bool clear_logs); | 168 void GetGCMStatistics(bool clear_logs); |
168 void SetGCMRecording(bool recording); | 169 void SetGCMRecording(bool recording); |
169 | 170 |
170 // For testing purpose. Can be called from UI thread. Use with care. | 171 // For testing purpose. Can be called from UI thread. Use with care. |
171 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } | 172 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } |
172 | 173 |
173 private: | 174 private: |
| 175 scoped_refptr<base::SequencedTaskRunner> ui_thread_; |
| 176 scoped_refptr<base::SequencedTaskRunner> io_thread_; |
| 177 |
174 base::WeakPtr<GCMDriver> service_; | 178 base::WeakPtr<GCMDriver> service_; |
175 | 179 |
176 scoped_ptr<GCMClient> gcm_client_; | 180 scoped_ptr<GCMClient> gcm_client_; |
177 | 181 |
178 DISALLOW_COPY_AND_ASSIGN(IOWorker); | 182 DISALLOW_COPY_AND_ASSIGN(IOWorker); |
179 }; | 183 }; |
180 | 184 |
181 GCMDriver::IOWorker::IOWorker() { | 185 GCMDriver::IOWorker::IOWorker( |
182 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 186 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 187 const scoped_refptr<base::SequencedTaskRunner>& io_thread) |
| 188 : ui_thread_(ui_thread), |
| 189 io_thread_(io_thread) { |
| 190 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
183 } | 191 } |
184 | 192 |
185 GCMDriver::IOWorker::~IOWorker() { | 193 GCMDriver::IOWorker::~IOWorker() { |
186 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 194 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
187 } | 195 } |
188 | 196 |
189 void GCMDriver::IOWorker::Initialize( | 197 void GCMDriver::IOWorker::Initialize( |
190 scoped_ptr<GCMClientFactory> gcm_client_factory, | 198 scoped_ptr<GCMClientFactory> gcm_client_factory, |
191 const base::FilePath& store_path, | 199 const base::FilePath& store_path, |
192 const std::vector<std::string>& account_ids, | 200 const std::vector<std::string>& account_ids, |
193 const scoped_refptr<net::URLRequestContextGetter>& | 201 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
194 url_request_context_getter) { | 202 const scoped_refptr<base::SequencedTaskRunner> blocking_task_runner) { |
195 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 203 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
196 | 204 |
197 gcm_client_ = gcm_client_factory->BuildInstance(); | 205 gcm_client_ = gcm_client_factory->BuildInstance(); |
198 | 206 |
199 checkin_proto::ChromeBuildProto chrome_build_proto; | 207 checkin_proto::ChromeBuildProto chrome_build_proto; |
200 chrome_build_proto.set_platform(GetPlatform()); | 208 chrome_build_proto.set_platform(GetPlatform()); |
201 chrome_build_proto.set_chrome_version(GetVersion()); | 209 chrome_build_proto.set_chrome_version(GetVersion()); |
202 chrome_build_proto.set_channel(GetChannel()); | 210 chrome_build_proto.set_channel(GetChannel()); |
203 | 211 |
204 scoped_refptr<base::SequencedWorkerPool> worker_pool( | |
205 content::BrowserThread::GetBlockingPool()); | |
206 scoped_refptr<base::SequencedTaskRunner> blocking_task_runner( | |
207 worker_pool->GetSequencedTaskRunnerWithShutdownBehavior( | |
208 worker_pool->GetSequenceToken(), | |
209 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); | |
210 | |
211 gcm_client_->Initialize(chrome_build_proto, | 212 gcm_client_->Initialize(chrome_build_proto, |
212 store_path, | 213 store_path, |
213 account_ids, | 214 account_ids, |
214 blocking_task_runner, | 215 blocking_task_runner, |
215 url_request_context_getter, | 216 request_context, |
216 make_scoped_ptr<Encryptor>(new SystemEncryptor), | 217 make_scoped_ptr<Encryptor>(new SystemEncryptor), |
217 this); | 218 this); |
218 } | 219 } |
219 | 220 |
220 void GCMDriver::IOWorker::OnRegisterFinished( | 221 void GCMDriver::IOWorker::OnRegisterFinished( |
221 const std::string& app_id, | 222 const std::string& app_id, |
222 const std::string& registration_id, | 223 const std::string& registration_id, |
223 GCMClient::Result result) { | 224 GCMClient::Result result) { |
224 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 225 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
225 | 226 |
226 content::BrowserThread::PostTask(content::BrowserThread::UI, | 227 ui_thread_->PostTask( |
227 FROM_HERE, | 228 FROM_HERE, |
228 base::Bind(&GCMDriver::RegisterFinished, | 229 base::Bind(&GCMDriver::RegisterFinished, service_, app_id, |
229 service_, | 230 registration_id, result)); |
230 app_id, | |
231 registration_id, | |
232 result)); | |
233 } | 231 } |
234 | 232 |
235 void GCMDriver::IOWorker::OnUnregisterFinished(const std::string& app_id, | 233 void GCMDriver::IOWorker::OnUnregisterFinished(const std::string& app_id, |
236 GCMClient::Result result) { | 234 GCMClient::Result result) { |
237 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 235 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
238 | 236 |
239 content::BrowserThread::PostTask( | 237 ui_thread_->PostTask( |
240 content::BrowserThread::UI, | |
241 FROM_HERE, | 238 FROM_HERE, |
242 base::Bind(&GCMDriver::UnregisterFinished, service_, app_id, result)); | 239 base::Bind(&GCMDriver::UnregisterFinished, service_, app_id, result)); |
243 } | 240 } |
244 | 241 |
245 void GCMDriver::IOWorker::OnSendFinished(const std::string& app_id, | 242 void GCMDriver::IOWorker::OnSendFinished(const std::string& app_id, |
246 const std::string& message_id, | 243 const std::string& message_id, |
247 GCMClient::Result result) { | 244 GCMClient::Result result) { |
248 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 245 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
249 | 246 |
250 content::BrowserThread::PostTask(content::BrowserThread::UI, | 247 ui_thread_->PostTask( |
251 FROM_HERE, | 248 FROM_HERE, |
252 base::Bind(&GCMDriver::SendFinished, | 249 base::Bind(&GCMDriver::SendFinished, service_, app_id, message_id, |
253 service_, | 250 result)); |
254 app_id, | |
255 message_id, | |
256 result)); | |
257 } | 251 } |
258 | 252 |
259 void GCMDriver::IOWorker::OnMessageReceived( | 253 void GCMDriver::IOWorker::OnMessageReceived( |
260 const std::string& app_id, | 254 const std::string& app_id, |
261 const GCMClient::IncomingMessage& message) { | 255 const GCMClient::IncomingMessage& message) { |
262 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 256 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
263 | 257 |
264 content::BrowserThread::PostTask(content::BrowserThread::UI, | 258 ui_thread_->PostTask( |
265 FROM_HERE, | 259 FROM_HERE, |
266 base::Bind(&GCMDriver::MessageReceived, | 260 base::Bind(&GCMDriver::MessageReceived, service_, app_id, message)); |
267 service_, | |
268 app_id, | |
269 message)); | |
270 } | 261 } |
271 | 262 |
272 void GCMDriver::IOWorker::OnMessagesDeleted(const std::string& app_id) { | 263 void GCMDriver::IOWorker::OnMessagesDeleted(const std::string& app_id) { |
273 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 264 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
274 | 265 |
275 content::BrowserThread::PostTask(content::BrowserThread::UI, | 266 ui_thread_->PostTask( |
276 FROM_HERE, | 267 FROM_HERE, |
277 base::Bind(&GCMDriver::MessagesDeleted, | 268 base::Bind(&GCMDriver::MessagesDeleted, service_, app_id)); |
278 service_, | |
279 app_id)); | |
280 } | 269 } |
281 | 270 |
282 void GCMDriver::IOWorker::OnMessageSendError( | 271 void GCMDriver::IOWorker::OnMessageSendError( |
283 const std::string& app_id, | 272 const std::string& app_id, |
284 const GCMClient::SendErrorDetails& send_error_details) { | 273 const GCMClient::SendErrorDetails& send_error_details) { |
285 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 274 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
286 | 275 |
287 content::BrowserThread::PostTask(content::BrowserThread::UI, | 276 ui_thread_->PostTask( |
288 FROM_HERE, | 277 FROM_HERE, |
289 base::Bind(&GCMDriver::MessageSendError, | 278 base::Bind(&GCMDriver::MessageSendError, service_, app_id, |
290 service_, | 279 send_error_details)); |
291 app_id, | |
292 send_error_details)); | |
293 } | 280 } |
294 | 281 |
295 void GCMDriver::IOWorker::OnGCMReady() { | 282 void GCMDriver::IOWorker::OnGCMReady() { |
296 content::BrowserThread::PostTask(content::BrowserThread::UI, | 283 ui_thread_->PostTask( |
297 FROM_HERE, | 284 FROM_HERE, |
298 base::Bind(&GCMDriver::GCMClientReady, | 285 base::Bind(&GCMDriver::GCMClientReady, service_)); |
299 service_)); | |
300 } | 286 } |
301 | 287 |
302 void GCMDriver::IOWorker::OnActivityRecorded() { | 288 void GCMDriver::IOWorker::OnActivityRecorded() { |
303 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 289 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
304 // When an activity is recorded, get all the stats and refresh the UI of | 290 // When an activity is recorded, get all the stats and refresh the UI of |
305 // gcm-internals page. | 291 // gcm-internals page. |
306 GetGCMStatistics(false); | 292 GetGCMStatistics(false); |
307 } | 293 } |
308 | 294 |
309 void GCMDriver::IOWorker::Start(const base::WeakPtr<GCMDriver>& service) { | 295 void GCMDriver::IOWorker::Start(const base::WeakPtr<GCMDriver>& service) { |
310 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 296 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
311 | 297 |
312 service_ = service; | 298 service_ = service; |
313 gcm_client_->Start(); | 299 gcm_client_->Start(); |
314 } | 300 } |
315 | 301 |
316 void GCMDriver::IOWorker::Stop() { | 302 void GCMDriver::IOWorker::Stop() { |
317 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 303 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
318 | 304 |
319 gcm_client_->Stop(); | 305 gcm_client_->Stop(); |
320 } | 306 } |
321 | 307 |
322 void GCMDriver::IOWorker::CheckOut() { | 308 void GCMDriver::IOWorker::CheckOut() { |
323 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 309 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
324 | 310 |
325 gcm_client_->CheckOut(); | 311 gcm_client_->CheckOut(); |
326 | 312 |
327 // Note that we still need to keep GCMClient instance alive since the | 313 // Note that we still need to keep GCMClient instance alive since the |
328 // GCMDriver may check in again. | 314 // GCMDriver may check in again. |
329 } | 315 } |
330 | 316 |
331 void GCMDriver::IOWorker::Register( | 317 void GCMDriver::IOWorker::Register( |
332 const std::string& app_id, | 318 const std::string& app_id, |
333 const std::vector<std::string>& sender_ids) { | 319 const std::vector<std::string>& sender_ids) { |
334 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 320 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
335 | 321 |
336 gcm_client_->Register(app_id, sender_ids); | 322 gcm_client_->Register(app_id, sender_ids); |
337 } | 323 } |
338 | 324 |
339 void GCMDriver::IOWorker::Unregister(const std::string& app_id) { | 325 void GCMDriver::IOWorker::Unregister(const std::string& app_id) { |
340 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 326 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
341 | 327 |
342 gcm_client_->Unregister(app_id); | 328 gcm_client_->Unregister(app_id); |
343 } | 329 } |
344 | 330 |
345 void GCMDriver::IOWorker::Send(const std::string& app_id, | 331 void GCMDriver::IOWorker::Send(const std::string& app_id, |
346 const std::string& receiver_id, | 332 const std::string& receiver_id, |
347 const GCMClient::OutgoingMessage& message) { | 333 const GCMClient::OutgoingMessage& message) { |
348 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 334 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
349 | 335 |
350 gcm_client_->Send(app_id, receiver_id, message); | 336 gcm_client_->Send(app_id, receiver_id, message); |
351 } | 337 } |
352 | 338 |
353 void GCMDriver::IOWorker::GetGCMStatistics(bool clear_logs) { | 339 void GCMDriver::IOWorker::GetGCMStatistics(bool clear_logs) { |
354 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 340 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
355 gcm::GCMClient::GCMStatistics stats; | 341 gcm::GCMClient::GCMStatistics stats; |
356 | 342 |
357 if (gcm_client_.get()) { | 343 if (gcm_client_.get()) { |
358 if (clear_logs) | 344 if (clear_logs) |
359 gcm_client_->ClearActivityLogs(); | 345 gcm_client_->ClearActivityLogs(); |
360 stats = gcm_client_->GetStatistics(); | 346 stats = gcm_client_->GetStatistics(); |
361 } | 347 } |
362 | 348 |
363 content::BrowserThread::PostTask( | 349 ui_thread_->PostTask( |
364 content::BrowserThread::UI, | |
365 FROM_HERE, | 350 FROM_HERE, |
366 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); | 351 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); |
367 } | 352 } |
368 | 353 |
369 void GCMDriver::IOWorker::SetGCMRecording(bool recording) { | 354 void GCMDriver::IOWorker::SetGCMRecording(bool recording) { |
370 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 355 DCHECK(io_thread_->RunsTasksOnCurrentThread()); |
371 gcm::GCMClient::GCMStatistics stats; | 356 gcm::GCMClient::GCMStatistics stats; |
372 | 357 |
373 if (gcm_client_.get()) { | 358 if (gcm_client_.get()) { |
374 gcm_client_->SetRecording(recording); | 359 gcm_client_->SetRecording(recording); |
375 stats = gcm_client_->GetStatistics(); | 360 stats = gcm_client_->GetStatistics(); |
376 stats.gcm_client_created = true; | 361 stats.gcm_client_created = true; |
377 } | 362 } |
378 | 363 |
379 content::BrowserThread::PostTask( | 364 ui_thread_->PostTask( |
380 content::BrowserThread::UI, | |
381 FROM_HERE, | 365 FROM_HERE, |
382 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); | 366 base::Bind(&GCMDriver::GetGCMStatisticsFinished, service_, stats)); |
383 } | 367 } |
384 | 368 |
385 GCMDriver::GCMDriver( | 369 GCMDriver::GCMDriver( |
386 scoped_ptr<GCMClientFactory> gcm_client_factory, | 370 scoped_ptr<GCMClientFactory> gcm_client_factory, |
387 scoped_ptr<IdentityProvider> identity_provider, | 371 scoped_ptr<IdentityProvider> identity_provider, |
388 const base::FilePath& store_path, | 372 const base::FilePath& store_path, |
389 const scoped_refptr<net::URLRequestContextGetter>& request_context) | 373 const scoped_refptr<net::URLRequestContextGetter>& request_context, |
| 374 const scoped_refptr<base::SequencedTaskRunner>& ui_thread, |
| 375 const scoped_refptr<base::SequencedTaskRunner>& io_thread, |
| 376 const scoped_refptr<base::SequencedTaskRunner>& blocking_task_runner) |
390 : gcm_enabled_(true), | 377 : gcm_enabled_(true), |
391 gcm_client_ready_(false), | 378 gcm_client_ready_(false), |
392 identity_provider_(identity_provider.Pass()), | 379 identity_provider_(identity_provider.Pass()), |
| 380 ui_thread_(ui_thread), |
| 381 io_thread_(io_thread), |
393 weak_ptr_factory_(this) { | 382 weak_ptr_factory_(this) { |
394 // Get the list of available accounts. | 383 // Get the list of available accounts. |
395 std::vector<std::string> account_ids; | 384 std::vector<std::string> account_ids; |
396 #if !defined(OS_ANDROID) | 385 #if !defined(OS_ANDROID) |
397 account_ids = identity_provider_->GetTokenService()->GetAccounts(); | 386 account_ids = identity_provider_->GetTokenService()->GetAccounts(); |
398 #endif | 387 #endif |
399 | 388 |
400 // Create and initialize the GCMClient. Note that this does not initiate the | 389 // Create and initialize the GCMClient. Note that this does not initiate the |
401 // GCM check-in. | 390 // GCM check-in. |
402 io_worker_.reset(new IOWorker()); | 391 io_worker_.reset(new IOWorker(ui_thread, io_thread)); |
403 content::BrowserThread::PostTask( | 392 io_thread_->PostTask( |
404 content::BrowserThread::IO, | |
405 FROM_HERE, | 393 FROM_HERE, |
406 base::Bind(&GCMDriver::IOWorker::Initialize, | 394 base::Bind(&GCMDriver::IOWorker::Initialize, |
407 base::Unretained(io_worker_.get()), | 395 base::Unretained(io_worker_.get()), |
408 base::Passed(&gcm_client_factory), | 396 base::Passed(&gcm_client_factory), |
409 store_path, | 397 store_path, |
410 account_ids, | 398 account_ids, |
411 request_context)); | 399 request_context, |
| 400 blocking_task_runner)); |
412 | 401 |
413 identity_provider_->AddObserver(this); | 402 identity_provider_->AddObserver(this); |
414 } | 403 } |
415 | 404 |
416 GCMDriver::GCMDriver() | 405 GCMDriver::GCMDriver() |
417 : gcm_enabled_(true), | 406 : gcm_enabled_(true), |
418 gcm_client_ready_(false), | 407 gcm_client_ready_(false), |
419 weak_ptr_factory_(this) { | 408 weak_ptr_factory_(this) { |
420 } | 409 } |
421 | 410 |
422 GCMDriver::~GCMDriver() { | 411 GCMDriver::~GCMDriver() { |
423 } | 412 } |
424 | 413 |
425 void GCMDriver::Enable() { | 414 void GCMDriver::Enable() { |
426 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 415 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
427 | 416 |
428 if (gcm_enabled_) | 417 if (gcm_enabled_) |
429 return; | 418 return; |
430 gcm_enabled_ = true; | 419 gcm_enabled_ = true; |
431 | 420 |
432 EnsureStarted(); | 421 EnsureStarted(); |
433 } | 422 } |
434 | 423 |
435 void GCMDriver::Disable() { | 424 void GCMDriver::Disable() { |
436 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 425 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
437 | 426 |
438 if (!gcm_enabled_) | 427 if (!gcm_enabled_) |
439 return; | 428 return; |
440 gcm_enabled_ = false; | 429 gcm_enabled_ = false; |
441 | 430 |
442 Stop(); | 431 Stop(); |
443 } | 432 } |
444 | 433 |
445 void GCMDriver::Stop() { | 434 void GCMDriver::Stop() { |
446 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 435 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
447 | 436 |
448 // No need to stop GCM service if not started yet. | 437 // No need to stop GCM service if not started yet. |
449 if (account_id_.empty()) | 438 if (account_id_.empty()) |
450 return; | 439 return; |
451 | 440 |
452 RemoveCachedData(); | 441 RemoveCachedData(); |
453 | 442 |
454 content::BrowserThread::PostTask( | 443 io_thread_->PostTask( |
455 content::BrowserThread::IO, | |
456 FROM_HERE, | 444 FROM_HERE, |
457 base::Bind(&GCMDriver::IOWorker::Stop, | 445 base::Bind(&GCMDriver::IOWorker::Stop, |
458 base::Unretained(io_worker_.get()))); | 446 base::Unretained(io_worker_.get()))); |
459 } | 447 } |
460 | 448 |
461 void GCMDriver::Shutdown() { | 449 void GCMDriver::Shutdown() { |
462 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 450 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
463 identity_provider_->RemoveObserver(this); | 451 identity_provider_->RemoveObserver(this); |
464 for (GCMAppHandlerMap::const_iterator iter = app_handlers_.begin(); | 452 for (GCMAppHandlerMap::const_iterator iter = app_handlers_.begin(); |
465 iter != app_handlers_.end(); ++iter) { | 453 iter != app_handlers_.end(); ++iter) { |
466 iter->second->ShutdownHandler(); | 454 iter->second->ShutdownHandler(); |
467 } | 455 } |
468 app_handlers_.clear(); | 456 app_handlers_.clear(); |
469 content::BrowserThread::DeleteSoon(content::BrowserThread::IO, | 457 io_thread_->DeleteSoon(FROM_HERE, io_worker_.release()); |
470 FROM_HERE, | |
471 io_worker_.release()); | |
472 } | 458 } |
473 | 459 |
474 void GCMDriver::AddAppHandler(const std::string& app_id, | 460 void GCMDriver::AddAppHandler(const std::string& app_id, |
475 GCMAppHandler* handler) { | 461 GCMAppHandler* handler) { |
476 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 462 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
477 DCHECK(!app_id.empty()); | 463 DCHECK(!app_id.empty()); |
478 DCHECK(handler); | 464 DCHECK(handler); |
479 DCHECK(app_handlers_.find(app_id) == app_handlers_.end()); | 465 DCHECK(app_handlers_.find(app_id) == app_handlers_.end()); |
480 | 466 |
481 app_handlers_[app_id] = handler; | 467 app_handlers_[app_id] = handler; |
482 | 468 |
483 // Ensures that the GCM service is started when there is an interest. | 469 // Ensures that the GCM service is started when there is an interest. |
484 EnsureStarted(); | 470 EnsureStarted(); |
485 } | 471 } |
486 | 472 |
487 void GCMDriver::RemoveAppHandler(const std::string& app_id) { | 473 void GCMDriver::RemoveAppHandler(const std::string& app_id) { |
488 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 474 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
489 DCHECK(!app_id.empty()); | 475 DCHECK(!app_id.empty()); |
490 | 476 |
491 app_handlers_.erase(app_id); | 477 app_handlers_.erase(app_id); |
492 } | 478 } |
493 | 479 |
494 void GCMDriver::Register(const std::string& app_id, | 480 void GCMDriver::Register(const std::string& app_id, |
495 const std::vector<std::string>& sender_ids, | 481 const std::vector<std::string>& sender_ids, |
496 const RegisterCallback& callback) { | 482 const RegisterCallback& callback) { |
497 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 483 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
498 DCHECK(!app_id.empty()); | 484 DCHECK(!app_id.empty()); |
499 DCHECK(!sender_ids.empty()); | 485 DCHECK(!sender_ids.empty()); |
500 DCHECK(!callback.is_null()); | 486 DCHECK(!callback.is_null()); |
501 | 487 |
502 GCMClient::Result result = EnsureStarted(); | 488 GCMClient::Result result = EnsureStarted(); |
503 if (result != GCMClient::SUCCESS) { | 489 if (result != GCMClient::SUCCESS) { |
504 callback.Run(std::string(), result); | 490 callback.Run(std::string(), result); |
505 return; | 491 return; |
506 } | 492 } |
507 | 493 |
(...skipping 12 matching lines...) Expand all Loading... |
520 app_id, | 506 app_id, |
521 sender_ids)); | 507 sender_ids)); |
522 return; | 508 return; |
523 } | 509 } |
524 | 510 |
525 DoRegister(app_id, sender_ids); | 511 DoRegister(app_id, sender_ids); |
526 } | 512 } |
527 | 513 |
528 void GCMDriver::DoRegister(const std::string& app_id, | 514 void GCMDriver::DoRegister(const std::string& app_id, |
529 const std::vector<std::string>& sender_ids) { | 515 const std::vector<std::string>& sender_ids) { |
530 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 516 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
531 std::map<std::string, RegisterCallback>::iterator callback_iter = | 517 std::map<std::string, RegisterCallback>::iterator callback_iter = |
532 register_callbacks_.find(app_id); | 518 register_callbacks_.find(app_id); |
533 if (callback_iter == register_callbacks_.end()) { | 519 if (callback_iter == register_callbacks_.end()) { |
534 // The callback could have been removed when the app is uninstalled. | 520 // The callback could have been removed when the app is uninstalled. |
535 return; | 521 return; |
536 } | 522 } |
537 | 523 |
538 // Normalize the sender IDs by making them sorted. | 524 // Normalize the sender IDs by making them sorted. |
539 std::vector<std::string> normalized_sender_ids = sender_ids; | 525 std::vector<std::string> normalized_sender_ids = sender_ids; |
540 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); | 526 std::sort(normalized_sender_ids.begin(), normalized_sender_ids.end()); |
541 | 527 |
542 content::BrowserThread::PostTask( | 528 io_thread_->PostTask( |
543 content::BrowserThread::IO, | |
544 FROM_HERE, | 529 FROM_HERE, |
545 base::Bind(&GCMDriver::IOWorker::Register, | 530 base::Bind(&GCMDriver::IOWorker::Register, |
546 base::Unretained(io_worker_.get()), | 531 base::Unretained(io_worker_.get()), |
547 app_id, | 532 app_id, |
548 normalized_sender_ids)); | 533 normalized_sender_ids)); |
549 } | 534 } |
550 | 535 |
551 void GCMDriver::Unregister(const std::string& app_id, | 536 void GCMDriver::Unregister(const std::string& app_id, |
552 const UnregisterCallback& callback) { | 537 const UnregisterCallback& callback) { |
553 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 538 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
554 DCHECK(!app_id.empty()); | 539 DCHECK(!app_id.empty()); |
555 DCHECK(!callback.is_null()); | 540 DCHECK(!callback.is_null()); |
556 | 541 |
557 GCMClient::Result result = EnsureStarted(); | 542 GCMClient::Result result = EnsureStarted(); |
558 if (result != GCMClient::SUCCESS) { | 543 if (result != GCMClient::SUCCESS) { |
559 callback.Run(result); | 544 callback.Run(result); |
560 return; | 545 return; |
561 } | 546 } |
562 | 547 |
563 // If previous un/register operation is still in progress, bail out. | 548 // If previous un/register operation is still in progress, bail out. |
564 if (IsAsyncOperationPending(app_id)) { | 549 if (IsAsyncOperationPending(app_id)) { |
565 callback.Run(GCMClient::ASYNC_OPERATION_PENDING); | 550 callback.Run(GCMClient::ASYNC_OPERATION_PENDING); |
566 return; | 551 return; |
567 } | 552 } |
568 | 553 |
569 unregister_callbacks_[app_id] = callback; | 554 unregister_callbacks_[app_id] = callback; |
570 | 555 |
571 // Delay the unregister operation until GCMClient is ready. | 556 // Delay the unregister operation until GCMClient is ready. |
572 if (!delayed_task_controller_->CanRunTaskWithoutDelay()) { | 557 if (!delayed_task_controller_->CanRunTaskWithoutDelay()) { |
573 delayed_task_controller_->AddTask(base::Bind(&GCMDriver::DoUnregister, | 558 delayed_task_controller_->AddTask(base::Bind(&GCMDriver::DoUnregister, |
574 weak_ptr_factory_.GetWeakPtr(), | 559 weak_ptr_factory_.GetWeakPtr(), |
575 app_id)); | 560 app_id)); |
576 return; | 561 return; |
577 } | 562 } |
578 | 563 |
579 DoUnregister(app_id); | 564 DoUnregister(app_id); |
580 } | 565 } |
581 | 566 |
582 void GCMDriver::DoUnregister(const std::string& app_id) { | 567 void GCMDriver::DoUnregister(const std::string& app_id) { |
583 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 568 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
584 | 569 |
585 // Ask the server to unregister it. There could be a small chance that the | 570 // Ask the server to unregister it. There could be a small chance that the |
586 // unregister request fails. If this occurs, it does not bring any harm since | 571 // unregister request fails. If this occurs, it does not bring any harm since |
587 // we simply reject the messages/events received from the server. | 572 // we simply reject the messages/events received from the server. |
588 content::BrowserThread::PostTask( | 573 io_thread_->PostTask( |
589 content::BrowserThread::IO, | |
590 FROM_HERE, | 574 FROM_HERE, |
591 base::Bind(&GCMDriver::IOWorker::Unregister, | 575 base::Bind(&GCMDriver::IOWorker::Unregister, |
592 base::Unretained(io_worker_.get()), | 576 base::Unretained(io_worker_.get()), |
593 app_id)); | 577 app_id)); |
594 } | 578 } |
595 | 579 |
596 void GCMDriver::Send(const std::string& app_id, | 580 void GCMDriver::Send(const std::string& app_id, |
597 const std::string& receiver_id, | 581 const std::string& receiver_id, |
598 const GCMClient::OutgoingMessage& message, | 582 const GCMClient::OutgoingMessage& message, |
599 const SendCallback& callback) { | 583 const SendCallback& callback) { |
600 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 584 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
601 DCHECK(!app_id.empty()); | 585 DCHECK(!app_id.empty()); |
602 DCHECK(!receiver_id.empty()); | 586 DCHECK(!receiver_id.empty()); |
603 DCHECK(!callback.is_null()); | 587 DCHECK(!callback.is_null()); |
604 | 588 |
605 GCMClient::Result result = EnsureStarted(); | 589 GCMClient::Result result = EnsureStarted(); |
606 if (result != GCMClient::SUCCESS) { | 590 if (result != GCMClient::SUCCESS) { |
607 callback.Run(std::string(), result); | 591 callback.Run(std::string(), result); |
608 return; | 592 return; |
609 } | 593 } |
610 | 594 |
(...skipping 15 matching lines...) Expand all Loading... |
626 message)); | 610 message)); |
627 return; | 611 return; |
628 } | 612 } |
629 | 613 |
630 DoSend(app_id, receiver_id, message); | 614 DoSend(app_id, receiver_id, message); |
631 } | 615 } |
632 | 616 |
633 void GCMDriver::DoSend(const std::string& app_id, | 617 void GCMDriver::DoSend(const std::string& app_id, |
634 const std::string& receiver_id, | 618 const std::string& receiver_id, |
635 const GCMClient::OutgoingMessage& message) { | 619 const GCMClient::OutgoingMessage& message) { |
636 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 620 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
637 content::BrowserThread::PostTask( | 621 io_thread_->PostTask( |
638 content::BrowserThread::IO, | |
639 FROM_HERE, | 622 FROM_HERE, |
640 base::Bind(&GCMDriver::IOWorker::Send, | 623 base::Bind(&GCMDriver::IOWorker::Send, |
641 base::Unretained(io_worker_.get()), | 624 base::Unretained(io_worker_.get()), |
642 app_id, | 625 app_id, |
643 receiver_id, | 626 receiver_id, |
644 message)); | 627 message)); |
645 } | 628 } |
646 | 629 |
647 GCMClient* GCMDriver::GetGCMClientForTesting() const { | 630 GCMClient* GCMDriver::GetGCMClientForTesting() const { |
648 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 631 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
649 return io_worker_ ? io_worker_->gcm_client_for_testing() : NULL; | 632 return io_worker_ ? io_worker_->gcm_client_for_testing() : NULL; |
650 } | 633 } |
651 | 634 |
652 bool GCMDriver::IsStarted() const { | 635 bool GCMDriver::IsStarted() const { |
653 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 636 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
654 return !account_id_.empty(); | 637 return !account_id_.empty(); |
655 } | 638 } |
656 | 639 |
657 bool GCMDriver::IsGCMClientReady() const { | 640 bool GCMDriver::IsGCMClientReady() const { |
658 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 641 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
659 return gcm_client_ready_; | 642 return gcm_client_ready_; |
660 } | 643 } |
661 | 644 |
662 void GCMDriver::GetGCMStatistics(const GetGCMStatisticsCallback& callback, | 645 void GCMDriver::GetGCMStatistics(const GetGCMStatisticsCallback& callback, |
663 bool clear_logs) { | 646 bool clear_logs) { |
664 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 647 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
665 DCHECK(!callback.is_null()); | 648 DCHECK(!callback.is_null()); |
666 | 649 |
667 request_gcm_statistics_callback_ = callback; | 650 request_gcm_statistics_callback_ = callback; |
668 content::BrowserThread::PostTask( | 651 io_thread_->PostTask( |
669 content::BrowserThread::IO, | |
670 FROM_HERE, | 652 FROM_HERE, |
671 base::Bind(&GCMDriver::IOWorker::GetGCMStatistics, | 653 base::Bind(&GCMDriver::IOWorker::GetGCMStatistics, |
672 base::Unretained(io_worker_.get()), | 654 base::Unretained(io_worker_.get()), |
673 clear_logs)); | 655 clear_logs)); |
674 } | 656 } |
675 | 657 |
676 void GCMDriver::SetGCMRecording(const GetGCMStatisticsCallback& callback, | 658 void GCMDriver::SetGCMRecording(const GetGCMStatisticsCallback& callback, |
677 bool recording) { | 659 bool recording) { |
678 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 660 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
679 | 661 |
680 request_gcm_statistics_callback_ = callback; | 662 request_gcm_statistics_callback_ = callback; |
681 content::BrowserThread::PostTask( | 663 io_thread_->PostTask( |
682 content::BrowserThread::IO, | |
683 FROM_HERE, | 664 FROM_HERE, |
684 base::Bind(&GCMDriver::IOWorker::SetGCMRecording, | 665 base::Bind(&GCMDriver::IOWorker::SetGCMRecording, |
685 base::Unretained(io_worker_.get()), | 666 base::Unretained(io_worker_.get()), |
686 recording)); | 667 recording)); |
687 } | 668 } |
688 | 669 |
689 void GCMDriver::OnActiveAccountLogin() { | 670 void GCMDriver::OnActiveAccountLogin() { |
690 EnsureStarted(); | 671 EnsureStarted(); |
691 } | 672 } |
692 | 673 |
693 void GCMDriver::OnActiveAccountLogout() { | 674 void GCMDriver::OnActiveAccountLogout() { |
694 CheckOut(); | 675 CheckOut(); |
695 } | 676 } |
696 | 677 |
697 GCMClient::Result GCMDriver::EnsureStarted() { | 678 GCMClient::Result GCMDriver::EnsureStarted() { |
698 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 679 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
699 | 680 |
700 if (!gcm_enabled_) | 681 if (!gcm_enabled_) |
701 return GCMClient::GCM_DISABLED; | 682 return GCMClient::GCM_DISABLED; |
702 | 683 |
703 // Is the user signed in? | 684 // Is the user signed in? |
704 const std::string account_id = identity_provider_->GetActiveAccountId(); | 685 const std::string account_id = identity_provider_->GetActiveAccountId(); |
705 if (account_id.empty()) | 686 if (account_id.empty()) |
706 return GCMClient::NOT_SIGNED_IN; | 687 return GCMClient::NOT_SIGNED_IN; |
707 | 688 |
708 // CheckIn could be called more than once when: | 689 // CheckIn could be called more than once when: |
709 // 1) The password changes. | 690 // 1) The password changes. |
710 // 2) Register/send function calls it to ensure CheckIn is done. | 691 // 2) Register/send function calls it to ensure CheckIn is done. |
711 if (account_id_ == account_id) | 692 if (account_id_ == account_id) |
712 return GCMClient::SUCCESS; | 693 return GCMClient::SUCCESS; |
713 account_id_ = account_id; | 694 account_id_ = account_id; |
714 | 695 |
715 DCHECK(!delayed_task_controller_); | 696 DCHECK(!delayed_task_controller_); |
716 delayed_task_controller_.reset(new DelayedTaskController); | 697 delayed_task_controller_.reset(new DelayedTaskController); |
717 | 698 |
718 // Note that we need to pass weak pointer again since the existing weak | 699 // Note that we need to pass weak pointer again since the existing weak |
719 // pointer in IOWorker might have been invalidated when check-out occurs. | 700 // pointer in IOWorker might have been invalidated when check-out occurs. |
720 content::BrowserThread::PostTask( | 701 io_thread_->PostTask( |
721 content::BrowserThread::IO, | |
722 FROM_HERE, | 702 FROM_HERE, |
723 base::Bind(&GCMDriver::IOWorker::Start, | 703 base::Bind(&GCMDriver::IOWorker::Start, |
724 base::Unretained(io_worker_.get()), | 704 base::Unretained(io_worker_.get()), |
725 weak_ptr_factory_.GetWeakPtr())); | 705 weak_ptr_factory_.GetWeakPtr())); |
726 | 706 |
727 return GCMClient::SUCCESS; | 707 return GCMClient::SUCCESS; |
728 } | 708 } |
729 | 709 |
730 void GCMDriver::RemoveCachedData() { | 710 void GCMDriver::RemoveCachedData() { |
731 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 711 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
732 // Remove all the queued tasks since they no longer make sense after | 712 // Remove all the queued tasks since they no longer make sense after |
733 // GCM service is stopped. | 713 // GCM service is stopped. |
734 weak_ptr_factory_.InvalidateWeakPtrs(); | 714 weak_ptr_factory_.InvalidateWeakPtrs(); |
735 | 715 |
736 account_id_.clear(); | 716 account_id_.clear(); |
737 gcm_client_ready_ = false; | 717 gcm_client_ready_ = false; |
738 delayed_task_controller_.reset(); | 718 delayed_task_controller_.reset(); |
739 register_callbacks_.clear(); | 719 register_callbacks_.clear(); |
740 send_callbacks_.clear(); | 720 send_callbacks_.clear(); |
741 } | 721 } |
742 | 722 |
743 void GCMDriver::CheckOut() { | 723 void GCMDriver::CheckOut() { |
744 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 724 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
745 | 725 |
746 // We still proceed with the check-out logic even if the check-in is not | 726 // We still proceed with the check-out logic even if the check-in is not |
747 // initiated in the current session. This will make sure that all the | 727 // initiated in the current session. This will make sure that all the |
748 // persisted data written previously will get purged. | 728 // persisted data written previously will get purged. |
749 | 729 |
750 RemoveCachedData(); | 730 RemoveCachedData(); |
751 | 731 |
752 content::BrowserThread::PostTask( | 732 io_thread_->PostTask( |
753 content::BrowserThread::IO, | |
754 FROM_HERE, | 733 FROM_HERE, |
755 base::Bind(&GCMDriver::IOWorker::CheckOut, | 734 base::Bind(&GCMDriver::IOWorker::CheckOut, |
756 base::Unretained(io_worker_.get()))); | 735 base::Unretained(io_worker_.get()))); |
757 } | 736 } |
758 | 737 |
759 bool GCMDriver::IsAsyncOperationPending(const std::string& app_id) const { | 738 bool GCMDriver::IsAsyncOperationPending(const std::string& app_id) const { |
760 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 739 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
761 return register_callbacks_.find(app_id) != register_callbacks_.end() || | 740 return register_callbacks_.find(app_id) != register_callbacks_.end() || |
762 unregister_callbacks_.find(app_id) != unregister_callbacks_.end(); | 741 unregister_callbacks_.find(app_id) != unregister_callbacks_.end(); |
763 } | 742 } |
764 | 743 |
765 void GCMDriver::RegisterFinished(const std::string& app_id, | 744 void GCMDriver::RegisterFinished(const std::string& app_id, |
766 const std::string& registration_id, | 745 const std::string& registration_id, |
767 GCMClient::Result result) { | 746 GCMClient::Result result) { |
768 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 747 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
769 | 748 |
770 std::map<std::string, RegisterCallback>::iterator callback_iter = | 749 std::map<std::string, RegisterCallback>::iterator callback_iter = |
771 register_callbacks_.find(app_id); | 750 register_callbacks_.find(app_id); |
772 if (callback_iter == register_callbacks_.end()) { | 751 if (callback_iter == register_callbacks_.end()) { |
773 // The callback could have been removed when the app is uninstalled. | 752 // The callback could have been removed when the app is uninstalled. |
774 return; | 753 return; |
775 } | 754 } |
776 | 755 |
777 RegisterCallback callback = callback_iter->second; | 756 RegisterCallback callback = callback_iter->second; |
778 register_callbacks_.erase(callback_iter); | 757 register_callbacks_.erase(callback_iter); |
779 callback.Run(registration_id, result); | 758 callback.Run(registration_id, result); |
780 } | 759 } |
781 | 760 |
782 void GCMDriver::UnregisterFinished(const std::string& app_id, | 761 void GCMDriver::UnregisterFinished(const std::string& app_id, |
783 GCMClient::Result result) { | 762 GCMClient::Result result) { |
784 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 763 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
785 | 764 |
786 std::map<std::string, UnregisterCallback>::iterator callback_iter = | 765 std::map<std::string, UnregisterCallback>::iterator callback_iter = |
787 unregister_callbacks_.find(app_id); | 766 unregister_callbacks_.find(app_id); |
788 if (callback_iter == unregister_callbacks_.end()) | 767 if (callback_iter == unregister_callbacks_.end()) |
789 return; | 768 return; |
790 | 769 |
791 UnregisterCallback callback = callback_iter->second; | 770 UnregisterCallback callback = callback_iter->second; |
792 unregister_callbacks_.erase(callback_iter); | 771 unregister_callbacks_.erase(callback_iter); |
793 callback.Run(result); | 772 callback.Run(result); |
794 } | 773 } |
795 | 774 |
796 void GCMDriver::SendFinished(const std::string& app_id, | 775 void GCMDriver::SendFinished(const std::string& app_id, |
797 const std::string& message_id, | 776 const std::string& message_id, |
798 GCMClient::Result result) { | 777 GCMClient::Result result) { |
799 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 778 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
800 | 779 |
801 std::map<std::pair<std::string, std::string>, SendCallback>::iterator | 780 std::map<std::pair<std::string, std::string>, SendCallback>::iterator |
802 callback_iter = send_callbacks_.find( | 781 callback_iter = send_callbacks_.find( |
803 std::pair<std::string, std::string>(app_id, message_id)); | 782 std::pair<std::string, std::string>(app_id, message_id)); |
804 if (callback_iter == send_callbacks_.end()) { | 783 if (callback_iter == send_callbacks_.end()) { |
805 // The callback could have been removed when the app is uninstalled. | 784 // The callback could have been removed when the app is uninstalled. |
806 return; | 785 return; |
807 } | 786 } |
808 | 787 |
809 SendCallback callback = callback_iter->second; | 788 SendCallback callback = callback_iter->second; |
810 send_callbacks_.erase(callback_iter); | 789 send_callbacks_.erase(callback_iter); |
811 callback.Run(message_id, result); | 790 callback.Run(message_id, result); |
812 } | 791 } |
813 | 792 |
814 void GCMDriver::MessageReceived(const std::string& app_id, | 793 void GCMDriver::MessageReceived(const std::string& app_id, |
815 GCMClient::IncomingMessage message) { | 794 GCMClient::IncomingMessage message) { |
816 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 795 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
817 | 796 |
818 // Drop the event if signed out. | 797 // Drop the event if signed out. |
819 if (account_id_.empty()) | 798 if (account_id_.empty()) |
820 return; | 799 return; |
821 | 800 |
822 GetAppHandler(app_id)->OnMessage(app_id, message); | 801 GetAppHandler(app_id)->OnMessage(app_id, message); |
823 } | 802 } |
824 | 803 |
825 void GCMDriver::MessagesDeleted(const std::string& app_id) { | 804 void GCMDriver::MessagesDeleted(const std::string& app_id) { |
826 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 805 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
827 | 806 |
828 // Drop the event if signed out. | 807 // Drop the event if signed out. |
829 if (account_id_.empty()) | 808 if (account_id_.empty()) |
830 return; | 809 return; |
831 | 810 |
832 GetAppHandler(app_id)->OnMessagesDeleted(app_id); | 811 GetAppHandler(app_id)->OnMessagesDeleted(app_id); |
833 } | 812 } |
834 | 813 |
835 void GCMDriver::MessageSendError( | 814 void GCMDriver::MessageSendError( |
836 const std::string& app_id, | 815 const std::string& app_id, |
837 const GCMClient::SendErrorDetails& send_error_details) { | 816 const GCMClient::SendErrorDetails& send_error_details) { |
838 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 817 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
839 | 818 |
840 // Drop the event if signed out. | 819 // Drop the event if signed out. |
841 if (account_id_.empty()) | 820 if (account_id_.empty()) |
842 return; | 821 return; |
843 | 822 |
844 GetAppHandler(app_id)->OnSendError(app_id, send_error_details); | 823 GetAppHandler(app_id)->OnSendError(app_id, send_error_details); |
845 } | 824 } |
846 | 825 |
847 void GCMDriver::GCMClientReady() { | 826 void GCMDriver::GCMClientReady() { |
848 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 827 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
849 | 828 |
850 if (gcm_client_ready_) | 829 if (gcm_client_ready_) |
851 return; | 830 return; |
852 gcm_client_ready_ = true; | 831 gcm_client_ready_ = true; |
853 | 832 |
854 delayed_task_controller_->SetReady(); | 833 delayed_task_controller_->SetReady(); |
855 } | 834 } |
856 | 835 |
857 GCMAppHandler* GCMDriver::GetAppHandler(const std::string& app_id) { | 836 GCMAppHandler* GCMDriver::GetAppHandler(const std::string& app_id) { |
858 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 837 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
859 | 838 |
860 std::map<std::string, GCMAppHandler*>::const_iterator iter = | 839 std::map<std::string, GCMAppHandler*>::const_iterator iter = |
861 app_handlers_.find(app_id); | 840 app_handlers_.find(app_id); |
862 return iter == app_handlers_.end() ? &default_app_handler_ : iter->second; | 841 return iter == app_handlers_.end() ? &default_app_handler_ : iter->second; |
863 } | 842 } |
864 | 843 |
865 void GCMDriver::GetGCMStatisticsFinished(GCMClient::GCMStatistics stats) { | 844 void GCMDriver::GetGCMStatisticsFinished(GCMClient::GCMStatistics stats) { |
866 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 845 DCHECK(ui_thread_->RunsTasksOnCurrentThread()); |
867 | 846 |
868 // Normally request_gcm_statistics_callback_ would not be null. | 847 // Normally request_gcm_statistics_callback_ would not be null. |
869 if (!request_gcm_statistics_callback_.is_null()) | 848 if (!request_gcm_statistics_callback_.is_null()) |
870 request_gcm_statistics_callback_.Run(stats); | 849 request_gcm_statistics_callback_.Run(stats); |
871 else | 850 else |
872 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; | 851 LOG(WARNING) << "request_gcm_statistics_callback_ is NULL."; |
873 } | 852 } |
874 | 853 |
875 std::string GCMDriver::SignedInUserName() const { | 854 std::string GCMDriver::SignedInUserName() const { |
876 if (IsStarted()) | 855 if (IsStarted()) |
877 return identity_provider_->GetActiveUsername(); | 856 return identity_provider_->GetActiveUsername(); |
878 return std::string(); | 857 return std::string(); |
879 } | 858 } |
880 | 859 |
881 } // namespace gcm | 860 } // namespace gcm |
OLD | NEW |