Index: chrome/browser/android/offline_pages/suggested_articles_observer.h |
diff --git a/chrome/browser/android/offline_pages/suggested_articles_observer.h b/chrome/browser/android/offline_pages/suggested_articles_observer.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..3f0994d2a7d0f0232bc5113d58f24f0fb232fde9 |
--- /dev/null |
+++ b/chrome/browser/android/offline_pages/suggested_articles_observer.h |
@@ -0,0 +1,81 @@ |
+// 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. |
+ |
+#ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_SUGGESTED_ARTICLES_OBSERVER_H_ |
+#define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_SUGGESTED_ARTICLES_OBSERVER_H_ |
+ |
+#include <memory> |
+ |
+#include "base/callback.h" |
+#include "base/memory/weak_ptr.h" |
+#include "components/ntp_snippets/content_suggestions_service.h" |
+#include "components/offline_pages/core/prefetch/prefetch_service.h" |
+ |
+namespace content { |
+class BrowserContext; |
+} // namespace content |
+ |
+namespace ntp_snippets { |
+class Category; |
+} |
+ |
+namespace offline_pages { |
+ |
+// Observes the ContentSuggestionsService, listening for new suggestions of a |
+// single category (initially ARTICLES). When those suggestions arrive, it |
jianli
2017/04/18 22:19:52
nit: now we can say :listening for new suggestions
dewittj
2017/04/20 18:34:18
Done.
|
+// then forwards them to the Prefetch Service, which does not know about Content |
+// Suggestions specifically. |
+class SuggestedArticlesObserver |
+ : public ntp_snippets::ContentSuggestionsService::Observer, |
+ base::SupportsUserData::Data { |
+ public: |
+ // Delegate exists to allow for dependency injection in unit tests. |
+ // SuggestedArticlesObserver implements its own delegate, |DefaultDelegate| in |
+ // the .cc file that forwards to the ContentSuggestionsService and the |
+ // PrefetchServiceFactory. Code inside |DefaultDelegate| should be as simple |
+ // as possible, since it will only be covered by instrumentation/browser |
+ // tests. |
+ class Delegate { |
+ public: |
+ virtual const std::vector<ntp_snippets::ContentSuggestion>& GetSuggestions( |
+ const ntp_snippets::Category& category) = 0; |
+ virtual PrefetchService* GetPrefetchService( |
+ content::BrowserContext* context) = 0; |
+ virtual ~Delegate() = default; |
+ }; |
+ |
+ // This API creates a new SuggestedArticlesObserver and adds it as an |
+ // observer to the ContentSuggestionsService provided. Its lifetime is |
+ // managed by the ContentSuggestionsService. |
+ static void ObserveContentSuggestionsService( |
+ content::BrowserContext* browser_context, |
+ ntp_snippets::ContentSuggestionsService* service); |
+ |
+ SuggestedArticlesObserver(content::BrowserContext* browser_context, |
+ std::unique_ptr<Delegate> delegate); |
+ ~SuggestedArticlesObserver() override; |
+ |
+ // ContentSuggestionsService::Observer overrides. |
+ void OnNewSuggestions(ntp_snippets::Category category) override; |
+ void OnCategoryStatusChanged( |
+ ntp_snippets::Category category, |
+ ntp_snippets::CategoryStatus new_status) override; |
+ void OnSuggestionInvalidated( |
+ const ntp_snippets::ContentSuggestion::ID& suggestion_id) override; |
+ void OnFullRefreshRequired() override; |
+ void ContentSuggestionsServiceShutdown() override; |
+ |
+ private: |
+ content::BrowserContext* browser_context_; |
+ ntp_snippets::CategoryStatus category_status_ = |
+ ntp_snippets::CategoryStatus::INITIALIZING; |
+ ntp_snippets::Category category_; |
+ std::unique_ptr<Delegate> delegate_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SuggestedArticlesObserver); |
+}; |
+ |
+} // namespace offline_pages |
+ |
+#endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_SUGGESTED_ARTICLES_OBSERVER_H_ |