Chromium Code Reviews| Index: components/mus/input_devices/input_device_struct_traits.h |
| diff --git a/components/mus/input_devices/input_device_struct_traits.h b/components/mus/input_devices/input_device_struct_traits.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..41434011560005f84f2faf51ae08357c77cdb96f |
| --- /dev/null |
| +++ b/components/mus/input_devices/input_device_struct_traits.h |
| @@ -0,0 +1,81 @@ |
| +// 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. |
| + |
| +#ifndef COMPONENTS_MUS_INPUT_DEVICES_INPUT_DEVICE_STRUCT_TRAITS_H_ |
| +#define COMPONENTS_MUS_INPUT_DEVICES_INPUT_DEVICE_STRUCT_TRAITS_H_ |
| + |
| +#include <string> |
| + |
| +#include "base/strings/string_piece.h" |
| +#include "components/mus/public/interfaces/input_devices/input_devices.mojom.h" |
| +#include "ui/events/devices/input_device.h" |
| + |
| +namespace mojo { |
| + |
| +template <> |
| +struct StructTraits<input_device::mojom::InputDevice, ui::InputDevice> { |
| + static int32_t id(const ui::InputDevice& device) { return device.id; } |
| + |
| + static input_device::mojom::InputDeviceType type( |
| + const ui::InputDevice& device) { |
| + switch (device.type) { |
| + case ui::INPUT_DEVICE_INTERNAL: |
| + return input_device::mojom::InputDeviceType::INPUT_DEVICE_INTERNAL; |
| + case ui::INPUT_DEVICE_EXTERNAL: |
| + return input_device::mojom::InputDeviceType::INPUT_DEVICE_EXTERNAL; |
| + case ui::INPUT_DEVICE_UNKNOWN: |
| + return input_device::mojom::InputDeviceType::INPUT_DEVICE_UNKNOWN; |
| + } |
| + } |
| + |
| + static std::string name(const ui::InputDevice& device) { return device.name; } |
|
sadrul
2016/06/07 03:39:25
Can the return type be const-ref in struct-traits?
kylechar
2016/06/07 16:36:41
I don't see why not. Done.
|
| + |
| + static base::StringPiece sys_path(const ui::InputDevice& device) { |
| + return device.sys_path.AsUTF8Unsafe(); |
| + } |
| + |
| + static uint32_t vendor_id(const ui::InputDevice& device) { |
| + return device.vendor_id; |
| + } |
| + |
| + static uint32_t product_id(const ui::InputDevice& device) { |
| + return device.product_id; |
| + } |
| + |
| + static bool Read(input_device::mojom::InputDeviceDataView data, |
| + ui::InputDevice* out) { |
| + out->id = data.id(); |
| + |
| + switch (data.type()) { |
| + case input_device::mojom::InputDeviceType::INPUT_DEVICE_INTERNAL: |
| + out->type = ui::INPUT_DEVICE_INTERNAL; |
| + break; |
| + case input_device::mojom::InputDeviceType::INPUT_DEVICE_EXTERNAL: |
| + out->type = ui::INPUT_DEVICE_EXTERNAL; |
| + break; |
| + case input_device::mojom::InputDeviceType::INPUT_DEVICE_UNKNOWN: |
| + out->type = ui::INPUT_DEVICE_UNKNOWN; |
| + break; |
| + default: |
|
sadrul
2016/06/07 03:39:25
Remove default case?
kylechar
2016/06/07 16:36:41
Do we want that? We can't necessarily control what
sadrul
2016/06/08 16:31:09
Hm, good point. I was going for the 'omit default:
|
| + return false; |
| + } |
| + |
| + if (!data.ReadName(&out->name)) |
| + return false; |
| + |
| + base::StringPiece sys_path_string; |
| + if (!data.ReadSysPath(&sys_path_string)) |
| + return false; |
| + out->sys_path = base::FilePath::FromUTF8Unsafe(sys_path_string); |
| + |
| + out->vendor_id = data.vendor_id(); |
| + out->product_id = data.product_id(); |
| + |
| + return true; |
| + } |
| +}; |
| + |
| +} // namespace mojo |
| + |
| +#endif // COMPONENTS_MUS_INPUT_DEVICES_INPUT_DEVICE_STRUCT_TRAITS_H_ |