Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/ntp_snippets/breaking_news/content_suggestions_gcm_app_hand ler.h" | |
| 6 | |
| 7 #include "base/strings/string_util.h" | |
| 8 #include "components/gcm_driver/gcm_driver.h" | |
| 9 #include "components/gcm_driver/gcm_profile_service.h" | |
| 10 #include "components/gcm_driver/instance_id/instance_id.h" | |
| 11 #include "components/gcm_driver/instance_id/instance_id_driver.h" | |
| 12 #include "components/ntp_snippets/pref_names.h" | |
| 13 | |
| 14 using instance_id::InstanceID; | |
| 15 | |
| 16 namespace ntp_snippets { | |
| 17 | |
| 18 const char kContentSuggestionsGCMAppID[] = "com.google.contentsuggestions.gcm"; | |
| 19 | |
| 20 // The sender ID is used in the registration process. | |
| 21 // See: https://developers.google.com/cloud-messaging/gcm#senderid | |
| 22 const char kContentSuggestionsGCMSenderId[] = "128223710667"; | |
| 23 | |
| 24 // OAuth2 Scope passed to getToken to obtain GCM registration tokens. | |
| 25 // Must match Java GoogleCloudMessaging.INSTANCE_ID_SCOPE. | |
| 26 const char kGCMScope[] = "GCM"; | |
| 27 | |
| 28 ContentSuggestionsGCMAppHandler::ContentSuggestionsGCMAppHandler( | |
| 29 gcm::GCMDriver* gcm_driver, | |
| 30 instance_id::InstanceIDDriver* instance_id_driver, | |
| 31 PrefService* pref_service, | |
| 32 std::unique_ptr<SubscriptionManager> subscription_manager) | |
| 33 : gcm_driver_(gcm_driver), | |
| 34 instance_id_driver_(instance_id_driver), | |
| 35 pref_service_(pref_service), | |
| 36 subscription_manager_(std::move(subscription_manager)), | |
| 37 weak_factory_(this) { | |
| 38 #if !defined(OS_ANDROID) | |
|
sfiera
2017/06/07 11:34:38
"!"!?
mamir
2017/06/07 14:29:17
Done.
| |
| 39 DependsOn(gcm::GCMProfileServiceFactory::GetInstance()); | |
| 40 DependsOn(instance_id::InstanceIDProfileServiceFactory::GetInstance()); | |
| 41 #endif | |
| 42 } | |
| 43 ContentSuggestionsGCMAppHandler::~ContentSuggestionsGCMAppHandler() { | |
| 44 if (gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID) == this) { | |
| 45 StopListening(); | |
| 46 } | |
| 47 } | |
| 48 | |
| 49 void ContentSuggestionsGCMAppHandler::StartListening() { | |
| 50 #if !defined(OS_ANDROID) | |
| 51 NOTREACHED() | |
| 52 << "The ContentSuggestionsGCMAppHandler should only be used on Android."; | |
| 53 #endif | |
| 54 Subscribe(); | |
| 55 if (gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID) == this) { | |
|
sfiera
2017/06/07 11:34:38
The docs say that StartListening() must not be cal
mamir
2017/06/07 14:29:17
Done.
| |
| 56 // GCM app handler already added. | |
| 57 return; | |
| 58 } | |
| 59 gcm_driver_->AddAppHandler(kContentSuggestionsGCMAppID, this); | |
| 60 } | |
| 61 | |
| 62 void ContentSuggestionsGCMAppHandler::StopListening() { | |
| 63 DCHECK_EQ(gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID), this); | |
| 64 gcm_driver_->RemoveAppHandler(kContentSuggestionsGCMAppID); | |
| 65 } | |
| 66 | |
| 67 void ContentSuggestionsGCMAppHandler::Subscribe() { | |
| 68 std::string token = pref_service_->GetString( | |
| 69 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | |
| 70 if (!token.empty()) { | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 instance_id_driver_->GetInstanceID(kContentSuggestionsGCMAppID) | |
| 75 ->GetToken(kContentSuggestionsGCMSenderId, kGCMScope, | |
| 76 std::map<std::string, std::string>() /* options */, | |
| 77 base::Bind(&ContentSuggestionsGCMAppHandler::DidSubscribe, | |
| 78 weak_factory_.GetWeakPtr())); | |
| 79 } | |
| 80 | |
| 81 void ContentSuggestionsGCMAppHandler::DidSubscribe( | |
| 82 const std::string& subscription_id, | |
| 83 InstanceID::Result result) { | |
| 84 switch (result) { | |
| 85 case InstanceID::SUCCESS: | |
| 86 pref_service_->SetString( | |
| 87 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache, | |
| 88 subscription_id); | |
| 89 subscription_manager_->Subscribe(subscription_id); | |
| 90 return; | |
| 91 case InstanceID::INVALID_PARAMETER: | |
| 92 case InstanceID::DISABLED: | |
| 93 case InstanceID::ASYNC_OPERATION_PENDING: | |
| 94 case InstanceID::SERVER_ERROR: | |
| 95 case InstanceID::UNKNOWN_ERROR: | |
| 96 DLOG(WARNING) | |
| 97 << "Push messaging subscription failed; InstanceID::Result = " | |
| 98 << result; | |
| 99 break; | |
| 100 case InstanceID::NETWORK_ERROR: | |
| 101 break; | |
| 102 } | |
| 103 } | |
| 104 | |
| 105 void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} | |
| 106 | |
| 107 void ContentSuggestionsGCMAppHandler::OnStoreReset() { | |
| 108 pref_service_->ClearPref( | |
| 109 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | |
| 110 } | |
| 111 | |
| 112 void ContentSuggestionsGCMAppHandler::OnMessage( | |
| 113 const std::string& app_id, | |
| 114 const gcm::IncomingMessage& message) { | |
| 115 // TODO(mamir): Implement Show notification and update the feed. | |
| 116 } | |
| 117 | |
| 118 void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( | |
| 119 const std::string& app_id) { | |
| 120 // Messages don't get deleted. | |
| 121 NOTREACHED() << "ContentSuggestionsGCMAppHandler messages don't get deleted."; | |
| 122 } | |
| 123 | |
| 124 void ContentSuggestionsGCMAppHandler::OnSendError( | |
| 125 const std::string& app_id, | |
| 126 const gcm::GCMClient::SendErrorDetails& details) { | |
| 127 // Should never be called because we don't send GCM messages to | |
| 128 // the server. | |
| 129 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; | |
| 130 } | |
| 131 | |
| 132 void ContentSuggestionsGCMAppHandler::OnSendAcknowledged( | |
| 133 const std::string& app_id, | |
| 134 const std::string& message_id) { | |
| 135 // Should never be called because we don't send GCM messages to | |
| 136 // the server. | |
| 137 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; | |
| 138 } | |
| 139 | |
| 140 void ContentSuggestionsGCMAppHandler::RegisterProfilePrefs( | |
| 141 PrefRegistrySimple* registry) { | |
| 142 registry->RegisterStringPref( | |
| 143 prefs::kContentSuggestionsGCMSubscriptionTokenCache, std::string()); | |
| 144 } | |
| 145 | |
| 146 } // namespace ntp_snippets | |
| OLD | NEW |