Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #ifndef CHROME_BROWSER_ANDROID_OFFLINE_PAGES_SUGGESTIONS_OBSERVER_H_ | |
| 6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_SUGGESTIONS_OBSERVER_H_ | |
| 7 | |
| 8 #include <memory> | |
| 9 | |
| 10 #include "base/callback.h" | |
| 11 #include "base/memory/weak_ptr.h" | |
| 12 #include "components/ntp_snippets/content_suggestions_service.h" | |
| 13 #include "components/offline_pages/core/prefetch/prefetch_service.h" | |
| 14 | |
| 15 namespace content { | |
| 16 class BrowserContext; | |
| 17 } // namespace content | |
| 18 | |
| 19 namespace ntp_snippets { | |
| 20 class Category; | |
| 21 } | |
| 22 | |
| 23 namespace offline_pages { | |
| 24 | |
| 25 // Observes the ContentSuggestionsService, listening for new suggestions of a | |
| 26 // single category (initially ARTICLES). When those suggestions arrive, it | |
| 27 // then forwards them to the Prefetch Service, which does not know about Content | |
| 28 // Suggestions specifically. | |
| 29 class SuggestionsObserver | |
| 30 : public ntp_snippets::ContentSuggestionsService::Observer, | |
| 31 base::SupportsUserData::Data { | |
| 32 public: | |
| 33 // Delegate exists to allow for dependency injection in unit tests. | |
| 34 // SuggestionsObserver implements its own delegate, |DefaultDelegate| in the | |
| 35 // .cc file that forwards to the ContentSuggestionsService and the | |
| 36 // PrefetchServiceFactory. Code inside |DefaultDelegate| should be as simple | |
| 37 // 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.
| |
| 38 // tests. | |
| 39 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
| |
| 40 public: | |
| 41 virtual const std::vector<ntp_snippets::ContentSuggestion>& GetSuggestions( | |
| 42 const ntp_snippets::Category& category) = 0; | |
| 43 virtual PrefetchService* GetPrefetchService( | |
| 44 content::BrowserContext* context) = 0; | |
| 45 virtual ~Delegate() = default; | |
| 46 }; | |
| 47 | |
| 48 // This API creates a new SuggestionsObserver and adds it as an | |
| 49 // observer to the ContentSuggestionsService provided. Its lifetime is | |
| 50 // managed by the ContentSuggestionsService. | |
| 51 static void ObserveContentSuggestionsService( | |
| 52 content::BrowserContext* browser_context, | |
| 53 ntp_snippets::ContentSuggestionsService* service); | |
| 54 | |
| 55 SuggestionsObserver(content::BrowserContext* browser_context, | |
| 56 const ntp_snippets::Category& category, | |
| 57 std::unique_ptr<Delegate> delegate); | |
| 58 ~SuggestionsObserver() override; | |
| 59 | |
| 60 // ContentSuggestionsService::Observer overrides. | |
| 61 void OnNewSuggestions(ntp_snippets::Category category) override; | |
| 62 void OnCategoryStatusChanged( | |
| 63 ntp_snippets::Category category, | |
| 64 ntp_snippets::CategoryStatus new_status) override; | |
| 65 void OnSuggestionInvalidated( | |
| 66 const ntp_snippets::ContentSuggestion::ID& suggestion_id) override; | |
| 67 void OnFullRefreshRequired() override; | |
| 68 void ContentSuggestionsServiceShutdown() override; | |
| 69 | |
| 70 private: | |
| 71 content::BrowserContext* browser_context_; | |
| 72 ntp_snippets::CategoryStatus category_status_ = | |
| 73 ntp_snippets::CategoryStatus::INITIALIZING; | |
| 74 ntp_snippets::Category category_; | |
| 75 std::unique_ptr<Delegate> delegate_; | |
| 76 | |
| 77 DISALLOW_COPY_AND_ASSIGN(SuggestionsObserver); | |
| 78 }; | |
| 79 | |
| 80 } // namespace offline_pages | |
| 81 | |
| 82 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_SUGGESTIONS_OBSERVER_H_ | |
| OLD | NEW |