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 observer_(nullptr), | |
21 offline_page_model_(offline_page_model) { | |
22 offline_page_model_->AddObserver(this); | |
23 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
| |
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 } | |
41 | |
42 ContentSuggestionsCategoryStatus | |
43 OfflinePageSuggestionsProvider::GetCategoryStatus( | |
44 ContentSuggestionsCategory category) { | |
45 return category_status_; | |
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 DCHECK_EQ(model, offline_page_model_); | |
71 // Ignore (when offline pages have been loaded, OnOfflinePagesLoaded will be | |
72 // called as a delayed callback, see ctor). | |
73 } | |
74 | |
75 void OfflinePageSuggestionsProvider::OfflinePageModelChanged( | |
76 OfflinePageModel* model) { | |
77 FetchOfflinePages(); | |
Marc Treib
2016/07/14 14:17:24
DCHECK also here :)
Philipp Keck
2016/07/14 15:30:06
Done.
| |
78 } | |
79 | |
80 void OfflinePageSuggestionsProvider::OfflinePageDeleted( | |
81 int64_t offline_id, | |
82 const offline_pages::ClientId& client_id) { | |
83 // TODO(pke): Implement, suggestion has to be removed from UI immediately. | |
84 } | |
85 | |
86 void OfflinePageSuggestionsProvider::FetchOfflinePages() { | |
87 offline_page_model_->GetAllPages( | |
88 base::Bind(&OfflinePageSuggestionsProvider::OnOfflinePagesLoaded, | |
89 base::Unretained(this))); | |
90 } | |
91 | |
92 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded( | |
93 const MultipleOfflinePageItemResult& result) { | |
94 if (!observer_) { | |
95 category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE; | |
96 return; | |
97 } | |
98 | |
99 if (category_status_ != ContentSuggestionsCategoryStatus::AVAILABLE) { | |
100 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
| |
101 observer_->OnCategoryStatusChanged( | |
102 ContentSuggestionsCategory::OFFLINE_PAGES, category_status_); | |
103 } | |
104 | |
105 std::vector<ContentSuggestion> suggestions; | |
106 for (const OfflinePageItem& item : result) { | |
107 ContentSuggestion suggestion( | |
108 MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES, | |
109 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!
| |
110 item.GetOfflineURL()); | |
111 | |
112 // TODO(pke): Sort my most recently visited and only keep the top one of | |
113 // multiple entries for the same URL. | |
114 // TODO(pke): Get more reasonable data from the OfflinePageModel here. | |
115 suggestion.set_title(item.url.spec()); | |
116 suggestion.set_snippet_text(std::string()); | |
117 suggestion.set_publish_date(item.creation_time); | |
118 suggestion.set_publisher_name(item.url.host()); | |
119 suggestions.emplace_back(std::move(suggestion)); | |
120 } | |
121 | |
122 observer_->OnNewSuggestions(ContentSuggestionsCategory::OFFLINE_PAGES, | |
123 std::move(suggestions)); | |
124 } | |
125 | |
126 } // namespace ntp_snippets | |
OLD | NEW |