Index: components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc |
diff --git a/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f58427495dba54fc75a90936100a3eea9ac222a7 |
--- /dev/null |
+++ b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc |
@@ -0,0 +1,118 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.h" |
+ |
+#include "base/bind.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/strings/string_util.h" |
+#include "base/strings/utf_string_conversions.h" |
+ |
+namespace ntp_snippets { |
+ |
+namespace { |
+ |
+const size_t kMaxSuggestionsCount = 10; |
+ |
+} // namespace |
+ |
+// TODO(vitaliii): remove when Physical Web C++ interface is provided. |
+UrlInfo::UrlInfo() {} |
+UrlInfo::~UrlInfo() {} |
+UrlInfo::UrlInfo(const UrlInfo& other) = default; |
+ |
+PhysicalWebPageSuggestionsProvider::PhysicalWebPageSuggestionsProvider( |
+ ContentSuggestionsProvider::Observer* observer, |
+ CategoryFactory* category_factory) |
+ : ContentSuggestionsProvider(observer, category_factory), |
+ category_status_(CategoryStatus::AVAILABLE_LOADING), |
+ provided_category_(category_factory->FromKnownCategory( |
+ KnownCategories::PHYSICAL_WEB_PAGES)) {} |
+ |
+PhysicalWebPageSuggestionsProvider::~PhysicalWebPageSuggestionsProvider() {} |
+ |
+void PhysicalWebPageSuggestionsProvider::OnDisplayableUrlsChanged( |
+ const std::vector<UrlInfo>& urls) { |
+ NotifyStatusChanged(CategoryStatus::AVAILABLE); |
+ std::vector<ContentSuggestion> suggestions; |
+ |
+ for (const UrlInfo& url_info : urls) { |
+ if (suggestions.size() >= kMaxSuggestionsCount) { |
Bernhard Bauer
2016/08/12 12:55:19
Nit: Braces are optional for single-line bodies. I
vitaliii
2016/08/12 13:32:47
Done.
|
+ break; |
+ } |
+ |
+ ContentSuggestion suggestion( |
+ MakeUniqueID(provided_category_, url_info.site_url.spec()), |
+ url_info.site_url); |
+ |
+ suggestion.set_title(base::UTF8ToUTF16(url_info.title)); |
+ suggestion.set_snippet_text(base::UTF8ToUTF16(url_info.description)); |
+ suggestion.set_publish_date(url_info.scan_time); |
+ suggestion.set_publisher_name(base::UTF8ToUTF16(url_info.site_url.host())); |
+ suggestions.emplace_back(std::move(suggestion)); |
Bernhard Bauer
2016/08/12 12:55:19
Nit: Use push_back()? It feels a bit strange to us
vitaliii
2016/08/12 13:32:47
Done.
|
+ } |
+ |
+ observer()->OnNewSuggestions(this, provided_category_, |
+ std::move(suggestions)); |
+} |
+ |
+std::vector<Category> |
+PhysicalWebPageSuggestionsProvider::GetProvidedCategories() { |
+ return std::vector<Category>({provided_category_}); |
+} |
+ |
+CategoryStatus PhysicalWebPageSuggestionsProvider::GetCategoryStatus( |
+ Category category) { |
+ return category_status_; |
+} |
+ |
+CategoryInfo PhysicalWebPageSuggestionsProvider::GetCategoryInfo( |
+ Category category) { |
+ // TODO(vitaliii): Use a proper string once it's been agreed on. |
+ return CategoryInfo(base::ASCIIToUTF16("Physical web pages"), |
+ ContentSuggestionsCardLayout::MINIMAL_CARD); |
+} |
+ |
+void PhysicalWebPageSuggestionsProvider::DismissSuggestion( |
+ const std::string& suggestion_id) { |
+ // TODO(vitaliii): Implement this and then |
+ // ClearDismissedSuggestionsForDebugging. |
+} |
+ |
+void PhysicalWebPageSuggestionsProvider::FetchSuggestionImage( |
+ const std::string& suggestion_id, |
+ const ImageFetchedCallback& callback) { |
+ // TODO(vitaliii): Implement. |
+} |
+ |
+void PhysicalWebPageSuggestionsProvider::ClearCachedSuggestionsForDebugging( |
+ Category category) { |
+ // Ignored |
+} |
+ |
+std::vector<ContentSuggestion> |
+PhysicalWebPageSuggestionsProvider::GetDismissedSuggestionsForDebugging( |
+ Category category) { |
+ // Not implemented. |
+ return std::vector<ContentSuggestion>(); |
+} |
+ |
+void PhysicalWebPageSuggestionsProvider::ClearDismissedSuggestionsForDebugging( |
+ Category category) { |
+ // TODO(vitaliii): Implement when dismissed suggestions are supported. |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// Private methods |
+ |
+// Updates the |category_status_| and notifies the |observer_|, if necessary. |
+void PhysicalWebPageSuggestionsProvider::NotifyStatusChanged( |
+ CategoryStatus new_status) { |
+ if (category_status_ == new_status) |
+ return; |
+ category_status_ = new_status; |
+ observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
+} |
+ |
+} // namespace ntp_snippets |