OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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_profile_service.h" | 5 #include "chrome/browser/services/gcm/gcm_profile_service.h" |
6 | 6 |
7 #include "base/base64.h" | 7 #include "base/base64.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/prefs/pref_service.h" | 9 #include "base/prefs/pref_service.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "chrome/browser/chrome_notification_types.h" | 11 #include "chrome/browser/chrome_notification_types.h" |
12 #include "chrome/browser/extensions/extension_service.h" | 12 #include "chrome/browser/extensions/extension_service.h" |
13 #include "chrome/browser/extensions/extension_system.h" | 13 #include "chrome/browser/extensions/extension_system.h" |
14 #include "chrome/browser/extensions/state_store.h" | 14 #include "chrome/browser/extensions/state_store.h" |
15 #include "chrome/browser/services/gcm/gcm_client_factory.h" | |
15 #include "chrome/browser/services/gcm/gcm_event_router.h" | 16 #include "chrome/browser/services/gcm/gcm_event_router.h" |
16 #include "chrome/browser/signin/signin_manager.h" | 17 #include "chrome/browser/signin/signin_manager.h" |
17 #include "chrome/browser/signin/signin_manager_factory.h" | 18 #include "chrome/browser/signin/signin_manager_factory.h" |
18 #include "chrome/common/chrome_version_info.h" | 19 #include "chrome/common/chrome_version_info.h" |
19 #include "chrome/common/pref_names.h" | 20 #include "chrome/common/pref_names.h" |
20 #include "components/user_prefs/pref_registry_syncable.h" | 21 #include "components/user_prefs/pref_registry_syncable.h" |
21 #include "components/webdata/encryptor/encryptor.h" | 22 #include "components/webdata/encryptor/encryptor.h" |
22 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
23 #include "content/public/browser/notification_details.h" | 24 #include "content/public/browser/notification_details.h" |
24 #include "content/public/browser/notification_source.h" | 25 #include "content/public/browser/notification_source.h" |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
193 GCMClient::Result result) OVERRIDE; | 194 GCMClient::Result result) OVERRIDE; |
194 virtual void OnMessageReceived( | 195 virtual void OnMessageReceived( |
195 const std::string& app_id, | 196 const std::string& app_id, |
196 const GCMClient::IncomingMessage& message) OVERRIDE; | 197 const GCMClient::IncomingMessage& message) OVERRIDE; |
197 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; | 198 virtual void OnMessagesDeleted(const std::string& app_id) OVERRIDE; |
198 virtual void OnMessageSendError(const std::string& app_id, | 199 virtual void OnMessageSendError(const std::string& app_id, |
199 const std::string& message_id, | 200 const std::string& message_id, |
200 GCMClient::Result result) OVERRIDE; | 201 GCMClient::Result result) OVERRIDE; |
201 virtual GCMClient::CheckinInfo GetCheckinInfo() const OVERRIDE; | 202 virtual GCMClient::CheckinInfo GetCheckinInfo() const OVERRIDE; |
202 virtual void OnLoadingCompleted() OVERRIDE; | 203 virtual void OnLoadingCompleted() OVERRIDE; |
203 virtual base::TaskRunner* GetFileTaskRunner() OVERRIDE; | |
204 | 204 |
205 void CheckGCMClientLoading(); | 205 void CheckGCMClientLoading(); |
206 void SetUser(const std::string& username); | 206 void SetUser(const std::string& username); |
207 void RemoveUser(const std::string& username); | 207 void RemoveUser(const std::string& username); |
208 void CheckIn(); | 208 void CheckIn(); |
209 // TODO(fgorski): Update to pass by const ref. | 209 // TODO(fgorski): Update to pass by const ref. |
210 void SetCheckinInfo(GCMClient::CheckinInfo checkin_info); | 210 void SetCheckinInfo(GCMClient::CheckinInfo checkin_info); |
211 void CheckOut(); | 211 void CheckOut(); |
212 void Register(const std::string& app_id, | 212 void Register(const std::string& app_id, |
213 const std::vector<std::string>& sender_ids, | 213 const std::vector<std::string>& sender_ids, |
214 const std::string& cert); | 214 const std::string& cert); |
215 void Unregister(const std::string& app_id); | 215 void Unregister(const std::string& app_id); |
216 void Send(const std::string& app_id, | 216 void Send(const std::string& app_id, |
217 const std::string& receiver_id, | 217 const std::string& receiver_id, |
218 const GCMClient::OutgoingMessage& message); | 218 const GCMClient::OutgoingMessage& message); |
219 | 219 |
220 private: | 220 private: |
221 friend class base::RefCountedThreadSafe<IOWorker>; | 221 friend class base::RefCountedThreadSafe<IOWorker>; |
222 virtual ~IOWorker(); | 222 virtual ~IOWorker(); |
223 | 223 |
224 const base::WeakPtr<GCMProfileService> service_; | 224 const base::WeakPtr<GCMProfileService> service_; |
225 | 225 |
226 // Not owned. | |
227 GCMClient* gcm_client_; | |
228 | |
226 // The username (email address) of the signed-in user. | 229 // The username (email address) of the signed-in user. |
227 std::string username_; | 230 std::string username_; |
228 | 231 |
229 // The checkin info obtained from the server for the signed in user associated | 232 // The checkin info obtained from the server for the signed in user associated |
230 // with the profile. | 233 // with the profile. |
231 GCMClient::CheckinInfo checkin_info_; | 234 GCMClient::CheckinInfo checkin_info_; |
232 }; | 235 }; |
233 | 236 |
234 GCMProfileService::IOWorker::IOWorker( | 237 GCMProfileService::IOWorker::IOWorker( |
235 const base::WeakPtr<GCMProfileService>& service) | 238 const base::WeakPtr<GCMProfileService>& service) |
236 : service_(service) { | 239 : service_(service) { |
240 gcm_client_ = GCMClientFactory::GetClient(); | |
fgorski
2014/01/16 23:18:31
will it work on the initialization list?
: service
jianli
2014/01/17 00:43:54
Yes, your suggestion works. However, I need to mov
| |
237 } | 241 } |
238 | 242 |
239 GCMProfileService::IOWorker::~IOWorker() { | 243 GCMProfileService::IOWorker::~IOWorker() { |
240 } | 244 } |
241 | 245 |
242 void GCMProfileService::IOWorker::OnCheckInFinished( | 246 void GCMProfileService::IOWorker::OnCheckInFinished( |
243 const GCMClient::CheckinInfo& checkin_info, | 247 const GCMClient::CheckinInfo& checkin_info, |
244 GCMClient::Result result) { | 248 GCMClient::Result result) { |
245 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 249 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
246 | 250 |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
333 } | 337 } |
334 | 338 |
335 void GCMProfileService::IOWorker::OnLoadingCompleted() { | 339 void GCMProfileService::IOWorker::OnLoadingCompleted() { |
336 content::BrowserThread::PostTask( | 340 content::BrowserThread::PostTask( |
337 content::BrowserThread::UI, | 341 content::BrowserThread::UI, |
338 FROM_HERE, | 342 FROM_HERE, |
339 base::Bind(&GCMProfileService::GCMClientLoadingFinished, | 343 base::Bind(&GCMProfileService::GCMClientLoadingFinished, |
340 service_)); | 344 service_)); |
341 } | 345 } |
342 | 346 |
343 base::TaskRunner* GCMProfileService::IOWorker::GetFileTaskRunner() { | |
344 // TODO(jianli): to be implemented. | |
345 return NULL; | |
346 } | |
347 | |
348 void GCMProfileService::IOWorker::CheckGCMClientLoading() { | 347 void GCMProfileService::IOWorker::CheckGCMClientLoading() { |
349 content::BrowserThread::PostTask( | 348 content::BrowserThread::PostTask( |
350 content::BrowserThread::UI, | 349 content::BrowserThread::UI, |
351 FROM_HERE, | 350 FROM_HERE, |
352 base::Bind(&GCMProfileService::CheckGCMClientLoadingFinished, | 351 base::Bind(&GCMProfileService::CheckGCMClientLoadingFinished, |
353 service_, | 352 service_, |
354 GCMClient::Get()->IsLoading())); | 353 gcm_client_->IsLoading())); |
355 } | 354 } |
356 | 355 |
357 void GCMProfileService::IOWorker::SetUser(const std::string& username) { | 356 void GCMProfileService::IOWorker::SetUser(const std::string& username) { |
358 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 357 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
359 DCHECK(username_.empty() && !username.empty()); | 358 DCHECK(username_.empty() && !username.empty()); |
360 | 359 |
361 username_ = username; | 360 username_ = username; |
362 GCMClient::Get()->SetUserDelegate(username_, this); | 361 gcm_client_->SetUserDelegate(username_, this); |
363 } | 362 } |
364 | 363 |
365 void GCMProfileService::IOWorker::RemoveUser(const std::string& username) { | 364 void GCMProfileService::IOWorker::RemoveUser(const std::string& username) { |
366 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 365 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
367 DCHECK(!username.empty()); | 366 DCHECK(!username.empty()); |
368 | 367 |
369 if (username_.empty()) | 368 if (username_.empty()) |
370 return; | 369 return; |
371 username_.clear(); | 370 username_.clear(); |
372 GCMClient::Get()->SetUserDelegate(username_, NULL); | 371 gcm_client_->SetUserDelegate(username_, NULL); |
373 } | 372 } |
374 | 373 |
375 void GCMProfileService::IOWorker::CheckIn() { | 374 void GCMProfileService::IOWorker::CheckIn() { |
376 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 375 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
377 | 376 |
378 GCMClient::Get()->CheckIn(username_); | 377 gcm_client_->CheckIn(username_); |
379 } | 378 } |
380 | 379 |
381 void GCMProfileService::IOWorker::SetCheckinInfo( | 380 void GCMProfileService::IOWorker::SetCheckinInfo( |
382 GCMClient::CheckinInfo checkin_info) { | 381 GCMClient::CheckinInfo checkin_info) { |
383 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 382 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
384 | 383 |
385 checkin_info_ = checkin_info; | 384 checkin_info_ = checkin_info; |
386 } | 385 } |
387 | 386 |
388 void GCMProfileService::IOWorker::CheckOut() { | 387 void GCMProfileService::IOWorker::CheckOut() { |
389 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 388 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
390 | 389 |
391 username_.clear(); | 390 username_.clear(); |
392 checkin_info_.Reset(); | 391 checkin_info_.Reset(); |
393 } | 392 } |
394 | 393 |
395 void GCMProfileService::IOWorker::Register( | 394 void GCMProfileService::IOWorker::Register( |
396 const std::string& app_id, | 395 const std::string& app_id, |
397 const std::vector<std::string>& sender_ids, | 396 const std::vector<std::string>& sender_ids, |
398 const std::string& cert) { | 397 const std::string& cert) { |
399 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 398 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
400 DCHECK(!username_.empty() && checkin_info_.IsValid()); | 399 DCHECK(!username_.empty() && checkin_info_.IsValid()); |
401 | 400 |
402 GCMClient::Get()->Register(username_, app_id, cert, sender_ids); | 401 gcm_client_->Register(username_, app_id, cert, sender_ids); |
403 } | 402 } |
404 | 403 |
405 void GCMProfileService::IOWorker::Unregister(const std::string& app_id) { | 404 void GCMProfileService::IOWorker::Unregister(const std::string& app_id) { |
406 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 405 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
407 DCHECK(!username_.empty() && checkin_info_.IsValid()); | 406 DCHECK(!username_.empty() && checkin_info_.IsValid()); |
408 | 407 |
409 GCMClient::Get()->Unregister(username_, app_id); | 408 gcm_client_->Unregister(username_, app_id); |
410 } | 409 } |
411 | 410 |
412 void GCMProfileService::IOWorker::Send( | 411 void GCMProfileService::IOWorker::Send( |
413 const std::string& app_id, | 412 const std::string& app_id, |
414 const std::string& receiver_id, | 413 const std::string& receiver_id, |
415 const GCMClient::OutgoingMessage& message) { | 414 const GCMClient::OutgoingMessage& message) { |
416 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 415 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
417 DCHECK(!username_.empty() && checkin_info_.IsValid()); | 416 DCHECK(!username_.empty() && checkin_info_.IsValid()); |
418 | 417 |
419 GCMClient::Get()->Send(username_, app_id, receiver_id, message); | 418 gcm_client_->Send(username_, app_id, receiver_id, message); |
420 } | 419 } |
421 | 420 |
422 GCMProfileService::RegistrationInfo::RegistrationInfo() { | 421 GCMProfileService::RegistrationInfo::RegistrationInfo() { |
423 } | 422 } |
424 | 423 |
425 GCMProfileService::RegistrationInfo::~RegistrationInfo() { | 424 GCMProfileService::RegistrationInfo::~RegistrationInfo() { |
426 } | 425 } |
427 | 426 |
428 bool GCMProfileService::RegistrationInfo::IsValid() const { | 427 bool GCMProfileService::RegistrationInfo::IsValid() const { |
429 return !sender_ids.empty() && !registration_id.empty(); | 428 return !sender_ids.empty() && !registration_id.empty(); |
(...skipping 28 matching lines...) Expand all Loading... | |
458 prefs::kGCMUserToken, | 457 prefs::kGCMUserToken, |
459 "", | 458 "", |
460 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 459 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
461 } | 460 } |
462 | 461 |
463 GCMProfileService::GCMProfileService(Profile* profile) | 462 GCMProfileService::GCMProfileService(Profile* profile) |
464 : profile_(profile), | 463 : profile_(profile), |
465 testing_delegate_(NULL), | 464 testing_delegate_(NULL), |
466 weak_ptr_factory_(this) { | 465 weak_ptr_factory_(this) { |
467 DCHECK(!profile->IsOffTheRecord()); | 466 DCHECK(!profile->IsOffTheRecord()); |
467 | |
468 Init(); | 468 Init(); |
469 } | 469 } |
470 | 470 |
471 GCMProfileService::GCMProfileService(Profile* profile, | 471 GCMProfileService::GCMProfileService(Profile* profile, |
472 TestingDelegate* testing_delegate) | 472 TestingDelegate* testing_delegate) |
473 : profile_(profile), | 473 : profile_(profile), |
474 testing_delegate_(testing_delegate), | 474 testing_delegate_(testing_delegate), |
475 weak_ptr_factory_(this) { | 475 weak_ptr_factory_(this) { |
476 Init(); | 476 Init(); |
477 } | 477 } |
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
969 | 969 |
970 return true; | 970 return true; |
971 } | 971 } |
972 | 972 |
973 // static | 973 // static |
974 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { | 974 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { |
975 return kRegistrationKey; | 975 return kRegistrationKey; |
976 } | 976 } |
977 | 977 |
978 } // namespace gcm | 978 } // namespace gcm |
OLD | NEW |