Commit aff2af14 authored by Sky Malice's avatar Sky Malice Committed by Commit Bot

[PrefService] Add GetTimeDelta() and SetTimeDelta() convenience methods.

While not as common of a pattern as storing base::Time in Prefs, it
does show up in a few places, and is the logical continuation of the
Set/GetTime() methods that we currently have.

Change-Id: Ib38c2f4b431733bc4f40323756ea5fc6f68dfbed
Reviewed-on: https://chromium-review.googlesource.com/1103147
Commit-Queue: Sky Malice <skym@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#568089}
parent 31d8f60d
...@@ -91,3 +91,9 @@ void PrefRegistrySimple::RegisterTimePref(const std::string& path, ...@@ -91,3 +91,9 @@ void PrefRegistrySimple::RegisterTimePref(const std::string& path,
RegisterInt64Pref( RegisterInt64Pref(
path, default_value.ToDeltaSinceWindowsEpoch().InMicroseconds(), flags); path, default_value.ToDeltaSinceWindowsEpoch().InMicroseconds(), flags);
} }
void PrefRegistrySimple::RegisterTimeDeltaPref(const std::string& path,
base::TimeDelta default_value,
uint32_t flags) {
RegisterInt64Pref(path, default_value.InMicroseconds(), flags);
}
...@@ -75,6 +75,10 @@ class COMPONENTS_PREFS_EXPORT PrefRegistrySimple : public PrefRegistry { ...@@ -75,6 +75,10 @@ class COMPONENTS_PREFS_EXPORT PrefRegistrySimple : public PrefRegistry {
base::Time default_value, base::Time default_value,
uint32_t flags = NO_REGISTRATION_FLAGS); uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterTimeDeltaPref(const std::string& path,
base::TimeDelta default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
protected: protected:
~PrefRegistrySimple() override; ~PrefRegistrySimple() override;
......
...@@ -480,6 +480,14 @@ base::Time PrefService::GetTime(const std::string& path) const { ...@@ -480,6 +480,14 @@ base::Time PrefService::GetTime(const std::string& path) const {
base::TimeDelta::FromMicroseconds(GetInt64(path))); base::TimeDelta::FromMicroseconds(GetInt64(path)));
} }
void PrefService::SetTimeDelta(const std::string& path, base::TimeDelta value) {
SetInt64(path, value.InMicroseconds());
}
base::TimeDelta PrefService::GetTimeDelta(const std::string& path) const {
return base::TimeDelta::FromMicroseconds(GetInt64(path));
}
base::Value* PrefService::GetMutableUserPref(const std::string& path, base::Value* PrefService::GetMutableUserPref(const std::string& path,
base::Value::Type type) { base::Value::Type type) {
CHECK(type == base::Value::Type::DICTIONARY || CHECK(type == base::Value::Type::DICTIONARY ||
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <memory> #include <memory>
#include <set> #include <set>
#include <string> #include <string>
#include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
...@@ -247,11 +248,14 @@ class COMPONENTS_PREFS_EXPORT PrefService { ...@@ -247,11 +248,14 @@ class COMPONENTS_PREFS_EXPORT PrefService {
uint64_t GetUint64(const std::string& path) const; uint64_t GetUint64(const std::string& path) const;
// Time helper methods that actually store the given value as a string, which // Time helper methods that actually store the given value as a string, which
// represents the number of microseconds elapsed since the Windows epoch. Note // represents the number of microseconds elapsed (absolute for TimeDelta and
// that if obtaining the named value via GetDictionary or GetList, the Value // relative to Windows epoch for Time variants). Note that if obtaining the
// type will be Type::STRING. // named value via GetDictionary or GetList, the Value type will be
// Type::STRING.
void SetTime(const std::string& path, base::Time value); void SetTime(const std::string& path, base::Time value);
base::Time GetTime(const std::string& path) const; base::Time GetTime(const std::string& path) const;
void SetTimeDelta(const std::string& path, base::TimeDelta value);
base::TimeDelta GetTimeDelta(const std::string& path) const;
// Returns the value of the given preference, from the user pref store. If // Returns the value of the given preference, from the user pref store. If
// the preference is not set in the user pref store, returns NULL. // the preference is not set in the user pref store, returns NULL.
......
...@@ -259,6 +259,35 @@ TEST(PrefServiceTest, SetTimeValue_NullTime) { ...@@ -259,6 +259,35 @@ TEST(PrefServiceTest, SetTimeValue_NullTime) {
EXPECT_TRUE(prefs.GetTime(kPrefName).is_null()); EXPECT_TRUE(prefs.GetTime(kPrefName).is_null());
} }
TEST(PrefServiceTest, SetTimeDeltaValue_RegularTimeDelta) {
TestingPrefServiceSimple prefs;
// Register a zero time delta as the default.
prefs.registry()->RegisterTimeDeltaPref(kPrefName, base::TimeDelta());
EXPECT_TRUE(prefs.GetTimeDelta(kPrefName).is_zero());
// Set a time delta and make sure that we can read it without any loss of
// precision.
const base::TimeDelta delta = base::Time::Now() - base::Time();
prefs.SetTimeDelta(kPrefName, delta);
EXPECT_EQ(delta, prefs.GetTimeDelta(kPrefName));
}
TEST(PrefServiceTest, SetTimeDeltaValue_ZeroTimeDelta) {
TestingPrefServiceSimple prefs;
// Register a non-zero time delta as the default.
const base::TimeDelta default_delta =
base::TimeDelta::FromMicroseconds(12345);
prefs.registry()->RegisterTimeDeltaPref(kPrefName, default_delta);
EXPECT_FALSE(prefs.GetTimeDelta(kPrefName).is_zero());
// Set a zero time delta and make sure that it remains zero upon
// deserialization.
prefs.SetTimeDelta(kPrefName, base::TimeDelta());
EXPECT_TRUE(prefs.GetTimeDelta(kPrefName).is_zero());
}
// A PrefStore which just stores the last write flags that were used to write // A PrefStore which just stores the last write flags that were used to write
// values to it. // values to it.
class WriteFlagChecker : public TestingPrefStore { class WriteFlagChecker : public TestingPrefStore {
......
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