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

Unified Diff: components/ntp_snippets/offline_pages/offline_page_suggestions_provider.cc

Issue 2149453004: Implement first version of OfflinePageSuggestionsProvider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Move the factory to c/b/android/ntp and Marc's comments Created 4 years, 5 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/offline_pages/offline_page_suggestions_provider.cc
diff --git a/components/ntp_snippets/offline_pages/offline_page_suggestions_provider.cc b/components/ntp_snippets/offline_pages/offline_page_suggestions_provider.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0545da89999109cff86f2e0850f5f86063fa8cbf
--- /dev/null
+++ b/components/ntp_snippets/offline_pages/offline_page_suggestions_provider.cc
@@ -0,0 +1,126 @@
+// 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/offline_pages/offline_page_suggestions_provider.h"
+
+#include "base/bind.h"
+#include "base/strings/stringprintf.h"
+
+using offline_pages::MultipleOfflinePageItemResult;
+using offline_pages::OfflinePageModel;
+using offline_pages::OfflinePageItem;
+
+namespace ntp_snippets {
+
+OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider(
+ OfflinePageModel* offline_page_model)
+ : ContentSuggestionsProvider({ContentSuggestionsCategory::OFFLINE_PAGES}),
+ category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING),
+ observer_(nullptr),
+ offline_page_model_(offline_page_model) {
+ offline_page_model_->AddObserver(this);
+ FetchOfflinePages();
Marc Treib 2016/07/14 14:17:24 Hm, this should probably happen in SetObserver, to
Philipp Keck 2016/07/14 15:30:06 Done. The same goes for the NTPSnippetsService as
+}
+
+OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() {}
+
+// Inherited from KeyedService.
+void OfflinePageSuggestionsProvider::Shutdown() {
+ offline_page_model_->RemoveObserver(this);
+ category_status_ = ContentSuggestionsCategoryStatus::NOT_PROVIDED;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+// Private methods
+
+void OfflinePageSuggestionsProvider::SetObserver(
+ ContentSuggestionsProvider::Observer* observer) {
+ observer_ = observer;
+}
+
+ContentSuggestionsCategoryStatus
+OfflinePageSuggestionsProvider::GetCategoryStatus(
+ ContentSuggestionsCategory category) {
+ return category_status_;
+}
+
+void OfflinePageSuggestionsProvider::DiscardSuggestion(
+ const std::string& suggestion_id) {
+ // TODO(pke): Implement some "dont show on NTP anymore" behaviour,
+ // then also implement ClearDiscardedSuggestionsForDebugging.
+}
+
+void OfflinePageSuggestionsProvider::FetchSuggestionImage(
+ const std::string& suggestion_id,
+ const ImageFetchedCallback& callback) {
+ // TODO(pke): Implement
+}
+
+void OfflinePageSuggestionsProvider::ClearCachedSuggestionsForDebugging() {
+ // Ignore
+}
+
+void OfflinePageSuggestionsProvider::ClearDiscardedSuggestionsForDebugging() {
+ // Ignore
+}
+
+void OfflinePageSuggestionsProvider::OfflinePageModelLoaded(
+ OfflinePageModel* model) {
+ DCHECK_EQ(model, offline_page_model_);
+ // Ignore (when offline pages have been loaded, OnOfflinePagesLoaded will be
+ // called as a delayed callback, see ctor).
+}
+
+void OfflinePageSuggestionsProvider::OfflinePageModelChanged(
+ OfflinePageModel* model) {
+ FetchOfflinePages();
Marc Treib 2016/07/14 14:17:24 DCHECK also here :)
Philipp Keck 2016/07/14 15:30:06 Done.
+}
+
+void OfflinePageSuggestionsProvider::OfflinePageDeleted(
+ int64_t offline_id,
+ const offline_pages::ClientId& client_id) {
+ // TODO(pke): Implement, suggestion has to be removed from UI immediately.
+}
+
+void OfflinePageSuggestionsProvider::FetchOfflinePages() {
+ offline_page_model_->GetAllPages(
+ base::Bind(&OfflinePageSuggestionsProvider::OnOfflinePagesLoaded,
+ base::Unretained(this)));
+}
+
+void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded(
+ const MultipleOfflinePageItemResult& result) {
+ if (!observer_) {
+ category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE;
+ return;
+ }
+
+ if (category_status_ != ContentSuggestionsCategoryStatus::AVAILABLE) {
+ category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE;
Marc Treib 2016/07/14 14:17:24 nit: For readability, I'd prefer having this assig
Philipp Keck 2016/07/14 15:30:06 Ok. Maybe that helper method should move to Conten
Marc Treib 2016/07/14 16:13:16 Hm, if so, then all the status handling needs to g
Philipp Keck 2016/07/15 09:08:42 Having all the status handling there would be diff
+ observer_->OnCategoryStatusChanged(
+ ContentSuggestionsCategory::OFFLINE_PAGES, category_status_);
+ }
+
+ std::vector<ContentSuggestion> suggestions;
+ for (const OfflinePageItem& item : result) {
+ ContentSuggestion suggestion(
+ MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES,
+ std::to_string(item.offline_id)),
Marc Treib 2016/07/14 14:17:24 This isn't allowed yet, see https://chromium-cpp.a
Philipp Keck 2016/07/14 15:30:06 Right. I'm sorry that things like this (or TODOs w
Marc Treib 2016/07/14 16:13:16 No worries!
+ item.GetOfflineURL());
+
+ // TODO(pke): Sort my most recently visited and only keep the top one of
+ // multiple entries for the same URL.
+ // TODO(pke): Get more reasonable data from the OfflinePageModel here.
+ suggestion.set_title(item.url.spec());
+ suggestion.set_snippet_text(std::string());
+ suggestion.set_publish_date(item.creation_time);
+ suggestion.set_publisher_name(item.url.host());
+ suggestions.emplace_back(std::move(suggestion));
+ }
+
+ observer_->OnNewSuggestions(ContentSuggestionsCategory::OFFLINE_PAGES,
+ std::move(suggestions));
+}
+
+} // namespace ntp_snippets

Powered by Google App Engine
This is Rietveld 408576698