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

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

Issue 2611523004: [Background fetching] Background fetching when opening an NTP. (Closed)
Patch Set: Tim's comments + unit-tests Created 3 years, 11 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER_H_ 5 #ifndef COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER_H_
6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER_H_ 6 #define COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER_H_
7 7
8 #include <memory> 8 #include <memory>
9 #include <set> 9 #include <set>
10 #include <string> 10 #include <string>
11 #include <utility>
11 #include <vector> 12 #include <vector>
12 13
13 #include "base/macros.h" 14 #include "base/macros.h"
14 #include "base/time/time.h" 15 #include "base/time/time.h"
15 #include "components/ntp_snippets/content_suggestions_provider.h" 16 #include "components/ntp_snippets/content_suggestions_provider.h"
16 #include "components/ntp_snippets/remote/persistent_scheduler.h" 17 #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_provider.h"
18 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h" 19 #include "components/ntp_snippets/remote/remote_suggestions_scheduler.h"
19 20
20 class PrefRegistrySimple; 21 class PrefRegistrySimple;
21 class PrefService; 22 class PrefService;
22 23
24 namespace base {
25 class Clock;
26 }
27
23 namespace ntp_snippets { 28 namespace ntp_snippets {
24 29
25 struct Status; 30 struct Status;
26 class UserClassifier; 31 class UserClassifier;
27 32
28 // A wrapper around RemoteSuggestionsProvider that introduces periodic fetching. 33 // A wrapper around RemoteSuggestionsProvider that introduces periodic fetching.
29 // 34 //
30 // The class initiates fetches on its own in these situations: 35 // The class initiates fetches on its own in these situations:
31 // - initial fetch when the provider is constructed and we have no suggestions; 36 // - initial fetch when the provider is constructed and we have no suggestions;
32 // - regular fetches according to its schedule. 37 // - regular fetches according to its schedule.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 base::Time begin, 94 base::Time begin,
90 base::Time end, 95 base::Time end,
91 const base::Callback<bool(const GURL& url)>& filter) override; 96 const base::Callback<bool(const GURL& url)>& filter) override;
92 void ClearCachedSuggestions(Category category) override; 97 void ClearCachedSuggestions(Category category) override;
93 void OnSignInStateChanged() override; 98 void OnSignInStateChanged() override;
94 void GetDismissedSuggestionsForDebugging( 99 void GetDismissedSuggestionsForDebugging(
95 Category category, 100 Category category,
96 const DismissedSuggestionsCallback& callback) override; 101 const DismissedSuggestionsCallback& callback) override;
97 void ClearDismissedSuggestionsForDebugging(Category category) override; 102 void ClearDismissedSuggestionsForDebugging(Category category) override;
98 103
104 // Overrides internal clock for testing purposes.
105 void SetClockForTesting(std::unique_ptr<base::Clock> clock) {
106 clock_ = std::move(clock);
tschumann 2017/01/04 10:43:52 can we dependency inject this into the constructor
jkrcal 2017/01/04 14:19:03 Done.
107 }
108
99 private: 109 private:
100 // Abstract description of the fetching schedule. 110 // Abstract description of the fetching schedule.
101 struct FetchingSchedule; 111 struct FetchingSchedule {
112 base::TimeDelta interval_persistent_wifi;
tschumann 2017/01/04 10:43:52 members go after methods.
jkrcal 2017/01/04 14:19:03 Done.
113 base::TimeDelta interval_persistent_fallback;
114 base::TimeDelta interval_soft_on_usage_event;
115
116 static FetchingSchedule Empty();
117 bool operator==(const FetchingSchedule& other) const;
118 bool operator!=(const FetchingSchedule& other) const;
119 bool is_empty() const;
120 };
102 121
103 // Callback that is notified whenever the status of |provider_| changes. 122 // Callback that is notified whenever the status of |provider_| changes.
104 void OnProviderStatusChanged( 123 void OnProviderStatusChanged(
105 RemoteSuggestionsProvider::ProviderStatus status); 124 RemoteSuggestionsProvider::ProviderStatus status);
106 125
107 // After the call, updates will be scheduled in the future. Idempotent, can be 126 // After the call, updates will be scheduled in the future. Idempotent, can be
108 // run any time later without impacting the current schedule. 127 // run any time later without impacting the current schedule.
109 // If you want to enforce rescheduling, call Unschedule() and then Schedule(). 128 // If you want to enforce rescheduling, call Unschedule() and then Schedule().
110 void StartScheduling(); 129 void StartScheduling();
111 130
112 // After the call, no updates will happen before another call to Schedule(). 131 // After the call, no updates will happen before another call to Schedule().
113 // Idempotent, can be run any time later without impacting the current 132 // Idempotent, can be run any time later without impacting the current
114 // schedule. 133 // schedule.
115 void StopScheduling(); 134 void StopScheduling();
116 135
136 // Checks whether it is time to perform a soft background fetch, according to
137 // |schedule|.
138 bool ShouldRefetchInTheBackgroundNow();
139
117 // Callback after Fetch is completed. 140 // Callback after Fetch is completed.
118 void FetchFinished(const FetchDoneCallback& callback, 141 void FetchFinished(const FetchDoneCallback& callback,
119 Status status_code, 142 Status fetch_status,
120 std::vector<ContentSuggestion> suggestions); 143 std::vector<ContentSuggestion> suggestions);
121 144
145 // Callback after RefetchInTheBackground is completed.
146 void RefetchInTheBackgroundFinished(
147 std::unique_ptr<FetchStatusCallback> callback,
148 Status fetch_status);
149
150 // Common function to call after a fetch of any type is finished.
151 void OnFetchCompleted(Status fetch_status);
152
122 FetchingSchedule GetDesiredFetchingSchedule() const; 153 FetchingSchedule GetDesiredFetchingSchedule() const;
123 FetchingSchedule GetLastFetchingSchedule() const;
124 void StoreLastFetchingSchedule(const FetchingSchedule& schedule);
125 154
126 // Common function to call after each fetch. 155 // Load and store |schedule_|.
127 void OnFetchCompleted(Status status); 156 void LoadLastFetchingSchedule();
157 void StoreFetchingSchedule();
128 158
129 // Applies the provided |schedule|. 159 // Applies the schedule given by |schedule_|.
130 void ApplyFetchingSchedule(const FetchingSchedule& schedule); 160 void ApplyFetchingSchedule(bool also_apply_persistent_schedule);
131 161
132 // Interface for doing all the actual work (apart from scheduling). 162 // Interface for doing all the actual work (apart from scheduling).
133 std::unique_ptr<RemoteSuggestionsProvider> provider_; 163 std::unique_ptr<RemoteSuggestionsProvider> provider_;
134 164
135 // Interface for scheduling hard fetches, OS dependent. Not owned, may be 165 // Interface for scheduling hard fetches, OS dependent. Not owned, may be
136 // null. 166 // null.
137 PersistentScheduler* persistent_scheduler_; 167 PersistentScheduler* persistent_scheduler_;
138 168
169 FetchingSchedule schedule_;
170
tschumann 2017/01/04 10:43:52 nit: in general, we should aim to limit vertical w
jkrcal 2017/01/04 14:19:03 Done.
171 bool background_fetch_in_progress_;
172
139 // Used to adapt the schedule based on usage activity of the user. Not owned. 173 // Used to adapt the schedule based on usage activity of the user. Not owned.
140 const UserClassifier* user_classifier_; 174 const UserClassifier* user_classifier_;
141 175
142 PrefService* pref_service_; 176 PrefService* pref_service_;
143 177
178 // Allow for an injectable clock for testing.
tschumann 2017/01/04 10:43:52 this comment is not necessary.
jkrcal 2017/01/04 14:19:03 Done.
179 std::unique_ptr<base::Clock> clock_;
180
144 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider); 181 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider);
145 }; 182 };
146 183
147 } // namespace ntp_snippets 184 } // namespace ntp_snippets
148 185
149 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER _H_ 186 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER _H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698