skym | fe6d903 | 2016-04-22 14:10:23 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
maxbogue | f1885f7d | 2016-08-16 19:13:20 | [diff] [blame^] | 5 | #include "components/sync/device_info/device_info_util.h" |
skym | fe6d903 | 2016-04-22 14:10:23 | [diff] [blame] | 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | #include "base/strings/string_util.h" |
Max Bogue | fef332d | 2016-07-28 22:09:09 | [diff] [blame] | 10 | #include "components/sync/protocol/sync.pb.h" |
skym | fe6d903 | 2016-04-22 14:10:23 | [diff] [blame] | 11 | |
| 12 | namespace sync_driver { |
| 13 | |
| 14 | using base::Time; |
| 15 | using base::TimeDelta; |
| 16 | using sync_pb::DeviceInfoSpecifics; |
| 17 | |
| 18 | const char DeviceInfoUtil::kClientTagPrefix[] = "DeviceInfo_"; |
| 19 | const TimeDelta DeviceInfoUtil::kPulseInterval = TimeDelta::FromDays(1); |
| 20 | const TimeDelta DeviceInfoUtil::kActiveThreshold = TimeDelta::FromDays(14); |
| 21 | |
| 22 | namespace { |
| 23 | |
| 24 | base::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 |
| 32 | base::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 |
| 40 | bool DeviceInfoUtil::IsActive(const Time last_update, const Time now) { |
| 41 | return Age(last_update, now) < kActiveThreshold; |
| 42 | } |
| 43 | |
| 44 | // static |
| 45 | std::string DeviceInfoUtil::SpecificsToTag( |
| 46 | const sync_pb::DeviceInfoSpecifics& specifics) { |
| 47 | return kClientTagPrefix + specifics.cache_guid(); |
| 48 | } |
| 49 | |
| 50 | // static |
| 51 | std::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 |