OLD | NEW |
(Empty) | |
| 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 #ifndef CONTENT_COMMON_INTER_PROCESS_TIME_TICKS_CONVERTER_H_ |
| 6 #define CONTENT_COMMON_INTER_PROCESS_TIME_TICKS_CONVERTER_H_ |
| 7 |
| 8 #include "base/time.h" |
| 9 |
| 10 namespace content { |
| 11 |
| 12 class LocalTimeDelta; |
| 13 class LocalTimeTicks; |
| 14 class RemoteTimeDelta; |
| 15 class RemoteTimeTicks; |
| 16 |
| 17 // On Windows, TimeTicks are not consistent between processes. Often, the values |
| 18 // on one process have a static offset relative to another. Occasionally, these |
| 19 // offsets shift while running. |
| 20 // |
| 21 // To combat this, any TimeTicks values sent from the remote process to the |
| 22 // local process must be tweaked in order to appear monotonic. |
| 23 // |
| 24 // In order to properly tweak ticks, we need 4 reference points: |
| 25 // |
| 26 // - |local_lower_bound|: A known point, recorded on the local process, that |
| 27 // occurs before any remote values that will be |
| 28 // converted. |
| 29 // - |remote_lower_bound|: The equivalent point on the remote process. This |
| 30 // should be recorded immediately after |
| 31 // |local_lower_bound|. |
| 32 // - |local_upper_bound|: A known point, recorded on the local process, that |
| 33 // occurs after any remote values that will be |
| 34 // converted. |
| 35 // - |remote_upper_bound|: The equivalent point on the remote process. This |
| 36 // should be recorded immediately before |
| 37 // |local_upper_bound|. |
| 38 // |
| 39 // Once these bounds are determined, values within the remote process's range |
| 40 // can be converted to the local process's range. The values are converted as |
| 41 // follows: |
| 42 // |
| 43 // 1. If the remote's range exceeds the local's range, it is scaled to fit. |
| 44 // Any values converted will have the same scale factor applied. |
| 45 // |
| 46 // 2. The remote's range is shifted so that it is centered within the |
| 47 // local's range. Any values converted will be shifted the same amount. |
| 48 class InterProcessTimeTicksConverter { |
| 49 public: |
| 50 InterProcessTimeTicksConverter(const LocalTimeTicks& local_lower_bound, |
| 51 const LocalTimeTicks& local_upper_bound, |
| 52 const RemoteTimeTicks& remote_lower_bound, |
| 53 const RemoteTimeTicks& remote_upper_bound); |
| 54 |
| 55 // Returns the value within the local's bounds that correlates to |
| 56 // |remote_ms|. |
| 57 LocalTimeTicks ToLocalTimeTicks(const RemoteTimeTicks& remote_ms); |
| 58 |
| 59 // Returns the equivalent delta after applying remote-to-local scaling to |
| 60 // |remote_delta|. |
| 61 LocalTimeDelta ToLocalTimeDelta(const RemoteTimeDelta& remote_delta); |
| 62 |
| 63 private: |
| 64 int64 Convert(int64 value); |
| 65 |
| 66 int64 offset_; |
| 67 int64 numerator_; |
| 68 int64 denominator_; |
| 69 |
| 70 int64 remote_lower_bound_; |
| 71 int64 remote_upper_bound_; |
| 72 }; |
| 73 |
| 74 class LocalTimeDelta { |
| 75 public: |
| 76 int ToInt32() const { return value_; } |
| 77 |
| 78 private: |
| 79 friend class InterProcessTimeTicksConverter; |
| 80 friend class LocalTimeTicks; |
| 81 |
| 82 LocalTimeDelta(int value) : value_(value) {} |
| 83 |
| 84 int value_; |
| 85 }; |
| 86 |
| 87 class LocalTimeTicks { |
| 88 public: |
| 89 static LocalTimeTicks FromTimeTicks(const base::TimeTicks& value) { |
| 90 return LocalTimeTicks(value.ToInternalValue()); |
| 91 } |
| 92 |
| 93 base::TimeTicks ToTimeTicks() { |
| 94 return base::TimeTicks::FromInternalValue(value_); |
| 95 } |
| 96 |
| 97 LocalTimeTicks operator+(const LocalTimeDelta& delta) { |
| 98 return LocalTimeTicks(value_ + delta.value_); |
| 99 } |
| 100 |
| 101 private: |
| 102 friend class InterProcessTimeTicksConverter; |
| 103 |
| 104 LocalTimeTicks(int64 value) : value_(value) {} |
| 105 |
| 106 int64 value_; |
| 107 }; |
| 108 |
| 109 class RemoteTimeDelta { |
| 110 public: |
| 111 static RemoteTimeDelta FromRawDelta(int delta) { |
| 112 return RemoteTimeDelta(delta); |
| 113 } |
| 114 |
| 115 private: |
| 116 friend class InterProcessTimeTicksConverter; |
| 117 friend class RemoteTimeTicks; |
| 118 |
| 119 RemoteTimeDelta(int value) : value_(value) {} |
| 120 |
| 121 int value_; |
| 122 }; |
| 123 |
| 124 class RemoteTimeTicks { |
| 125 public: |
| 126 static RemoteTimeTicks FromTimeTicks(const base::TimeTicks& ticks) { |
| 127 return RemoteTimeTicks(ticks.ToInternalValue()); |
| 128 } |
| 129 |
| 130 RemoteTimeDelta operator-(const RemoteTimeTicks& rhs) const { |
| 131 return RemoteTimeDelta(value_ - rhs.value_); |
| 132 } |
| 133 |
| 134 private: |
| 135 friend class InterProcessTimeTicksConverter; |
| 136 |
| 137 RemoteTimeTicks(int64 value) : value_(value) {} |
| 138 |
| 139 int64 value_; |
| 140 }; |
| 141 |
| 142 } // namespace content |
| 143 |
| 144 #endif // CONTENT_COMMON_INTER_PROCESS_TIME_TICKS_CONVERTER_H_ |
OLD | NEW |