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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/android/offline_pages/offline_page_suggestions_observer.cc
diff --git a/chrome/browser/android/offline_pages/offline_page_suggestions_observer.cc b/chrome/browser/android/offline_pages/offline_page_suggestions_observer.cc
new file mode 100644
index 0000000000000000000000000000000000000000..0834f85e566279aa3b1d70e54756780750ca9b67
--- /dev/null
+++ b/chrome/browser/android/offline_pages/offline_page_suggestions_observer.cc
@@ -0,0 +1,135 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/android/offline_pages/offline_page_suggestions_observer.h"
+
+#include <unordered_set>
+
+#include "base/memory/ptr_util.h"
+#include "base/threading/thread_task_runner_handle.h"
+#include "chrome/browser/android/offline_pages/prefetch_service_factory.h"
+#include "components/ntp_snippets/category.h"
+#include "components/ntp_snippets/category_status.h"
+#include "components/offline_pages/core/offline_page_feature.h"
+#include "components/offline_pages/core/prefetch/prefetch_service_impl.h"
+
+namespace offline_pages {
+
+int kOfflinePageSuggestionsObserverUserDataKey;
+
+namespace {
+
+// The default delegate that contains external dependencies for the Offline Page
+// Suggestions Observer. This is unused in tests, which implement their own
+// Delegate.
+class DefaultDelegate : public SuggestionsObserver::Delegate {
+ public:
+ explicit DefaultDelegate(ntp_snippets::ContentSuggestionsService* service);
+ ~DefaultDelegate() override = default;
+
+ std::vector<GURL> GetSuggestionURLs(
+ const ntp_snippets::Category& category) override;
+ PrefetchService* GetPrefetchService(
+ content::BrowserContext* context) override;
+
+ private:
+ ntp_snippets::ContentSuggestionsService* service_;
+};
+
+DefaultDelegate::DefaultDelegate(
+ ntp_snippets::ContentSuggestionsService* service)
+ : service_(service) {}
+
+std::vector<GURL> DefaultDelegate::GetSuggestionURLs(
+ const ntp_snippets::Category& category) {
+ std::vector<GURL> suggestion_urls;
+ auto& categories = service_->GetSuggestionsForCategory(category);
+ for (const ntp_snippets::ContentSuggestion& suggestion : categories) {
+ suggestion_urls.emplace_back(suggestion.url());
+ }
+ return suggestion_urls;
+}
+
+PrefetchService* DefaultDelegate::GetPrefetchService(
+ content::BrowserContext* context) {
+ return PrefetchServiceFactory::GetForBrowserContext(context);
+}
+
+} // namespace
+
+// static
+void SuggestionsObserver::ObserveContentSuggestionsService(
+ content::BrowserContext* browser_context,
+ ntp_snippets::ContentSuggestionsService* service) {
+ if (!offline_pages::IsPrefetchingOfflinePagesEnabled())
+ return;
+
+ auto category = ntp_snippets::Category::FromKnownCategory(
+ ntp_snippets::KnownCategories::ARTICLES);
+ auto suggestions_observer = base::MakeUnique<SuggestionsObserver>(
+ browser_context, base::MakeUnique<DefaultDelegate>(service), category);
+ service->AddObserver(suggestions_observer.get());
+ service->SetUserData(&kOfflinePageSuggestionsObserverUserDataKey,
+ 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
+}
+
+SuggestionsObserver::SuggestionsObserver(
+ content::BrowserContext* browser_context,
+ 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.
+ const ntp_snippets::Category& category)
+ : 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.
+ browser_context_(browser_context),
+ category_(category) {}
+
+SuggestionsObserver::~SuggestionsObserver() = default;
+
+void SuggestionsObserver::OnNewSuggestions(ntp_snippets::Category category) {
+ if (category != category_ ||
+ category_status_ != ntp_snippets::CategoryStatus::AVAILABLE) {
+ return;
+ }
+
+ std::vector<GURL> suggestion_urls = delegate_->GetSuggestionURLs(category_);
+ if (suggestion_urls.empty())
+ return;
+
+ PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
+ service->OnNewURLsToPrefetch(suggestion_urls);
+}
+
+void SuggestionsObserver::OnCategoryStatusChanged(
+ ntp_snippets::Category category,
+ ntp_snippets::CategoryStatus new_status) {
+ if (category != category_)
+ return;
+
+ 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
+
+ if (category_status_ ==
+ ntp_snippets::CategoryStatus::CATEGORY_EXPLICITLY_DISABLED ||
+ category_status_ ==
+ ntp_snippets::CategoryStatus::ALL_SUGGESTIONS_EXPLICITLY_DISABLED) {
+ PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
+ 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.
+ }
+}
+
+void SuggestionsObserver::OnSuggestionInvalidated(
+ const ntp_snippets::ContentSuggestion::ID& suggestion_id) {
+ // TODO(dewittj): Keep track of ContentSuggestion::IDs so we can invalidate
+ // suggestions as well.
+ NOTIMPLEMENTED();
+}
+
+void SuggestionsObserver::OnFullRefreshRequired() {
+ PrefetchService* service = delegate_->GetPrefetchService(browser_context_);
+ service->RemoveAllUnprocessedURLsToPrefetch();
+ OnNewSuggestions(category_);
+}
+
+void SuggestionsObserver::ContentSuggestionsServiceShutdown() {
+ // No need to do anything here, we will just stop getting events.
+}
+
+} // namespace offline_pages

Powered by Google App Engine
This is Rietveld 408576698