Commit 0ef461ea authored by Sky Malice's avatar Sky Malice Committed by Commit Bot

[NTP Snippets] Switch RemoteSuggestionsSchedulerImpl to Time and TimeDelta pref methods.

Because time_serialization.cc stored Time and DeltaTime in
microseconds, and Time since the windows epoch, which is the exact
same way that PrefService is storing these time fields, this change
should be fully backwards compatible.

Bug: 853755
Change-Id: I22d734d030ca550a654319e37abf6653890c1bdb
Reviewed-on: https://chromium-review.googlesource.com/1104581Reviewed-by: default avatarFilip Gorski <fgorski@chromium.org>
Commit-Queue: Sky Malice <skym@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568282}
parent 064bdb85
...@@ -22,7 +22,6 @@ ...@@ -22,7 +22,6 @@
#include "components/ntp_snippets/remote/persistent_scheduler.h" #include "components/ntp_snippets/remote/persistent_scheduler.h"
#include "components/ntp_snippets/remote/remote_suggestions_provider.h" #include "components/ntp_snippets/remote/remote_suggestions_provider.h"
#include "components/ntp_snippets/status.h" #include "components/ntp_snippets/status.h"
#include "components/ntp_snippets/time_serialization.h"
#include "components/ntp_snippets/user_classifier.h" #include "components/ntp_snippets/user_classifier.h"
#include "components/prefs/pref_registry_simple.h" #include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h" #include "components/prefs/pref_service.h"
...@@ -481,16 +480,21 @@ RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default; ...@@ -481,16 +480,21 @@ RemoteSuggestionsSchedulerImpl::~RemoteSuggestionsSchedulerImpl() = default;
// static // static
void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs( void RemoteSuggestionsSchedulerImpl::RegisterProfilePrefs(
PrefRegistrySimple* registry) { PrefRegistrySimple* registry) {
registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalWifi, 0); registry->RegisterTimeDeltaPref(prefs::kSnippetPersistentFetchingIntervalWifi,
registry->RegisterInt64Pref(prefs::kSnippetPersistentFetchingIntervalFallback, base::TimeDelta());
0); registry->RegisterTimeDeltaPref(
registry->RegisterInt64Pref(prefs::kSnippetStartupFetchingIntervalWifi, 0); prefs::kSnippetPersistentFetchingIntervalFallback, base::TimeDelta());
registry->RegisterInt64Pref(prefs::kSnippetStartupFetchingIntervalFallback, registry->RegisterTimeDeltaPref(prefs::kSnippetStartupFetchingIntervalWifi,
0); base::TimeDelta());
registry->RegisterInt64Pref(prefs::kSnippetShownFetchingIntervalWifi, 0); registry->RegisterTimeDeltaPref(
registry->RegisterInt64Pref(prefs::kSnippetShownFetchingIntervalFallback, 0); prefs::kSnippetStartupFetchingIntervalFallback, base::TimeDelta());
registry->RegisterInt64Pref(prefs::kSnippetLastFetchAttemptTime, 0); registry->RegisterTimeDeltaPref(prefs::kSnippetShownFetchingIntervalWifi,
registry->RegisterInt64Pref(prefs::kSnippetLastSuccessfulFetchTime, 0); base::TimeDelta());
registry->RegisterTimeDeltaPref(prefs::kSnippetShownFetchingIntervalFallback,
base::TimeDelta());
registry->RegisterTimePref(prefs::kSnippetLastFetchAttemptTime, base::Time());
registry->RegisterTimePref(prefs::kSnippetLastSuccessfulFetchTime,
base::Time());
} }
void RemoteSuggestionsSchedulerImpl::SetProvider( void RemoteSuggestionsSchedulerImpl::SetProvider(
...@@ -644,38 +648,34 @@ RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const { ...@@ -644,38 +648,34 @@ RemoteSuggestionsSchedulerImpl::GetDesiredFetchingSchedule() const {
} }
void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() { void RemoteSuggestionsSchedulerImpl::LoadLastFetchingSchedule() {
schedule_.interval_persistent_wifi = DeserializeTimeDelta( schedule_.interval_persistent_wifi = profile_prefs_->GetTimeDelta(
profile_prefs_->GetInt64(prefs::kSnippetPersistentFetchingIntervalWifi)); prefs::kSnippetPersistentFetchingIntervalWifi);
schedule_.interval_persistent_fallback = schedule_.interval_persistent_fallback = profile_prefs_->GetTimeDelta(
DeserializeTimeDelta(profile_prefs_->GetInt64( prefs::kSnippetPersistentFetchingIntervalFallback);
prefs::kSnippetPersistentFetchingIntervalFallback)); schedule_.interval_startup_wifi =
schedule_.interval_startup_wifi = DeserializeTimeDelta( profile_prefs_->GetTimeDelta(prefs::kSnippetStartupFetchingIntervalWifi);
profile_prefs_->GetInt64(prefs::kSnippetStartupFetchingIntervalWifi)); schedule_.interval_startup_fallback = profile_prefs_->GetTimeDelta(
schedule_.interval_startup_fallback = DeserializeTimeDelta( prefs::kSnippetStartupFetchingIntervalFallback);
profile_prefs_->GetInt64(prefs::kSnippetStartupFetchingIntervalFallback)); schedule_.interval_shown_wifi =
schedule_.interval_shown_wifi = DeserializeTimeDelta( profile_prefs_->GetTimeDelta(prefs::kSnippetShownFetchingIntervalWifi);
profile_prefs_->GetInt64(prefs::kSnippetShownFetchingIntervalWifi)); schedule_.interval_shown_fallback = profile_prefs_->GetTimeDelta(
schedule_.interval_shown_fallback = DeserializeTimeDelta( prefs::kSnippetShownFetchingIntervalFallback);
profile_prefs_->GetInt64(prefs::kSnippetShownFetchingIntervalFallback));
} }
void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() { void RemoteSuggestionsSchedulerImpl::StoreFetchingSchedule() {
profile_prefs_->SetInt64( profile_prefs_->SetTimeDelta(prefs::kSnippetPersistentFetchingIntervalWifi,
prefs::kSnippetPersistentFetchingIntervalWifi, schedule_.interval_persistent_wifi);
SerializeTimeDelta(schedule_.interval_persistent_wifi)); profile_prefs_->SetTimeDelta(
profile_prefs_->SetInt64(
prefs::kSnippetPersistentFetchingIntervalFallback, prefs::kSnippetPersistentFetchingIntervalFallback,
SerializeTimeDelta(schedule_.interval_persistent_fallback)); schedule_.interval_persistent_fallback);
profile_prefs_->SetInt64(prefs::kSnippetStartupFetchingIntervalWifi, profile_prefs_->SetTimeDelta(prefs::kSnippetStartupFetchingIntervalWifi,
SerializeTimeDelta(schedule_.interval_startup_wifi)); schedule_.interval_startup_wifi);
profile_prefs_->SetInt64( profile_prefs_->SetTimeDelta(prefs::kSnippetStartupFetchingIntervalFallback,
prefs::kSnippetStartupFetchingIntervalFallback, schedule_.interval_startup_fallback);
SerializeTimeDelta(schedule_.interval_startup_fallback)); profile_prefs_->SetTimeDelta(prefs::kSnippetShownFetchingIntervalWifi,
profile_prefs_->SetInt64(prefs::kSnippetShownFetchingIntervalWifi, schedule_.interval_shown_wifi);
SerializeTimeDelta(schedule_.interval_shown_wifi)); profile_prefs_->SetTimeDelta(prefs::kSnippetShownFetchingIntervalFallback,
profile_prefs_->SetInt64( schedule_.interval_shown_fallback);
prefs::kSnippetShownFetchingIntervalFallback,
SerializeTimeDelta(schedule_.interval_shown_fallback));
} }
bool RemoteSuggestionsSchedulerImpl::IsLastSuccessfulFetchStale() const { bool RemoteSuggestionsSchedulerImpl::IsLastSuccessfulFetchStale() const {
...@@ -685,8 +685,8 @@ bool RemoteSuggestionsSchedulerImpl::IsLastSuccessfulFetchStale() const { ...@@ -685,8 +685,8 @@ bool RemoteSuggestionsSchedulerImpl::IsLastSuccessfulFetchStale() const {
if (!profile_prefs_->HasPrefPath(prefs::kSnippetLastSuccessfulFetchTime)) { if (!profile_prefs_->HasPrefPath(prefs::kSnippetLastSuccessfulFetchTime)) {
return false; return false;
} }
const base::Time last_successful_fetch_time = DeserializeTime( const base::Time last_successful_fetch_time =
profile_prefs_->GetInt64(prefs::kSnippetLastSuccessfulFetchTime)); profile_prefs_->GetTime(prefs::kSnippetLastSuccessfulFetchTime);
return clock_->Now() - last_successful_fetch_time > return clock_->Now() - last_successful_fetch_time >
schedule_.GetStalenessInterval(); schedule_.GetStalenessInterval();
...@@ -719,8 +719,8 @@ void RemoteSuggestionsSchedulerImpl::RefetchIfAppropriate(TriggerType trigger) { ...@@ -719,8 +719,8 @@ void RemoteSuggestionsSchedulerImpl::RefetchIfAppropriate(TriggerType trigger) {
return; return;
} }
const base::Time last_fetch_attempt_time = base::Time::FromInternalValue( const base::Time last_fetch_attempt_time =
profile_prefs_->GetInt64(prefs::kSnippetLastFetchAttemptTime)); profile_prefs_->GetTime(prefs::kSnippetLastFetchAttemptTime);
if (trigger == TriggerType::SURFACE_OPENED && if (trigger == TriggerType::SURFACE_OPENED &&
!time_until_first_shown_trigger_reported_) { !time_until_first_shown_trigger_reported_) {
...@@ -866,8 +866,7 @@ void RemoteSuggestionsSchedulerImpl::RefetchFinished(Status fetch_status) { ...@@ -866,8 +866,7 @@ void RemoteSuggestionsSchedulerImpl::RefetchFinished(Status fetch_status) {
} }
void RemoteSuggestionsSchedulerImpl::OnFetchCompleted(Status fetch_status) { void RemoteSuggestionsSchedulerImpl::OnFetchCompleted(Status fetch_status) {
profile_prefs_->SetInt64(prefs::kSnippetLastFetchAttemptTime, profile_prefs_->SetTime(prefs::kSnippetLastFetchAttemptTime, clock_->Now());
SerializeTime(clock_->Now()));
time_until_first_shown_trigger_reported_ = false; time_until_first_shown_trigger_reported_ = false;
time_until_first_startup_trigger_reported_ = false; time_until_first_startup_trigger_reported_ = false;
...@@ -879,8 +878,8 @@ void RemoteSuggestionsSchedulerImpl::OnFetchCompleted(Status fetch_status) { ...@@ -879,8 +878,8 @@ void RemoteSuggestionsSchedulerImpl::OnFetchCompleted(Status fetch_status) {
return; return;
} }
profile_prefs_->SetInt64(prefs::kSnippetLastSuccessfulFetchTime, profile_prefs_->SetTime(prefs::kSnippetLastSuccessfulFetchTime,
SerializeTime(clock_->Now())); clock_->Now());
ApplyPersistentFetchingSchedule(); ApplyPersistentFetchingSchedule();
} }
...@@ -889,8 +888,7 @@ void RemoteSuggestionsSchedulerImpl::ClearLastFetchAttemptTime() { ...@@ -889,8 +888,7 @@ void RemoteSuggestionsSchedulerImpl::ClearLastFetchAttemptTime() {
profile_prefs_->ClearPref(prefs::kSnippetLastFetchAttemptTime); profile_prefs_->ClearPref(prefs::kSnippetLastFetchAttemptTime);
// To mark the last fetch as stale, we need to keep the time in prefs, only // To mark the last fetch as stale, we need to keep the time in prefs, only
// making sure it is long ago. // making sure it is long ago.
profile_prefs_->SetInt64(prefs::kSnippetLastSuccessfulFetchTime, profile_prefs_->SetTime(prefs::kSnippetLastSuccessfulFetchTime, base::Time());
SerializeTime(base::Time()));
} }
std::set<RemoteSuggestionsSchedulerImpl::TriggerType> std::set<RemoteSuggestionsSchedulerImpl::TriggerType>
......
...@@ -7,19 +7,11 @@ ...@@ -7,19 +7,11 @@
namespace ntp_snippets { namespace ntp_snippets {
int64_t SerializeTime(const base::Time& time) { int64_t SerializeTime(const base::Time& time) {
return SerializeTimeDelta(time - base::Time()); return (time - base::Time()).InMicroseconds();
} }
base::Time DeserializeTime(int64_t serialized_time) { base::Time DeserializeTime(int64_t serialized_time) {
return base::Time() + DeserializeTimeDelta(serialized_time); return base::Time() + base::TimeDelta::FromMicroseconds(serialized_time);
}
int64_t SerializeTimeDelta(const base::TimeDelta& time_delta) {
return time_delta.InMicroseconds();
}
base::TimeDelta DeserializeTimeDelta(int64_t serialized_time_delta) {
return base::TimeDelta::FromMicroseconds(serialized_time_delta);
} }
} // namespace ntp_snippets } // namespace ntp_snippets
...@@ -17,10 +17,6 @@ namespace ntp_snippets { ...@@ -17,10 +17,6 @@ namespace ntp_snippets {
int64_t SerializeTime(const base::Time& time); int64_t SerializeTime(const base::Time& time);
base::Time DeserializeTime(int64_t serialized_time); base::Time DeserializeTime(int64_t serialized_time);
// Same as above, but for base::TimeDelta.
int64_t SerializeTimeDelta(const base::TimeDelta& time_delta);
base::TimeDelta DeserializeTimeDelta(int64_t serialized_time_delta);
} // namespace ntp_snippets } // namespace ntp_snippets
#endif // COMPONENTS_NTP_SNIPPETS_TIME_SERIALIZATION_H_ #endif // COMPONENTS_NTP_SNIPPETS_TIME_SERIALIZATION_H_
...@@ -22,17 +22,4 @@ TEST(TimeSerializationTest, TimeSerialization) { ...@@ -22,17 +22,4 @@ TEST(TimeSerializationTest, TimeSerialization) {
} }
} }
TEST(TimeSerializationTest, TimeDeltaSerialization) {
std::vector<base::TimeDelta> values_to_test = {
base::TimeDelta::Min(), base::TimeDelta::FromHours(-1),
base::TimeDelta::FromSeconds(0), base::TimeDelta::FromHours(1),
base::TimeDelta::Max()};
for (const base::TimeDelta& value : values_to_test) {
EXPECT_EQ(SerializeTimeDelta(value), value.ToInternalValue());
EXPECT_EQ(base::TimeDelta::FromInternalValue(SerializeTimeDelta(value)),
value);
EXPECT_EQ(DeserializeTimeDelta(SerializeTimeDelta(value)), value);
}
}
} // namespace ntp_snippets } // namespace ntp_snippets
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment