Chromium Code Reviews| Index: chrome/browser/android/offline_pages/suggestions_observer.h |
| diff --git a/chrome/browser/android/offline_pages/suggestions_observer.h b/chrome/browser/android/offline_pages/suggestions_observer.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7246e8c8ccd9c9fec05780d2e46beb1026da0091 |
| --- /dev/null |
| +++ b/chrome/browser/android/offline_pages/suggestions_observer.h |
| @@ -0,0 +1,82 @@ |
| +// 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_SUGGESTIONS_OBSERVER_H_ |
| +#define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_SUGGESTIONS_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 |
| +// then forwards them to the Prefetch Service, which does not know about Content |
| +// Suggestions specifically. |
| +class SuggestionsObserver |
| + : public ntp_snippets::ContentSuggestionsService::Observer, |
| + base::SupportsUserData::Data { |
| + public: |
| + // Delegate exists to allow for dependency injection in unit tests. |
| + // SuggestionsObserver 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 |
|
carlosk
2017/04/18 00:36:09
nit: if by covered you mean overridden or re-imple
dewittj
2017/04/18 18:20:25
no, I mean test coverage in particular.
carlosk
2017/04/18 20:17:22
Acknowledged.
|
| + // tests. |
| + class Delegate { |
|
jianli
2017/04/17 21:52:58
It just seems to me that using this technique thou
dewittj
2017/04/17 22:22:18
Mocking ContentSuggestionService turned out to be
|
| + 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 SuggestionsObserver 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); |
| + |
| + SuggestionsObserver(content::BrowserContext* browser_context, |
| + const ntp_snippets::Category& category, |
| + std::unique_ptr<Delegate> delegate); |
| + ~SuggestionsObserver() 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(SuggestionsObserver); |
| +}; |
| + |
| +} // namespace offline_pages |
| + |
| +#endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_SUGGESTIONS_OBSERVER_H_ |