OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 DEVICE_BLUETOOTH_PUBLIC_INTERFACES_BLUETOOTH_UUID_STRUCT_TRAITS_H_ |
| 6 #define DEVICE_BLUETOOTH_PUBLIC_INTERFACES_BLUETOOTH_UUID_STRUCT_TRAITS_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/optional.h" |
| 11 #include "base/strings/string_piece.h" |
| 12 #include "device/bluetooth/bluetooth_uuid.h" |
| 13 #include "device/bluetooth/public/interfaces/bluetooth_uuid.mojom.h" |
| 14 |
| 15 namespace mojo { |
| 16 |
| 17 template <> |
| 18 struct StructTraits<device::mojom::BluetoothUUID, |
| 19 base::Optional<device::BluetoothUUID>> { |
| 20 static bool IsNull(const base::Optional<device::BluetoothUUID>& uuid) { |
| 21 return !uuid; |
| 22 } |
| 23 |
| 24 static void SetToNull(base::Optional<device::BluetoothUUID>* output) { |
| 25 *output = base::nullopt; |
| 26 } |
| 27 |
| 28 static std::string uuid(const base::Optional<device::BluetoothUUID>& uuid) { |
| 29 DCHECK(uuid); |
| 30 return uuid->canonical_value(); |
| 31 } |
| 32 |
| 33 static bool Read(device::mojom::BluetoothUUIDDataView input, |
| 34 base::Optional<device::BluetoothUUID>* output) { |
| 35 std::string result; |
| 36 if (!input.ReadUuid(&result)) |
| 37 return false; |
| 38 *output = base::make_optional(device::BluetoothUUID(result)); |
| 39 |
| 40 // If the format isn't 128-bit, .value() would return a different answer |
| 41 // than .canonical_value(). Then if browser-side code accidentally checks |
| 42 // .value() against a 128-bit string literal, a hostile renderer could use |
| 43 // the 16- or 32-bit format and evade the check. |
| 44 return output->value().IsValid() && |
| 45 output->value().format() == device::BluetoothUUID::kFormat128Bit; |
| 46 } |
| 47 }; |
| 48 |
| 49 } // namespace mojo |
| 50 |
| 51 #endif // DEVICE_BLUETOOTH_PUBLIC_INTERFACES_BLUETOOTH_UUID_STRUCT_TRAITS_H_ |
OLD | NEW |