Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(224)

Unified Diff: components/ntp_snippets/breaking_news/content_suggestions_gcm_app_handler.cc

Issue 2922543002: [NTP::Push] Add Content Suggestions GCM App Handler (Closed)
Patch Set: Add missing dependency. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..4ea1acc6dc57e9d54fd7c4be42369da963800646
--- /dev/null
+++ b/components/ntp_snippets/breaking_news/content_suggestions_gcm_app_handler.cc
@@ -0,0 +1,144 @@
+// 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) {}
+
+ContentSuggestionsGCMAppHandler::~ContentSuggestionsGCMAppHandler() {
+ StopListening();
+}
+
+void ContentSuggestionsGCMAppHandler::StartListening() {
+#if !defined(OS_ANDROID)
+ NOTREACHED()
+ << "The ContentSuggestionsGCMAppHandler should only be used on Android.";
+#endif
+ Subscribe();
+ gcm_driver_->AddAppHandler(kContentSuggestionsGCMAppID, this);
+}
+
+void ContentSuggestionsGCMAppHandler::StopListening() {
+ DCHECK_EQ(gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID), this);
+ gcm_driver_->RemoveAppHandler(kContentSuggestionsGCMAppID);
+ std::string token = pref_service_->GetString(
+ ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache);
+ subscription_manager_->Unsubscribe(token);
+}
+
+void ContentSuggestionsGCMAppHandler::Subscribe() {
+ std::string token = pref_service_->GetString(
+ ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache);
+ // If a token has been already obtained, subscribe directly at the content
+ // suggestions server.
+ if (!token.empty()) {
+ if (!subscription_manager_->IsSubscribed()) {
+ subscription_manager_->Subscribe(token);
+ }
+ 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

Powered by Google App Engine
This is Rietveld 408576698