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..507207ce45bd2c0cf66d5c82b400ca2ef94389bb |
--- /dev/null |
+++ b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc |
@@ -0,0 +1,111 @@ |
+// 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 int kMaxSuggestionsCount = 10; |
+ |
+} // namespace |
+ |
+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)) { |
+ |
+ // TODO(vitaliii): remove when Physical Web service C++ interface is provided. |
+ std::vector<UrlInfo> urls; |
+ UrlInfo url_info; |
+ url_info.title = "Mockup PW suggestion"; |
+ url_info.description = "This is a hardcoded PW suggestion."; |
+ url_info.scan_time = base::Time::Now(); |
+ url_info.site_url = GURL("https://google.com"); |
+ urls.push_back(url_info); |
+ |
+ url_info.title = "Another mockup PW suggestion"; |
+ url_info.description = "This is another hardcoded PW suggestion."; |
+ urls.push_back(url_info); |
+ |
+ onDisplayableUrlsChanged(urls); |
+} |
+ |
+PhysicalWebPageSuggestionsProvider::~PhysicalWebPageSuggestionsProvider() { |
+} |
+ |
+//////////////////////////////////////////////////////////////////////////////// |
+// Private methods |
+ |
+std::vector<Category> PhysicalWebPageSuggestionsProvider::GetProvidedCategories() { |
tschumann
2016/08/09 15:24:59
line too long. clang-format (go/clang-format) sugg
vitaliii
2016/08/10 14:18:41
Done.
|
+ return std::vector<Category>({provided_category_}); |
+} |
+ |
+CategoryStatus PhysicalWebPageSuggestionsProvider::GetCategoryStatus( |
+ Category category) { |
+ return category_status_; |
+} |
+ |
+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() { |
+ // Ignored. |
+} |
+ |
+void PhysicalWebPageSuggestionsProvider::ClearDismissedSuggestionsForDebugging() { |
tschumann
2016/08/09 15:24:59
line > 80 columns.
clangformat (go/clangformat) su
vitaliii
2016/08/10 14:18:41
Done.
|
+ // TODO(vitaliii): Implement when dismissed suggestions are supported. |
+} |
+ |
+void PhysicalWebPageSuggestionsProvider::onDisplayableUrlsChanged( |
+ const std::vector<UrlInfo>& urls) { |
+ NotifyStatusChanged(CategoryStatus::AVAILABLE); |
+ std::vector<ContentSuggestion> suggestions; |
+ |
+ for (int i = 0; i < static_cast<int>(urls.size()) && i < kMaxSuggestionsCount; |
+ ++i) { |
Marc Treib
2016/08/09 12:45:17
range-based for ftw!
for (const UrlInfo& url_info
vitaliii
2016/08/10 14:18:41
Done.
|
+ const UrlInfo& url_info = urls[i]; |
+ 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)); |
+ } |
+ |
+ observer()->OnNewSuggestions(this, provided_category_, |
+ std::move(suggestions)); |
tschumann
2016/08/09 15:24:59
indentation.
vitaliii
2016/08/10 14:18:41
Done.
|
+} |
+ |
+void PhysicalWebPageSuggestionsProvider::NotifyStatusChanged( |
+ CategoryStatus new_status) { |
+ if (category_status_ == new_status) |
+ return; |
+ category_status_ = new_status; |
+ |
tschumann
2016/08/09 15:24:59
drop the blank line? In general, i'd try to minimi
vitaliii
2016/08/10 14:18:41
Done.
|
+ observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
+} |
+ |
+} // namespace ntp_snippets |