Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Unified Diff: components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc

Issue 2228553003: a provider of Physical Web pages. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: added unittest Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..ced08da13e1549d473f64897f251396df4847659
--- /dev/null
+++ b/components/ntp_snippets/physical_web_pages/physical_web_page_suggestions_provider.cc
@@ -0,0 +1,113 @@
+// 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
+
+// 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 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.
+ 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.
+ 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));
+ }
+
+ 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_;
+}
+
+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) {
+ // 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.
+ 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

Powered by Google App Engine
This is Rietveld 408576698