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

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: Refactor to remove GCM dependency. Created 3 years, 7 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(&kOfflinePageSuggestedArticlesObserverUserDataKey,
80 std::move(suggestions_observer));
81 }
82
83 SuggestedArticlesObserver::SuggestedArticlesObserver(
84 content::BrowserContext* browser_context,
85 std::unique_ptr<Delegate> delegate)
86 : browser_context_(browser_context), delegate_(std::move(delegate)) {}
87
88 SuggestedArticlesObserver::~SuggestedArticlesObserver() = default;
89
90 void SuggestedArticlesObserver::OnNewSuggestions(Category category) {
91 // TODO(dewittj): Change this to check whether a given category is not
92 // a _remote_ category.
93 if (category != ArticlesCategory() ||
94 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) {
95 return;
96 }
97
98 const std::vector<ContentSuggestion>& suggestions =
99 delegate_->GetSuggestions(ArticlesCategory());
100 if (suggestions.empty())
101 return;
102
103 std::vector<PrefetchService::PrefetchURL> prefetch_urls;
104 for (const ContentSuggestion& suggestion : suggestions) {
105 prefetch_urls.push_back(
106 {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()});
107 }
108
109 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
110 if (service == nullptr) {
111 DVLOG(1) << "PrefetchService unavailable to the "
112 "SuggestedArticlesObserver.";
113 return;
114 }
115 service->AddCandidatePrefetchURLs(prefetch_urls);
116 }
117
118 void SuggestedArticlesObserver::OnCategoryStatusChanged(
119 Category category,
120 ntp_snippets::CategoryStatus new_status) {
121 if (category != ArticlesCategory() || category_status_ == new_status)
122 return;
123
124 category_status_ = new_status;
125
126 if (category_status_ ==
127 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED ||
128 category_status_ ==
129 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) {
130 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
131 if (service == nullptr) {
132 DVLOG(1) << "PrefetchService unavailable to the "
133 "SuggestedArticlesObserver.";
134 return;
135 }
136 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace);
137 }
138 }
139
140 void SuggestedArticlesObserver::OnSuggestionInvalidated(
141 const ContentSuggestion::ID& suggestion_id) {
142 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
143 if (service == nullptr) {
144 DVLOG(1) << "PrefetchService unavailable to the "
145 "SuggestedArticlesObserver.";
146 return;
147 }
148 service->RemovePrefetchURLsByClientId(
149 CreateClientIDFromSuggestionId(suggestion_id));
150 }
151
152 void SuggestedArticlesObserver::OnFullRefreshRequired() {
153 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
154 if (service == nullptr) {
155 DVLOG(1) << "PrefetchService unavailable to the "
156 "SuggestedArticlesObserver.";
157 return;
158 }
159 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace);
160 OnNewSuggestions(ArticlesCategory());
161 }
162
163 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() {
164 // No need to do anything here, we will just stop getting events.
165 }
166
167 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698