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,
RegisterInt64Pref(
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 {
base::Time default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
void RegisterTimeDeltaPref(const std::string& path,
base::TimeDelta default_value,
uint32_t flags = NO_REGISTRATION_FLAGS);
protected:
~PrefRegistrySimple() override;
......
......@@ -480,6 +480,14 @@ base::Time PrefService::GetTime(const std::string& path) const {
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::Type type) {
CHECK(type == base::Value::Type::DICTIONARY ||
......
......@@ -16,6 +16,7 @@
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/compiler_specific.h"
......@@ -247,11 +248,14 @@ class COMPONENTS_PREFS_EXPORT PrefService {
uint64_t GetUint64(const std::string& path) const;
// Time helper methods that actually store the given value as a string, which
// represents the number of microseconds elapsed since the Windows epoch. Note
// that if obtaining the named value via GetDictionary or GetList, the Value
// type will be Type::STRING.
// represents the number of microseconds elapsed (absolute for TimeDelta and
// relative to Windows epoch for Time variants). Note that if obtaining the
// named value via GetDictionary or GetList, the Value type will be
// Type::STRING.
void SetTime(const std::string& path, base::Time value);
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
// the preference is not set in the user pref store, returns NULL.
......
......@@ -259,6 +259,35 @@ TEST(PrefServiceTest, SetTimeValue_NullTime) {
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
// values to it.
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