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

Side by Side 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: Small fixes 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide r.h"
6
7 #include "base/bind.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/stringprintf.h"
10
11 using offline_pages::MultipleOfflinePageItemResult;
12 using offline_pages::OfflinePageModel;
13 using offline_pages::OfflinePageItem;
14
15 namespace ntp_snippets {
16
17 OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider(
18 OfflinePageModel* offline_page_model)
19 : ContentSuggestionsProvider({ContentSuggestionsCategory::OFFLINE_PAGES}),
20 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING),
21 observer_(nullptr),
22 offline_page_model_(offline_page_model) {
23 offline_page_model_->AddObserver(this);
24 }
25
26 OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() {}
27
28 // Inherited from KeyedService.
29 void OfflinePageSuggestionsProvider::Shutdown() {
30 offline_page_model_->RemoveObserver(this);
31 category_status_ = ContentSuggestionsCategoryStatus::NOT_PROVIDED;
32 }
33
34 ////////////////////////////////////////////////////////////////////////////////
35 // Private methods
36
37 void OfflinePageSuggestionsProvider::SetObserver(
38 ContentSuggestionsProvider::Observer* observer) {
39 observer_ = observer;
40 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.
41 FetchOfflinePages();
42 }
43 }
44
45 ContentSuggestionsCategoryStatus
46 OfflinePageSuggestionsProvider::GetCategoryStatus(
47 ContentSuggestionsCategory category) {
48 return category_status_;
49 }
50
51 void OfflinePageSuggestionsProvider::DiscardSuggestion(
52 const std::string& suggestion_id) {
53 // TODO(pke): Implement some "dont show on NTP anymore" behaviour,
54 // then also implement ClearDiscardedSuggestionsForDebugging.
55 }
56
57 void OfflinePageSuggestionsProvider::FetchSuggestionImage(
58 const std::string& suggestion_id,
59 const ImageFetchedCallback& callback) {
60 // TODO(pke): Implement
dewittj 2016/07/14 18:13:28 Nit: Implement.
Philipp Keck 2016/07/15 09:08:42 Done.
61 }
62
63 void OfflinePageSuggestionsProvider::ClearCachedSuggestionsForDebugging() {
64 // Ignore
dewittj 2016/07/14 18:13:29 Nit: Ignored.
Philipp Keck 2016/07/15 09:08:42 Done.
65 }
66
67 void OfflinePageSuggestionsProvider::ClearDiscardedSuggestionsForDebugging() {
68 // 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.
69 }
70
71 void OfflinePageSuggestionsProvider::OfflinePageModelLoaded(
72 OfflinePageModel* model) {
73 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
74 // Ignore (when offline pages have been loaded, OnOfflinePagesLoaded will be
75 // 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.
76 }
77
78 void OfflinePageSuggestionsProvider::OfflinePageModelChanged(
79 OfflinePageModel* model) {
80 DCHECK_EQ(model, offline_page_model_);
81 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
82 }
83
84 void OfflinePageSuggestionsProvider::OfflinePageDeleted(
85 int64_t offline_id,
86 const offline_pages::ClientId& client_id) {
87 // TODO(pke): Implement, suggestion has to be removed from UI immediately.
88 }
89
90 void OfflinePageSuggestionsProvider::FetchOfflinePages() {
91 offline_page_model_->GetAllPages(
92 base::Bind(&OfflinePageSuggestionsProvider::OnOfflinePagesLoaded,
93 base::Unretained(this)));
94 }
95
96 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded(
97 const MultipleOfflinePageItemResult& result) {
98 if (!observer_) {
99 category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE;
100 return;
101 }
102
103 if (category_status_ != ContentSuggestionsCategoryStatus::AVAILABLE) {
104 category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE;
105 observer_->OnCategoryStatusChanged(
106 ContentSuggestionsCategory::OFFLINE_PAGES, category_status_);
107 }
108
109 std::vector<ContentSuggestion> suggestions;
110 for (const OfflinePageItem& item : result) {
111 ContentSuggestion suggestion(
112 MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES,
113 base::IntToString(item.offline_id)),
114 item.GetOfflineURL());
115
116 // TODO(pke): Sort my most recently visited and only keep the top one of
117 // multiple entries for the same URL.
118 // TODO(pke): Get more reasonable data from the OfflinePageModel here.
119 suggestion.set_title(item.url.spec());
120 suggestion.set_snippet_text(std::string());
121 suggestion.set_publish_date(item.creation_time);
122 suggestion.set_publisher_name(item.url.host());
123 suggestions.emplace_back(std::move(suggestion));
124 }
125
126 observer_->OnNewSuggestions(ContentSuggestionsCategory::OFFLINE_PAGES,
127 std::move(suggestions));
128 }
129
130 } // namespace ntp_snippets
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698