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..58c216e7d9b111b3fb93734361183d3bfbdc1d89 |
--- /dev/null |
+++ b/components/ntp_snippets/breaking_news/content_suggestions_gcm_app_handler.cc |
@@ -0,0 +1,109 @@ |
+// 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 "content/public/common/push_messaging_status.h" |
+ |
+using instance_id::InstanceID; |
+ |
+namespace ntp_snippets { |
+ |
+// Our sender ID we send up with all of our GCM messages. |
sfiera
2017/06/02 08:43:05
This doesn't tell me a lot more than the variable
mamir
2017/06/02 13:13:56
Done.
|
+const char* kContentSuggestionsGCMAppID = "com.google.contentsuggestions.gcm"; |
+const char* kContentSuggestionsGCMSenderId = "128223710667"; |
+ |
+// Scope passed to getToken to obtain GCM registration tokens. |
sfiera
2017/06/02 08:43:04
// OAuth2 scope…
mamir
2017/06/02 13:13:57
Done.
|
+// Must match Java GoogleCloudMessaging.INSTANCE_ID_SCOPE. |
+const char kGCMScope[] = "GCM"; |
+ |
+ContentSuggestionsGCMAppHandler::ContentSuggestionsGCMAppHandler( |
+ gcm::GCMDriver* gcm_driver, |
+ std::unique_ptr<BreakingNewsSubscriptionManager> subscription_manager) |
+ : gcm_driver_(gcm_driver), |
+ subscription_manager_(std::move(subscription_manager)), |
+ weak_factory_(this) { |
+ if (gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID) == this) { |
sfiera
2017/06/02 08:43:05
I'm not sure how this is possible. We are in the c
mamir
2017/06/02 13:13:57
Acknowledged.
|
+ // GCM app handler already added. |
+ return; |
+ } |
+ |
+ gcm_driver_->AddAppHandler(kContentSuggestionsGCMAppID, this); |
+} |
+ |
+void ContentSuggestionsGCMAppHandler::DoSubscribe( |
+ instance_id::InstanceIDDriver* instance_id_driver) { |
+ instance_id_driver->GetInstanceID(kContentSuggestionsGCMAppID) |
+ ->GetToken(kContentSuggestionsGCMSenderId, kGCMScope, |
+ std::map<std::string, std::string>() /* options */, |
+ base::Bind(&ContentSuggestionsGCMAppHandler::DidSubscribe, |
+ weak_factory_.GetWeakPtr())); |
+} |
+ |
+void ContentSuggestionsGCMAppHandler::Shutdown() { |
sfiera
2017/06/02 08:43:05
Can this object be destroyed without being Shutdow
mamir
2017/06/02 13:13:57
Done.
|
+ gcm_driver_->RemoveAppHandler(kContentSuggestionsGCMAppID); |
+} |
+ |
+void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} |
+ |
+void ContentSuggestionsGCMAppHandler::OnStoreReset() {} |
+ |
+void ContentSuggestionsGCMAppHandler::OnMessage( |
+ const std::string& app_id, |
+ const gcm::IncomingMessage& message) { |
+ // TODO(mamir): Implement Show notification and update the feed. |
sfiera
2017/06/02 08:43:04
Add a DVLOG(1) for now?
mamir
2017/06/02 13:13:57
Done.
|
+} |
+ |
+void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( |
+ const std::string& app_id) { |
+ // 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. |
+} |
+ |
+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. |
+} |
+ |
+void ContentSuggestionsGCMAppHandler::DidSubscribe( |
+ const std::string& subscription_id, |
+ InstanceID::Result result) { |
+ content::PushRegistrationStatus status = |
+ content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
+ |
+ switch (result) { |
+ case InstanceID::SUCCESS: |
+ // TODO (mamir): store the subscription_id in prefs. |
+ 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; |
+ status = content::PUSH_REGISTRATION_STATUS_SERVICE_ERROR; |
+ break; |
+ case InstanceID::NETWORK_ERROR: |
+ status = content::PUSH_REGISTRATION_STATUS_NETWORK_ERROR; |
+ break; |
+ } |
+ DLOG(WARNING) << "Status = " << status; |
+} |
+} // namespace ntp_snippets |