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 "components/offline_pages/content/suggested_articles_observer.h" | |
6 | |
7 #include <unordered_set> | |
8 | |
9 #include "base/memory/ptr_util.h" | |
10 #include "components/ntp_snippets/category.h" | |
11 #include "components/ntp_snippets/category_status.h" | |
12 #include "components/offline_pages/content/prefetch_service_factory.h" | |
13 #include "components/offline_pages/core/client_namespace_constants.h" | |
14 #include "components/offline_pages/core/offline_page_feature.h" | |
15 #include "components/offline_pages/core/prefetch/prefetch_service_impl.h" | |
16 | |
17 using ntp_snippets::Category; | |
18 using ntp_snippets::ContentSuggestion; | |
19 | |
20 namespace offline_pages { | |
21 | |
22 namespace { | |
23 | |
24 int kOfflinePageSuggestedArticlesObserverUserDataKey; | |
25 | |
26 const ntp_snippets::Category& ArticlesCategory() { | |
27 static ntp_snippets::Category articles = | |
28 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES); | |
29 return articles; | |
30 } | |
31 | |
32 ClientId CreateClientIDFromSuggestionId(const ContentSuggestion::ID& id) { | |
33 return ClientId(kSuggestedArticlesNamespace, id.id_within_category()); | |
34 } | |
35 | |
36 // The default delegate that contains external dependencies for the Offline Page | |
37 // Suggestions Observer. This is unused in tests, which implement their own | |
38 // Delegate. | |
39 class DefaultDelegate : public SuggestedArticlesObserver::Delegate { | |
40 public: | |
41 explicit DefaultDelegate(ntp_snippets::ContentSuggestionsService* service); | |
42 ~DefaultDelegate() override = default; | |
43 | |
44 const std::vector<ContentSuggestion>& GetSuggestions( | |
45 const Category& category) override; | |
46 PrefetchService* GetPrefetchService( | |
47 content::BrowserContext* context) override; | |
48 | |
49 private: | |
50 ntp_snippets::ContentSuggestionsService* service_; | |
51 }; | |
52 | |
53 DefaultDelegate::DefaultDelegate( | |
54 ntp_snippets::ContentSuggestionsService* service) | |
55 : service_(service) {} | |
56 | |
57 const std::vector<ContentSuggestion>& DefaultDelegate::GetSuggestions( | |
58 const Category& category) { | |
59 return service_->GetSuggestionsForCategory(category); | |
60 } | |
61 | |
62 PrefetchService* DefaultDelegate::GetPrefetchService( | |
63 content::BrowserContext* context) { | |
64 return PrefetchServiceFactory::GetForBrowserContext(context); | |
65 } | |
66 | |
67 } // namespace | |
68 | |
69 // static | |
70 void SuggestedArticlesObserver::ObserveContentSuggestionsService( | |
71 content::BrowserContext* browser_context, | |
72 ntp_snippets::ContentSuggestionsService* service) { | |
73 if (!offline_pages::IsPrefetchingOfflinePagesEnabled()) | |
74 return; | |
75 | |
76 auto suggestions_observer = base::MakeUnique<SuggestedArticlesObserver>( | |
77 browser_context, base::MakeUnique<DefaultDelegate>(service)); | |
78 service->AddObserver(suggestions_observer.get()); | |
79 service->SetUserData( | |
80 &kOfflinePageSuggestedArticlesObserverUserDataKey, | |
81 // Note that |service| will take ownership of suggestions_observer | |
82 // despite accepting a raw pointer. | |
83 suggestions_observer.release()); | |
84 } | |
85 | |
86 SuggestedArticlesObserver::SuggestedArticlesObserver( | |
87 content::BrowserContext* browser_context, | |
88 std::unique_ptr<Delegate> delegate) | |
89 : browser_context_(browser_context), delegate_(std::move(delegate)) {} | |
90 | |
91 SuggestedArticlesObserver::~SuggestedArticlesObserver() = default; | |
92 | |
93 void SuggestedArticlesObserver::OnNewSuggestions(Category category) { | |
94 if (category != ArticlesCategory() || | |
jkrcal
2017/04/21 13:57:06
This special-casing to KnownCategories::ARTICLES i
dewittj
2017/04/21 21:59:04
Done.
| |
95 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) { | |
96 return; | |
97 } | |
98 | |
99 const std::vector<ContentSuggestion>& suggestions = | |
100 delegate_->GetSuggestions(ArticlesCategory()); | |
101 if (suggestions.empty()) | |
102 return; | |
103 | |
104 std::vector<PrefetchService::PrefetchURL> prefetch_urls; | |
105 for (const ContentSuggestion& suggestion : suggestions) { | |
106 prefetch_urls.push_back( | |
107 {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()}); | |
108 } | |
109 | |
110 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | |
jkrcal
2017/04/21 13:57:07
Shouldn't you check for nullptr?
Does the factory
dewittj
2017/04/21 21:59:04
Done.
| |
111 service->AddCandidatePrefetchURLs(prefetch_urls); | |
112 } | |
113 | |
114 void SuggestedArticlesObserver::OnCategoryStatusChanged( | |
115 Category category, | |
116 ntp_snippets::CategoryStatus new_status) { | |
117 if (category != ArticlesCategory() || category_status_ == new_status) | |
118 return; | |
119 | |
120 category_status_ = new_status; | |
121 | |
122 if (category_status_ == | |
123 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED || | |
124 category_status_ == | |
125 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) { | |
126 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | |
jkrcal
2017/04/21 13:57:07
ditto
dewittj
2017/04/21 21:59:04
Done.
| |
127 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace); | |
128 } | |
129 } | |
130 | |
131 void SuggestedArticlesObserver::OnSuggestionInvalidated( | |
132 const ContentSuggestion::ID& suggestion_id) { | |
133 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | |
134 service->RemovePrefetchURLsByClientId( | |
135 CreateClientIDFromSuggestionId(suggestion_id)); | |
136 } | |
137 | |
138 void SuggestedArticlesObserver::OnFullRefreshRequired() { | |
139 PrefetchService* service = delegate_->GetPrefetchService(browser_context_); | |
140 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace); | |
141 OnNewSuggestions(ArticlesCategory()); | |
142 } | |
143 | |
144 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() { | |
145 // No need to do anything here, we will just stop getting events. | |
146 } | |
147 | |
148 } // namespace offline_pages | |
OLD | NEW |