Chromium Code Reviews| 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 "base/optional.h" | |
| 9 #include "base/strings/string_piece.h" | |
| 10 #include "device/bluetooth/bluetooth_uuid.h" | |
| 11 #include "device/bluetooth/public/interfaces/bluetooth_uuid.mojom.h" | |
| 12 | |
| 13 namespace mojo { | |
| 14 | |
| 15 template <> | |
| 16 struct StructTraits<device::mojom::BluetoothUUID, | |
| 17 base::Optional<device::BluetoothUUID>> { | |
| 18 static bool IsNull(const base::Optional<device::BluetoothUUID>& uuid) { | |
| 19 return !uuid; | |
| 20 } | |
| 21 | |
| 22 static void SetToNull(base::Optional<device::BluetoothUUID>* output) { | |
| 23 *output = base::nullopt; | |
| 24 } | |
| 25 | |
| 26 static base::StringPiece uuid( | |
|
yzshen1
2016/06/01 16:12:59
nit: you could simply use static const std::string
ortuno
2016/06/01 18:28:12
Nice. Done.
| |
| 27 const base::Optional<device::BluetoothUUID>& uuid) { | |
| 28 DCHECK(uuid); | |
| 29 return base::StringPiece(uuid->canonical_value().c_str(), | |
| 30 uuid->canonical_value().length()); | |
| 31 } | |
| 32 | |
| 33 static bool Read(device::mojom::BluetoothUUIDDataView input, | |
| 34 base::Optional<device::BluetoothUUID>* output) { | |
| 35 base::StringPiece result; | |
| 36 if (!input.ReadUuid(&result)) | |
| 37 return false; | |
| 38 *output = base::make_optional(device::BluetoothUUID(result.as_string())); | |
| 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 |