| 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 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 | 10 |
| 11 using offline_pages::MultipleOfflinePageItemResult; | 11 using offline_pages::MultipleOfflinePageItemResult; |
| 12 using offline_pages::OfflinePageModel; | 12 using offline_pages::OfflinePageModel; |
| 13 using offline_pages::OfflinePageItem; | 13 using offline_pages::OfflinePageItem; |
| 14 | 14 |
| 15 namespace ntp_snippets { | 15 namespace ntp_snippets { |
| 16 | 16 |
| 17 namespace { | 17 namespace { |
| 18 | 18 |
| 19 const int kMaxSuggestionsCount = 5; | 19 const int kMaxSuggestionsCount = 5; |
| 20 | 20 |
| 21 } // namespace | 21 } // namespace |
| 22 | 22 |
| 23 OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider( | 23 OfflinePageSuggestionsProvider::OfflinePageSuggestionsProvider( |
| 24 ContentSuggestionsProvider::Observer* observer, |
| 24 CategoryFactory* category_factory, | 25 CategoryFactory* category_factory, |
| 25 OfflinePageModel* offline_page_model) | 26 OfflinePageModel* offline_page_model) |
| 26 : ContentSuggestionsProvider(category_factory), | 27 : ContentSuggestionsProvider(observer, category_factory), |
| 27 category_status_(CategoryStatus::AVAILABLE_LOADING), | 28 category_status_(CategoryStatus::AVAILABLE_LOADING), |
| 28 observer_(nullptr), | |
| 29 offline_page_model_(offline_page_model), | 29 offline_page_model_(offline_page_model), |
| 30 provided_category_( | 30 provided_category_( |
| 31 category_factory->FromKnownCategory(KnownCategories::OFFLINE_PAGES)) { | 31 category_factory->FromKnownCategory(KnownCategories::OFFLINE_PAGES)) { |
| 32 offline_page_model_->AddObserver(this); | 32 offline_page_model_->AddObserver(this); |
| 33 FetchOfflinePages(); |
| 33 } | 34 } |
| 34 | 35 |
| 35 OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() {} | 36 OfflinePageSuggestionsProvider::~OfflinePageSuggestionsProvider() { |
| 36 | |
| 37 // Inherited from KeyedService. | |
| 38 void OfflinePageSuggestionsProvider::Shutdown() { | |
| 39 offline_page_model_->RemoveObserver(this); | 37 offline_page_model_->RemoveObserver(this); |
| 40 category_status_ = CategoryStatus::NOT_PROVIDED; | |
| 41 } | 38 } |
| 42 | 39 |
| 43 //////////////////////////////////////////////////////////////////////////////// | 40 //////////////////////////////////////////////////////////////////////////////// |
| 44 // Private methods | 41 // Private methods |
| 45 | 42 |
| 46 std::vector<Category> OfflinePageSuggestionsProvider::GetProvidedCategories() { | 43 std::vector<Category> OfflinePageSuggestionsProvider::GetProvidedCategories() { |
| 47 return std::vector<Category>({provided_category_}); | 44 return std::vector<Category>({provided_category_}); |
| 48 } | 45 } |
| 49 | 46 |
| 50 void OfflinePageSuggestionsProvider::SetObserver( | |
| 51 ContentSuggestionsProvider::Observer* observer) { | |
| 52 observer_ = observer; | |
| 53 if (observer) | |
| 54 FetchOfflinePages(); | |
| 55 } | |
| 56 | |
| 57 CategoryStatus OfflinePageSuggestionsProvider::GetCategoryStatus( | 47 CategoryStatus OfflinePageSuggestionsProvider::GetCategoryStatus( |
| 58 Category category) { | 48 Category category) { |
| 59 return category_status_; | 49 return category_status_; |
| 60 } | 50 } |
| 61 | 51 |
| 62 void OfflinePageSuggestionsProvider::DismissSuggestion( | 52 void OfflinePageSuggestionsProvider::DismissSuggestion( |
| 63 const std::string& suggestion_id) { | 53 const std::string& suggestion_id) { |
| 64 // TODO(pke): Implement some "dont show on NTP anymore" behaviour, | 54 // TODO(pke): Implement some "dont show on NTP anymore" behaviour, |
| 65 // then also implement ClearDismissedSuggestionsForDebugging. | 55 // then also implement ClearDismissedSuggestionsForDebugging. |
| 66 } | 56 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 | 88 |
| 99 void OfflinePageSuggestionsProvider::FetchOfflinePages() { | 89 void OfflinePageSuggestionsProvider::FetchOfflinePages() { |
| 100 offline_page_model_->GetAllPages( | 90 offline_page_model_->GetAllPages( |
| 101 base::Bind(&OfflinePageSuggestionsProvider::OnOfflinePagesLoaded, | 91 base::Bind(&OfflinePageSuggestionsProvider::OnOfflinePagesLoaded, |
| 102 base::Unretained(this))); | 92 base::Unretained(this))); |
| 103 } | 93 } |
| 104 | 94 |
| 105 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded( | 95 void OfflinePageSuggestionsProvider::OnOfflinePagesLoaded( |
| 106 const MultipleOfflinePageItemResult& result) { | 96 const MultipleOfflinePageItemResult& result) { |
| 107 NotifyStatusChanged(CategoryStatus::AVAILABLE); | 97 NotifyStatusChanged(CategoryStatus::AVAILABLE); |
| 108 if (!observer_) | |
| 109 return; | |
| 110 | 98 |
| 111 std::vector<ContentSuggestion> suggestions; | 99 std::vector<ContentSuggestion> suggestions; |
| 112 for (const OfflinePageItem& item : result) { | 100 for (const OfflinePageItem& item : result) { |
| 113 // TODO(pke): Make sure the URL is actually opened as an offline URL. | 101 // TODO(pke): Make sure the URL is actually opened as an offline URL. |
| 114 // Currently, the browser opens the offline URL and then immediately | 102 // Currently, the browser opens the offline URL and then immediately |
| 115 // redirects to the online URL if the device is online. | 103 // redirects to the online URL if the device is online. |
| 116 ContentSuggestion suggestion( | 104 ContentSuggestion suggestion( |
| 117 MakeUniqueID(provided_category_, base::IntToString(item.offline_id)), | 105 MakeUniqueID(provided_category_, base::IntToString(item.offline_id)), |
| 118 item.GetOfflineURL()); | 106 item.GetOfflineURL()); |
| 119 | 107 |
| 120 // TODO(pke): Sort my most recently visited and only keep the top one of | 108 // TODO(pke): Sort my most recently visited and only keep the top one of |
| 121 // multiple entries for the same URL. | 109 // multiple entries for the same URL. |
| 122 // TODO(pke): Get more reasonable data from the OfflinePageModel here. | 110 // TODO(pke): Get more reasonable data from the OfflinePageModel here. |
| 123 suggestion.set_title(base::UTF8ToUTF16(item.url.spec())); | 111 suggestion.set_title(base::UTF8ToUTF16(item.url.spec())); |
| 124 suggestion.set_snippet_text(base::string16()); | 112 suggestion.set_snippet_text(base::string16()); |
| 125 suggestion.set_publish_date(item.creation_time); | 113 suggestion.set_publish_date(item.creation_time); |
| 126 suggestion.set_publisher_name(base::UTF8ToUTF16(item.url.host())); | 114 suggestion.set_publisher_name(base::UTF8ToUTF16(item.url.host())); |
| 127 suggestions.emplace_back(std::move(suggestion)); | 115 suggestions.emplace_back(std::move(suggestion)); |
| 128 if (suggestions.size() == kMaxSuggestionsCount) | 116 if (suggestions.size() == kMaxSuggestionsCount) |
| 129 break; | 117 break; |
| 130 } | 118 } |
| 131 | 119 |
| 132 observer_->OnNewSuggestions(this, provided_category_, std::move(suggestions)); | 120 observer()->OnNewSuggestions(this, provided_category_, |
| 121 std::move(suggestions)); |
| 133 } | 122 } |
| 134 | 123 |
| 135 void OfflinePageSuggestionsProvider::NotifyStatusChanged( | 124 void OfflinePageSuggestionsProvider::NotifyStatusChanged( |
| 136 CategoryStatus new_status) { | 125 CategoryStatus new_status) { |
| 137 if (category_status_ == new_status) | 126 if (category_status_ == new_status) |
| 138 return; | 127 return; |
| 139 category_status_ = new_status; | 128 category_status_ = new_status; |
| 140 | 129 |
| 141 if (!observer_) | 130 observer()->OnCategoryStatusChanged(this, provided_category_, new_status); |
| 142 return; | |
| 143 observer_->OnCategoryStatusChanged(this, provided_category_, new_status); | |
| 144 } | 131 } |
| 145 | 132 |
| 146 } // namespace ntp_snippets | 133 } // namespace ntp_snippets |
| OLD | NEW |