| OLD | NEW |
| 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 15 matching lines...) Expand all Loading... |
| 48 // final? (see the same comment for RemoteSuggestionsProvider) | 53 // final? (see the same comment for RemoteSuggestionsProvider) |
| 49 class SchedulingRemoteSuggestionsProvider final | 54 class SchedulingRemoteSuggestionsProvider final |
| 50 : public RemoteSuggestionsProvider, | 55 : public RemoteSuggestionsProvider, |
| 51 public RemoteSuggestionsScheduler { | 56 public RemoteSuggestionsScheduler { |
| 52 public: | 57 public: |
| 53 SchedulingRemoteSuggestionsProvider( | 58 SchedulingRemoteSuggestionsProvider( |
| 54 Observer* observer, | 59 Observer* observer, |
| 55 std::unique_ptr<RemoteSuggestionsProvider> provider, | 60 std::unique_ptr<RemoteSuggestionsProvider> provider, |
| 56 PersistentScheduler* persistent_scheduler, | 61 PersistentScheduler* persistent_scheduler, |
| 57 const UserClassifier* user_classifier, | 62 const UserClassifier* user_classifier, |
| 58 PrefService* pref_service); | 63 PrefService* pref_service, |
| 64 std::unique_ptr<base::Clock> clock); |
| 59 | 65 |
| 60 ~SchedulingRemoteSuggestionsProvider() override; | 66 ~SchedulingRemoteSuggestionsProvider() override; |
| 61 | 67 |
| 62 static void RegisterProfilePrefs(PrefRegistrySimple* registry); | 68 static void RegisterProfilePrefs(PrefRegistrySimple* registry); |
| 63 | 69 |
| 64 // RemoteSuggestionsScheduler implementation. | 70 // RemoteSuggestionsScheduler implementation. |
| 65 void RescheduleFetching() override; | 71 void RescheduleFetching() override; |
| 66 void OnPersistentSchedulerWakeUp() override; | 72 void OnPersistentSchedulerWakeUp() override; |
| 67 void OnBrowserStartup() override; | 73 void OnBrowserStartup() override; |
| 68 void OnNTPOpened() override; | 74 void OnNTPOpened() override; |
| (...skipping 22 matching lines...) Expand all Loading... |
| 91 const base::Callback<bool(const GURL& url)>& filter) override; | 97 const base::Callback<bool(const GURL& url)>& filter) override; |
| 92 void ClearCachedSuggestions(Category category) override; | 98 void ClearCachedSuggestions(Category category) override; |
| 93 void OnSignInStateChanged() override; | 99 void OnSignInStateChanged() override; |
| 94 void GetDismissedSuggestionsForDebugging( | 100 void GetDismissedSuggestionsForDebugging( |
| 95 Category category, | 101 Category category, |
| 96 const DismissedSuggestionsCallback& callback) override; | 102 const DismissedSuggestionsCallback& callback) override; |
| 97 void ClearDismissedSuggestionsForDebugging(Category category) override; | 103 void ClearDismissedSuggestionsForDebugging(Category category) override; |
| 98 | 104 |
| 99 private: | 105 private: |
| 100 // Abstract description of the fetching schedule. | 106 // Abstract description of the fetching schedule. |
| 101 struct FetchingSchedule; | 107 struct FetchingSchedule { |
| 108 static FetchingSchedule Empty(); |
| 109 bool operator==(const FetchingSchedule& other) const; |
| 110 bool operator!=(const FetchingSchedule& other) const; |
| 111 bool is_empty() const; |
| 112 |
| 113 base::TimeDelta interval_persistent_wifi; |
| 114 base::TimeDelta interval_persistent_fallback; |
| 115 base::TimeDelta interval_soft_on_usage_event; |
| 116 }; |
| 102 | 117 |
| 103 // Callback that is notified whenever the status of |provider_| changes. | 118 // Callback that is notified whenever the status of |provider_| changes. |
| 104 void OnProviderStatusChanged( | 119 void OnProviderStatusChanged( |
| 105 RemoteSuggestionsProvider::ProviderStatus status); | 120 RemoteSuggestionsProvider::ProviderStatus status); |
| 106 | 121 |
| 107 // After the call, updates will be scheduled in the future. Idempotent, can be | 122 // After the call, updates will be scheduled in the future. Idempotent, can be |
| 108 // run any time later without impacting the current schedule. | 123 // run any time later without impacting the current schedule. |
| 109 // If you want to enforce rescheduling, call Unschedule() and then Schedule(). | 124 // If you want to enforce rescheduling, call Unschedule() and then Schedule(). |
| 110 void StartScheduling(); | 125 void StartScheduling(); |
| 111 | 126 |
| 112 // After the call, no updates will happen before another call to Schedule(). | 127 // After the call, no updates will happen before another call to Schedule(). |
| 113 // Idempotent, can be run any time later without impacting the current | 128 // Idempotent, can be run any time later without impacting the current |
| 114 // schedule. | 129 // schedule. |
| 115 void StopScheduling(); | 130 void StopScheduling(); |
| 116 | 131 |
| 132 // Checks whether it is time to perform a soft background fetch, according to |
| 133 // |schedule|. |
| 134 bool ShouldRefetchInTheBackgroundNow(); |
| 135 |
| 117 // Callback after Fetch is completed. | 136 // Callback after Fetch is completed. |
| 118 void FetchFinished(const FetchDoneCallback& callback, | 137 void FetchFinished(const FetchDoneCallback& callback, |
| 119 Status status_code, | 138 Status fetch_status, |
| 120 std::vector<ContentSuggestion> suggestions); | 139 std::vector<ContentSuggestion> suggestions); |
| 121 | 140 |
| 141 // Callback after RefetchInTheBackground is completed. |
| 142 void RefetchInTheBackgroundFinished( |
| 143 std::unique_ptr<FetchStatusCallback> callback, |
| 144 Status fetch_status); |
| 145 |
| 146 // Common function to call after a fetch of any type is finished. |
| 147 void OnFetchCompleted(Status fetch_status); |
| 148 |
| 122 FetchingSchedule GetDesiredFetchingSchedule() const; | 149 FetchingSchedule GetDesiredFetchingSchedule() const; |
| 123 FetchingSchedule GetLastFetchingSchedule() const; | |
| 124 void StoreLastFetchingSchedule(const FetchingSchedule& schedule); | |
| 125 | 150 |
| 126 // Common function to call after each fetch. | 151 // Load and store |schedule_|. |
| 127 void OnFetchCompleted(Status status); | 152 void LoadLastFetchingSchedule(); |
| 153 void StoreFetchingSchedule(); |
| 154 bool BackgroundFetchesDisabled() const; |
| 128 | 155 |
| 129 // Applies the provided |schedule|. | 156 // Applies the persistent schedule given by |schedule_|. |
| 130 void ApplyFetchingSchedule(const FetchingSchedule& schedule); | 157 void ApplyPersistentFetchingSchedule(); |
| 131 | 158 |
| 132 // Interface for doing all the actual work (apart from scheduling). | 159 // Interface for doing all the actual work (apart from scheduling). |
| 133 std::unique_ptr<RemoteSuggestionsProvider> provider_; | 160 std::unique_ptr<RemoteSuggestionsProvider> provider_; |
| 134 | 161 |
| 135 // Interface for scheduling hard fetches, OS dependent. Not owned, may be | 162 // Interface for scheduling hard fetches, OS dependent. Not owned, may be |
| 136 // null. | 163 // null. |
| 137 PersistentScheduler* persistent_scheduler_; | 164 PersistentScheduler* persistent_scheduler_; |
| 138 | 165 |
| 166 FetchingSchedule schedule_; |
| 167 bool background_fetch_in_progress_; |
| 168 |
| 139 // Used to adapt the schedule based on usage activity of the user. Not owned. | 169 // Used to adapt the schedule based on usage activity of the user. Not owned. |
| 140 const UserClassifier* user_classifier_; | 170 const UserClassifier* user_classifier_; |
| 141 | 171 |
| 142 PrefService* pref_service_; | 172 PrefService* pref_service_; |
| 173 std::unique_ptr<base::Clock> clock_; |
| 143 | 174 |
| 144 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider); | 175 DISALLOW_COPY_AND_ASSIGN(SchedulingRemoteSuggestionsProvider); |
| 145 }; | 176 }; |
| 146 | 177 |
| 147 } // namespace ntp_snippets | 178 } // namespace ntp_snippets |
| 148 | 179 |
| 149 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER
_H_ | 180 #endif // COMPONENTS_NTP_SNIPPETS_REMOTE_SCHEDULING_REMOTE_SUGGESTIONS_PROVIDER
_H_ |
| OLD | NEW |