blob: 557a6302a6ab84860449460abf5dc6da57dd00fc [file] [log] [blame]
skymfe6d9032016-04-22 14:10:231// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
maxboguef1885f7d2016-08-16 19:13:205#include "components/sync/device_info/device_info_util.h"
skymfe6d9032016-04-22 14:10:236
7#include <algorithm>
8
9#include "base/strings/string_util.h"
Max Boguefef332d2016-07-28 22:09:0910#include "components/sync/protocol/sync.pb.h"
skymfe6d9032016-04-22 14:10:2311
12namespace sync_driver {
13
14using base::Time;
15using base::TimeDelta;
16using sync_pb::DeviceInfoSpecifics;
17
18const char DeviceInfoUtil::kClientTagPrefix[] = "DeviceInfo_";
19const TimeDelta DeviceInfoUtil::kPulseInterval = TimeDelta::FromDays(1);
20const TimeDelta DeviceInfoUtil::kActiveThreshold = TimeDelta::FromDays(14);
21
22namespace {
23
24base::TimeDelta Age(const base::Time last_update, const base::Time now) {
25 // Don't allow negative age for things somehow updated in the future.
26 return std::max(base::TimeDelta(), now - last_update);
27}
28
29} // namespace
30
31// static
32base::TimeDelta DeviceInfoUtil::CalculatePulseDelay(
33 const base::Time last_update,
34 const base::Time now) {
35 // Don't allow negative delays for very stale data, use delay of 0.
36 return std::max(base::TimeDelta(), kPulseInterval - Age(last_update, now));
37}
38
39// static
40bool DeviceInfoUtil::IsActive(const Time last_update, const Time now) {
41 return Age(last_update, now) < kActiveThreshold;
42}
43
44// static
45std::string DeviceInfoUtil::SpecificsToTag(
46 const sync_pb::DeviceInfoSpecifics& specifics) {
47 return kClientTagPrefix + specifics.cache_guid();
48}
49
50// static
51std::string DeviceInfoUtil::TagToCacheGuid(const std::string& tag) {
52 DCHECK(base::StartsWith(tag, kClientTagPrefix, base::CompareCase::SENSITIVE));
53 return tag.substr(strlen(kClientTagPrefix));
54}
55
56} // namespace sync_driver