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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/base64.h" | 10 #include "base/base64.h" |
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 void Reset(); | 266 void Reset(); |
267 void Load(const base::WeakPtr<GCMProfileService>& service); | 267 void Load(const base::WeakPtr<GCMProfileService>& service); |
268 void CheckOut(); | 268 void CheckOut(); |
269 void Register(const std::string& app_id, | 269 void Register(const std::string& app_id, |
270 const std::vector<std::string>& sender_ids, | 270 const std::vector<std::string>& sender_ids, |
271 const std::string& cert); | 271 const std::string& cert); |
272 void Unregister(const std::string& app_id); | 272 void Unregister(const std::string& app_id); |
273 void Send(const std::string& app_id, | 273 void Send(const std::string& app_id, |
274 const std::string& receiver_id, | 274 const std::string& receiver_id, |
275 const GCMClient::OutgoingMessage& message); | 275 const GCMClient::OutgoingMessage& message); |
| 276 void RequestGCMStatistics(); |
276 | 277 |
277 // For testing purpose. Can be called from UI thread. Use with care. | 278 // For testing purpose. Can be called from UI thread. Use with care. |
278 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } | 279 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } |
279 | 280 |
280 private: | 281 private: |
281 friend class base::RefCountedThreadSafe<IOWorker>; | 282 friend class base::RefCountedThreadSafe<IOWorker>; |
282 virtual ~IOWorker(); | 283 virtual ~IOWorker(); |
283 | 284 |
284 base::WeakPtr<GCMProfileService> service_; | 285 base::WeakPtr<GCMProfileService> service_; |
285 | 286 |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
452 | 453 |
453 void GCMProfileService::IOWorker::Send( | 454 void GCMProfileService::IOWorker::Send( |
454 const std::string& app_id, | 455 const std::string& app_id, |
455 const std::string& receiver_id, | 456 const std::string& receiver_id, |
456 const GCMClient::OutgoingMessage& message) { | 457 const GCMClient::OutgoingMessage& message) { |
457 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 458 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
458 | 459 |
459 gcm_client_->Send(app_id, receiver_id, message); | 460 gcm_client_->Send(app_id, receiver_id, message); |
460 } | 461 } |
461 | 462 |
| 463 void GCMProfileService::IOWorker::RequestGCMStatistics() { |
| 464 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
| 465 gcm::GCMClient::GCMStatistics stats; |
| 466 |
| 467 if (gcm_client_.get()) { |
| 468 stats.gcm_client_created = true; |
| 469 stats = gcm_client_->GetStatistics(); |
| 470 } |
| 471 |
| 472 content::BrowserThread::PostTask( |
| 473 content::BrowserThread::UI, |
| 474 FROM_HERE, |
| 475 base::Bind(&GCMProfileService::RequestGCMStatisticsFinished, |
| 476 service_, |
| 477 stats)); |
| 478 } |
| 479 |
| 480 std::string GCMProfileService::GetGCMEnabledStateString(GCMEnabledState state) { |
| 481 switch (state) { |
| 482 case GCMProfileService::ALWAYS_ENABLED: |
| 483 return "ALWAYS_ENABLED"; |
| 484 case GCMProfileService::ENABLED_FOR_APPS: |
| 485 return "ENABLED_FOR_APPS"; |
| 486 case GCMProfileService::ALWAYS_DISABLED: |
| 487 return "ALWAYS_DISABLED"; |
| 488 default: |
| 489 NOTREACHED(); |
| 490 return std::string(); |
| 491 } |
| 492 } |
| 493 |
462 GCMProfileService::RegistrationInfo::RegistrationInfo() { | 494 GCMProfileService::RegistrationInfo::RegistrationInfo() { |
463 } | 495 } |
464 | 496 |
465 GCMProfileService::RegistrationInfo::~RegistrationInfo() { | 497 GCMProfileService::RegistrationInfo::~RegistrationInfo() { |
466 } | 498 } |
467 | 499 |
468 bool GCMProfileService::RegistrationInfo::IsValid() const { | 500 bool GCMProfileService::RegistrationInfo::IsValid() const { |
469 return !sender_ids.empty() && !registration_id.empty(); | 501 return !sender_ids.empty() && !registration_id.empty(); |
470 } | 502 } |
471 | 503 |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 io_worker_, | 730 io_worker_, |
699 app_id, | 731 app_id, |
700 receiver_id, | 732 receiver_id, |
701 message)); | 733 message)); |
702 } | 734 } |
703 | 735 |
704 GCMClient* GCMProfileService::GetGCMClientForTesting() const { | 736 GCMClient* GCMProfileService::GetGCMClientForTesting() const { |
705 return io_worker_ ? io_worker_->gcm_client_for_testing() : NULL; | 737 return io_worker_ ? io_worker_->gcm_client_for_testing() : NULL; |
706 } | 738 } |
707 | 739 |
| 740 std::string GCMProfileService::SignedInUserName() const { |
| 741 return username_; |
| 742 } |
| 743 |
| 744 bool GCMProfileService::IsGCMClientReady() const { |
| 745 return gcm_client_ready_; |
| 746 } |
| 747 |
| 748 void GCMProfileService::RequestGCMStatistics( |
| 749 RequestGCMStatisticsCallback callback) { |
| 750 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 751 DCHECK(!callback.is_null()); |
| 752 |
| 753 request_gcm_statistics_callback_ = callback; |
| 754 content::BrowserThread::PostTask( |
| 755 content::BrowserThread::IO, |
| 756 FROM_HERE, |
| 757 base::Bind(&GCMProfileService::IOWorker::RequestGCMStatistics, |
| 758 io_worker_)); |
| 759 } |
| 760 |
708 void GCMProfileService::Observe(int type, | 761 void GCMProfileService::Observe(int type, |
709 const content::NotificationSource& source, | 762 const content::NotificationSource& source, |
710 const content::NotificationDetails& details) { | 763 const content::NotificationDetails& details) { |
711 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 764 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
712 | 765 |
713 switch (type) { | 766 switch (type) { |
714 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { | 767 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { |
715 if (GetGCMEnabledState(profile_) == ALWAYS_ENABLED) | 768 if (GetGCMEnabledState(profile_) == ALWAYS_ENABLED) |
716 EnsureLoaded(); | 769 EnsureLoaded(); |
717 break; | 770 break; |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1063 for (size_t i = 0; i < senders_list->GetSize(); ++i) { | 1116 for (size_t i = 0; i < senders_list->GetSize(); ++i) { |
1064 std::string sender; | 1117 std::string sender; |
1065 if (!senders_list->GetString(i, &sender)) | 1118 if (!senders_list->GetString(i, &sender)) |
1066 return false; | 1119 return false; |
1067 registration_info->sender_ids.push_back(sender); | 1120 registration_info->sender_ids.push_back(sender); |
1068 } | 1121 } |
1069 | 1122 |
1070 return true; | 1123 return true; |
1071 } | 1124 } |
1072 | 1125 |
| 1126 void GCMProfileService::RequestGCMStatisticsFinished( |
| 1127 GCMClient::GCMStatistics stats) { |
| 1128 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
| 1129 |
| 1130 request_gcm_statistics_callback_.Run(stats); |
| 1131 } |
| 1132 |
1073 // static | 1133 // static |
1074 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { | 1134 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { |
1075 return kRegistrationKey; | 1135 return kRegistrationKey; |
1076 } | 1136 } |
1077 | 1137 |
1078 } // namespace gcm | 1138 } // namespace gcm |
OLD | NEW |