Chromium Code Reviews| 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(Enabled enabled) | |
| 17 : enabled_(enabled) {} | |
| 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(suggestions_by_category_.empty()); | |
|
Marc Treib
2016/07/05 13:17:12
Double DCHECK
Philipp Keck
2016/07/05 13:51:59
Done.
| |
| 26 enabled_ = Enabled::DISABLE; | |
| 27 FOR_EACH_OBSERVER(Observer, observers_, ContentSuggestionsServiceShutdown()); | |
| 28 observers_.Clear(); | |
| 29 } | |
| 30 | |
| 31 ContentSuggestionsState ContentSuggestionsService::GetCategoryState( | |
| 32 ContentSuggestionCategory category) const { | |
| 33 if (enabled_ == Enabled::DISABLE) | |
| 34 return ContentSuggestionsState::ALL_SUGGESTIONS_EXPLICITLY_DISABLED; | |
| 35 | |
| 36 auto iterator = providers_.find(category); | |
| 37 if (iterator == providers_.end()) | |
| 38 return ContentSuggestionsState::NOT_PROVIDED; | |
| 39 | |
| 40 return iterator->second->GetCategoryState(category); | |
| 41 } | |
| 42 | |
| 43 const std::vector<ContentSuggestion>& | |
| 44 ContentSuggestionsService::GetSuggestionsForCategory( | |
| 45 ContentSuggestionCategory category) const { | |
| 46 auto iterator = suggestions_by_category_.find(category); | |
| 47 if (iterator == suggestions_by_category_.end()) { | |
| 48 return empty_categories_list_; | |
| 49 } | |
| 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 } | |
| 60 ContentSuggestionCategory category = id_category_map_[suggestion_id]; | |
| 61 if (!providers_.count(category)) { | |
| 62 LOG(WARNING) << "Requested image for suggestion " << suggestion_id | |
| 63 << " for unavailable category " << int(category); | |
| 64 callback.Run(suggestion_id, gfx::Image()); | |
| 65 return; | |
| 66 } | |
| 67 providers_[category]->FetchSuggestionImage(suggestion_id, callback); | |
| 68 } | |
| 69 | |
| 70 void ContentSuggestionsService::ClearCachedSuggestionsForDebugging() { | |
| 71 suggestions_by_category_.clear(); | |
| 72 id_category_map_.clear(); | |
| 73 for (auto& provider : providers_) { | |
| 74 provider.second->ClearCachedSuggestionsForDebugging(); | |
| 75 } | |
| 76 FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions()); | |
| 77 } | |
| 78 | |
| 79 void ContentSuggestionsService::ClearDiscardedSuggestionsForDebugging() { | |
| 80 for (auto& provider : providers_) { | |
| 81 provider.second->ClearDiscardedSuggestionsForDebugging(); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 void ContentSuggestionsService::DiscardSuggestion( | |
| 86 const std::string& suggestion_id) { | |
| 87 if (!id_category_map_.count(suggestion_id)) { | |
| 88 LOG(WARNING) << "Requested image for unknown suggestion " << suggestion_id; | |
|
Marc Treib
2016/07/05 13:17:12
Too much copypasta :)
Philipp Keck
2016/07/05 13:51:59
Done. And a return statement was missing, too.
| |
| 89 } | |
| 90 ContentSuggestionCategory category = id_category_map_[suggestion_id]; | |
| 91 if (!providers_.count(category)) { | |
| 92 LOG(WARNING) << "Discarded suggestion " << suggestion_id | |
| 93 << " for unavailable category " << int(category); | |
| 94 return; | |
| 95 } | |
| 96 providers_[category]->DiscardSuggestion(suggestion_id); | |
| 97 | |
| 98 // Remove the suggestion locally. | |
| 99 id_category_map_.erase(suggestion_id); | |
| 100 std::vector<ContentSuggestion>* suggestions = | |
| 101 &suggestions_by_category_[category]; | |
| 102 auto position = | |
| 103 std::find_if(suggestions->begin(), suggestions->end(), | |
| 104 [&suggestion_id](const ContentSuggestion& suggestion) { | |
| 105 return suggestion_id == suggestion.id(); | |
| 106 }); | |
| 107 if (position != suggestions->end()) { | |
|
Marc Treib
2016/07/05 13:17:12
DCHECK that it was found? It should never happen t
Philipp Keck
2016/07/05 13:51:59
Done.
| |
| 108 suggestions->erase(position); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void ContentSuggestionsService::AddObserver(Observer* observer) { | |
| 113 observers_.AddObserver(observer); | |
| 114 } | |
| 115 | |
| 116 void ContentSuggestionsService::RemoveObserver(Observer* observer) { | |
| 117 observers_.RemoveObserver(observer); | |
| 118 } | |
| 119 | |
| 120 void ContentSuggestionsService::RegisterProvider( | |
| 121 ContentSuggestionsProvider* provider) { | |
| 122 if (enabled_ == Enabled::DISABLE) | |
| 123 return; | |
| 124 | |
| 125 for (ContentSuggestionCategory category : provider->provided_categories()) { | |
| 126 DCHECK_EQ(0ul, providers_.count(category)); | |
| 127 providers_[category] = provider; | |
| 128 // TODO(pke) In the future, make sure that the categories have some useful | |
| 129 // (maybe constant, at least consistent) ordering for the UI. | |
| 130 categories_.push_back(category); | |
| 131 if (provider->GetCategoryState(category) <= | |
| 132 ContentSuggestionsState::AVAILABLE) { | |
|
Marc Treib
2016/07/05 13:17:12
No numeric comparisons on enums please - make a he
Philipp Keck
2016/07/05 13:51:59
Yep, I wanted a helper function but wasn't sure wh
Marc Treib
2016/07/05 14:50:50
I'd suggest a public function, next to the enum de
Philipp Keck
2016/07/05 16:53:34
Done.
| |
| 133 suggestions_by_category_[category].clear(); | |
| 134 } | |
| 135 provider->SetObserver(this); | |
| 136 FOR_EACH_OBSERVER( | |
| 137 Observer, observers_, | |
| 138 OnCategoryStateChanged(category, provider->GetCategoryState(category))); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 //////////////////////////////////////////////////////////////////////////////// | |
| 143 // Private methods | |
| 144 | |
| 145 void ContentSuggestionsService::OnNewSuggestions( | |
| 146 ContentSuggestionCategory changed_category, | |
| 147 std::vector<ContentSuggestion> new_suggestions) { | |
| 148 DCHECK(CategoryExists(changed_category)); | |
| 149 | |
| 150 for (const ContentSuggestion& suggestion : | |
| 151 suggestions_by_category_[changed_category]) { | |
| 152 id_category_map_.erase(suggestion.id()); | |
| 153 } | |
| 154 | |
| 155 suggestions_by_category_[changed_category] = std::move(new_suggestions); | |
| 156 | |
| 157 for (const ContentSuggestion& suggestion : new_suggestions) { | |
| 158 id_category_map_[suggestion.id()] = suggestion.category(); | |
| 159 } | |
| 160 | |
| 161 FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions()); | |
| 162 } | |
| 163 | |
| 164 void ContentSuggestionsService::OnCategoryStateChanged( | |
| 165 ContentSuggestionCategory changed_category, | |
| 166 ContentSuggestionsState new_state) { | |
| 167 if (new_state <= ContentSuggestionsState::AVAILABLE) { | |
|
Marc Treib
2016/07/05 13:17:12
Also here: Helper please
Philipp Keck
2016/07/05 13:51:59
See above.
| |
| 168 for (const ContentSuggestion& suggestion : | |
| 169 suggestions_by_category_[changed_category]) { | |
| 170 id_category_map_.erase(suggestion.id()); | |
| 171 } | |
| 172 suggestions_by_category_.erase(changed_category); | |
| 173 } | |
| 174 FOR_EACH_OBSERVER(Observer, observers_, | |
| 175 OnCategoryStateChanged(changed_category, new_state)); | |
| 176 } | |
| 177 | |
| 178 void ContentSuggestionsService::OnProviderShutdown( | |
| 179 ContentSuggestionsProvider* provider) { | |
| 180 for (ContentSuggestionCategory category : provider->provided_categories()) { | |
| 181 auto iterator = std::find(categories_.begin(), categories_.end(), category); | |
| 182 if (iterator != categories_.end()) { | |
|
Marc Treib
2016/07/05 13:17:12
Can it ever be not found?
Philipp Keck
2016/07/05 13:51:59
It's a precaution for providers that irreponsibly
| |
| 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 } | |
| 191 FOR_EACH_OBSERVER( | |
| 192 Observer, observers_, | |
| 193 OnCategoryStateChanged(category, GetCategoryState(category))); | |
| 194 } | |
| 195 } | |
| 196 | |
| 197 bool ContentSuggestionsService::CategoryExists( | |
| 198 ContentSuggestionCategory category) const { | |
| 199 return std::find(categories_.begin(), categories_.end(), category) != | |
| 200 categories_.end(); | |
| 201 } | |
| 202 | |
| 203 } // namespace ntp_snippets | |
| OLD | NEW |