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

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: Changes to make unit-tests pass Created 3 years, 12 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 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 std::unique_ptr<RemoteSuggestionsProvider> provider,
56 PersistentScheduler* persistent_scheduler,
57 const UserClassifier* user_classifier,
58 PrefService* pref_service);
59
60 ~SchedulingRemoteSuggestionsProvider() override;
61
62 static void RegisterProfilePrefs(PrefRegistrySimple* registry);
63
64 // RemoteSuggestionsScheduler implementation.
65 void RescheduleFetching() override;
66 void OnPersistentSchedulerWakeUp() override;
67
68 // ContentSuggestionsProvider implementation.
69 CategoryStatus GetCategoryStatus(Category category) override;
70 CategoryInfo GetCategoryInfo(Category category) override;
71 void DismissSuggestion(const ContentSuggestion::ID& suggestion_id) override;
72 void FetchSuggestionImage(const ContentSuggestion::ID& suggestion_id,
73 const ImageFetchedCallback& callback) override;
74 void Fetch(const Category& category,
75 const std::set<std::string>& known_suggestion_ids,
76 const FetchDoneCallback& callback) override;
77 void ReloadSuggestions() override;
78 void ClearHistory(
79 base::Time begin,
80 base::Time end,
81 const base::Callback<bool(const GURL& url)>& filter) override;
82 void ClearCachedSuggestions(Category category) override;
83 void OnSignInStateChanged() override;
84 void GetDismissedSuggestionsForDebugging(
85 Category category,
86 const DismissedSuggestionsCallback& callback) override;
87 void ClearDismissedSuggestionsForDebugging(Category category) override;
88
89 private:
90 // Abstract description of the fetching schedule.
91 struct FetchingSchedule;
92
93 // Callback that is notified whenever the status of |provider_| changes.
94 void OnProviderStatusChanged(
95 RemoteSuggestionsProvider::ProviderStatus status);
96
97 // After the call, updates will be scheduled in the future. Idempotent, can be
98 // run any time later without impacting the current schedule.
99 // If you want to enforce rescheduling, call Unschedule() and then Schedule().
100 void StartScheduling();
101
102 // After the call, no updates will happen before another call to Schedule().
103 // Idempotent, can be run any time later without impacting the current
104 // schedule.
105 void StopScheduling();
106
107 // Callback after Fetch is completed.
108 void FetchFinished(const FetchDoneCallback& callback,
109 Status status_code,
110 std::vector<ContentSuggestion> suggestions);
111
112 FetchingSchedule GetDesiredFetchingSchedule() const;
113 FetchingSchedule GetLastFetchingSchedule() const;
114 void StoreLastFetchingSchedule(const FetchingSchedule& schedule);
115
116 // Common function to call after each fetch.
117 void OnFetchCompleted(Status status);
118
119 // Applies the provided |schedule|.
120 void ApplyFetchingSchedule(const FetchingSchedule& schedule);
121
122 // Interface for doing all the actual work (apart from scheduling).
123 std::unique_ptr<RemoteSuggestionsProvider> provider_;
124
125 // Interface for scheduling hard fetches, OS dependent. Not owned, may be
126 // null.
127 PersistentScheduler* persistent_scheduler_;
128
129 // Used to adapt the schedule based on usage activity of the user. Not owned.
130 const UserClassifier* user_classifier_;
131
132 PrefService* pref_service_;
133
134 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider);
135 };
136
137 } // namespace ntp_snippets
138
139 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698