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..5d786acaea4055578dcccb8e2c59aad2d1f5cffc |
--- /dev/null |
+++ b/components/ntp_snippets/offline_pages/offline_page_suggestions_provider.cc |
@@ -0,0 +1,130 @@ |
+// 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/string_number_conversions.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); |
+} |
+ |
+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; |
+ if (observer) { |
Bernhard Bauer
2016/07/14 18:47:43
Nit: no braces for single-line bodies.
Philipp Keck
2016/07/15 09:08:42
Done.
|
+ FetchOfflinePages(); |
+ } |
+} |
+ |
+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 |
dewittj
2016/07/14 18:13:28
Nit: Implement.
Philipp Keck
2016/07/15 09:08:42
Done.
|
+} |
+ |
+void OfflinePageSuggestionsProvider::ClearCachedSuggestionsForDebugging() { |
+ // Ignore |
dewittj
2016/07/14 18:13:29
Nit: Ignored.
Philipp Keck
2016/07/15 09:08:42
Done.
|
+} |
+ |
+void OfflinePageSuggestionsProvider::ClearDiscardedSuggestionsForDebugging() { |
+ // Ignore |
dewittj
2016/07/14 18:13:28
Ignored.
Or possibly put a TODO(pke): Implement w
Philipp Keck
2016/07/15 09:08:42
Done.
|
+} |
+ |
+void OfflinePageSuggestionsProvider::OfflinePageModelLoaded( |
+ OfflinePageModel* model) { |
+ DCHECK_EQ(model, offline_page_model_); |
Bernhard Bauer
2016/07/14 18:47:44
Expected value goes first, for nicer error message
Philipp Keck
2016/07/15 09:08:42
I know. Depends on whether you expect "ours" to be
|
+ // Ignore (when offline pages have been loaded, OnOfflinePagesLoaded will be |
+ // called as a delayed callback, see ctor). |
dewittj
2016/07/14 18:13:28
This comment seems out of date, there's nothing in
Philipp Keck
2016/07/15 09:08:42
Done.
|
+} |
+ |
+void OfflinePageSuggestionsProvider::OfflinePageModelChanged( |
+ OfflinePageModel* model) { |
+ DCHECK_EQ(model, offline_page_model_); |
+ FetchOfflinePages(); |
dewittj
2016/07/14 18:13:28
This is probably the best we can do for now, but s
Philipp Keck
2016/07/15 09:08:42
The OfflinePageModel currently does not have a Off
|
+} |
+ |
+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; |
+ observer_->OnCategoryStatusChanged( |
+ ContentSuggestionsCategory::OFFLINE_PAGES, category_status_); |
+ } |
+ |
+ std::vector<ContentSuggestion> suggestions; |
+ for (const OfflinePageItem& item : result) { |
+ ContentSuggestion suggestion( |
+ MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES, |
+ base::IntToString(item.offline_id)), |
+ 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 |