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 if (all_history) { |
| 253 base::Time begin = base::Time(); |
| 254 base::Time end = base::Time::Max(); |
| 255 base::Callback<bool(const GURL& url)> filter = |
| 256 base::Bind([](const GURL& url) { return true; }); |
| 257 ClearHistory(begin, end, filter); |
| 258 } else { |
| 259 if (deleted_rows.empty()) |
| 260 return; |
| 261 |
| 262 base::Time begin = deleted_rows[0].last_visit(); |
| 263 base::Time end = deleted_rows[0].last_visit(); |
| 264 std::set<GURL> deleted_urls; |
| 265 for (const history::URLRow& row : deleted_rows) { |
| 266 if (row.last_visit() < begin) |
| 267 begin = row.last_visit(); |
| 268 if (row.last_visit() > end) |
| 269 end = row.last_visit(); |
| 270 deleted_urls.insert(row.url()); |
| 271 } |
| 272 base::Callback<bool(const GURL& url)> filter = base::Bind( |
| 273 [](const std::set<GURL>& set, const GURL& url) { |
| 274 return set.count(url) != 0; |
| 275 }, |
| 276 deleted_urls); |
| 277 ClearHistory(begin, end, filter); |
| 278 } |
| 279 } |
| 280 |
| 281 void ContentSuggestionsService::HistoryServiceBeingDeleted( |
| 282 history::HistoryService* history_service) { |
| 283 history_service_observer_.RemoveAll(); |
| 284 } |
| 285 |
233 bool ContentSuggestionsService::RegisterCategoryIfRequired( | 286 bool ContentSuggestionsService::RegisterCategoryIfRequired( |
234 ContentSuggestionsProvider* provider, | 287 ContentSuggestionsProvider* provider, |
235 Category category) { | 288 Category category) { |
236 auto it = providers_by_category_.find(category); | 289 auto it = providers_by_category_.find(category); |
237 if (it != providers_by_category_.end()) { | 290 if (it != providers_by_category_.end()) { |
238 DCHECK_EQ(it->second, provider); | 291 DCHECK_EQ(it->second, provider); |
239 return false; | 292 return false; |
240 } | 293 } |
241 | 294 |
242 providers_by_category_[category] = provider; | 295 providers_by_category_[category] = provider; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) | 345 if (left.IsKnownCategory(KnownCategories::BOOKMARKS)) |
293 return false; | 346 return false; |
294 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) | 347 if (right.IsKnownCategory(KnownCategories::BOOKMARKS)) |
295 return true; | 348 return true; |
296 } | 349 } |
297 return category_factory_.CompareCategories(left, right); | 350 return category_factory_.CompareCategories(left, right); |
298 }); | 351 }); |
299 } | 352 } |
300 | 353 |
301 } // namespace ntp_snippets | 354 } // namespace ntp_snippets |
OLD | NEW |