[email protected] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 1 | // 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] | bc8ddc3 | 2013-03-11 20:28:08 | [diff] [blame] | 8 | #include "base/strings/string_number_conversions.h" |
[email protected] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 9 | |
| 10 | namespace content { |
| 11 | |
| 12 | InterProcessTimeTicksConverter::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] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 19 | 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] | bc8ddc3 | 2013-03-11 20:28:08 | [diff] [blame] | 21 | DCHECK_GE(target_range, 0); |
| 22 | DCHECK_GE(source_range, 0); |
[email protected] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 23 | if (source_range <= target_range) { |
[email protected] | bc8ddc3 | 2013-03-11 20:28:08 | [diff] [blame] | 24 | // We fit! Center the source range on the target range. |
[email protected] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 25 | numerator_ = 1; |
| 26 | denominator_ = 1; |
[email protected] | bc8ddc3 | 2013-03-11 20:28:08 | [diff] [blame] | 27 | 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] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 34 | return; |
| 35 | } |
[email protected] | bc8ddc3 | 2013-03-11 20:28:08 | [diff] [blame] | 36 | |
| 37 | // Interpolate values so that remote range will be will exactly fit into the |
| 38 | // local range, if possible. |
[email protected] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 39 | numerator_ = target_range; |
| 40 | denominator_ = source_range; |
[email protected] | bc8ddc3 | 2013-03-11 20:28:08 | [diff] [blame] | 41 | 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] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | LocalTimeTicks InterProcessTimeTicksConverter::ToLocalTimeTicks( |
[email protected] | ec29880 | 2013-03-27 16:45:07 | [diff] [blame] | 51 | const RemoteTimeTicks& remote_ms) const { |
[email protected] | bc8ddc3 | 2013-03-11 20:28:08 | [diff] [blame] | 52 | // If input time is "null", return another "null" time. |
| 53 | if (remote_ms.value_ == 0) |
| 54 | return LocalTimeTicks(0); |
[email protected] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 55 | 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] | bc8ddc3 | 2013-03-11 20:28:08 | [diff] [blame] | 58 | return LocalTimeTicks(local_base_time_ + |
[email protected] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 59 | ToLocalTimeDelta(remote_delta).value_); |
| 60 | } |
| 61 | |
| 62 | LocalTimeDelta InterProcessTimeTicksConverter::ToLocalTimeDelta( |
[email protected] | ec29880 | 2013-03-27 16:45:07 | [diff] [blame] | 63 | const RemoteTimeDelta& remote_delta) const { |
[email protected] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 64 | DCHECK_GE(remote_upper_bound_, remote_lower_bound_ + remote_delta.value_); |
| 65 | return LocalTimeDelta(Convert(remote_delta.value_)); |
| 66 | } |
| 67 | |
[email protected] | ec29880 | 2013-03-27 16:45:07 | [diff] [blame] | 68 | int64 InterProcessTimeTicksConverter::Convert(int64 value) const { |
[email protected] | cb8a8742 | 2011-12-16 01:15:57 | [diff] [blame] | 69 | if (value <= 0) { |
| 70 | return value; |
| 71 | } |
| 72 | return numerator_ * value / denominator_; |
| 73 | } |
| 74 | |
| 75 | } // namespace content |