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( |
| 19 {ContentSuggestionsCategory::OFFLINE_PAGES_CACHED}), |
| 20 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING), |
| 21 offline_page_model_(offline_page_model) { |
| 22 offline_page_model_->AddObserver(this); |
| 23 FetchOfflinePages(); |
| 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 // TODO(pke) Is this ok? |
| 46 return ContentSuggestionsCategoryStatus::AVAILABLE; |
| 47 } |
| 48 |
| 49 void OfflinePageSuggestionsProvider::DiscardSuggestion( |
| 50 const std::string& suggestion_id) { |
| 51 // TODO(pke) Implement some "dont show on NTP anymore" behaviour, |
| 52 // then also implement ClearDiscardedSuggestionsForDebugging. |
| 53 } |
| 54 |
| 55 void OfflinePageSuggestionsProvider::FetchSuggestionImage( |
| 56 const std::string& suggestion_id, |
| 57 const ImageFetchedCallback& callback) { |
| 58 // TODO(pke) Implement |
| 59 } |
| 60 |
| 61 void OfflinePageSuggestionsProvider::ClearCachedSuggestionsForDebugging() { |
| 62 // Ignore |
| 63 } |
| 64 |
| 65 void OfflinePageSuggestionsProvider::ClearDiscardedSuggestionsForDebugging() { |
| 66 // Ignore |
| 67 } |
| 68 |
| 69 void OfflinePageSuggestionsProvider::OfflinePageModelLoaded( |
| 70 OfflinePageModel* 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 LOG(WARNING) << "Fetching ..."; |
| 78 FetchOfflinePages(); |
| 79 } |
| 80 |
| 81 void OfflinePageSuggestionsProvider::OfflinePageDeleted( |
| 82 int64_t offline_id, |
| 83 const offline_pages::ClientId& client_id) { |
| 84 // TODO(pke) Implement (does the ofp have to be cleared from UI immediately?) |
| 85 } |
| 86 |
| 87 void OfflinePageSuggestionsProvider::FetchOfflinePages() { |
| 88 offline_page_model_->GetAllPages( |
| 89 base::Bind(&OfflinePageSuggestionsProvider::OnOfflinePagesLoaded, |
| 90 base::Unretained(this))); |
| 91 } |
| 92 |
| 93 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded( |
| 94 const MultipleOfflinePageItemResult& result) { |
| 95 if (!observer_) { |
| 96 category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE; |
| 97 return; |
| 98 } |
| 99 |
| 100 if (category_status_ != ContentSuggestionsCategoryStatus::AVAILABLE) { |
| 101 category_status_ = ContentSuggestionsCategoryStatus::AVAILABLE; |
| 102 observer_->OnCategoryStatusChanged( |
| 103 ContentSuggestionsCategory::OFFLINE_PAGES_CACHED, category_status_); |
| 104 } |
| 105 |
| 106 std::vector<ContentSuggestion> suggestions; |
| 107 for (const OfflinePageItem& item : result) { |
| 108 ContentSuggestion suggestion = ContentSuggestion( |
| 109 MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES_CACHED, |
| 110 std::to_string(item.offline_id)), |
| 111 item.GetOfflineURL()); |
| 112 |
| 113 // TODO(pke): Possibly filter duplicates (only the newest per unique URL), |
| 114 // or use better OfflinePageModel::Get method that chooses the "best" |
| 115 // version automatically. |
| 116 // TODO(pke): Get reasonable data from the OfflinePageModel here. |
| 117 suggestion.set_title(item.url.spec()); |
| 118 suggestion.set_snippet_text(std::string()); |
| 119 suggestion.set_publish_date(item.creation_time); |
| 120 suggestion.set_publisher_name(item.url.host()); |
| 121 suggestions.emplace_back(std::move(suggestion)); |
| 122 } |
| 123 observer_->OnNewSuggestions(ContentSuggestionsCategory::OFFLINE_PAGES_CACHED, |
| 124 std::move(suggestions)); |
| 125 } |
| 126 |
| 127 } // namespace ntp_snippets |
OLD | NEW |