blob: de3fb13d9913d54f06136406bb3c095982edb358 [file] [log] [blame]
[email protected]cb8a87422011-12-16 01:15:571// Copyright (c) 2011 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
5#include "content/common/inter_process_time_ticks_converter.h"
6
7#include "base/logging.h"
[email protected]bc8ddc32013-03-11 20:28:088#include "base/strings/string_number_conversions.h"
[email protected]cb8a87422011-12-16 01:15:579
10namespace content {
11
12InterProcessTimeTicksConverter::InterProcessTimeTicksConverter(
13 const LocalTimeTicks& local_lower_bound,
14 const LocalTimeTicks& local_upper_bound,
15 const RemoteTimeTicks& remote_lower_bound,
16 const RemoteTimeTicks& remote_upper_bound)
17 : remote_lower_bound_(remote_lower_bound.value_),
18 remote_upper_bound_(remote_upper_bound.value_) {
[email protected]cb8a87422011-12-16 01:15:5719 int64 target_range = local_upper_bound.value_ - local_lower_bound.value_;
20 int64 source_range = remote_upper_bound.value_ - remote_lower_bound.value_;
[email protected]bc8ddc32013-03-11 20:28:0821 DCHECK_GE(target_range, 0);
22 DCHECK_GE(source_range, 0);
[email protected]cb8a87422011-12-16 01:15:5723 if (source_range <= target_range) {
[email protected]bc8ddc32013-03-11 20:28:0824 // We fit! Center the source range on the target range.
[email protected]cb8a87422011-12-16 01:15:5725 numerator_ = 1;
26 denominator_ = 1;
[email protected]bc8ddc32013-03-11 20:28:0827 local_base_time_ =
28 local_lower_bound.value_ + (target_range - source_range) / 2;
29 // When converting times, remote bounds should fall within local bounds.
30 DCHECK_LE(local_lower_bound.value_,
31 ToLocalTimeTicks(remote_lower_bound).value_);
32 DCHECK_GE(local_upper_bound.value_,
33 ToLocalTimeTicks(remote_upper_bound).value_);
[email protected]cb8a87422011-12-16 01:15:5734 return;
35 }
[email protected]bc8ddc32013-03-11 20:28:0836
37 // Interpolate values so that remote range will be will exactly fit into the
38 // local range, if possible.
[email protected]cb8a87422011-12-16 01:15:5739 numerator_ = target_range;
40 denominator_ = source_range;
[email protected]bc8ddc32013-03-11 20:28:0841 local_base_time_ = local_lower_bound.value_;
42 // When converting times, remote bounds should equal local bounds.
43 DCHECK_EQ(local_lower_bound.value_,
44 ToLocalTimeTicks(remote_lower_bound).value_);
45 DCHECK_EQ(local_upper_bound.value_,
46 ToLocalTimeTicks(remote_upper_bound).value_);
47 DCHECK_EQ(target_range, Convert(source_range));
[email protected]cb8a87422011-12-16 01:15:5748}
49
50LocalTimeTicks InterProcessTimeTicksConverter::ToLocalTimeTicks(
[email protected]ec298802013-03-27 16:45:0751 const RemoteTimeTicks& remote_ms) const {
[email protected]bc8ddc32013-03-11 20:28:0852 // If input time is "null", return another "null" time.
53 if (remote_ms.value_ == 0)
54 return LocalTimeTicks(0);
[email protected]cb8a87422011-12-16 01:15:5755 DCHECK_LE(remote_lower_bound_, remote_ms.value_);
56 DCHECK_GE(remote_upper_bound_, remote_ms.value_);
57 RemoteTimeDelta remote_delta = remote_ms - remote_lower_bound_;
[email protected]bc8ddc32013-03-11 20:28:0858 return LocalTimeTicks(local_base_time_ +
[email protected]cb8a87422011-12-16 01:15:5759 ToLocalTimeDelta(remote_delta).value_);
60}
61
62LocalTimeDelta InterProcessTimeTicksConverter::ToLocalTimeDelta(
[email protected]ec298802013-03-27 16:45:0763 const RemoteTimeDelta& remote_delta) const {
[email protected]cb8a87422011-12-16 01:15:5764 DCHECK_GE(remote_upper_bound_, remote_lower_bound_ + remote_delta.value_);
65 return LocalTimeDelta(Convert(remote_delta.value_));
66}
67
[email protected]ec298802013-03-27 16:45:0768int64 InterProcessTimeTicksConverter::Convert(int64 value) const {
[email protected]cb8a87422011-12-16 01:15:5769 if (value <= 0) {
70 return value;
71 }
72 return numerator_ * value / denominator_;
73}
74
75} // namespace content