Chromium Code Reviews| Index: components/ntp_snippets/breaking_news/content_suggestions_gcm_app_handler.cc |
| diff --git a/components/ntp_snippets/breaking_news/content_suggestions_gcm_app_handler.cc b/components/ntp_snippets/breaking_news/content_suggestions_gcm_app_handler.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e08622c0f7d2799d7cb1f7a8cb9271a1c330c29e |
| --- /dev/null |
| +++ b/components/ntp_snippets/breaking_news/content_suggestions_gcm_app_handler.cc |
| @@ -0,0 +1,146 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/ntp_snippets/breaking_news/content_suggestions_gcm_app_handler.h" |
| + |
| +#include "base/strings/string_util.h" |
| +#include "components/gcm_driver/gcm_driver.h" |
| +#include "components/gcm_driver/gcm_profile_service.h" |
| +#include "components/gcm_driver/instance_id/instance_id.h" |
| +#include "components/gcm_driver/instance_id/instance_id_driver.h" |
| +#include "components/ntp_snippets/pref_names.h" |
| + |
| +using instance_id::InstanceID; |
| + |
| +namespace ntp_snippets { |
| + |
| +const char kContentSuggestionsGCMAppID[] = "com.google.contentsuggestions.gcm"; |
| + |
| +// The sender ID is used in the registration process. |
| +// See: https://developers.google.com/cloud-messaging/gcm#senderid |
| +const char kContentSuggestionsGCMSenderId[] = "128223710667"; |
| + |
| +// OAuth2 Scope passed to getToken to obtain GCM registration tokens. |
| +// Must match Java GoogleCloudMessaging.INSTANCE_ID_SCOPE. |
| +const char kGCMScope[] = "GCM"; |
| + |
| +ContentSuggestionsGCMAppHandler::ContentSuggestionsGCMAppHandler( |
| + gcm::GCMDriver* gcm_driver, |
| + instance_id::InstanceIDDriver* instance_id_driver, |
| + PrefService* pref_service, |
| + std::unique_ptr<SubscriptionManager> subscription_manager) |
| + : gcm_driver_(gcm_driver), |
| + instance_id_driver_(instance_id_driver), |
| + pref_service_(pref_service), |
| + subscription_manager_(std::move(subscription_manager)), |
| + weak_factory_(this) { |
| +#if !defined(OS_ANDROID) |
|
sfiera
2017/06/07 11:34:38
"!"!?
mamir
2017/06/07 14:29:17
Done.
|
| + DependsOn(gcm::GCMProfileServiceFactory::GetInstance()); |
| + DependsOn(instance_id::InstanceIDProfileServiceFactory::GetInstance()); |
| +#endif |
| +} |
| +ContentSuggestionsGCMAppHandler::~ContentSuggestionsGCMAppHandler() { |
| + if (gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID) == this) { |
| + StopListening(); |
| + } |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::StartListening() { |
| +#if !defined(OS_ANDROID) |
| + NOTREACHED() |
| + << "The ContentSuggestionsGCMAppHandler should only be used on Android."; |
| +#endif |
| + Subscribe(); |
| + 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.
|
| + // GCM app handler already added. |
| + return; |
| + } |
| + gcm_driver_->AddAppHandler(kContentSuggestionsGCMAppID, this); |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::StopListening() { |
| + DCHECK_EQ(gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID), this); |
| + gcm_driver_->RemoveAppHandler(kContentSuggestionsGCMAppID); |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::Subscribe() { |
| + std::string token = pref_service_->GetString( |
| + ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); |
| + if (!token.empty()) { |
| + return; |
| + } |
| + |
| + instance_id_driver_->GetInstanceID(kContentSuggestionsGCMAppID) |
| + ->GetToken(kContentSuggestionsGCMSenderId, kGCMScope, |
| + std::map<std::string, std::string>() /* options */, |
| + base::Bind(&ContentSuggestionsGCMAppHandler::DidSubscribe, |
| + weak_factory_.GetWeakPtr())); |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::DidSubscribe( |
| + const std::string& subscription_id, |
| + InstanceID::Result result) { |
| + switch (result) { |
| + case InstanceID::SUCCESS: |
| + pref_service_->SetString( |
| + ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache, |
| + subscription_id); |
| + subscription_manager_->Subscribe(subscription_id); |
| + return; |
| + case InstanceID::INVALID_PARAMETER: |
| + case InstanceID::DISABLED: |
| + case InstanceID::ASYNC_OPERATION_PENDING: |
| + case InstanceID::SERVER_ERROR: |
| + case InstanceID::UNKNOWN_ERROR: |
| + DLOG(WARNING) |
| + << "Push messaging subscription failed; InstanceID::Result = " |
| + << result; |
| + break; |
| + case InstanceID::NETWORK_ERROR: |
| + break; |
| + } |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} |
| + |
| +void ContentSuggestionsGCMAppHandler::OnStoreReset() { |
| + pref_service_->ClearPref( |
| + ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::OnMessage( |
| + const std::string& app_id, |
| + const gcm::IncomingMessage& message) { |
| + // TODO(mamir): Implement Show notification and update the feed. |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( |
| + const std::string& app_id) { |
| + // Messages don't get deleted. |
| + NOTREACHED() << "ContentSuggestionsGCMAppHandler messages don't get deleted."; |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::OnSendError( |
| + const std::string& app_id, |
| + const gcm::GCMClient::SendErrorDetails& details) { |
| + // Should never be called because we don't send GCM messages to |
| + // the server. |
| + NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::OnSendAcknowledged( |
| + const std::string& app_id, |
| + const std::string& message_id) { |
| + // Should never be called because we don't send GCM messages to |
| + // the server. |
| + NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; |
| +} |
| + |
| +void ContentSuggestionsGCMAppHandler::RegisterProfilePrefs( |
| + PrefRegistrySimple* registry) { |
| + registry->RegisterStringPref( |
| + prefs::kContentSuggestionsGCMSubscriptionTokenCache, std::string()); |
| +} |
| + |
| +} // namespace ntp_snippets |