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 | |
| 39 ContentSuggestionsGCMAppHandler::~ContentSuggestionsGCMAppHandler() { | |
| 40 StopListening(); | |
| 41 } | |
| 42 | |
| 43 void ContentSuggestionsGCMAppHandler::StartListening() { | |
| 44 #if !defined(OS_ANDROID) | |
| 45 NOTREACHED() | |
| 46 << "The ContentSuggestionsGCMAppHandler should only be used on Android."; | |
| 47 #endif | |
| 48 Subscribe(); | |
|
jkrcal
2017/06/09 09:10:10
For the sake of symmetry, maybe add something like
mamir
2017/06/09 13:52:58
This has been discussed before in this CL
tl;dr; A
| |
| 49 gcm_driver_->AddAppHandler(kContentSuggestionsGCMAppID, this); | |
| 50 } | |
| 51 | |
| 52 void ContentSuggestionsGCMAppHandler::StopListening() { | |
| 53 DCHECK_EQ(gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID), this); | |
| 54 gcm_driver_->RemoveAppHandler(kContentSuggestionsGCMAppID); | |
| 55 std::string token = pref_service_->GetString( | |
| 56 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | |
| 57 subscription_manager_->Unsubscribe(token); | |
| 58 } | |
| 59 | |
| 60 void ContentSuggestionsGCMAppHandler::Subscribe() { | |
| 61 std::string token = pref_service_->GetString( | |
| 62 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | |
| 63 if (!token.empty()) { | |
| 64 return; | |
|
jkrcal
2017/06/09 09:10:09
I am puzzled by the assymetry in the code.
When y
mamir
2017/06/09 13:52:58
Thank you for spotting this. You are absolutely ri
| |
| 65 } | |
| 66 | |
| 67 instance_id_driver_->GetInstanceID(kContentSuggestionsGCMAppID) | |
| 68 ->GetToken(kContentSuggestionsGCMSenderId, kGCMScope, | |
| 69 std::map<std::string, std::string>() /* options */, | |
| 70 base::Bind(&ContentSuggestionsGCMAppHandler::DidSubscribe, | |
| 71 weak_factory_.GetWeakPtr())); | |
| 72 } | |
| 73 | |
| 74 void ContentSuggestionsGCMAppHandler::DidSubscribe( | |
| 75 const std::string& subscription_id, | |
| 76 InstanceID::Result result) { | |
| 77 switch (result) { | |
| 78 case InstanceID::SUCCESS: | |
| 79 pref_service_->SetString( | |
| 80 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache, | |
| 81 subscription_id); | |
| 82 subscription_manager_->Subscribe(subscription_id); | |
| 83 return; | |
| 84 case InstanceID::INVALID_PARAMETER: | |
| 85 case InstanceID::DISABLED: | |
| 86 case InstanceID::ASYNC_OPERATION_PENDING: | |
| 87 case InstanceID::SERVER_ERROR: | |
| 88 case InstanceID::UNKNOWN_ERROR: | |
| 89 DLOG(WARNING) | |
| 90 << "Push messaging subscription failed; InstanceID::Result = " | |
| 91 << result; | |
| 92 break; | |
| 93 case InstanceID::NETWORK_ERROR: | |
| 94 break; | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} | |
| 99 | |
| 100 void ContentSuggestionsGCMAppHandler::OnStoreReset() { | |
| 101 pref_service_->ClearPref( | |
| 102 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | |
| 103 } | |
| 104 | |
| 105 void ContentSuggestionsGCMAppHandler::OnMessage( | |
| 106 const std::string& app_id, | |
| 107 const gcm::IncomingMessage& message) { | |
| 108 // TODO(mamir): Implement Show notification and update the feed. | |
| 109 } | |
| 110 | |
| 111 void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( | |
| 112 const std::string& app_id) { | |
| 113 // Messages don't get deleted. | |
| 114 NOTREACHED() << "ContentSuggestionsGCMAppHandler messages don't get deleted."; | |
| 115 } | |
| 116 | |
| 117 void ContentSuggestionsGCMAppHandler::OnSendError( | |
| 118 const std::string& app_id, | |
| 119 const gcm::GCMClient::SendErrorDetails& details) { | |
| 120 // Should never be called because we don't send GCM messages to | |
| 121 // the server. | |
| 122 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; | |
| 123 } | |
| 124 | |
| 125 void ContentSuggestionsGCMAppHandler::OnSendAcknowledged( | |
| 126 const std::string& app_id, | |
| 127 const std::string& message_id) { | |
| 128 // Should never be called because we don't send GCM messages to | |
| 129 // the server. | |
| 130 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; | |
| 131 } | |
| 132 | |
| 133 void ContentSuggestionsGCMAppHandler::RegisterProfilePrefs( | |
| 134 PrefRegistrySimple* registry) { | |
| 135 registry->RegisterStringPref( | |
| 136 prefs::kContentSuggestionsGCMSubscriptionTokenCache, std::string()); | |
| 137 } | |
| 138 | |
| 139 } // namespace ntp_snippets | |
| OLD | NEW |