pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 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 | |
pke | 31b0794 | 2016-08-01 11:35:02 | [diff] [blame] | 16 | bool ContentSuggestionsService::CompareCategoriesByID::operator()( |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 17 | const Category& left, |
| 18 | const Category& right) const { |
pke | 31b0794 | 2016-08-01 11:35:02 | [diff] [blame] | 19 | return left.id() < right.id(); |
| 20 | } |
| 21 | |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 22 | ContentSuggestionsService::ContentSuggestionsService(State state) |
| 23 | : state_(state) {} |
| 24 | |
| 25 | ContentSuggestionsService::~ContentSuggestionsService() {} |
| 26 | |
| 27 | void ContentSuggestionsService::Shutdown() { |
| 28 | DCHECK(providers_.empty()); |
| 29 | DCHECK(categories_.empty()); |
| 30 | DCHECK(suggestions_by_category_.empty()); |
| 31 | DCHECK(id_category_map_.empty()); |
| 32 | state_ = State::DISABLED; |
| 33 | FOR_EACH_OBSERVER(Observer, observers_, ContentSuggestionsServiceShutdown()); |
| 34 | } |
| 35 | |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 36 | CategoryStatus ContentSuggestionsService::GetCategoryStatus( |
| 37 | Category category) const { |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 38 | if (state_ == State::DISABLED) { |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 39 | return CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED; |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | auto iterator = providers_.find(category); |
| 43 | if (iterator == providers_.end()) |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 44 | return CategoryStatus::NOT_PROVIDED; |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 45 | |
| 46 | return iterator->second->GetCategoryStatus(category); |
| 47 | } |
| 48 | |
| 49 | const std::vector<ContentSuggestion>& |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 50 | ContentSuggestionsService::GetSuggestionsForCategory(Category category) const { |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 51 | auto iterator = suggestions_by_category_.find(category); |
| 52 | if (iterator == suggestions_by_category_.end()) |
| 53 | return no_suggestions_; |
| 54 | return iterator->second; |
| 55 | } |
| 56 | |
| 57 | void ContentSuggestionsService::FetchSuggestionImage( |
| 58 | const std::string& suggestion_id, |
| 59 | const ImageFetchedCallback& callback) { |
| 60 | if (!id_category_map_.count(suggestion_id)) { |
| 61 | LOG(WARNING) << "Requested image for unknown suggestion " << suggestion_id; |
| 62 | callback.Run(suggestion_id, gfx::Image()); |
| 63 | return; |
| 64 | } |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 65 | Category category = id_category_map_.at(suggestion_id); |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 66 | if (!providers_.count(category)) { |
| 67 | LOG(WARNING) << "Requested image for suggestion " << suggestion_id |
pke | 31b0794 | 2016-08-01 11:35:02 | [diff] [blame] | 68 | << " for unavailable category " << category; |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 69 | callback.Run(suggestion_id, gfx::Image()); |
| 70 | return; |
| 71 | } |
| 72 | providers_[category]->FetchSuggestionImage(suggestion_id, callback); |
| 73 | } |
| 74 | |
| 75 | void ContentSuggestionsService::ClearCachedSuggestionsForDebugging() { |
| 76 | suggestions_by_category_.clear(); |
| 77 | id_category_map_.clear(); |
| 78 | for (auto& category_provider_pair : providers_) { |
| 79 | category_provider_pair.second->ClearCachedSuggestionsForDebugging(); |
| 80 | } |
| 81 | FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions()); |
| 82 | } |
| 83 | |
pke | 2646c95b | 2016-07-25 12:18:44 | [diff] [blame] | 84 | void ContentSuggestionsService::ClearDismissedSuggestionsForDebugging() { |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 85 | for (auto& category_provider_pair : providers_) { |
pke | 2646c95b | 2016-07-25 12:18:44 | [diff] [blame] | 86 | category_provider_pair.second->ClearDismissedSuggestionsForDebugging(); |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
pke | 2646c95b | 2016-07-25 12:18:44 | [diff] [blame] | 90 | void ContentSuggestionsService::DismissSuggestion( |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 91 | const std::string& suggestion_id) { |
| 92 | if (!id_category_map_.count(suggestion_id)) { |
pke | 2646c95b | 2016-07-25 12:18:44 | [diff] [blame] | 93 | LOG(WARNING) << "Dismissed unknown suggestion " << suggestion_id; |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 94 | return; |
| 95 | } |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 96 | Category category = id_category_map_.at(suggestion_id); |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 97 | if (!providers_.count(category)) { |
pke | 2646c95b | 2016-07-25 12:18:44 | [diff] [blame] | 98 | LOG(WARNING) << "Dismissed suggestion " << suggestion_id |
pke | 31b0794 | 2016-08-01 11:35:02 | [diff] [blame] | 99 | << " for unavailable category " << category; |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 100 | return; |
| 101 | } |
pke | 2646c95b | 2016-07-25 12:18:44 | [diff] [blame] | 102 | providers_[category]->DismissSuggestion(suggestion_id); |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 103 | |
| 104 | // Remove the suggestion locally. |
| 105 | id_category_map_.erase(suggestion_id); |
| 106 | std::vector<ContentSuggestion>* suggestions = |
| 107 | &suggestions_by_category_[category]; |
| 108 | auto position = |
| 109 | std::find_if(suggestions->begin(), suggestions->end(), |
| 110 | [&suggestion_id](const ContentSuggestion& suggestion) { |
| 111 | return suggestion_id == suggestion.id(); |
| 112 | }); |
pke | 27632ab | 2016-07-25 10:20:25 | [diff] [blame] | 113 | DCHECK(position != suggestions->end()) |
pke | 2646c95b | 2016-07-25 12:18:44 | [diff] [blame] | 114 | << "The dismissed suggestion " << suggestion_id |
pke | 27632ab | 2016-07-25 10:20:25 | [diff] [blame] | 115 | << " has already been removed. Providers must not call OnNewSuggestions" |
pke | 2646c95b | 2016-07-25 12:18:44 | [diff] [blame] | 116 | " in response to DismissSuggestion."; |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 117 | suggestions->erase(position); |
| 118 | } |
| 119 | |
| 120 | void ContentSuggestionsService::AddObserver(Observer* observer) { |
| 121 | observers_.AddObserver(observer); |
| 122 | } |
| 123 | |
| 124 | void ContentSuggestionsService::RemoveObserver(Observer* observer) { |
| 125 | observers_.RemoveObserver(observer); |
| 126 | } |
| 127 | |
| 128 | void ContentSuggestionsService::RegisterProvider( |
| 129 | ContentSuggestionsProvider* provider) { |
| 130 | // TODO(pke): When NTPSnippetsService is purely a provider, think about |
| 131 | // removing this state check. |
| 132 | if (state_ == State::DISABLED) |
| 133 | return; |
| 134 | |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 135 | for (Category category : provider->GetProvidedCategories()) { |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 136 | DCHECK_EQ(0ul, providers_.count(category)); |
| 137 | providers_[category] = provider; |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 138 | categories_.push_back(category); |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 139 | if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) { |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 140 | suggestions_by_category_[category] = std::vector<ContentSuggestion>(); |
| 141 | } |
| 142 | NotifyCategoryStatusChanged(category); |
| 143 | } |
pke | 31b0794 | 2016-08-01 11:35:02 | [diff] [blame] | 144 | std::sort(categories_.begin(), categories_.end(), |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 145 | [this](const Category& left, const Category& right) { |
pke | 31b0794 | 2016-08-01 11:35:02 | [diff] [blame] | 146 | return category_factory_.CompareCategories(left, right); |
| 147 | }); |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 148 | provider->SetObserver(this); |
| 149 | } |
| 150 | |
| 151 | //////////////////////////////////////////////////////////////////////////////// |
| 152 | // Private methods |
| 153 | |
| 154 | void ContentSuggestionsService::OnNewSuggestions( |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 155 | Category changed_category, |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 156 | std::vector<ContentSuggestion> new_suggestions) { |
| 157 | DCHECK(IsCategoryRegistered(changed_category)); |
| 158 | |
| 159 | for (const ContentSuggestion& suggestion : |
| 160 | suggestions_by_category_[changed_category]) { |
| 161 | id_category_map_.erase(suggestion.id()); |
| 162 | } |
| 163 | |
| 164 | for (const ContentSuggestion& suggestion : new_suggestions) { |
pke | 31b0794 | 2016-08-01 11:35:02 | [diff] [blame] | 165 | id_category_map_.insert(std::make_pair(suggestion.id(), changed_category)); |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | suggestions_by_category_[changed_category] = std::move(new_suggestions); |
| 169 | |
| 170 | FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions()); |
| 171 | } |
| 172 | |
| 173 | void ContentSuggestionsService::OnCategoryStatusChanged( |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 174 | Category changed_category, |
| 175 | CategoryStatus new_status) { |
| 176 | if (!IsCategoryStatusAvailable(new_status)) { |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 177 | for (const ContentSuggestion& suggestion : |
| 178 | suggestions_by_category_[changed_category]) { |
| 179 | id_category_map_.erase(suggestion.id()); |
| 180 | } |
| 181 | suggestions_by_category_.erase(changed_category); |
| 182 | } |
| 183 | NotifyCategoryStatusChanged(changed_category); |
| 184 | } |
| 185 | |
| 186 | void ContentSuggestionsService::OnProviderShutdown( |
| 187 | ContentSuggestionsProvider* provider) { |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 188 | for (Category category : provider->GetProvidedCategories()) { |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 189 | auto iterator = std::find(categories_.begin(), categories_.end(), category); |
| 190 | DCHECK(iterator != categories_.end()); |
| 191 | categories_.erase(iterator); |
| 192 | for (const ContentSuggestion& suggestion : |
| 193 | suggestions_by_category_[category]) { |
| 194 | id_category_map_.erase(suggestion.id()); |
| 195 | } |
| 196 | suggestions_by_category_.erase(category); |
| 197 | providers_.erase(category); |
| 198 | NotifyCategoryStatusChanged(category); |
| 199 | } |
| 200 | } |
| 201 | |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 202 | bool ContentSuggestionsService::IsCategoryRegistered(Category category) const { |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 203 | return std::find(categories_.begin(), categories_.end(), category) != |
| 204 | categories_.end(); |
| 205 | } |
| 206 | |
pke | 9c5095ac | 2016-08-01 13:53:12 | [diff] [blame^] | 207 | void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |
pke | 6dbb90af | 2016-07-08 14:00:46 | [diff] [blame] | 208 | FOR_EACH_OBSERVER( |
| 209 | Observer, observers_, |
| 210 | OnCategoryStatusChanged(category, GetCategoryStatus(category))); |
| 211 | } |
| 212 | |
| 213 | } // namespace ntp_snippets |