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 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" |
11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/threading/thread_task_runner_handle.h" |
12 #include "ui/gfx/image/image.h" | 14 #include "ui/gfx/image/image.h" |
13 | 15 |
14 namespace ntp_snippets { | 16 namespace ntp_snippets { |
15 | 17 |
16 bool ContentSuggestionsService::CompareCategoriesByID::operator()( | 18 bool ContentSuggestionsService::CompareCategoriesByID::operator()( |
17 const Category& left, | 19 const Category& left, |
18 const Category& right) const { | 20 const Category& right) const { |
19 return left.id() < right.id(); | 21 return left.id() < right.id(); |
20 } | 22 } |
21 | 23 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
54 if (iterator == suggestions_by_category_.end()) | 56 if (iterator == suggestions_by_category_.end()) |
55 return no_suggestions_; | 57 return no_suggestions_; |
56 return iterator->second; | 58 return iterator->second; |
57 } | 59 } |
58 | 60 |
59 void ContentSuggestionsService::FetchSuggestionImage( | 61 void ContentSuggestionsService::FetchSuggestionImage( |
60 const std::string& suggestion_id, | 62 const std::string& suggestion_id, |
61 const ImageFetchedCallback& callback) { | 63 const ImageFetchedCallback& callback) { |
62 if (!id_category_map_.count(suggestion_id)) { | 64 if (!id_category_map_.count(suggestion_id)) { |
63 LOG(WARNING) << "Requested image for unknown suggestion " << suggestion_id; | 65 LOG(WARNING) << "Requested image for unknown suggestion " << suggestion_id; |
64 callback.Run(suggestion_id, gfx::Image()); | 66 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 67 FROM_HERE, base::Bind(callback, suggestion_id, gfx::Image())); |
65 return; | 68 return; |
66 } | 69 } |
67 Category category = id_category_map_.at(suggestion_id); | 70 Category category = id_category_map_.at(suggestion_id); |
68 if (!providers_by_category_.count(category)) { | 71 if (!providers_by_category_.count(category)) { |
69 LOG(WARNING) << "Requested image for suggestion " << suggestion_id | 72 LOG(WARNING) << "Requested image for suggestion " << suggestion_id |
70 << " for unavailable category " << category; | 73 << " for unavailable category " << category; |
71 callback.Run(suggestion_id, gfx::Image()); | 74 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 75 FROM_HERE, base::Bind(callback, suggestion_id, gfx::Image())); |
72 return; | 76 return; |
73 } | 77 } |
74 providers_by_category_[category]->FetchSuggestionImage(suggestion_id, | 78 providers_by_category_[category]->FetchSuggestionImage(suggestion_id, |
75 callback); | 79 callback); |
76 } | 80 } |
77 | 81 |
78 void ContentSuggestionsService::ClearCachedSuggestionsForDebugging() { | 82 void ContentSuggestionsService::ClearCachedSuggestionsForDebugging() { |
79 suggestions_by_category_.clear(); | 83 suggestions_by_category_.clear(); |
80 id_category_map_.clear(); | 84 id_category_map_.clear(); |
81 for (auto& category_provider_pair : providers_by_category_) { | 85 for (auto& category_provider_pair : providers_by_category_) { |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 return true; | 216 return true; |
213 } | 217 } |
214 | 218 |
215 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { | 219 void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) { |
216 FOR_EACH_OBSERVER( | 220 FOR_EACH_OBSERVER( |
217 Observer, observers_, | 221 Observer, observers_, |
218 OnCategoryStatusChanged(category, GetCategoryStatus(category))); | 222 OnCategoryStatusChanged(category, GetCategoryStatus(category))); |
219 } | 223 } |
220 | 224 |
221 } // namespace ntp_snippets | 225 } // namespace ntp_snippets |
OLD | NEW |