Chromium Code Reviews| Index: device/bluetooth/mojo/bluetooth_uuid_struct_traits.h |
| diff --git a/device/bluetooth/mojo/bluetooth_uuid_struct_traits.h b/device/bluetooth/mojo/bluetooth_uuid_struct_traits.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c34246f24bb592cf00c568c6e2e707c473057d85 |
| --- /dev/null |
| +++ b/device/bluetooth/mojo/bluetooth_uuid_struct_traits.h |
| @@ -0,0 +1,49 @@ |
| +// 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 DEVICE_BLUETOOTH_MOJO_BLUETOOTH_UUID_STRUCT_TRAITS_H_ |
| +#define DEVICE_BLUETOOTH_MOJO_BLUETOOTH_UUID_STRUCT_TRAITS_H_ |
| + |
| +#include "base/strings/string_piece.h" |
| +#include "device/bluetooth/bluetooth_uuid.h" |
| +#include "device/bluetooth/mojo/bluetooth_uuid.mojom.h" |
| + |
| +namespace mojo { |
| + |
| +template <> |
| +struct StructTraits<device::mojom::BluetoothUUID, |
| + std::unique_ptr<device::BluetoothUUID>> { |
| + static bool IsNull(const std::unique_ptr<device::BluetoothUUID>& uuid) { |
| + return !uuid.get(); |
|
Ken Rockot(use gerrit already)
2016/05/26 21:05:07
nit: !uuid should be sufficient
ortuno
2016/05/27 14:38:36
Done.
|
| + } |
| + |
| + static void SetToNull(std::unique_ptr<device::BluetoothUUID>* output) { |
| + *output = nullptr; |
|
Ken Rockot(use gerrit already)
2016/05/26 21:05:07
optional style/opinion nit: perhaps output->reset(
ortuno
2016/05/27 14:38:36
Done.
|
| + } |
| + |
| + static base::StringPiece uuid( |
| + const std::unique_ptr<device::BluetoothUUID>& uuid) { |
| + DCHECK(uuid.get()); |
|
Ken Rockot(use gerrit already)
2016/05/26 21:05:07
nit: DCHECK(uuid) should be sufficient
ortuno
2016/05/27 14:38:36
Done.
|
| + return base::StringPiece(uuid->canonical_value().c_str(), |
| + uuid->canonical_value().length()); |
| + } |
| + |
| + static bool Read(device::mojom::BluetoothUUIDDataView input, |
| + std::unique_ptr<device::BluetoothUUID>* output) { |
| + base::StringPiece result; |
| + if (!input.ReadUuid(&result)) |
| + return false; |
| + *output = base::WrapUnique(new device::BluetoothUUID(result.as_string())); |
| + |
| + // If the format isn't 128-bit, .value() would return a different answer |
| + // than .canonical_value(). Then if browser-side code accidentally checks |
| + // .value() against a 128-bit string literal, a hostile renderer could use |
| + // the 16- or 32-bit format and evade the check. |
| + return (*output)->IsValid() && |
| + (*output)->format() == device::BluetoothUUID::kFormat128Bit; |
| + } |
| +}; |
| +} |
| + |
| +#endif // DEVICE_BLUETOOTH_MOJO_BLUETOOTH_UUID_STRUCT_TRAITS_H_ |