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/string_number_conversions.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 } |
| 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 if (observer) |
| 40 FetchOfflinePages(); |
| 41 } |
| 42 |
| 43 ContentSuggestionsCategoryStatus |
| 44 OfflinePageSuggestionsProvider::GetCategoryStatus( |
| 45 ContentSuggestionsCategory category) { |
| 46 return category_status_; |
| 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 // Ignored. |
| 63 } |
| 64 |
| 65 void OfflinePageSuggestionsProvider::ClearDiscardedSuggestionsForDebugging() { |
| 66 // TODO(pke): Implement when discarded suggestions are supported. |
| 67 } |
| 68 |
| 69 void OfflinePageSuggestionsProvider::OfflinePageModelLoaded( |
| 70 OfflinePageModel* model) { |
| 71 DCHECK_EQ(offline_page_model_, model); |
| 72 } |
| 73 |
| 74 void OfflinePageSuggestionsProvider::OfflinePageModelChanged( |
| 75 OfflinePageModel* model) { |
| 76 DCHECK_EQ(offline_page_model_, model); |
| 77 FetchOfflinePages(); |
| 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 NotifyStatusChanged(ContentSuggestionsCategoryStatus::AVAILABLE); |
| 95 if (!observer_) |
| 96 return; |
| 97 |
| 98 std::vector<ContentSuggestion> suggestions; |
| 99 for (const OfflinePageItem& item : result) { |
| 100 ContentSuggestion suggestion( |
| 101 MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES, |
| 102 base::IntToString(item.offline_id)), |
| 103 item.GetOfflineURL()); |
| 104 |
| 105 // TODO(pke): Sort my most recently visited and only keep the top one of |
| 106 // multiple entries for the same URL. |
| 107 // TODO(pke): Get more reasonable data from the OfflinePageModel here. |
| 108 suggestion.set_title(item.url.spec()); |
| 109 suggestion.set_snippet_text(std::string()); |
| 110 suggestion.set_publish_date(item.creation_time); |
| 111 suggestion.set_publisher_name(item.url.host()); |
| 112 suggestions.emplace_back(std::move(suggestion)); |
| 113 } |
| 114 |
| 115 observer_->OnNewSuggestions(ContentSuggestionsCategory::OFFLINE_PAGES, |
| 116 std::move(suggestions)); |
| 117 } |
| 118 |
| 119 void OfflinePageSuggestionsProvider::NotifyStatusChanged( |
| 120 ContentSuggestionsCategoryStatus new_status) { |
| 121 if (category_status_ == new_status) |
| 122 return; |
| 123 category_status_ = new_status; |
| 124 |
| 125 if (!observer_) |
| 126 return; |
| 127 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::OFFLINE_PAGES, |
| 128 new_status); |
| 129 } |
| 130 |
| 131 } // namespace ntp_snippets |
OLD | NEW |