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

Side by Side Diff: chrome/browser/android/offline_pages/suggested_articles_observer.h

Issue 2811813002: [Offline Pages] Set up the initial prefetching service. (Closed)
Patch Set: Go back to previous SetUserData API. 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 unified diff | Download patch
OLDNEW
(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_SUGGESTED_ARTICLES_OBSERVER_H_
6 #define CHROME_BROWSER_ANDROID_OFFLINE_PAGES_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 of a
26 // 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.
27 // then forwards them to the Prefetch Service, which does not know about Content
28 // Suggestions specifically.
29 class SuggestedArticlesObserver
30 : public ntp_snippets::ContentSuggestionsService::Observer,
31 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 ntp_snippets::Category category_;
74 std::unique_ptr<Delegate> delegate_;
75
76 DISALLOW_COPY_AND_ASSIGN(SuggestedArticlesObserver);
77 };
78
79 } // namespace offline_pages
80
81 #endif // CHROME_BROWSER_ANDROID_OFFLINE_PAGES_SUGGESTED_ARTICLES_OBSERVER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698