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(); |
| 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 a token has been already obtained, subscribe directly at the content |
| 64 // suggestions server. |
| 65 if (!token.empty()) { |
| 66 if (!subscription_manager_->IsSubscribed()) { |
| 67 subscription_manager_->Subscribe(token); |
| 68 } |
| 69 return; |
| 70 } |
| 71 |
| 72 instance_id_driver_->GetInstanceID(kContentSuggestionsGCMAppID) |
| 73 ->GetToken(kContentSuggestionsGCMSenderId, kGCMScope, |
| 74 std::map<std::string, std::string>() /* options */, |
| 75 base::Bind(&ContentSuggestionsGCMAppHandler::DidSubscribe, |
| 76 weak_factory_.GetWeakPtr())); |
| 77 } |
| 78 |
| 79 void ContentSuggestionsGCMAppHandler::DidSubscribe( |
| 80 const std::string& subscription_id, |
| 81 InstanceID::Result result) { |
| 82 switch (result) { |
| 83 case InstanceID::SUCCESS: |
| 84 pref_service_->SetString( |
| 85 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache, |
| 86 subscription_id); |
| 87 subscription_manager_->Subscribe(subscription_id); |
| 88 return; |
| 89 case InstanceID::INVALID_PARAMETER: |
| 90 case InstanceID::DISABLED: |
| 91 case InstanceID::ASYNC_OPERATION_PENDING: |
| 92 case InstanceID::SERVER_ERROR: |
| 93 case InstanceID::UNKNOWN_ERROR: |
| 94 DLOG(WARNING) |
| 95 << "Push messaging subscription failed; InstanceID::Result = " |
| 96 << result; |
| 97 break; |
| 98 case InstanceID::NETWORK_ERROR: |
| 99 break; |
| 100 } |
| 101 } |
| 102 |
| 103 void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} |
| 104 |
| 105 void ContentSuggestionsGCMAppHandler::OnStoreReset() { |
| 106 pref_service_->ClearPref( |
| 107 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); |
| 108 } |
| 109 |
| 110 void ContentSuggestionsGCMAppHandler::OnMessage( |
| 111 const std::string& app_id, |
| 112 const gcm::IncomingMessage& message) { |
| 113 // TODO(mamir): Implement Show notification and update the feed. |
| 114 } |
| 115 |
| 116 void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( |
| 117 const std::string& app_id) { |
| 118 // Messages don't get deleted. |
| 119 NOTREACHED() << "ContentSuggestionsGCMAppHandler messages don't get deleted."; |
| 120 } |
| 121 |
| 122 void ContentSuggestionsGCMAppHandler::OnSendError( |
| 123 const std::string& app_id, |
| 124 const gcm::GCMClient::SendErrorDetails& details) { |
| 125 // Should never be called because we don't send GCM messages to |
| 126 // the server. |
| 127 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; |
| 128 } |
| 129 |
| 130 void ContentSuggestionsGCMAppHandler::OnSendAcknowledged( |
| 131 const std::string& app_id, |
| 132 const std::string& message_id) { |
| 133 // Should never be called because we don't send GCM messages to |
| 134 // the server. |
| 135 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; |
| 136 } |
| 137 |
| 138 void ContentSuggestionsGCMAppHandler::RegisterProfilePrefs( |
| 139 PrefRegistrySimple* registry) { |
| 140 registry->RegisterStringPref( |
| 141 prefs::kContentSuggestionsGCMSubscriptionTokenCache, std::string()); |
| 142 } |
| 143 |
| 144 } // namespace ntp_snippets |
OLD | NEW |