OLD | NEW |
---|---|
(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/stringprintf.h" | |
9 | |
10 using offline_pages::MultipleOfflinePageItemResult; | |
11 using offline_pages::OfflinePageModel; | |
12 using offline_pages::OfflinePageItem; | |
13 | |
14 namespace ntp_snippets { | |
15 | |
16 OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider( | |
17 OfflinePageModel* offline_page_model) | |
18 : ContentSuggestionsProvider({ContentSuggestionsCategory::OFFLINE_PAGES}), | |
19 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING), | |
20 offline_page_model_(offline_page_model) { | |
21 offline_page_model_->AddObserver(this); | |
22 FetchOfflinePages(); | |
23 } | |
24 | |
25 OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() {} | |
26 | |
27 // Inherited from KeyedService. | |
28 void OfflinePageSuggestionsProvider::Shutdown() { | |
29 offline_page_model_->RemoveObserver(this); | |
30 category_status_ = ContentSuggestionsCategoryStatus::NOT_PROVIDED; | |
31 } | |
32 | |
33 //////////////////////////////////////////////////////////////////////////////// | |
34 // Private methods | |
35 | |
36 void OfflinePageSuggestionsProvider::SetObserver( | |
37 ContentSuggestionsProvider::Observer* observer) { | |
38 observer_ = observer; | |
39 } | |
40 | |
41 ContentSuggestionsCategoryStatus | |
42 OfflinePageSuggestionsProvider::GetCategoryStatus( | |
43 ContentSuggestionsCategory category) { | |
44 // TODO(pke) Is this ok? | |
Marc Treib
2016/07/14 12:16:53
nit: "TODO(pke):" (add colon)
Philipp Keck
2016/07/14 13:46:02
Done.
| |
45 return ContentSuggestionsCategoryStatus::AVAILABLE; | |
Marc Treib
2016/07/14 12:16:53
Why not "return category_status_;" ?
Philipp Keck
2016/07/14 13:46:02
Done.
I had changed this entire method when I int
| |
46 } | |
47 | |
48 void OfflinePageSuggestionsProvider::DiscardSuggestion( | |
49 const std::string& suggestion_id) { | |
50 // TODO(pke) Implement some "dont show on NTP anymore" behaviour, | |
51 // then also implement ClearDiscardedSuggestionsForDebugging. | |
52 } | |
53 | |
54 void OfflinePageSuggestionsProvider::FetchSuggestionImage( | |
55 const std::string& suggestion_id, | |
56 const ImageFetchedCallback& callback) { | |
57 // TODO(pke) Implement | |
58 } | |
59 | |
60 void OfflinePageSuggestionsProvider::ClearCachedSuggestionsForDebugging() { | |
61 // Ignore | |
62 } | |
63 | |
64 void OfflinePageSuggestionsProvider::ClearDiscardedSuggestionsForDebugging() { | |
65 // Ignore | |
66 } | |
67 | |
68 void OfflinePageSuggestionsProvider::OfflinePageModelLoaded( | |
69 OfflinePageModel* model) { | |
70 // Ignore (when offline pages have been loaded, OnOfflinePagesLoaded will be | |
Marc Treib
2016/07/14 12:16:53
nit: DCHECK that model is the same that we have?
Philipp Keck
2016/07/14 13:46:02
Done.
| |
71 // called as a delayed callback, see ctor). | |
72 } | |
73 | |
74 void OfflinePageSuggestionsProvider::OfflinePageModelChanged( | |
75 OfflinePageModel* model) { | |
76 FetchOfflinePages(); | |
77 } | |
78 | |
79 void OfflinePageSuggestionsProvider::OfflinePageDeleted( | |
80 int64_t offline_id, | |
81 const offline_pages::ClientId& client_id) { | |
82 // TODO(pke) Implement (does the ofp have to be cleared from UI immediately?) | |
Marc Treib
2016/07/14 12:16:53
It should be, yes (the card will link to the offli
Philipp Keck
2016/07/14 13:46:02
Done.
| |
83 } | |
84 | |
85 void OfflinePageSuggestionsProvider::FetchOfflinePages() { | |
86 offline_page_model_->GetAllPages( | |
87 base::Bind(&OfflinePageSuggestionsProvider::OnOfflinePagesLoaded, | |
88 base::Unretained(this))); | |
89 } | |
90 | |
91 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded( | |
92 const MultipleOfflinePageItemResult& result) { | |
93 if (!observer_) { | |
94 category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE; | |
95 return; | |
96 } | |
97 | |
98 if (category_status_ != ContentSuggestionsCategoryStatus::AVAILABLE) { | |
99 category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE; | |
100 observer_->OnCategoryStatusChanged( | |
101 ContentSuggestionsCategory::OFFLINE_PAGES, category_status_); | |
102 } | |
103 | |
104 std::vector<ContentSuggestion> suggestions; | |
105 for (const OfflinePageItem& item : result) { | |
106 ContentSuggestion suggestion = ContentSuggestion( | |
Marc Treib
2016/07/14 12:16:53
nit: Just "ContentSuggestions suggestion(...)"
Philipp Keck
2016/07/14 13:46:02
Done.
| |
107 MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES, | |
108 std::to_string(item.offline_id)), | |
109 item.GetOfflineURL()); | |
110 | |
111 // TODO(pke): Sort my most recently visited and only keep the top one of | |
112 // multiple entries for the same URL. | |
113 // TODO(pke): Get more reasonable data from the OfflinePageModel here. | |
114 suggestion.set_title(item.url.spec()); | |
115 suggestion.set_snippet_text(std::string()); | |
116 suggestion.set_publish_date(item.creation_time); | |
117 suggestion.set_publisher_name(item.url.host()); | |
118 suggestions.emplace_back(std::move(suggestion)); | |
119 } | |
120 | |
121 observer_->OnNewSuggestions(ContentSuggestionsCategory::OFFLINE_PAGES, | |
122 std::move(suggestions)); | |
123 } | |
124 | |
125 } // namespace ntp_snippets | |
OLD | NEW |