• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 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 MOJO_COMMON_TIME_STRUCT_TRAITS_H_
6 #define MOJO_COMMON_TIME_STRUCT_TRAITS_H_
7 
8 #include "base/time/time.h"
9 #include "mojo/common/time.mojom-shared.h"
10 
11 namespace mojo {
12 
13 template <>
14 struct StructTraits<common::mojom::TimeDataView, base::Time> {
15   static int64_t internal_value(const base::Time& time) {
16     return time.since_origin().InMicroseconds();
17   }
18 
19   static bool Read(common::mojom::TimeDataView data, base::Time* time) {
20     *time =
21         base::Time() + base::TimeDelta::FromMicroseconds(data.internal_value());
22     return true;
23   }
24 };
25 
26 template <>
27 struct StructTraits<common::mojom::TimeDeltaDataView, base::TimeDelta> {
28   static int64_t microseconds(const base::TimeDelta& delta) {
29     return delta.InMicroseconds();
30   }
31 
32   static bool Read(common::mojom::TimeDeltaDataView data,
33                    base::TimeDelta* delta) {
34     *delta = base::TimeDelta::FromMicroseconds(data.microseconds());
35     return true;
36   }
37 };
38 
39 template <>
40 struct StructTraits<common::mojom::TimeTicksDataView, base::TimeTicks> {
41   static int64_t internal_value(const base::TimeTicks& time) {
42     return time.since_origin().InMicroseconds();
43   }
44 
45   static bool Read(common::mojom::TimeTicksDataView data,
46                    base::TimeTicks* time) {
47     *time = base::TimeTicks() +
48             base::TimeDelta::FromMicroseconds(data.internal_value());
49     return true;
50   }
51 };
52 
53 }  // namespace mojo
54 
55 #endif  // MOJO_COMMON_TIME_STRUCT_TRAITS_H_
56