| 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 { | 16 namespace { |
| 17 | 17 |
| 18 const int kMaxSuggestionsCount = 5; | 18 const int kMaxSuggestionsCount = 5; |
| 19 | 19 |
| 20 } // namespace | 20 } // namespace |
| 21 | 21 |
| 22 OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider( | 22 OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider( |
| 23 ContentSuggestionsCategoryFactory* category_factory, |
| 23 OfflinePageModel* offline_page_model) | 24 OfflinePageModel* offline_page_model) |
| 24 : ContentSuggestionsProvider({ContentSuggestionsCategory::OFFLINE_PAGES}), | 25 : ContentSuggestionsProvider(category_factory), |
| 25 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING), | 26 category_status_(ContentSuggestionsCategoryStatus::AVAILABLE_LOADING), |
| 26 observer_(nullptr), | 27 observer_(nullptr), |
| 27 offline_page_model_(offline_page_model) { | 28 offline_page_model_(offline_page_model), |
| 29 provided_category_(category_factory->FromKnownCategory( |
| 30 KnownSuggestionsCategories::OFFLINE_PAGES)) { |
| 28 offline_page_model_->AddObserver(this); | 31 offline_page_model_->AddObserver(this); |
| 29 } | 32 } |
| 30 | 33 |
| 31 OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() {} | 34 OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() {} |
| 32 | 35 |
| 33 // Inherited from KeyedService. | 36 // Inherited from KeyedService. |
| 34 void OfflinePageSuggestionsProvider::Shutdown() { | 37 void OfflinePageSuggestionsProvider::Shutdown() { |
| 35 offline_page_model_->RemoveObserver(this); | 38 offline_page_model_->RemoveObserver(this); |
| 36 category_status_ = ContentSuggestionsCategoryStatus::NOT_PROVIDED; | 39 category_status_ = ContentSuggestionsCategoryStatus::NOT_PROVIDED; |
| 37 } | 40 } |
| 38 | 41 |
| 39 //////////////////////////////////////////////////////////////////////////////// | 42 //////////////////////////////////////////////////////////////////////////////// |
| 40 // Private methods | 43 // Private methods |
| 41 | 44 |
| 45 std::vector<ContentSuggestionsCategory> |
| 46 OfflinePageSuggestionsProvider::GetProvidedCategories() { |
| 47 return std::vector<ContentSuggestionsCategory>({provided_category_}); |
| 48 } |
| 49 |
| 42 void OfflinePageSuggestionsProvider::SetObserver( | 50 void OfflinePageSuggestionsProvider::SetObserver( |
| 43 ContentSuggestionsProvider::Observer* observer) { | 51 ContentSuggestionsProvider::Observer* observer) { |
| 44 observer_ = observer; | 52 observer_ = observer; |
| 45 if (observer) | 53 if (observer) |
| 46 FetchOfflinePages(); | 54 FetchOfflinePages(); |
| 47 } | 55 } |
| 48 | 56 |
| 49 ContentSuggestionsCategoryStatus | 57 ContentSuggestionsCategoryStatus |
| 50 OfflinePageSuggestionsProvider::GetCategoryStatus( | 58 OfflinePageSuggestionsProvider::GetCategoryStatus( |
| 51 ContentSuggestionsCategory category) { | 59 ContentSuggestionsCategory category) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 NotifyStatusChanged(ContentSuggestionsCategoryStatus::AVAILABLE); | 108 NotifyStatusChanged(ContentSuggestionsCategoryStatus::AVAILABLE); |
| 101 if (!observer_) | 109 if (!observer_) |
| 102 return; | 110 return; |
| 103 | 111 |
| 104 std::vector<ContentSuggestion> suggestions; | 112 std::vector<ContentSuggestion> suggestions; |
| 105 for (const OfflinePageItem& item : result) { | 113 for (const OfflinePageItem& item : result) { |
| 106 // TODO(pke): Make sure the URL is actually opened as an offline URL. | 114 // TODO(pke): Make sure the URL is actually opened as an offline URL. |
| 107 // Currently, the browser opens the offline URL and then immediately | 115 // Currently, the browser opens the offline URL and then immediately |
| 108 // redirects to the online URL if the device is online. | 116 // redirects to the online URL if the device is online. |
| 109 ContentSuggestion suggestion( | 117 ContentSuggestion suggestion( |
| 110 MakeUniqueID(ContentSuggestionsCategory::OFFLINE_PAGES, | 118 MakeUniqueID(provided_category_, base::IntToString(item.offline_id)), |
| 111 base::IntToString(item.offline_id)), | |
| 112 item.GetOfflineURL()); | 119 item.GetOfflineURL()); |
| 113 | 120 |
| 114 // TODO(pke): Sort my most recently visited and only keep the top one of | 121 // TODO(pke): Sort my most recently visited and only keep the top one of |
| 115 // multiple entries for the same URL. | 122 // multiple entries for the same URL. |
| 116 // TODO(pke): Get more reasonable data from the OfflinePageModel here. | 123 // TODO(pke): Get more reasonable data from the OfflinePageModel here. |
| 117 suggestion.set_title(item.url.spec()); | 124 suggestion.set_title(item.url.spec()); |
| 118 suggestion.set_snippet_text(std::string()); | 125 suggestion.set_snippet_text(std::string()); |
| 119 suggestion.set_publish_date(item.creation_time); | 126 suggestion.set_publish_date(item.creation_time); |
| 120 suggestion.set_publisher_name(item.url.host()); | 127 suggestion.set_publisher_name(item.url.host()); |
| 121 suggestions.emplace_back(std::move(suggestion)); | 128 suggestions.emplace_back(std::move(suggestion)); |
| 122 if (suggestions.size() == kMaxSuggestionsCount) | 129 if (suggestions.size() == kMaxSuggestionsCount) |
| 123 break; | 130 break; |
| 124 } | 131 } |
| 125 | 132 |
| 126 observer_->OnNewSuggestions(ContentSuggestionsCategory::OFFLINE_PAGES, | 133 observer_->OnNewSuggestions(provided_category_, std::move(suggestions)); |
| 127 std::move(suggestions)); | |
| 128 } | 134 } |
| 129 | 135 |
| 130 void OfflinePageSuggestionsProvider::NotifyStatusChanged( | 136 void OfflinePageSuggestionsProvider::NotifyStatusChanged( |
| 131 ContentSuggestionsCategoryStatus new_status) { | 137 ContentSuggestionsCategoryStatus new_status) { |
| 132 if (category_status_ == new_status) | 138 if (category_status_ == new_status) |
| 133 return; | 139 return; |
| 134 category_status_ = new_status; | 140 category_status_ = new_status; |
| 135 | 141 |
| 136 if (!observer_) | 142 if (!observer_) |
| 137 return; | 143 return; |
| 138 observer_->OnCategoryStatusChanged(ContentSuggestionsCategory::OFFLINE_PAGES, | 144 observer_->OnCategoryStatusChanged(provided_category_, new_status); |
| 139 new_status); | |
| 140 } | 145 } |
| 141 | 146 |
| 142 } // namespace ntp_snippets | 147 } // namespace ntp_snippets |
| OLD | NEW |