| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide
r.h" | 5 #include "components/ntp_snippets/offline_pages/offline_page_suggestions_provide
r.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
| 9 | 9 |
| 10 using offline_pages::MultipleOfflinePageItemResult; | 10 using offline_pages::MultipleOfflinePageItemResult; |
| 11 using offline_pages::OfflinePageModel; | 11 using offline_pages::OfflinePageModel; |
| 12 using offline_pages::OfflinePageItem; | 12 using offline_pages::OfflinePageItem; |
| 13 | 13 |
| 14 namespace ntp_snippets { | 14 namespace ntp_snippets { |
| 15 | 15 |
| 16 namespace { |
| 17 |
| 18 const int kMaxSuggestionsCount = 5; |
| 19 |
| 20 } // namespace |
| 21 |
| 16 OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider( | 22 OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider( |
| 17 OfflinePageModel* offline_page_model) | 23 OfflinePageModel* offline_page_model) |
| 18 : ContentSuggestionsProvider({ContentSuggestionsCategory::OFFLINE_PAGES}), | 24 : ContentSuggestionsProvider({ContentSuggestionsCategory::OFFLINE_PAGES}), |
| 19 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING), | 25 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING), |
| 20 observer_(nullptr), | 26 observer_(nullptr), |
| 21 offline_page_model_(offline_page_model) { | 27 offline_page_model_(offline_page_model) { |
| 22 offline_page_model_->AddObserver(this); | 28 offline_page_model_->AddObserver(this); |
| 23 } | 29 } |
| 24 | 30 |
| 25 OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() {} | 31 OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() {} |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 90 } | 96 } |
| 91 | 97 |
| 92 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded( | 98 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded( |
| 93 const MultipleOfflinePageItemResult& result) { | 99 const MultipleOfflinePageItemResult& result) { |
| 94 NotifyStatusChanged(ContentSuggestionsCategoryStatus::AVAILABLE); | 100 NotifyStatusChanged(ContentSuggestionsCategoryStatus::AVAILABLE); |
| 95 if (!observer_) | 101 if (!observer_) |
| 96 return; | 102 return; |
| 97 | 103 |
| 98 std::vector<ContentSuggestion> suggestions; | 104 std::vector<ContentSuggestion> suggestions; |
| 99 for (const OfflinePageItem& item : result) { | 105 for (const OfflinePageItem& item : result) { |
| 106 // TODO(pke): Make sure the URL is actually opened as an offline URL. |
| 107 // Currently, the browser opens the offline URL and then immediately |
| 108 // redirects to the online URL if the device is online. |
| 100 ContentSuggestion suggestion( | 109 ContentSuggestion suggestion( |
| 101 MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES, | 110 MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES, |
| 102 base::IntToString(item.offline_id)), | 111 base::IntToString(item.offline_id)), |
| 103 item.GetOfflineURL()); | 112 item.GetOfflineURL()); |
| 104 | 113 |
| 105 // TODO(pke): Sort my most recently visited and only keep the top one of | 114 // TODO(pke): Sort my most recently visited and only keep the top one of |
| 106 // multiple entries for the same URL. | 115 // multiple entries for the same URL. |
| 107 // TODO(pke): Get more reasonable data from the OfflinePageModel here. | 116 // TODO(pke): Get more reasonable data from the OfflinePageModel here. |
| 108 suggestion.set_title(item.url.spec()); | 117 suggestion.set_title(item.url.spec()); |
| 109 suggestion.set_snippet_text(std::string()); | 118 suggestion.set_snippet_text(std::string()); |
| 110 suggestion.set_publish_date(item.creation_time); | 119 suggestion.set_publish_date(item.creation_time); |
| 111 suggestion.set_publisher_name(item.url.host()); | 120 suggestion.set_publisher_name(item.url.host()); |
| 112 suggestions.emplace_back(std::move(suggestion)); | 121 suggestions.emplace_back(std::move(suggestion)); |
| 122 if (suggestions.size() == kMaxSuggestionsCount) |
| 123 break; |
| 113 } | 124 } |
| 114 | 125 |
| 115 observer_->OnNewSuggestions(ContentSuggestionsCategory::OFFLINE_PAGES, | 126 observer_->OnNewSuggestions(ContentSuggestionsCategory::OFFLINE_PAGES, |
| 116 std::move(suggestions)); | 127 std::move(suggestions)); |
| 117 } | 128 } |
| 118 | 129 |
| 119 void OfflinePageSuggestionsProvider::NotifyStatusChanged( | 130 void OfflinePageSuggestionsProvider::NotifyStatusChanged( |
| 120 ContentSuggestionsCategoryStatus new_status) { | 131 ContentSuggestionsCategoryStatus new_status) { |
| 121 if (category_status_ == new_status) | 132 if (category_status_ == new_status) |
| 122 return; | 133 return; |
| 123 category_status_ = new_status; | 134 category_status_ = new_status; |
| 124 | 135 |
| 125 if (!observer_) | 136 if (!observer_) |
| 126 return; | 137 return; |
| 127 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::OFFLINE_PAGES, | 138 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::OFFLINE_PAGES, |
| 128 new_status); | 139 new_status); |
| 129 } | 140 } |
| 130 | 141 |
| 131 } // namespace ntp_snippets | 142 } // namespace ntp_snippets |
| OLD | NEW |