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

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

Issue 2811813002: [Offline Pages] Set up the initial prefetching service. (Closed)
Patch Set: Remove offline page model. 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/offline_page_suggestions_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/offline_page_feature.h"
15 #include "components/offline_pages/core/prefetch/prefetch_service_impl.h"
16
17 namespace offline_pages {
18
19 int kOfflinePageSuggestionsObserverUserDataKey;
20
21 namespace {
22
23 // The default delegate that contains external dependencies for the Offline Page
24 // Suggestions Observer. This is unused in tests, which implement their own
25 // Delegate.
26 class DefaultDelegate : public SuggestionsObserver::Delegate {
27 public:
28 explicit DefaultDelegate(ntp_snippets::ContentSuggestionsService* service);
29 ~DefaultDelegate() override = default;
30
31 std::vector<GURL> GetSuggestionURLs(
32 const ntp_snippets::Category& category) override;
33 PrefetchService* GetPrefetchService(
34 content::BrowserContext* context) override;
35
36 private:
37 ntp_snippets::ContentSuggestionsService* service_;
38 };
39
40 DefaultDelegate::DefaultDelegate(
41 ntp_snippets::ContentSuggestionsService* service)
42 : service_(service) {}
43
44 std::vector<GURL> DefaultDelegate::GetSuggestionURLs(
45 const ntp_snippets::Category& category) {
46 std::vector<GURL> suggestion_urls;
47 auto& categories = service_->GetSuggestionsForCategory(category);
48 for (const ntp_snippets::ContentSuggestion& suggestion : categories) {
49 suggestion_urls.emplace_back(suggestion.url());
50 }
51 return suggestion_urls;
52 }
53
54 PrefetchService* DefaultDelegate::GetPrefetchService(
55 content::BrowserContext* context) {
56 return PrefetchServiceFactory::GetForBrowserContext(context);
57 }
58
59 } // namespace
60
61 // static
62 void SuggestionsObserver::ObserveContentSuggestionsService(
63 content::BrowserContext* browser_context,
64 ntp_snippets::ContentSuggestionsService* service) {
65 if (!offline_pages::IsPrefetchingOfflinePagesEnabled())
66 return;
67
68 auto category = ntp_snippets::Category::FromKnownCategory(
69 ntp_snippets::KnownCategories::ARTICLES);
70 auto suggestions_observer = base::MakeUnique<SuggestionsObserver>(
71 browser_context, base::MakeUnique<DefaultDelegate>(service), category);
72 service->AddObserver(suggestions_observer.get());
73 service->SetUserData(&kOfflinePageSuggestionsObserverUserDataKey,
74 suggestions_observer.release());
jianli 2017/04/11 22:39:55 Who is responsible to clean this up?
dewittj 2017/04/12 00:22:48 UserData::Data is owned by the class that inherits
75 }
76
77 SuggestionsObserver::SuggestionsObserver(
78 content::BrowserContext* browser_context,
79 std::unique_ptr<Delegate> delegate,
jianli 2017/04/11 22:39:55 nit: normally we pass delegate as the last argumen
dewittj 2017/04/12 00:22:48 Done.
80 const ntp_snippets::Category& category)
81 : delegate_(std::move(delegate)),
jianli 2017/04/11 22:39:55 nit: it would be better to declare these member va
dewittj 2017/04/12 00:22:48 Done.
82 browser_context_(browser_context),
83 category_(category) {}
84
85 SuggestionsObserver::~SuggestionsObserver() = default;
86
87 void SuggestionsObserver::OnNewSuggestions(ntp_snippets::Category category) {
88 if (category != category_ ||
89 category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) {
90 return;
91 }
92
93 std::vector<GURL> suggestion_urls = delegate_->GetSuggestionURLs(category_);
94 if (suggestion_urls.empty())
95 return;
96
97 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
98 service->OnNewURLsToPrefetch(suggestion_urls);
99 }
100
101 void SuggestionsObserver::OnCategoryStatusChanged(
102 ntp_snippets::Category category,
103 ntp_snippets::CategoryStatus new_status) {
104 if (category != category_)
105 return;
106
107 category_status_ = new_status;
jianli 2017/04/11 22:39:55 Do we want a DCHECK to ensure new_status is differ
dewittj 2017/04/12 00:22:48 It seems aggressive to do that. Perhaps an early
108
109 if (category_status_ ==
110 ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED ||
111 category_status_ ==
112 ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) {
113 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
114 service->RemoveAllUnprocessedURLsToPrefetch();
Dmitry Titov 2017/04/11 21:50:46 Do you mean to have a TODO here to actually remove
dewittj 2017/04/12 00:22:48 Done.
115 }
116 }
117
118 void SuggestionsObserver::OnSuggestionInvalidated(
119 const ntp_snippets::ContentSuggestion::ID& suggestion_id) {
120 // TODO(dewittj): Keep track of ContentSuggestion::IDs so we can invalidate
121 // suggestions as well.
122 NOTIMPLEMENTED();
123 }
124
125 void SuggestionsObserver::OnFullRefreshRequired() {
126 PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
127 service->RemoveAllUnprocessedURLsToPrefetch();
128 OnNewSuggestions(category_);
129 }
130
131 void SuggestionsObserver::ContentSuggestionsServiceShutdown() {
132 // No need to do anything here, we will just stop getting events.
133 }
134
135 } // namespace offline_pages
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698