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 size_t 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 PhysicalWebPageSuggestionsProvider::~PhysicalWebPageSuggestionsProvider() {} |
| 34 |
| 35 void PhysicalWebPageSuggestionsProvider::OnDisplayableUrlsChanged( |
| 36 const std::vector<UrlInfo>& urls) { |
| 37 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
| 38 std::vector<ContentSuggestion> suggestions; |
| 39 |
| 40 for (const UrlInfo& url_info : urls) { |
| 41 if (suggestions.size() >= kMaxSuggestionsCount) break; |
| 42 |
| 43 ContentSuggestion suggestion( |
| 44 MakeUniqueID(provided_category_, url_info.site_url.spec()), |
| 45 url_info.site_url); |
| 46 |
| 47 suggestion.set_title(base::UTF8ToUTF16(url_info.title)); |
| 48 suggestion.set_snippet_text(base::UTF8ToUTF16(url_info.description)); |
| 49 suggestion.set_publish_date(url_info.scan_time); |
| 50 suggestion.set_publisher_name(base::UTF8ToUTF16(url_info.site_url.host())); |
| 51 suggestions.push_back(std::move(suggestion)); |
| 52 } |
| 53 |
| 54 observer()->OnNewSuggestions(this, provided_category_, |
| 55 std::move(suggestions)); |
| 56 } |
| 57 |
| 58 std::vector<Category> |
| 59 PhysicalWebPageSuggestionsProvider::GetProvidedCategories() { |
| 60 return std::vector<Category>({provided_category_}); |
| 61 } |
| 62 |
| 63 CategoryStatus PhysicalWebPageSuggestionsProvider::GetCategoryStatus( |
| 64 Category category) { |
| 65 return category_status_; |
| 66 } |
| 67 |
| 68 CategoryInfo PhysicalWebPageSuggestionsProvider::GetCategoryInfo( |
| 69 Category category) { |
| 70 // TODO(vitaliii): Use a proper string once it's been agreed on. |
| 71 return CategoryInfo(base::ASCIIToUTF16("Physical web pages"), |
| 72 ContentSuggestionsCardLayout::MINIMAL_CARD, |
| 73 /* has_more_button */ true); |
| 74 } |
| 75 |
| 76 void PhysicalWebPageSuggestionsProvider::DismissSuggestion( |
| 77 const std::string& suggestion_id) { |
| 78 // TODO(vitaliii): Implement this and then |
| 79 // ClearDismissedSuggestionsForDebugging. |
| 80 } |
| 81 |
| 82 void PhysicalWebPageSuggestionsProvider::FetchSuggestionImage( |
| 83 const std::string& suggestion_id, const ImageFetchedCallback& callback) { |
| 84 // TODO(vitaliii): Implement. |
| 85 } |
| 86 |
| 87 void PhysicalWebPageSuggestionsProvider::ClearCachedSuggestionsForDebugging( |
| 88 Category category) { |
| 89 // Ignored |
| 90 } |
| 91 |
| 92 std::vector<ContentSuggestion> |
| 93 PhysicalWebPageSuggestionsProvider::GetDismissedSuggestionsForDebugging( |
| 94 Category category) { |
| 95 // Not implemented. |
| 96 return std::vector<ContentSuggestion>(); |
| 97 } |
| 98 |
| 99 void PhysicalWebPageSuggestionsProvider::ClearDismissedSuggestionsForDebugging( |
| 100 Category category) { |
| 101 // TODO(vitaliii): Implement when dismissed suggestions are supported. |
| 102 } |
| 103 |
| 104 //////////////////////////////////////////////////////////////////////////////// |
| 105 // Private methods |
| 106 |
| 107 // Updates the |category_status_| and notifies the |observer_|, if necessary. |
| 108 void PhysicalWebPageSuggestionsProvider::NotifyStatusChanged( |
| 109 CategoryStatus new_status) { |
| 110 if (category_status_ == new_status) return; |
| 111 category_status_ = new_status; |
| 112 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 113 } |
| 114 |
| 115 } // namespace ntp_snippets |
OLD | NEW |