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 18 matching lines...) Expand all Loading... | |
29 #include "chrome/common/chrome_constants.h" | 29 #include "chrome/common/chrome_constants.h" |
30 #include "chrome/common/chrome_paths.h" | 30 #include "chrome/common/chrome_paths.h" |
31 #include "chrome/common/chrome_version_info.h" | 31 #include "chrome/common/chrome_version_info.h" |
32 #include "chrome/common/pref_names.h" | 32 #include "chrome/common/pref_names.h" |
33 #include "components/user_prefs/pref_registry_syncable.h" | 33 #include "components/user_prefs/pref_registry_syncable.h" |
34 #include "content/public/browser/browser_thread.h" | 34 #include "content/public/browser/browser_thread.h" |
35 #include "content/public/browser/notification_details.h" | 35 #include "content/public/browser/notification_details.h" |
36 #include "content/public/browser/notification_source.h" | 36 #include "content/public/browser/notification_source.h" |
37 #include "extensions/browser/extension_system.h" | 37 #include "extensions/browser/extension_system.h" |
38 #include "extensions/common/extension.h" | 38 #include "extensions/common/extension.h" |
39 #include "google_apis/gcm/gcm_client.h" | |
jianli
2014/03/04 23:47:39
nit: not needed
juyik
2014/03/05 05:08:08
Done.
| |
39 #include "google_apis/gcm/protocol/android_checkin.pb.h" | 40 #include "google_apis/gcm/protocol/android_checkin.pb.h" |
40 #include "net/url_request/url_request_context_getter.h" | 41 #include "net/url_request/url_request_context_getter.h" |
41 | 42 |
42 using extensions::Extension; | 43 using extensions::Extension; |
43 | 44 |
44 namespace gcm { | 45 namespace gcm { |
45 | 46 |
46 namespace { | 47 namespace { |
47 | 48 |
48 const char kRegistrationKey[] = "gcm.registration"; | 49 const char kRegistrationKey[] = "gcm.registration"; |
(...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
266 void Reset(); | 267 void Reset(); |
267 void Load(const base::WeakPtr<GCMProfileService>& service); | 268 void Load(const base::WeakPtr<GCMProfileService>& service); |
268 void CheckOut(); | 269 void CheckOut(); |
269 void Register(const std::string& app_id, | 270 void Register(const std::string& app_id, |
270 const std::vector<std::string>& sender_ids, | 271 const std::vector<std::string>& sender_ids, |
271 const std::string& cert); | 272 const std::string& cert); |
272 void Unregister(const std::string& app_id); | 273 void Unregister(const std::string& app_id); |
273 void Send(const std::string& app_id, | 274 void Send(const std::string& app_id, |
274 const std::string& receiver_id, | 275 const std::string& receiver_id, |
275 const GCMClient::OutgoingMessage& message); | 276 const GCMClient::OutgoingMessage& message); |
277 bool IsGCMClientCreated(); | |
278 void RequestGCMStatistics(); | |
276 | 279 |
277 // For testing purpose. Can be called from UI thread. Use with care. | 280 // For testing purpose. Can be called from UI thread. Use with care. |
278 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } | 281 GCMClient* gcm_client_for_testing() const { return gcm_client_.get(); } |
279 | 282 |
280 private: | 283 private: |
281 friend class base::RefCountedThreadSafe<IOWorker>; | 284 friend class base::RefCountedThreadSafe<IOWorker>; |
282 virtual ~IOWorker(); | 285 virtual ~IOWorker(); |
283 | 286 |
284 base::WeakPtr<GCMProfileService> service_; | 287 base::WeakPtr<GCMProfileService> service_; |
285 | 288 |
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
452 | 455 |
453 void GCMProfileService::IOWorker::Send( | 456 void GCMProfileService::IOWorker::Send( |
454 const std::string& app_id, | 457 const std::string& app_id, |
455 const std::string& receiver_id, | 458 const std::string& receiver_id, |
456 const GCMClient::OutgoingMessage& message) { | 459 const GCMClient::OutgoingMessage& message) { |
457 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | 460 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); |
458 | 461 |
459 gcm_client_->Send(app_id, receiver_id, message); | 462 gcm_client_->Send(app_id, receiver_id, message); |
460 } | 463 } |
461 | 464 |
465 bool GCMProfileService::IOWorker::IsGCMClientCreated() { | |
466 return (gcm_client_.get() != NULL); | |
467 } | |
468 | |
469 void GCMProfileService::IOWorker::RequestGCMStatistics() { | |
470 gcm::GCMClient::GCMStatistics stats; | |
471 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)); | |
472 | |
473 if (gcm_client_.get()) { | |
474 stats = gcm_client_->GetStatistics(); | |
475 } | |
476 stats.gcm_client_created = (gcm_client_.get() != NULL); | |
477 | |
478 content::BrowserThread::PostTask( | |
479 content::BrowserThread::UI, | |
480 FROM_HERE, | |
481 base::Bind(&GCMProfileService::RequestGCMStatisticsFinished, | |
482 service_, | |
483 stats)); | |
484 } | |
485 | |
486 std::string GCMProfileService::GetGCMEnabledStateString(GCMEnabledState state) { | |
487 switch (state) { | |
488 case GCMProfileService::ALWAYS_ENABLED: | |
489 return "ALWAYS_ENABLED"; | |
490 case GCMProfileService::ENABLED_FOR_APPS: | |
491 return "ENABLED_FOR_APPS"; | |
492 case GCMProfileService::ALWAYS_DISABLED: | |
493 return "ALWAYS_DISABLED"; | |
494 } | |
495 return std::string(); | |
496 } | |
497 | |
462 GCMProfileService::RegistrationInfo::RegistrationInfo() { | 498 GCMProfileService::RegistrationInfo::RegistrationInfo() { |
463 } | 499 } |
464 | 500 |
465 GCMProfileService::RegistrationInfo::~RegistrationInfo() { | 501 GCMProfileService::RegistrationInfo::~RegistrationInfo() { |
466 } | 502 } |
467 | 503 |
468 bool GCMProfileService::RegistrationInfo::IsValid() const { | 504 bool GCMProfileService::RegistrationInfo::IsValid() const { |
469 return !sender_ids.empty() && !registration_id.empty(); | 505 return !sender_ids.empty() && !registration_id.empty(); |
470 } | 506 } |
471 | 507 |
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
698 io_worker_, | 734 io_worker_, |
699 app_id, | 735 app_id, |
700 receiver_id, | 736 receiver_id, |
701 message)); | 737 message)); |
702 } | 738 } |
703 | 739 |
704 GCMClient* GCMProfileService::GetGCMClientForTesting() const { | 740 GCMClient* GCMProfileService::GetGCMClientForTesting() const { |
705 return io_worker_ ? io_worker_->gcm_client_for_testing() : NULL; | 741 return io_worker_ ? io_worker_->gcm_client_for_testing() : NULL; |
706 } | 742 } |
707 | 743 |
744 bool GCMProfileService::IsSignedIn() const { | |
745 return !username_.empty(); | |
746 } | |
747 | |
748 bool GCMProfileService::IsGCMClientCreated() const { | |
749 return io_worker_->IsGCMClientCreated(); | |
jianli
2014/03/04 23:47:39
This is not thread safe. You should not try to acc
juyik
2014/03/05 05:08:08
Done. I forgot to delete this method.
| |
750 } | |
751 | |
752 bool GCMProfileService::IsGCMClientReady() const { | |
753 return gcm_client_ready_; | |
754 } | |
755 | |
756 void GCMProfileService::RequestGCMStatistics( | |
757 RequestGCMStatisticsCallback callback) { | |
758 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
759 DCHECK(!callback.is_null()); | |
760 | |
761 request_gcm_statistics_callback_ = callback; | |
762 content::BrowserThread::PostTask( | |
763 content::BrowserThread::IO, | |
764 FROM_HERE, | |
765 base::Bind(&GCMProfileService::IOWorker::RequestGCMStatistics, | |
766 io_worker_)); | |
767 } | |
768 | |
708 void GCMProfileService::Observe(int type, | 769 void GCMProfileService::Observe(int type, |
709 const content::NotificationSource& source, | 770 const content::NotificationSource& source, |
710 const content::NotificationDetails& details) { | 771 const content::NotificationDetails& details) { |
711 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | 772 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); |
712 | 773 |
713 switch (type) { | 774 switch (type) { |
714 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { | 775 case chrome::NOTIFICATION_GOOGLE_SIGNIN_SUCCESSFUL: { |
715 if (GetGCMEnabledState(profile_) == ALWAYS_ENABLED) | 776 if (GetGCMEnabledState(profile_) == ALWAYS_ENABLED) |
716 EnsureLoaded(); | 777 EnsureLoaded(); |
717 break; | 778 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) { | 1124 for (size_t i = 0; i < senders_list->GetSize(); ++i) { |
1064 std::string sender; | 1125 std::string sender; |
1065 if (!senders_list->GetString(i, &sender)) | 1126 if (!senders_list->GetString(i, &sender)) |
1066 return false; | 1127 return false; |
1067 registration_info->sender_ids.push_back(sender); | 1128 registration_info->sender_ids.push_back(sender); |
1068 } | 1129 } |
1069 | 1130 |
1070 return true; | 1131 return true; |
1071 } | 1132 } |
1072 | 1133 |
1134 void GCMProfileService::RequestGCMStatisticsFinished( | |
1135 GCMClient::GCMStatistics stats) { | |
1136 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); | |
1137 | |
1138 request_gcm_statistics_callback_.Run(stats); | |
1139 } | |
1140 | |
1073 // static | 1141 // static |
1074 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { | 1142 const char* GCMProfileService::GetPersistentRegisterKeyForTesting() { |
1075 return kRegistrationKey; | 1143 return kRegistrationKey; |
1076 } | 1144 } |
1077 | 1145 |
1078 } // namespace gcm | 1146 } // namespace gcm |
OLD | NEW |