OLD | NEW |
---|---|
(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 | |
19 class PrefRegistrySimple; | |
20 class PrefService; | |
21 | |
22 namespace ntp_snippets { | |
23 | |
24 class UserClassifier; | |
25 | |
26 // A wrapper around RemoteSuggestionsProvider that introduces periodic fetching. | |
27 // | |
28 // The class initiates fetches on its own in these situations: | |
29 // - initial fetch when the provider is constructed and we have no suggestions; | |
30 // - regular fetches according to its schedule (when notified via | |
31 // PersistentScheduler::Listener::OnFetchDue). | |
32 // TODO(jkrcal): After soft fetch on Chrome startup is introduced, remove | |
33 // the initial fetch completely. | |
34 // | |
35 // The class also needs to understand when last fetch trials and successful | |
36 // fetches happen and thus it intercepts following interactive fetch requests: | |
37 // - Fetch() - after "More" button of a remote section is pressed in the UI; | |
38 // TODO(jkrcal): Clarify what Fetch() should do for this provider and maybe stop | |
39 // intercepting it. | |
40 // TODO(jkrcal): Intercept also ReloadSuggestions() call (after the user swipes | |
41 // away everything incl. all empty sections and presses "More"); Not done in the | |
42 // first shot because it implements a public interface function without any | |
43 // callback. | |
44 class SchedulingRemoteSuggestionsProvider | |
45 : public ContentSuggestionsProvider, | |
46 public PersistentScheduler::Listener { | |
47 public: | |
48 // The passed in |updater| is called when an update is due according to the | |
49 // schedule. Note that hard fetches get access to the |updater| via the keyed | |
50 // ContentSuggestionService because the concrete instance passed to | |
51 // RemoteSuggestionsScheduler when the hard fetch was scheduled may not exist | |
52 // any more when the hard update is due. | |
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 // PersistentScheduler::Listener implementation | |
66 void RescheduleFetching() override; | |
67 void OnFetchDue() 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 { | |
tschumann
2016/12/15 19:27:00
let's move this into the .cc file and just forward
jkrcal
2016/12/19 09:40:24
Done.
| |
93 base::TimeDelta interval_wifi; | |
94 base::TimeDelta interval_fallback; | |
95 | |
96 static FetchingSchedule Empty() { | |
97 return FetchingSchedule{base::TimeDelta::FromHours(0), | |
98 base::TimeDelta::FromHours(0)}; | |
99 } | |
100 | |
101 bool operator!=(const FetchingSchedule& other) const { | |
102 return interval_wifi != other.interval_wifi || | |
103 interval_fallback != other.interval_fallback; | |
104 } | |
105 | |
106 bool is_empty() const { | |
107 return interval_wifi.is_zero() && interval_fallback.is_zero(); | |
108 } | |
109 }; | |
110 | |
111 // Observer of activeness of the |provider_|. | |
112 void OnProviderActivenessChanged(bool active); | |
113 | |
114 // One of the following two functions must be called on startup of Chrome for | |
115 // the scheduler to work correctly. | |
116 // After the call, updates will be scheduled in the future. Idempotent, can be | |
117 // run any time later without impacting the current schedule. | |
118 // If you want to enforce rescheduling, call Unschedule() and then Schedule(). | |
119 void StartScheduling(); | |
120 | |
121 // After the call, no updates will happen before another call to Schedule(). | |
122 // Idempotent, can be run any time later without impacting the current | |
123 // schedule. | |
124 void StopScheduling(); | |
125 | |
126 // Callback after Fetch is completed. | |
127 void FetchFinished(const FetchDoneCallback& callback, | |
128 Status status_code, | |
129 std::vector<ContentSuggestion> suggestions); | |
130 | |
131 FetchingSchedule GetDesiredFetchingSchedule(); | |
132 FetchingSchedule GetLastFetchingSchedule() const; | |
133 void StoreLastFetchingSchedule(FetchingSchedule schedule); | |
134 | |
135 // Common function to call after each fetch. | |
136 void OnFetchCompleted(Status status_code); | |
137 | |
138 // Applies the provided |schedule|. | |
139 void ApplyFetchingSchedule(FetchingSchedule schedule); | |
140 | |
141 // Interface for doing the actual updates, when they are due. Not owned. | |
142 std::unique_ptr<RemoteSuggestionsProvider> provider_; | |
143 | |
144 // Interface for scheduling hard fetches, OS dependent. Not owned, may be | |
145 // null. | |
146 PersistentScheduler* persistent_scheduler_; | |
147 | |
148 // Used to adapt the schedule based on usage activity of the user. Not owned. | |
149 const UserClassifier* user_classifier_; | |
150 | |
151 PrefService* pref_service_; | |
152 | |
153 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider); | |
154 }; | |
155 | |
156 } // namespace ntp_snippets | |
157 | |
158 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER _H_ | |
OLD | NEW |