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

Side by Side Diff: chrome/browser/android/offline_pages/suggested_articles_observer.cc

Issue 2811813002: [Offline Pages] Set up the initial prefetching service. (Closed)
Patch Set: Go back to previous SetUserData API. 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 "chrome/browser/android/offline_pages/suggested_articles_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 namespace {
24
25 int kOfflinePageSuggestedArticlesObserverUserDataKey;
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 SuggestedArticlesObserver::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 SuggestedArticlesObserver::ObserveContentSuggestionsService(
66 content::BrowserContext* browser_context,
67 ntp_snippets::ContentSuggestionsService* service) {
68 if (!offline_pages::IsPrefetchingOfflinePagesEnabled())
69 return;
70
71 auto suggestions_observer = base::MakeUnique<SuggestedArticlesObserver>(
72 browser_context, base::MakeUnique<DefaultDelegate>(service));
73 service->AddObserver(suggestions_observer.get());
74 service->SetUserData(
75 &kOfflinePageSuggestedArticlesObserverUserDataKey,
76 // Note that |service| will take ownership of suggestions_observer
77 // despite accepting a raw pointer.
78 suggestions_observer.release());
79 }
80
81 SuggestedArticlesObserver::SuggestedArticlesObserver(
82 content::BrowserContext* browser_context,
83 std::unique_ptr<Delegate> delegate)
84 : browser_context_(browser_context),
85 category_(
jianli 2017/04/18 22:19:51 nit: there is not need to save category now. Just
dewittj 2017/04/20 18:34:18 I don't want a static initializer, so how do you l
86 Category::FromKnownCategory(ntp_snippets::KnownCategories::ARTICLES)),
87 delegate_(std::move(delegate)) {}
88
89 SuggestedArticlesObserver::~SuggestedArticlesObserver() = default;
90
91 void SuggestedArticlesObserver::OnNewSuggestions(Category category) {
92 if (category != category_ ||
93 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) {
94 return;
95 }
96
97 const std::vector<ContentSuggestion>& suggestions =
98 delegate_->GetSuggestions(category_);
99 if (suggestions.empty())
100 return;
101
102 std::vector<PrefetchService::PrefetchURL> prefetch_urls;
103 for (const ContentSuggestion& suggestion : suggestions) {
104 prefetch_urls.push_back(
105 {CreateClientIDFromSuggestionId(suggestion.id()), suggestion.url()});
106 }
107
108 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
109 service->AddCandidatePrefetchURLs(prefetch_urls);
110 }
111
112 void SuggestedArticlesObserver::OnCategoryStatusChanged(
113 Category category,
114 ntp_snippets::CategoryStatus new_status) {
115 if (category != category_ || category_status_ == new_status)
116 return;
117
118 category_status_ = new_status;
119
120 if (category_status_ ==
121 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED ||
122 category_status_ ==
123 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) {
124 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
125 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace);
126 }
127 }
128
129 void SuggestedArticlesObserver::OnSuggestionInvalidated(
130 const ContentSuggestion::ID& suggestion_id) {
131 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
132 service->RemovePrefetchURLsByClientId(
133 CreateClientIDFromSuggestionId(suggestion_id));
134 }
135
136 void SuggestedArticlesObserver::OnFullRefreshRequired() {
137 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
138 service->RemoveAllUnprocessedPrefetchURLs(kSuggestedArticlesNamespace);
139 OnNewSuggestions(category_);
140 }
141
142 void SuggestedArticlesObserver::ContentSuggestionsServiceShutdown() {
143 // No need to do anything here, we will just stop getting events.
144 }
145
146 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698