Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "chrome/browser/android/offline_pages/suggestions_observer.h" | |
| 6 | |
| 7 #include <unordered_set> | |
| 8 | |
| 9 #include "base/memory/ptr_util.h" | |
| 10 #include "base/threading/thread_task_runner_handle.h" | |
| 11 #include "chrome/browser/android/offline_pages/prefetch_service_factory.h" | |
| 12 #include "components/ntp_snippets/category.h" | |
| 13 #include "components/ntp_snippets/category_status.h" | |
| 14 #include "components/offline_pages/core/client_namespace_constants.h" | |
| 15 #include "components/offline_pages/core/offline_page_feature.h" | |
| 16 #include "components/offline_pages/core/prefetch/prefetch_service_impl.h" | |
| 17 | |
| 18 using ntp_snippets::Category; | |
| 19 using ntp_snippets::ContentSuggestion; | |
| 20 | |
| 21 namespace offline_pages { | |
| 22 | |
| 23 int kOfflinePageSuggestionsObserverUserDataKey; | |
|
carlosk
2017/04/18 00:36:09
Shouldn't this be inside the anonymous namespace t
dewittj
2017/04/18 18:20:25
Done.
| |
| 24 | |
| 25 namespace { | |
|
carlosk
2017/04/18 00:36:09
Are anonymous namespaces within named namespaces a
dewittj
2017/04/18 18:20:25
I'm not sure what you are trying to ask. In my ex
carlosk
2017/04/18 20:17:22
Acknowledged.
| |
| 26 | |
| 27 ClientId CreateClientIDFromSuggestionId(const ContentSuggestion::ID& id) { | |
| 28 return ClientId(kSuggestedArticlesNamespace, id.id_within_category()); | |
| 29 } | |
| 30 | |
| 31 // The default delegate that contains external dependencies for the Offline Page | |
| 32 // Suggestions Observer. This is unused in tests, which implement their own | |
| 33 // Delegate. | |
| 34 class DefaultDelegate : public SuggestionsObserver::Delegate { | |
| 35 public: | |
| 36 explicit DefaultDelegate(ntp_snippets::ContentSuggestionsService* service); | |
| 37 ~DefaultDelegate() override = default; | |
| 38 | |
| 39 const std::vector<ContentSuggestion>& GetSuggestions( | |
| 40 const Category& category) override; | |
| 41 PrefetchService* GetPrefetchService( | |
| 42 content::BrowserContext* context) override; | |
| 43 | |
| 44 private: | |
| 45 ntp_snippets::ContentSuggestionsService* service_; | |
| 46 }; | |
| 47 | |
| 48 DefaultDelegate::DefaultDelegate( | |
| 49 ntp_snippets::ContentSuggestionsService* service) | |
| 50 : service_(service) {} | |
| 51 | |
| 52 const std::vector<ContentSuggestion>& DefaultDelegate::GetSuggestions( | |
| 53 const Category& category) { | |
| 54 return service_->GetSuggestionsForCategory(category); | |
| 55 } | |
| 56 | |
| 57 PrefetchService* DefaultDelegate::GetPrefetchService( | |
| 58 content::BrowserContext* context) { | |
| 59 return PrefetchServiceFactory::GetForBrowserContext(context); | |
| 60 } | |
| 61 | |
| 62 } // namespace | |
| 63 | |
| 64 // static | |
| 65 void SuggestionsObserver::ObserveContentSuggestionsService( | |
| 66 content::BrowserContext* browser_context, | |
| 67 ntp_snippets::ContentSuggestionsService* service) { | |
| 68 if (!offline_pages::IsPrefetchingOfflinePagesEnabled()) | |
| 69 return; | |
| 70 | |
| 71 auto category = | |
| 72 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | |
| 73 auto suggestions_observer = base::MakeUnique<SuggestionsObserver>( | |
| 74 browser_context, category, base::MakeUnique<DefaultDelegate>(service)); | |
| 75 service->AddObserver(suggestions_observer.get()); | |
| 76 service->SetUserData(&kOfflinePageSuggestionsObserverUserDataKey, | |
| 77 suggestions_observer.release()); | |
|
carlosk
2017/04/18 00:36:09
Is it impossible to use std::move here?
dewittj
2017/04/18 18:20:25
Done. Note that the obvious way seemed to fail bec
| |
| 78 } | |
| 79 | |
| 80 SuggestionsObserver::SuggestionsObserver( | |
| 81 content::BrowserContext* browser_context, | |
| 82 const Category& category, | |
| 83 std::unique_ptr<Delegate> delegate) | |
| 84 : browser_context_(browser_context), | |
| 85 category_(category), | |
| 86 delegate_(std::move(delegate)) {} | |
| 87 | |
| 88 SuggestionsObserver::~SuggestionsObserver() = default; | |
| 89 | |
| 90 void SuggestionsObserver::OnNewSuggestions(Category category) { | |
| 91 if (category != category_ || | |
| 92 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) { | |
| 93 return; | |
| 94 } | |
| 95 | |
| 96 const std::vector<ContentSuggestion>& suggestions = | |
| 97 delegate_->GetSuggestions(category_); | |
| 98 if (suggestions.empty()) | |
| 99 return; | |
| 100 | |
| 101 std::vector<PrefetchService::PrefetchURL> prefetch_urls; | |
| 102 for (auto& suggestion : suggestions) { | |
| 103 prefetch_urls.push_back( | |
| 104 {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()}); | |
| 105 } | |
| 106 | |
| 107 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | |
| 108 service->OnNewURLsToPrefetch(prefetch_urls); | |
| 109 } | |
| 110 | |
| 111 void SuggestionsObserver::OnCategoryStatusChanged( | |
| 112 Category category, | |
| 113 ntp_snippets::CategoryStatus new_status) { | |
| 114 if (category != category_ || category_status_ == new_status) | |
| 115 return; | |
| 116 | |
| 117 category_status_ = new_status; | |
| 118 | |
| 119 if (category_status_ == | |
| 120 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED || | |
| 121 category_status_ == | |
| 122 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) { | |
| 123 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | |
| 124 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void SuggestionsObserver::OnSuggestionInvalidated( | |
| 129 const ContentSuggestion::ID& suggestion_id) { | |
| 130 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | |
| 131 service->RemovePrefetchURLsByClientId( | |
| 132 CreateClientIDFromSuggestionId(suggestion_id)); | |
| 133 } | |
| 134 | |
| 135 void SuggestionsObserver::OnFullRefreshRequired() { | |
| 136 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | |
| 137 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace); | |
|
jianli
2017/04/17 21:52:58
Are all PrefetchService operations asynchronous? I
dewittj
2017/04/17 22:22:18
They are, but hopefully they will be serialized us
| |
| 138 OnNewSuggestions(category_); | |
| 139 } | |
| 140 | |
| 141 void SuggestionsObserver::ContentSuggestionsServiceShutdown() { | |
| 142 // No need to do anything here, we will just stop getting events. | |
| 143 } | |
| 144 | |
| 145 } // namespace offline_pages | |
| OLD | NEW |