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

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

Issue 2864293003: [Offline Pages] Add a GCMAppHandler for offline page prefetch. (Closed)
Patch Set: Fix windows Created 3 years, 6 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_dispatcher.h"
16 #include "components/offline_pages/core/prefetch/prefetch_service.h"
17
18 using ntp_snippets::Category;
19 using ntp_snippets::ContentSuggestion;
20
21 namespace offline_pages {
22
23 namespace {
24
25 int kOfflinePageSuggestedArticlesObserverUserDataKey;
26
27 const ntp_snippets::Category& ArticlesCategory() {
28 static ntp_snippets::Category articles =
29 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES);
30 return articles;
31 }
32
33 ClientId CreateClientIDFromSuggestionId(const ContentSuggestion::ID& id) {
34 return ClientId(kSuggestedArticlesNamespace, id.id_within_category());
35 }
36
37 // The default delegate that contains external dependencies for the Offline Page
38 // Suggestions Observer. This is unused in tests, which implement their own
39 // Delegate.
40 class DefaultDelegate : public SuggestedArticlesObserver::Delegate {
41 public:
42 explicit DefaultDelegate(ntp_snippets::ContentSuggestionsService* service);
43 ~DefaultDelegate() override = default;
44
45 const std::vector<ContentSuggestion>& GetSuggestions(
46 const Category& category) override;
47 PrefetchService* GetPrefetchService(
48 content::BrowserContext* context) override;
49
50 private:
51 ntp_snippets::ContentSuggestionsService* service_;
52 };
53
54 DefaultDelegate::DefaultDelegate(
55 ntp_snippets::ContentSuggestionsService* service)
56 : service_(service) {}
57
58 const std::vector<ContentSuggestion>& DefaultDelegate::GetSuggestions(
59 const Category& category) {
60 return service_->GetSuggestionsForCategory(category);
61 }
62
63 PrefetchService* DefaultDelegate::GetPrefetchService(
64 content::BrowserContext* context) {
65 return PrefetchServiceFactory::GetForBrowserContext(context);
66 }
67
68 } // namespace
69
70 // static
71 void SuggestedArticlesObserver::ObserveContentSuggestionsService(
72 content::BrowserContext* browser_context,
73 ntp_snippets::ContentSuggestionsService* service) {
74 if (!offline_pages::IsPrefetchingOfflinePagesEnabled())
75 return;
76
77 auto suggestions_observer = base::MakeUnique<SuggestedArticlesObserver>(
78 browser_context, base::MakeUnique<DefaultDelegate>(service));
79 service->AddObserver(suggestions_observer.get());
80 service->SetUserData(&kOfflinePageSuggestedArticlesObserverUserDataKey,
81 std::move(suggestions_observer));
82 }
83
84 SuggestedArticlesObserver::SuggestedArticlesObserver(
85 content::BrowserContext* browser_context,
86 std::unique_ptr<Delegate> delegate)
87 : browser_context_(browser_context), delegate_(std::move(delegate)) {}
88
89 SuggestedArticlesObserver::~SuggestedArticlesObserver() = default;
90
91 void SuggestedArticlesObserver::OnNewSuggestions(Category category) {
92 // TODO(dewittj): Change this to check whether a given category is not
93 // a _remote_ category.
94 if (category != ArticlesCategory() ||
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<PrefetchDispatcher::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_);
111 if (service == nullptr) {
112 DVLOG(1) << "PrefetchService unavailable to the "
113 "SuggestedArticlesObserver.";
114 return;
115 }
116 service->GetDispatcher()->AddCandidatePrefetchURLs(prefetch_urls);
117 }
118
119 void SuggestedArticlesObserver::OnCategoryStatusChanged(
120 Category category,
121 ntp_snippets::CategoryStatus new_status) {
122 if (category != ArticlesCategory() || category_status_ == new_status)
123 return;
124
125 category_status_ = new_status;
126
127 if (category_status_ ==
128 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED ||
129 category_status_ ==
130 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) {
131 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
132 if (service == nullptr) {
133 DVLOG(1) << "PrefetchService unavailable to the "
134 "SuggestedArticlesObserver.";
135 return;
136 }
137 service->GetDispatcher()->RemoveAllUnprocessedPrefetchURLs(
138 kSuggestedArticlesNamespace);
139 }
140 }
141
142 void SuggestedArticlesObserver::OnSuggestionInvalidated(
143 const ContentSuggestion::ID& suggestion_id) {
144 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
145 if (service == nullptr) {
146 DVLOG(1) << "PrefetchService unavailable to the "
147 "SuggestedArticlesObserver.";
148 return;
149 }
150 service->GetDispatcher()->RemovePrefetchURLsByClientId(
151 CreateClientIDFromSuggestionId(suggestion_id));
152 }
153
154 void SuggestedArticlesObserver::OnFullRefreshRequired() {
155 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
156 if (service == nullptr) {
157 DVLOG(1) << "PrefetchService unavailable to the "
158 "SuggestedArticlesObserver.";
159 return;
160 }
161 service->GetDispatcher()->RemoveAllUnprocessedPrefetchURLs(
162 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