OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/content_suggestions_service.h" | 5 #include "components/ntp_snippets/content_suggestions_service.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <iterator> | 8 #include <iterator> |
9 #include <set> | |
9 | 10 |
10 #include "base/bind.h" | 11 #include "base/bind.h" |
11 #include "base/location.h" | 12 #include "base/location.h" |
12 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 14 #include "base/threading/thread_task_runner_handle.h" |
14 #include "ui/gfx/image/image.h" | 15 #include "ui/gfx/image/image.h" |
15 | 16 |
16 namespace ntp_snippets { | 17 namespace ntp_snippets { |
17 | 18 |
18 ContentSuggestionsService::ContentSuggestionsService(State state) | 19 ContentSuggestionsService::ContentSuggestionsService( |
19 : state_(state) {} | 20 State state, |
21 history::HistoryService* history_service) | |
22 : state_(state), history_service_observer_(this) { | |
23 // Can be null in tests. | |
24 if (history_service) | |
25 history_service_observer_.Add(history_service); | |
26 } | |
20 | 27 |
21 ContentSuggestionsService::~ContentSuggestionsService() {} | 28 ContentSuggestionsService::~ContentSuggestionsService() {} |
22 | 29 |
23 void ContentSuggestionsService::Shutdown() { | 30 void ContentSuggestionsService::Shutdown() { |
24 ntp_snippets_service_ = nullptr; | 31 ntp_snippets_service_ = nullptr; |
25 id_category_map_.clear(); | 32 id_category_map_.clear(); |
26 suggestions_by_category_.clear(); | 33 suggestions_by_category_.clear(); |
27 providers_by_category_.clear(); | 34 providers_by_category_.clear(); |
28 categories_.clear(); | 35 categories_.clear(); |
29 providers_.clear(); | 36 providers_.clear(); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
223 | 230 |
224 void ContentSuggestionsService::OnSuggestionInvalidated( | 231 void ContentSuggestionsService::OnSuggestionInvalidated( |
225 ContentSuggestionsProvider* provider, | 232 ContentSuggestionsProvider* provider, |
226 Category category, | 233 Category category, |
227 const std::string& suggestion_id) { | 234 const std::string& suggestion_id) { |
228 RemoveSuggestionByID(category, suggestion_id); | 235 RemoveSuggestionByID(category, suggestion_id); |
229 FOR_EACH_OBSERVER(Observer, observers_, | 236 FOR_EACH_OBSERVER(Observer, observers_, |
230 OnSuggestionInvalidated(category, suggestion_id)); | 237 OnSuggestionInvalidated(category, suggestion_id)); |
231 } | 238 } |
232 | 239 |
240 // history::HistoryServiceObserver implementation. | |
241 void ContentSuggestionsService::OnURLsDeleted( | |
242 history::HistoryService* history_service, | |
243 bool all_history, | |
244 bool expired, | |
245 const history::URLRows& deleted_rows, | |
246 const std::set<GURL>& favicon_urls) { | |
247 // We don't care about expired entries. | |
248 if (expired) | |
249 return; | |
250 | |
251 // Redirect to ClearHistory(). | |
252 base::Time begin; | |
253 base::Time end; | |
254 base::Callback<bool(const GURL& url)> filter; | |
255 auto return_true = [](const GURL& url) { return true; }; | |
256 auto set_contains = [](const std::set<GURL>& set, const GURL& url) { | |
Marc Treib
2016/09/02 14:45:19
Just inline these two definitions into the respect
vitaliii
2016/09/02 16:40:06
Done.
| |
257 return set.count(url) != 0 ? true : false; | |
Marc Treib
2016/09/02 14:45:19
remove the " ? true : false", it doesn't do anythi
vitaliii
2016/09/02 16:40:06
|count| returns |unsigned int|.
Marc Treib
2016/09/02 16:50:35
Yes, you can keep the "!= 0" (that's nicer than th
vitaliii
2016/09/02 17:14:10
Done.
| |
258 }; | |
259 if (all_history) { | |
260 begin = base::Time::UnixEpoch(); | |
261 end = base::Time::Now(); | |
262 filter = base::Bind(return_true); | |
263 } else { | |
264 if (deleted_rows.empty()) | |
265 return; | |
266 else { | |
Marc Treib
2016/09/02 14:45:19
No else after return.
Or, if you initialize begin/
vitaliii
2016/09/02 16:40:06
Done.
| |
267 begin = deleted_rows[0].last_visit(); | |
268 end = deleted_rows[0].last_visit(); | |
269 std::set<GURL> deleted_urls; | |
270 for (const history::URLRow& row : deleted_rows) { | |
271 if (row.last_visit() < begin) | |
272 begin = row.last_visit(); | |
273 if (row.last_visit() > end) | |
274 end = row.last_visit(); | |
275 deleted_urls.insert(row.url()); | |
276 } | |
277 filter = base::Bind(set_contains, deleted_urls); | |
278 } | |
279 } | |
280 | |
281 for (const auto& provider : providers_) { | |
Marc Treib
2016/09/02 14:45:19
Just call this class's ClearHistory
vitaliii
2016/09/02 16:40:06
Done.
| |
282 provider->ClearHistory(begin, end, filter); | |
283 } | |
284 } | |
285 | |
286 void ContentSuggestionsService::HistoryServiceBeingDeleted( | |
287 history::HistoryService* history_service) { | |
288 history_service_observer_.RemoveAll(); | |
289 } | |
290 | |
233 bool ContentSuggestionsService::RegisterCategoryIfRequired( | 291 bool ContentSuggestionsService::RegisterCategoryIfRequired( |
234 ContentSuggestionsProvider* provider, | 292 ContentSuggestionsProvider* provider, |
235 Category category) { | 293 Category category) { |
236 auto it = providers_by_category_.find(category); | 294 auto it = providers_by_category_.find(category); |
237 if (it != providers_by_category_.end()) { | 295 if (it != providers_by_category_.end()) { |
238 DCHECK_EQ(it->second, provider); | 296 DCHECK_EQ(it->second, provider); |
239 return false; | 297 return false; |
240 } | 298 } |
241 | 299 |
242 providers_by_category_[category] = provider; | 300 providers_by_category_[category] = provider; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) | 350 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) |
293 return false; | 351 return false; |
294 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) | 352 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) |
295 return true; | 353 return true; |
296 } | 354 } |
297 return category_factory_.CompareCategories(left, right); | 355 return category_factory_.CompareCategories(left, right); |
298 }); | 356 }); |
299 } | 357 } |
300 | 358 |
301 } // namespace ntp_snippets | 359 } // namespace ntp_snippets |
OLD | NEW |