 Chromium Code Reviews
 Chromium Code Reviews Issue 2925053003:
  [NTP::Push] Adding BreakingNewsSuggestionsProvider  (Closed)
    
  
    Issue 2925053003:
  [NTP::Push] Adding BreakingNewsSuggestionsProvider  (Closed) 
  | 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(); | |
| 55 std::string token = pref_service_->GetString( | 58 std::string token = pref_service_->GetString( | 
| 56 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | 59 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | 
| 57 subscription_manager_->Unsubscribe(token); | 60 subscription_manager_->Unsubscribe(token); | 
| 58 } | 61 } | 
| 59 | 62 | 
| 60 void ContentSuggestionsGCMAppHandler::Subscribe() { | 63 void ContentSuggestionsGCMAppHandler::Subscribe() { | 
| 61 std::string token = pref_service_->GetString( | 64 std::string token = pref_service_->GetString( | 
| 62 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | 65 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | 
| 63 if (!token.empty()) { | 66 if (!token.empty()) { | 
| 64 return; | 67 return; | 
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 98 void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} | 101 void ContentSuggestionsGCMAppHandler::ShutdownHandler() {} | 
| 99 | 102 | 
| 100 void ContentSuggestionsGCMAppHandler::OnStoreReset() { | 103 void ContentSuggestionsGCMAppHandler::OnStoreReset() { | 
| 101 pref_service_->ClearPref( | 104 pref_service_->ClearPref( | 
| 102 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | 105 ntp_snippets::prefs::kContentSuggestionsGCMSubscriptionTokenCache); | 
| 103 } | 106 } | 
| 104 | 107 | 
| 105 void ContentSuggestionsGCMAppHandler::OnMessage( | 108 void ContentSuggestionsGCMAppHandler::OnMessage( | 
| 106 const std::string& app_id, | 109 const std::string& app_id, | 
| 107 const gcm::IncomingMessage& message) { | 110 const gcm::IncomingMessage& message) { | 
| 108 // TODO(mamir): Implement Show notification and update the feed. | 111 // TODO(mamir): Build the message. | 
| 112 on_new_content_callback_.Run(base::Value()); | |
| 
jkrcal
2017/06/09 11:32:59
Are you sure this will never get called after Stop
 
mamir
2017/06/09 14:41:01
Yes, this won't be called after StopListening beca
 | |
| 109 } | 113 } | 
| 110 | 114 | 
| 111 void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( | 115 void ContentSuggestionsGCMAppHandler::OnMessagesDeleted( | 
| 112 const std::string& app_id) { | 116 const std::string& app_id) { | 
| 113 // Messages don't get deleted. | 117 // Messages don't get deleted. | 
| 114 NOTREACHED() << "ContentSuggestionsGCMAppHandler messages don't get deleted."; | 118 NOTREACHED() << "ContentSuggestionsGCMAppHandler messages don't get deleted."; | 
| 115 } | 119 } | 
| 116 | 120 | 
| 117 void ContentSuggestionsGCMAppHandler::OnSendError( | 121 void ContentSuggestionsGCMAppHandler::OnSendError( | 
| 118 const std::string& app_id, | 122 const std::string& app_id, | 
| (...skipping 11 matching lines...) Expand all Loading... | |
| 130 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; | 134 NOTREACHED() << "ContentSuggestionsGCMAppHandler doesn't send GCM messages."; | 
| 131 } | 135 } | 
| 132 | 136 | 
| 133 void ContentSuggestionsGCMAppHandler::RegisterProfilePrefs( | 137 void ContentSuggestionsGCMAppHandler::RegisterProfilePrefs( | 
| 134 PrefRegistrySimple* registry) { | 138 PrefRegistrySimple* registry) { | 
| 135 registry->RegisterStringPref( | 139 registry->RegisterStringPref( | 
| 136 prefs::kContentSuggestionsGCMSubscriptionTokenCache, std::string()); | 140 prefs::kContentSuggestionsGCMSubscriptionTokenCache, std::string()); | 
| 137 } | 141 } | 
| 138 | 142 | 
| 139 } // namespace ntp_snippets | 143 } // namespace ntp_snippets | 
| OLD | NEW |