| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_snippets/breaking_news/content_suggestions_gcm_app_hand
ler.h" | 5 #include "components/ntp_snippets/breaking_news/content_suggestions_gcm_app_hand
ler.h" |
| 6 | 6 |
| 7 #include "base/strings/string_util.h" | 7 #include "base/strings/string_util.h" |
| 8 #include "components/gcm_driver/gcm_driver.h" | 8 #include "components/gcm_driver/gcm_driver.h" |
| 9 #include "components/gcm_driver/gcm_profile_service.h" | 9 #include "components/gcm_driver/gcm_profile_service.h" |
| 10 #include "components/gcm_driver/instance_id/instance_id.h" | 10 #include "components/gcm_driver/instance_id/instance_id.h" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 : gcm_driver_(gcm_driver), | 33 : gcm_driver_(gcm_driver), |
| 34 instance_id_driver_(instance_id_driver), | 34 instance_id_driver_(instance_id_driver), |
| 35 pref_service_(pref_service), | 35 pref_service_(pref_service), |
| 36 subscription_manager_(std::move(subscription_manager)), | 36 subscription_manager_(std::move(subscription_manager)), |
| 37 weak_factory_(this) {} | 37 weak_factory_(this) {} |
| 38 | 38 |
| 39 ContentSuggestionsGCMAppHandler::~ContentSuggestionsGCMAppHandler() { | 39 ContentSuggestionsGCMAppHandler::~ContentSuggestionsGCMAppHandler() { |
| 40 StopListening(); | 40 StopListening(); |
| 41 } | 41 } |
| 42 | 42 |
| 43 void ContentSuggestionsGCMAppHandler::StartListening() { | 43 void ContentSuggestionsGCMAppHandler::StartListening( |
| 44 OnNewContentCallback on_new_content_callback) { |
| 44 #if !defined(OS_ANDROID) | 45 #if !defined(OS_ANDROID) |
| 45 NOTREACHED() | 46 NOTREACHED() |
| 46 << "The ContentSuggestionsGCMAppHandler should only be used on Android."; | 47 << "The ContentSuggestionsGCMAppHandler should only be used on Android."; |
| 47 #endif | 48 #endif |
| 48 Subscribe(); | 49 Subscribe(); |
| 50 on_new_content_callback_ = std::move(on_new_content_callback); |
| 49 gcm_driver_->AddAppHandler(kContentSuggestionsGCMAppID, this); | 51 gcm_driver_->AddAppHandler(kContentSuggestionsGCMAppID, this); |
| 50 } | 52 } |
| 51 | 53 |
| 52 void ContentSuggestionsGCMAppHandler::StopListening() { | 54 void ContentSuggestionsGCMAppHandler::StopListening() { |
| 53 DCHECK_EQ(gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID), this); | 55 DCHECK_EQ(gcm_driver_->GetAppHandler(kContentSuggestionsGCMAppID), this); |
| 54 gcm_driver_->RemoveAppHandler(kContentSuggestionsGCMAppID); | 56 gcm_driver_->RemoveAppHandler(kContentSuggestionsGCMAppID); |
| 57 on_new_content_callback_ = OnNewContentCallback(); |
| 58 // TODO(mamir): Check which token should be used for unsubscription when |
| 59 // handling change in the token. |
| 55 std::string token = pref_service_->GetString( | 60 std::string token = pref_service_->GetString( |
| 56 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | 61 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); |
| 57 subscription_manager_->Unsubscribe(token); | 62 subscription_manager_->Unsubscribe(token); |
| 58 } | 63 } |
| 59 | 64 |
| 60 void ContentSuggestionsGCMAppHandler::Subscribe() { | 65 void ContentSuggestionsGCMAppHandler::Subscribe() { |
| 61 std::string token = pref_service_->GetString( | 66 std::string token = pref_service_->GetString( |
| 62 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | 67 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); |
| 63 // If a token has been already obtained, subscribe directly at the content | 68 // If a token has been already obtained, subscribe directly at the content |
| 64 // suggestions server. | 69 // suggestions server. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 103 void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} | 108 void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} |
| 104 | 109 |
| 105 void ContentSuggestionsGCMAppHandler::OnStoreReset() { | 110 void ContentSuggestionsGCMAppHandler::OnStoreReset() { |
| 106 pref_service_->ClearPref( | 111 pref_service_->ClearPref( |
| 107 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | 112 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); |
| 108 } | 113 } |
| 109 | 114 |
| 110 void ContentSuggestionsGCMAppHandler::OnMessage( | 115 void ContentSuggestionsGCMAppHandler::OnMessage( |
| 111 const std::string& app_id, | 116 const std::string& app_id, |
| 112 const gcm::IncomingMessage& message) { | 117 const gcm::IncomingMessage& message) { |
| 113 // TODO(mamir): Implement Show notification and update the feed. | 118 // TODO(mamir): Build the message. |
| 119 on_new_content_callback_.Run(base::Value()); |
| 114 } | 120 } |
| 115 | 121 |
| 116 void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( | 122 void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( |
| 117 const std::string& app_id) { | 123 const std::string& app_id) { |
| 118 // Messages don't get deleted. | 124 // Messages don't get deleted. |
| 119 NOTREACHED() << "ContentSuggestionsGCMAppHandler messages don't get deleted."; | 125 NOTREACHED() << "ContentSuggestionsGCMAppHandler messages don't get deleted."; |
| 120 } | 126 } |
| 121 | 127 |
| 122 void ContentSuggestionsGCMAppHandler::OnSendError( | 128 void ContentSuggestionsGCMAppHandler::OnSendError( |
| 123 const std::string& app_id, | 129 const std::string& app_id, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 135 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; | 141 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; |
| 136 } | 142 } |
| 137 | 143 |
| 138 void ContentSuggestionsGCMAppHandler::RegisterProfilePrefs( | 144 void ContentSuggestionsGCMAppHandler::RegisterProfilePrefs( |
| 139 PrefRegistrySimple* registry) { | 145 PrefRegistrySimple* registry) { |
| 140 registry->RegisterStringPref( | 146 registry->RegisterStringPref( |
| 141 prefs::kContentSuggestionsGCMSubscriptionTokenCache, std::string()); | 147 prefs::kContentSuggestionsGCMSubscriptionTokenCache, std::string()); |
| 142 } | 148 } |
| 143 | 149 |
| 144 } // namespace ntp_snippets | 150 } // namespace ntp_snippets |
| OLD | NEW |