| 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 COMPONENTS_OFFLINE_PAGES_CONTENT_SUGGESTED_ARTICLES_OBSERVER_H_ | |
| 6 #define COMPONENTS_OFFLINE_PAGES_CONTENT_SUGGESTED_ARTICLES_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 in the | |
| 26 // ARTICLES category. When those suggestions arrive, it then forwards them to | |
| 27 // the Prefetch Service, which does not know about Content Suggestions | |
| 28 // specifically. | |
| 29 class SuggestedArticlesObserver | |
| 30 : public ntp_snippets::ContentSuggestionsService::Observer, | |
| 31 public base::SupportsUserData::Data { | |
| 32 public: | |
| 33 // Delegate exists to allow for dependency injection in unit tests. | |
| 34 // SuggestedArticlesObserver implements its own delegate, |DefaultDelegate| in | |
| 35 // the .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 | |
| 38 // tests. | |
| 39 class Delegate { | |
| 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 SuggestedArticlesObserver 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 SuggestedArticlesObserver(content::BrowserContext* browser_context, | |
| 56 std::unique_ptr<Delegate> delegate); | |
| 57 ~SuggestedArticlesObserver() override; | |
| 58 | |
| 59 // ContentSuggestionsService::Observer overrides. | |
| 60 void OnNewSuggestions(ntp_snippets::Category category) override; | |
| 61 void OnCategoryStatusChanged( | |
| 62 ntp_snippets::Category category, | |
| 63 ntp_snippets::CategoryStatus new_status) override; | |
| 64 void OnSuggestionInvalidated( | |
| 65 const ntp_snippets::ContentSuggestion::ID& suggestion_id) override; | |
| 66 void OnFullRefreshRequired() override; | |
| 67 void ContentSuggestionsServiceShutdown() override; | |
| 68 | |
| 69 private: | |
| 70 content::BrowserContext* browser_context_; | |
| 71 ntp_snippets::CategoryStatus category_status_ = | |
| 72 ntp_snippets::CategoryStatus::INITIALIZING; | |
| 73 std::unique_ptr<Delegate> delegate_; | |
| 74 | |
| 75 DISALLOW_COPY_AND_ASSIGN(SuggestedArticlesObserver); | |
| 76 }; | |
| 77 | |
| 78 } // namespace offline_pages | |
| 79 | |
| 80 #endif // COMPONENTS_OFFLINE_PAGES_CONTENT_SUGGESTED_ARTICLES_OBSERVER_H_ | |
| OLD | NEW |