| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2016 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/content_suggestions_service.h" |
| 6 |
| 7 #include <algorithm> |
| 8 #include <iterator> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "ui/gfx/image/image.h" |
| 13 |
| 14 namespace ntp_snippets { |
| 15 |
| 16 ContentSuggestionsService::ContentSuggestionsService(State state) |
| 17 : state_(state) {} |
| 18 |
| 19 ContentSuggestionsService::~ContentSuggestionsService() {} |
| 20 |
| 21 void ContentSuggestionsService::Shutdown() { |
| 22 DCHECK(providers_.empty()); |
| 23 DCHECK(categories_.empty()); |
| 24 DCHECK(suggestions_by_category_.empty()); |
| 25 DCHECK(id_category_map_.empty()); |
| 26 state_ = State::DISABLED; |
| 27 FOR_EACH_OBSERVER(Observer, observers_, ContentSuggestionsServiceShutdown()); |
| 28 } |
| 29 |
| 30 ContentSuggestionsCategoryStatus ContentSuggestionsService::GetCategoryStatus( |
| 31 ContentSuggestionsCategory category) const { |
| 32 if (state_ == State::DISABLED) { |
| 33 return ContentSuggestionsCategoryStatus:: |
| 34 ALL_SUGGESTIONS_EXPLICITLY_DISABLED; |
| 35 } |
| 36 |
| 37 auto iterator = providers_.find(category); |
| 38 if (iterator == providers_.end()) |
| 39 return ContentSuggestionsCategoryStatus::NOT_PROVIDED; |
| 40 |
| 41 return iterator->second->GetCategoryStatus(category); |
| 42 } |
| 43 |
| 44 const std::vector<ContentSuggestion>& |
| 45 ContentSuggestionsService::GetSuggestionsForCategory( |
| 46 ContentSuggestionsCategory category) const { |
| 47 auto iterator = suggestions_by_category_.find(category); |
| 48 if (iterator == suggestions_by_category_.end()) |
| 49 return no_suggestions_; |
| 50 return iterator->second; |
| 51 } |
| 52 |
| 53 void ContentSuggestionsService::FetchSuggestionImage( |
| 54 const std::string& suggestion_id, |
| 55 const ImageFetchedCallback& callback) { |
| 56 if (!id_category_map_.count(suggestion_id)) { |
| 57 LOG(WARNING) << "Requested image for unknown suggestion " << suggestion_id; |
| 58 callback.Run(suggestion_id, gfx::Image()); |
| 59 return; |
| 60 } |
| 61 ContentSuggestionsCategory category = id_category_map_[suggestion_id]; |
| 62 if (!providers_.count(category)) { |
| 63 LOG(WARNING) << "Requested image for suggestion " << suggestion_id |
| 64 << " for unavailable category " << int(category); |
| 65 callback.Run(suggestion_id, gfx::Image()); |
| 66 return; |
| 67 } |
| 68 providers_[category]->FetchSuggestionImage(suggestion_id, callback); |
| 69 } |
| 70 |
| 71 void ContentSuggestionsService::ClearCachedSuggestionsForDebugging() { |
| 72 suggestions_by_category_.clear(); |
| 73 id_category_map_.clear(); |
| 74 for (auto& category_provider_pair : providers_) { |
| 75 category_provider_pair.second->ClearCachedSuggestionsForDebugging(); |
| 76 } |
| 77 FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions()); |
| 78 } |
| 79 |
| 80 void ContentSuggestionsService::ClearDiscardedSuggestionsForDebugging() { |
| 81 for (auto& category_provider_pair : providers_) { |
| 82 category_provider_pair.second->ClearDiscardedSuggestionsForDebugging(); |
| 83 } |
| 84 } |
| 85 |
| 86 void ContentSuggestionsService::DiscardSuggestion( |
| 87 const std::string& suggestion_id) { |
| 88 if (!id_category_map_.count(suggestion_id)) { |
| 89 LOG(WARNING) << "Discarded unknown suggestion " << suggestion_id; |
| 90 return; |
| 91 } |
| 92 ContentSuggestionsCategory category = id_category_map_[suggestion_id]; |
| 93 if (!providers_.count(category)) { |
| 94 LOG(WARNING) << "Discarded suggestion " << suggestion_id |
| 95 << " for unavailable category " << int(category); |
| 96 return; |
| 97 } |
| 98 providers_[category]->DiscardSuggestion(suggestion_id); |
| 99 |
| 100 // Remove the suggestion locally. |
| 101 id_category_map_.erase(suggestion_id); |
| 102 std::vector<ContentSuggestion>* suggestions = |
| 103 &suggestions_by_category_[category]; |
| 104 auto position = |
| 105 std::find_if(suggestions->begin(), suggestions->end(), |
| 106 [&suggestion_id](const ContentSuggestion& suggestion) { |
| 107 return suggestion_id == suggestion.id(); |
| 108 }); |
| 109 DCHECK(position != suggestions->end()); |
| 110 suggestions->erase(position); |
| 111 } |
| 112 |
| 113 void ContentSuggestionsService::AddObserver(Observer* observer) { |
| 114 observers_.AddObserver(observer); |
| 115 } |
| 116 |
| 117 void ContentSuggestionsService::RemoveObserver(Observer* observer) { |
| 118 observers_.RemoveObserver(observer); |
| 119 } |
| 120 |
| 121 void ContentSuggestionsService::RegisterProvider( |
| 122 ContentSuggestionsProvider* provider) { |
| 123 // TODO(pke): When NTPSnippetsService is purely a provider, think about |
| 124 // removing this state check. |
| 125 if (state_ == State::DISABLED) |
| 126 return; |
| 127 |
| 128 for (ContentSuggestionsCategory category : provider->provided_categories()) { |
| 129 DCHECK_EQ(0ul, providers_.count(category)); |
| 130 providers_[category] = provider; |
| 131 // TODO(pke): In the future, make sure that the categories have some useful |
| 132 // (maybe constant, at least consistent) ordering for the UI. |
| 133 categories_.push_back(category); |
| 134 if (IsContentSuggestionsCategoryStatusAvailable( |
| 135 provider->GetCategoryStatus(category))) { |
| 136 suggestions_by_category_[category] = std::vector<ContentSuggestion>(); |
| 137 } |
| 138 NotifyCategoryStatusChanged(category); |
| 139 } |
| 140 provider->SetObserver(this); |
| 141 } |
| 142 |
| 143 //////////////////////////////////////////////////////////////////////////////// |
| 144 // Private methods |
| 145 |
| 146 void ContentSuggestionsService::OnNewSuggestions( |
| 147 ContentSuggestionsCategory changed_category, |
| 148 std::vector<ContentSuggestion> new_suggestions) { |
| 149 DCHECK(IsCategoryRegistered(changed_category)); |
| 150 |
| 151 for (const ContentSuggestion& suggestion : |
| 152 suggestions_by_category_[changed_category]) { |
| 153 id_category_map_.erase(suggestion.id()); |
| 154 } |
| 155 |
| 156 for (const ContentSuggestion& suggestion : new_suggestions) { |
| 157 id_category_map_[suggestion.id()] = changed_category; |
| 158 } |
| 159 |
| 160 suggestions_by_category_[changed_category] = std::move(new_suggestions); |
| 161 |
| 162 FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions()); |
| 163 } |
| 164 |
| 165 void ContentSuggestionsService::OnCategoryStatusChanged( |
| 166 ContentSuggestionsCategory changed_category, |
| 167 ContentSuggestionsCategoryStatus new_status) { |
| 168 if (!IsContentSuggestionsCategoryStatusAvailable(new_status)) { |
| 169 for (const ContentSuggestion& suggestion : |
| 170 suggestions_by_category_[changed_category]) { |
| 171 id_category_map_.erase(suggestion.id()); |
| 172 } |
| 173 suggestions_by_category_.erase(changed_category); |
| 174 } |
| 175 NotifyCategoryStatusChanged(changed_category); |
| 176 } |
| 177 |
| 178 void ContentSuggestionsService::OnProviderShutdown( |
| 179 ContentSuggestionsProvider* provider) { |
| 180 for (ContentSuggestionsCategory category : provider->provided_categories()) { |
| 181 auto iterator = std::find(categories_.begin(), categories_.end(), category); |
| 182 DCHECK(iterator != categories_.end()); |
| 183 categories_.erase(iterator); |
| 184 for (const ContentSuggestion& suggestion : |
| 185 suggestions_by_category_[category]) { |
| 186 id_category_map_.erase(suggestion.id()); |
| 187 } |
| 188 suggestions_by_category_.erase(category); |
| 189 providers_.erase(category); |
| 190 NotifyCategoryStatusChanged(category); |
| 191 } |
| 192 } |
| 193 |
| 194 bool ContentSuggestionsService::IsCategoryRegistered( |
| 195 ContentSuggestionsCategory category) const { |
| 196 return std::find(categories_.begin(), categories_.end(), category) != |
| 197 categories_.end(); |
| 198 } |
| 199 |
| 200 void ContentSuggestionsService::NotifyCategoryStatusChanged( |
| 201 ContentSuggestionsCategory category) { |
| 202 FOR_EACH_OBSERVER( |
| 203 Observer, observers_, |
| 204 OnCategoryStatusChanged(category, GetCategoryStatus(category))); |
| 205 } |
| 206 |
| 207 } // namespace ntp_snippets |
| OLD | NEW |