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.
5 #ifndef CONTENT_COMMON_INTER_PROCESS_TIME_TICKS_CONVERTER_H_
6 #define CONTENT_COMMON_INTER_PROCESS_TIME_TICKS_CONVERTER_H_
9 #include "content/common/content_export.h"
15 class RemoteTimeDelta
;
16 class RemoteTimeTicks
;
18 // On Windows, TimeTicks are not consistent between processes. Often, the values
19 // on one process have a static offset relative to another. Occasionally, these
20 // offsets shift while running.
22 // To combat this, any TimeTicks values sent from the remote process to the
23 // local process must be tweaked in order to appear monotonic.
25 // In order to properly tweak ticks, we need 4 reference points:
27 // - |local_lower_bound|: A known point, recorded on the local process, that
28 // occurs before any remote values that will be
30 // - |remote_lower_bound|: The equivalent point on the remote process. This
31 // should be recorded immediately after
32 // |local_lower_bound|.
33 // - |local_upper_bound|: A known point, recorded on the local process, that
34 // occurs after any remote values that will be
36 // - |remote_upper_bound|: The equivalent point on the remote process. This
37 // should be recorded immediately before
38 // |local_upper_bound|.
40 // Once these bounds are determined, values within the remote process's range
41 // can be converted to the local process's range. The values are converted as
44 // 1. If the remote's range exceeds the local's range, it is scaled to fit.
45 // Any values converted will have the same scale factor applied.
47 // 2. The remote's range is shifted so that it is centered within the
48 // local's range. Any values converted will be shifted the same amount.
49 class CONTENT_EXPORT InterProcessTimeTicksConverter
{
51 InterProcessTimeTicksConverter(const LocalTimeTicks
& local_lower_bound
,
52 const LocalTimeTicks
& local_upper_bound
,
53 const RemoteTimeTicks
& remote_lower_bound
,
54 const RemoteTimeTicks
& remote_upper_bound
);
56 // Returns the value within the local's bounds that correlates to
58 LocalTimeTicks
ToLocalTimeTicks(const RemoteTimeTicks
& remote_ms
);
60 // Returns the equivalent delta after applying remote-to-local scaling to
62 LocalTimeDelta
ToLocalTimeDelta(const RemoteTimeDelta
& remote_delta
);
65 int64
Convert(int64 value
);
71 int64 remote_lower_bound_
;
72 int64 remote_upper_bound_
;
75 class CONTENT_EXPORT LocalTimeDelta
{
77 int ToInt32() const { return value_
; }
80 friend class InterProcessTimeTicksConverter
;
81 friend class LocalTimeTicks
;
83 LocalTimeDelta(int value
) : value_(value
) {}
88 class CONTENT_EXPORT LocalTimeTicks
{
90 static LocalTimeTicks
FromTimeTicks(const base::TimeTicks
& value
) {
91 return LocalTimeTicks(value
.ToInternalValue());
94 base::TimeTicks
ToTimeTicks() {
95 return base::TimeTicks::FromInternalValue(value_
);
98 LocalTimeTicks
operator+(const LocalTimeDelta
& delta
) {
99 return LocalTimeTicks(value_
+ delta
.value_
);
103 friend class InterProcessTimeTicksConverter
;
105 LocalTimeTicks(int64 value
) : value_(value
) {}
110 class CONTENT_EXPORT RemoteTimeDelta
{
112 static RemoteTimeDelta
FromRawDelta(int delta
) {
113 return RemoteTimeDelta(delta
);
117 friend class InterProcessTimeTicksConverter
;
118 friend class RemoteTimeTicks
;
120 RemoteTimeDelta(int value
) : value_(value
) {}
125 class CONTENT_EXPORT RemoteTimeTicks
{
127 static RemoteTimeTicks
FromTimeTicks(const base::TimeTicks
& ticks
) {
128 return RemoteTimeTicks(ticks
.ToInternalValue());
131 RemoteTimeDelta
operator-(const RemoteTimeTicks
& rhs
) const {
132 return RemoteTimeDelta(value_
- rhs
.value_
);
136 friend class InterProcessTimeTicksConverter
;
138 RemoteTimeTicks(int64 value
) : value_(value
) {}
143 } // namespace content
145 #endif // CONTENT_COMMON_INTER_PROCESS_TIME_TICKS_CONVERTER_H_