| 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
|
| index ee8a25484f9aa172a3904d64b6e5d2d666c62368..897bc5b45e0356fc4062cd8cd7940c2943a38bb4 100644
|
| --- a/components/ntp_snippets/offline_pages/offline_page_suggestions_provider.cc
|
| +++ b/components/ntp_snippets/offline_pages/offline_page_suggestions_provider.cc
|
| @@ -9,6 +9,10 @@
|
| #include "base/strings/string_number_conversions.h"
|
| #include "base/strings/utf_string_conversions.h"
|
| #include "base/threading/thread_task_runner_handle.h"
|
| +#include "base/values.h"
|
| +#include "components/ntp_snippets/pref_names.h"
|
| +#include "components/prefs/pref_registry_simple.h"
|
| +#include "components/prefs/pref_service.h"
|
| #include "ui/gfx/image/image.h"
|
|
|
| using offline_pages::MultipleOfflinePageItemResult;
|
| @@ -26,13 +30,16 @@ const int kMaxSuggestionsCount = 5;
|
| OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider(
|
| ContentSuggestionsProvider::Observer* observer,
|
| CategoryFactory* category_factory,
|
| - OfflinePageModel* offline_page_model)
|
| + OfflinePageModel* offline_page_model,
|
| + PrefService* pref_service)
|
| : ContentSuggestionsProvider(observer, category_factory),
|
| category_status_(CategoryStatus::AVAILABLE_LOADING),
|
| offline_page_model_(offline_page_model),
|
| provided_category_(
|
| - category_factory->FromKnownCategory(KnownCategories::OFFLINE_PAGES)) {
|
| + category_factory->FromKnownCategory(KnownCategories::OFFLINE_PAGES)),
|
| + pref_service_(pref_service) {
|
| offline_page_model_->AddObserver(this);
|
| + ReadDismissedIDsFromPrefs();
|
| FetchOfflinePages();
|
| }
|
|
|
| @@ -40,6 +47,12 @@ OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() {
|
| offline_page_model_->RemoveObserver(this);
|
| }
|
|
|
| +// static
|
| +void OfflinePageSuggestionsProvider::RegisterProfilePrefs(
|
| + PrefRegistrySimple* registry) {
|
| + registry->RegisterListPref(prefs::kDismissedOfflinePageSuggestions);
|
| +}
|
| +
|
| ////////////////////////////////////////////////////////////////////////////////
|
| // Private methods
|
|
|
| @@ -62,8 +75,9 @@ CategoryInfo OfflinePageSuggestionsProvider::GetCategoryInfo(
|
|
|
| void OfflinePageSuggestionsProvider::DismissSuggestion(
|
| const std::string& suggestion_id) {
|
| - // TODO(pke): Implement some "dont show on NTP anymore" behaviour,
|
| - // then also implement ClearDismissedSuggestionsForDebugging.
|
| + std::string offline_page_id = GetWithinCategoryIDFromUniqueID(suggestion_id);
|
| + dismissed_ids_.insert(offline_page_id);
|
| + StoreDismissedIDsToPrefs();
|
| }
|
|
|
| void OfflinePageSuggestionsProvider::FetchSuggestionImage(
|
| @@ -84,15 +98,26 @@ void OfflinePageSuggestionsProvider::ClearCachedSuggestionsForDebugging(
|
| std::vector<ContentSuggestion>
|
| OfflinePageSuggestionsProvider::GetDismissedSuggestionsForDebugging(
|
| Category category) {
|
| + // TODO(pke): Make GetDismissedSuggestionsForDebugging asynchronous so this
|
| + // can return proper values.
|
| DCHECK_EQ(category, provided_category_);
|
| - // TODO(pke): Implement when dismissed suggestions are supported.
|
| - return std::vector<ContentSuggestion>();
|
| + std::vector<ContentSuggestion> suggestions;
|
| + for (const std::string& dismissed_id : dismissed_ids_) {
|
| + ContentSuggestion suggestion(
|
| + MakeUniqueID(provided_category_, dismissed_id),
|
| + GURL("http://dismissed-offline-page-" + dismissed_id));
|
| + suggestion.set_title(base::UTF8ToUTF16("Title not available"));
|
| + suggestions.push_back(std::move(suggestion));
|
| + }
|
| + return suggestions;
|
| }
|
|
|
| void OfflinePageSuggestionsProvider::ClearDismissedSuggestionsForDebugging(
|
| Category category) {
|
| DCHECK_EQ(category, provided_category_);
|
| - // TODO(pke): Implement when dismissed suggestions are supported.
|
| + dismissed_ids_.clear();
|
| + StoreDismissedIDsToPrefs();
|
| + FetchOfflinePages();
|
| }
|
|
|
| void OfflinePageSuggestionsProvider::OfflinePageModelLoaded(
|
| @@ -124,21 +149,9 @@ void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded(
|
|
|
| std::vector<ContentSuggestion> suggestions;
|
| for (const OfflinePageItem& item : result) {
|
| - // TODO(pke): Make sure the URL is actually opened as an offline URL.
|
| - // Currently, the browser opens the offline URL and then immediately
|
| - // redirects to the online URL if the device is online.
|
| - ContentSuggestion suggestion(
|
| - MakeUniqueID(provided_category_, 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(base::UTF8ToUTF16(item.url.spec()));
|
| - suggestion.set_snippet_text(base::string16());
|
| - suggestion.set_publish_date(item.creation_time);
|
| - suggestion.set_publisher_name(base::UTF8ToUTF16(item.url.host()));
|
| - suggestions.emplace_back(std::move(suggestion));
|
| + if (dismissed_ids_.count(base::IntToString(item.offline_id)))
|
| + continue;
|
| + suggestions.push_back(ConvertOfflinePage(item));
|
| if (suggestions.size() == kMaxSuggestionsCount)
|
| break;
|
| }
|
| @@ -156,4 +169,43 @@ void OfflinePageSuggestionsProvider::NotifyStatusChanged(
|
| observer()->OnCategoryStatusChanged(this, provided_category_, new_status);
|
| }
|
|
|
| +ContentSuggestion OfflinePageSuggestionsProvider::ConvertOfflinePage(
|
| + const OfflinePageItem& offline_page) const {
|
| + // TODO(pke): Make sure the URL is actually opened as an offline URL.
|
| + // Currently, the browser opens the offline URL and then immediately
|
| + // redirects to the online URL if the device is online.
|
| + ContentSuggestion suggestion(
|
| + MakeUniqueID(provided_category_,
|
| + base::IntToString(offline_page.offline_id)),
|
| + offline_page.GetOfflineURL());
|
| +
|
| + // TODO(pke): Sort by 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(base::UTF8ToUTF16(offline_page.url.spec()));
|
| + suggestion.set_snippet_text(base::string16());
|
| + suggestion.set_publish_date(offline_page.creation_time);
|
| + suggestion.set_publisher_name(base::UTF8ToUTF16(offline_page.url.host()));
|
| + return suggestion;
|
| +}
|
| +
|
| +void OfflinePageSuggestionsProvider::ReadDismissedIDsFromPrefs() {
|
| + dismissed_ids_.clear();
|
| + const base::ListValue* list =
|
| + pref_service_->GetList(prefs::kDismissedOfflinePageSuggestions);
|
| + for (const std::unique_ptr<base::Value>& value : *list) {
|
| + std::string dismissed_id;
|
| + bool success = value->GetAsString(&dismissed_id);
|
| + DCHECK(success) << "Failed to parse dismissed offline page ID from prefs";
|
| + dismissed_ids_.insert(std::move(dismissed_id));
|
| + }
|
| +}
|
| +
|
| +void OfflinePageSuggestionsProvider::StoreDismissedIDsToPrefs() {
|
| + base::ListValue list;
|
| + for (const std::string& dismissed_id : dismissed_ids_)
|
| + list.AppendString(dismissed_id);
|
| + pref_service_->Set(prefs::kDismissedOfflinePageSuggestions, list);
|
| +}
|
| +
|
| } // namespace ntp_snippets
|
|
|