Index: device/sensors/public/interfaces/sensor_struct_traits.cc |
diff --git a/device/sensors/public/interfaces/sensor_struct_traits.cc b/device/sensors/public/interfaces/sensor_struct_traits.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b6fc64c15dba3ab675aac25574a32606d62c7992 |
--- /dev/null |
+++ b/device/sensors/public/interfaces/sensor_struct_traits.cc |
@@ -0,0 +1,122 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "device/sensors/public/interfaces/sensor_struct_traits.h" |
+ |
+#define CHECK_ENUM(VALUE1, VALUE2) \ |
+ static_assert(static_cast<int32_t>(VALUE1) == static_cast<int32_t>(VALUE2), \ |
+ "Enum value mismatch.") |
+ |
+namespace mojo { |
+ |
+// static |
+bool StructTraits<device::mojom::SensorConfiguration, |
+ device::PlatformSensorConfiguration>:: |
+ Read(device::mojom::SensorConfigurationDataView data, |
+ device::PlatformSensorConfiguration* out) { |
+ // Maximum allowed frequency is capped to 60Hz. |
+ if (data.frequency() > 60.0 || data.frequency() <= 0.0) { |
+ return false; |
+ } |
+ |
+ out->set_frequency(data.frequency()); |
+ return true; |
+} |
+ |
+// static asserts to verify that enums are in sync |
+CHECK_ENUM(device::ReportingMode::ON_CHANGE, |
+ device::mojom::ReportingMode::ON_CHANGE); |
+CHECK_ENUM(device::ReportingMode::CONTINUOUS, |
+ device::mojom::ReportingMode::CONTINUOUS); |
+ |
+device::mojom::ReportingMode |
+EnumTraits<device::mojom::ReportingMode, device::ReportingMode>::ToMojom( |
+ device::ReportingMode reporting_mode) { |
+ switch (reporting_mode) { |
+ case device::ReportingMode::ON_CHANGE: |
+ return device::mojom::ReportingMode::ON_CHANGE; |
+ case device::ReportingMode::CONTINUOUS: |
+ return device::mojom::ReportingMode::CONTINUOUS; |
+ } |
+ |
+ NOTREACHED(); |
+ return device::mojom::ReportingMode::ON_CHANGE; |
+} |
+ |
+// static |
+bool EnumTraits<device::mojom::ReportingMode, device::ReportingMode>::FromMojom( |
+ device::mojom::ReportingMode input, |
+ device::ReportingMode* out) { |
+ switch (input) { |
+ case device::mojom::ReportingMode::ON_CHANGE: |
+ *out = device::ReportingMode::ON_CHANGE; |
+ return true; |
+ case device::mojom::ReportingMode::CONTINUOUS: |
+ *out = device::ReportingMode::CONTINUOUS; |
+ return true; |
+ } |
+ |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
+// static asserts to verify that enums are in sync |
+CHECK_ENUM(device::SensorType::FIRST, device::mojom::SensorType::FIRST); |
+CHECK_ENUM(device::SensorType::AMBIENT_LIGHT, |
+ device::mojom::SensorType::AMBIENT_LIGHT); |
+CHECK_ENUM(device::SensorType::PROXIMITY, device::mojom::SensorType::PROXIMITY); |
+CHECK_ENUM(device::SensorType::ACCELEROMETER, |
+ device::mojom::SensorType::ACCELEROMETER); |
+CHECK_ENUM(device::SensorType::GYROSCOPE, device::mojom::SensorType::GYROSCOPE); |
+CHECK_ENUM(device::SensorType::PRESSURE, device::mojom::SensorType::PRESSURE); |
+CHECK_ENUM(device::SensorType::LAST, device::mojom::SensorType::LAST); |
+ |
+// static |
+device::mojom::SensorType |
+EnumTraits<device::mojom::SensorType, device::SensorType>::ToMojom( |
+ device::SensorType sensor_type) { |
+ switch (sensor_type) { |
+ case device::SensorType::AMBIENT_LIGHT: |
+ return device::mojom::SensorType::AMBIENT_LIGHT; |
+ case device::SensorType::PROXIMITY: |
+ return device::mojom::SensorType::PROXIMITY; |
+ case device::SensorType::ACCELEROMETER: |
+ return device::mojom::SensorType::ACCELEROMETER; |
+ case device::SensorType::GYROSCOPE: |
+ return device::mojom::SensorType::GYROSCOPE; |
+ case device::SensorType::PRESSURE: |
+ return device::mojom::SensorType::PRESSURE; |
+ } |
+ |
+ NOTREACHED(); |
+ return device::mojom::SensorType::AMBIENT_LIGHT; |
+} |
+ |
+// static |
+bool EnumTraits<device::mojom::SensorType, device::SensorType>::FromMojom( |
+ device::mojom::SensorType input, |
+ device::SensorType* out) { |
+ switch (input) { |
+ case device::mojom::SensorType::AMBIENT_LIGHT: |
+ *out = device::SensorType::AMBIENT_LIGHT; |
+ return true; |
+ case device::mojom::SensorType::PROXIMITY: |
+ *out = device::SensorType::PROXIMITY; |
+ return true; |
+ case device::mojom::SensorType::ACCELEROMETER: |
+ *out = device::SensorType::ACCELEROMETER; |
+ return true; |
+ case device::mojom::SensorType::GYROSCOPE: |
+ *out = device::SensorType::GYROSCOPE; |
+ return true; |
+ case device::mojom::SensorType::PRESSURE: |
+ *out = device::SensorType::PRESSURE; |
+ return true; |
+ } |
+ |
+ NOTREACHED(); |
+ return false; |
+} |
+ |
+} // namespace mojo |