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/physical_web_pages/physical_web_page_suggestio ns_provider.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/strings/string_number_conversions.h" | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 | |
| 12 namespace ntp_snippets { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const int kMaxSuggestionsCount = 10; | |
| 17 | |
| 18 } // namespace | |
| 19 | |
| 20 // TODO(vitaliii): remove when Physical Web C++ interface is provided. | |
| 21 UrlInfo::UrlInfo() {} | |
| 22 UrlInfo::~UrlInfo() {} | |
| 23 UrlInfo::UrlInfo(const UrlInfo& other) = default; | |
| 24 | |
| 25 PhysicalWebPageSuggestionsProvider::PhysicalWebPageSuggestionsProvider( | |
| 26 ContentSuggestionsProvider::Observer* observer, | |
| 27 CategoryFactory* category_factory) | |
| 28 : ContentSuggestionsProvider(observer, category_factory), | |
| 29 category_status_(CategoryStatus::AVAILABLE_LOADING), | |
| 30 provided_category_(category_factory->FromKnownCategory( | |
| 31 KnownCategories::PHYSICAL_WEB_PAGES)) { | |
| 32 } | |
| 33 | |
| 34 PhysicalWebPageSuggestionsProvider::~PhysicalWebPageSuggestionsProvider() { | |
| 35 } | |
| 36 | |
| 37 void PhysicalWebPageSuggestionsProvider::OnDisplayableUrlsChanged( | |
| 38 const std::vector<UrlInfo>& urls) { | |
| 39 NotifyStatusChanged(CategoryStatus::AVAILABLE); | |
| 40 std::vector<ContentSuggestion> suggestions; | |
| 41 | |
| 42 for (const auto& url_info: urls) { | |
|
Marc Treib
2016/08/10 16:22:03
I'd use the actual type (UrlInfo) here instead of
vitaliii
2016/08/11 12:15:24
Done.
| |
| 43 if (static_cast<int>(suggestions.size()) >= kMaxSuggestionsCount) { | |
|
Marc Treib
2016/08/10 16:22:03
You could make kMaxSuggestionsCount a size_t and a
tschumann
2016/08/10 17:37:57
I'd probably leave the check at the beginning as y
vitaliii
2016/08/11 12:15:24
The check at the beginning handles kMaxSuggestions
Marc Treib
2016/08/11 12:53:09
...which should never actually happen. But okay, f
vitaliii
2016/08/11 14:29:58
Acknowledged.
| |
| 44 break; | |
| 45 } | |
| 46 | |
| 47 ContentSuggestion suggestion( | |
| 48 MakeUniqueID(provided_category_, url_info.site_url.spec()), | |
| 49 url_info.site_url); | |
| 50 | |
| 51 suggestion.set_title(base::UTF8ToUTF16(url_info.title)); | |
| 52 suggestion.set_snippet_text(base::UTF8ToUTF16(url_info.description)); | |
| 53 suggestion.set_publish_date(url_info.scan_time); | |
| 54 suggestion.set_publisher_name(base::UTF8ToUTF16(url_info.site_url.host())); | |
| 55 suggestions.emplace_back(std::move(suggestion)); | |
| 56 } | |
| 57 | |
| 58 observer()->OnNewSuggestions(this, provided_category_, | |
| 59 std::move(suggestions)); | |
| 60 } | |
| 61 | |
| 62 std::vector<Category> | |
| 63 PhysicalWebPageSuggestionsProvider::GetProvidedCategories() { | |
| 64 return std::vector<Category>({provided_category_}); | |
| 65 } | |
| 66 | |
| 67 CategoryStatus PhysicalWebPageSuggestionsProvider::GetCategoryStatus( | |
| 68 Category category) { | |
| 69 return category_status_; | |
| 70 } | |
| 71 | |
| 72 void PhysicalWebPageSuggestionsProvider::DismissSuggestion( | |
| 73 const std::string& suggestion_id) { | |
| 74 // TODO(vitaliii): Implement this and then | |
| 75 // ClearDismissedSuggestionsForDebugging. | |
| 76 } | |
| 77 | |
| 78 void PhysicalWebPageSuggestionsProvider::FetchSuggestionImage( | |
| 79 const std::string& suggestion_id, | |
| 80 const ImageFetchedCallback& callback) { | |
| 81 // TODO(vitaliii): Implement. | |
| 82 } | |
| 83 | |
| 84 void PhysicalWebPageSuggestionsProvider::ClearCachedSuggestionsForDebugging( | |
| 85 Category category) { | |
| 86 // Ignored | |
| 87 } | |
| 88 | |
| 89 std::vector<ContentSuggestion> | |
| 90 PhysicalWebPageSuggestionsProvider::GetDismissedSuggestionsForDebugging( | |
| 91 Category category) { | |
| 92 // Ignored. | |
|
Marc Treib
2016/08/10 16:22:03
This is really "not implemented" or "not supported
vitaliii
2016/08/11 12:15:24
Done.
| |
| 93 return std::vector<ContentSuggestion>(); | |
| 94 } | |
| 95 | |
| 96 void PhysicalWebPageSuggestionsProvider:: | |
| 97 ClearDismissedSuggestionsForDebugging(Category category) { | |
| 98 // TODO(vitaliii): Implement when dismissed suggestions are supported. | |
| 99 } | |
| 100 | |
| 101 //////////////////////////////////////////////////////////////////////////////// | |
| 102 // Private methods | |
| 103 | |
| 104 // Updates the |category_status_| and notifies the |observer_|, if necessary. | |
| 105 void PhysicalWebPageSuggestionsProvider::NotifyStatusChanged( | |
| 106 CategoryStatus new_status) { | |
| 107 if (category_status_ == new_status) | |
| 108 return; | |
| 109 category_status_ = new_status; | |
| 110 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); | |
| 111 } | |
| 112 | |
| 113 } // namespace ntp_snippets | |
| OLD | NEW |