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