Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(205)

Side by Side Diff: components/offline_pages/content/suggested_articles_observer.cc

Issue 2811813002: [Offline Pages] Set up the initial prefetching service. (Closed)
Patch Set: fix compile >:-( Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 // TODO(dewittj): Change this to check whether a given category is not
95 // a _remote_ category.
96 if (category != ArticlesCategory() ||
97 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) {
98 return;
99 }
100
101 const std::vector<ContentSuggestion>& suggestions =
102 delegate_->GetSuggestions(ArticlesCategory());
103 if (suggestions.empty())
104 return;
105
106 std::vector<PrefetchService::PrefetchURL> prefetch_urls;
107 for (const ContentSuggestion& suggestion : suggestions) {
108 prefetch_urls.push_back(
109 {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()});
110 }
111
112 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
113 if (service == nullptr) {
114 DVLOG(1) << "PrefetchService unavailable to the "
115 "SuggestedArticlesObserver.";
116 return;
117 }
118 service->AddCandidatePrefetchURLs(prefetch_urls);
119 }
120
121 void SuggestedArticlesObserver::OnCategoryStatusChanged(
122 Category category,
123 ntp_snippets::CategoryStatus new_status) {
124 if (category != ArticlesCategory() || category_status_ == new_status)
125 return;
126
127 category_status_ = new_status;
128
129 if (category_status_ ==
130 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED ||
131 category_status_ ==
132 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) {
133 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
134 if (service == nullptr) {
135 DVLOG(1) << "PrefetchService unavailable to the "
136 "SuggestedArticlesObserver.";
137 return;
138 }
139 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace);
140 }
141 }
142
143 void SuggestedArticlesObserver::OnSuggestionInvalidated(
144 const ContentSuggestion::ID& suggestion_id) {
145 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
146 if (service == nullptr) {
147 DVLOG(1) << "PrefetchService unavailable to the "
148 "SuggestedArticlesObserver.";
149 return;
150 }
151 service->RemovePrefetchURLsByClientId(
152 CreateClientIDFromSuggestionId(suggestion_id));
153 }
154
155 void SuggestedArticlesObserver::OnFullRefreshRequired() {
156 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
157 if (service == nullptr) {
158 DVLOG(1) << "PrefetchService unavailable to the "
159 "SuggestedArticlesObserver.";
160 return;
161 }
162 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace);
163 OnNewSuggestions(ArticlesCategory());
164 }
165
166 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() {
167 // No need to do anything here, we will just stop getting events.
168 }
169
170 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698