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

Side by Side Diff: components/ntp_snippets/remote/scheduling_remote_suggestions_provider.h

Issue 2557363002: [NTP Snippets] Refactor background scheduling for remote suggestions (Closed)
Patch Set: Rebase Created 4 years 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 2016 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_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER_H_
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER_H_
7
8 #include <memory>
9 #include <set>
10 #include <string>
11 #include <vector>
12
13 #include "base/macros.h"
14 #include "base/time/time.h"
15 #include "components/ntp_snippets/content_suggestions_provider.h"
16 #include "components/ntp_snippets/remote/persistent_scheduler.h"
17 #include "components/ntp_snippets/remote/remote_suggestions_provider.h"
18 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h"
19
20 class PrefRegistrySimple;
21 class PrefService;
22
23 namespace ntp_snippets {
24
25 struct Status;
26 class UserClassifier;
27
28 // A wrapper around RemoteSuggestionsProvider that introduces periodic fetching.
29 //
30 // The class initiates fetches on its own in these situations:
31 // - initial fetch when the provider is constructed and we have no suggestions;
32 // - regular fetches according to its schedule.
33 // TODO(jkrcal): After soft fetch on Chrome startup is introduced, remove
34 // the initial fetch completely.
35 //
36 // The class also needs to understand when last fetch trials and successful
37 // fetches happen and thus it intercepts following interactive fetch requests:
38 // - Fetch() - after "More" button of a remote section is pressed in the UI;
39 // TODO(jkrcal): Clarify what Fetch() should do for this provider and maybe stop
40 // intercepting it.
41 // TODO(jkrcal): Intercept also ReloadSuggestions() call (after the user swipes
42 // away everything incl. all empty sections and presses "More"); Not done in the
43 // first shot because it implements a public interface function without any
44 // callback.
45 // This class is final because it does things in its constructor which make it
46 // unsafe to derive from it.
47 // TODO(jkrcal): Introduce two-phase initialization and make the class not
48 // final? (see the same comment for RemoteSuggestionsProvider)
49 class SchedulingRemoteSuggestionsProvider final
50 : public ContentSuggestionsProvider,
51 public RemoteSuggestionsScheduler {
52 public:
53 SchedulingRemoteSuggestionsProvider(
54 Observer* observer,
55 CategoryFactory* category_factory,
56 std::unique_ptr<RemoteSuggestionsProvider> provider,
57 PersistentScheduler* persistent_scheduler,
58 const UserClassifier* user_classifier,
59 PrefService* pref_service);
60
61 ~SchedulingRemoteSuggestionsProvider() override;
62
63 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
64
65 // RemoteSuggestionsScheduler implementation.
66 void RescheduleFetching() override;
67 void OnPersistentSchedulerWakeUp() override;
68
69 // ContentSuggestionsProvider implementation.
70 CategoryStatus GetCategoryStatus(Category category) override;
71 CategoryInfo GetCategoryInfo(Category category) override;
72 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override;
73 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id,
74 const ImageFetchedCallback& callback) override;
75 void Fetch(const Category& category,
76 const std::set<std::string>& known_suggestion_ids,
77 const FetchDoneCallback& callback) override;
78 void ReloadSuggestions() override;
79 void ClearHistory(
80 base::Time begin,
81 base::Time end,
82 const base::Callback<bool(const GURL& url)>& filter) override;
83 void ClearCachedSuggestions(Category category) override;
84 void OnSignInStateChanged() override;
85 void GetDismissedSuggestionsForDebugging(
86 Category category,
87 const DismissedSuggestionsCallback& callback) override;
88 void ClearDismissedSuggestionsForDebugging(Category category) override;
89
90 private:
91 // Abstract description of the fetching schedule.
92 struct FetchingSchedule;
93
94 // Callback that is notified whenever the status of |provider_| changes.
95 void OnProviderStatusChanged(
96 RemoteSuggestionsProvider::ProviderStatus status);
97
98 // After the call, updates will be scheduled in the future. Idempotent, can be
99 // run any time later without impacting the current schedule.
100 // If you want to enforce rescheduling, call Unschedule() and then Schedule().
101 void StartScheduling();
102
103 // After the call, no updates will happen before another call to Schedule().
104 // Idempotent, can be run any time later without impacting the current
105 // schedule.
106 void StopScheduling();
107
108 // Callback after Fetch is completed.
109 void FetchFinished(const FetchDoneCallback& callback,
110 Status status_code,
111 std::vector<ContentSuggestion> suggestions);
112
113 FetchingSchedule GetDesiredFetchingSchedule() const;
114 FetchingSchedule GetLastFetchingSchedule() const;
115 void StoreLastFetchingSchedule(const FetchingSchedule& schedule);
116
117 // Common function to call after each fetch.
118 void OnFetchCompleted(Status status);
119
120 // Applies the provided |schedule|.
121 void ApplyFetchingSchedule(const FetchingSchedule& schedule);
122
123 // Interface for doing all the actual work (apart from scheduling).
124 std::unique_ptr<RemoteSuggestionsProvider> provider_;
125
126 // Interface for scheduling hard fetches, OS dependent. Not owned, may be
127 // null.
128 PersistentScheduler* persistent_scheduler_;
129
130 // Used to adapt the schedule based on usage activity of the user. Not owned.
131 const UserClassifier* user_classifier_;
132
133 PrefService* pref_service_;
134
135 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider);
136 };
137
138 } // namespace ntp_snippets
139
140 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698