1 // Copyright 2018 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 #include "third_party/blink/renderer/modules/bluetooth/bluetooth_service_data_map.h"
6
7 #include "third_party/blink/renderer/core/typed_arrays/dom_data_view.h"
8 #include "third_party/blink/renderer/modules/bluetooth/bluetooth_remote_gatt_utils.h"
9
10 namespace blink {
11
12 class BluetoothServiceDataMapIterationSource final
13 : public PairIterable<String, Member<DOMDataView>>::IterationSource {
14 public:
BluetoothServiceDataMapIterationSource(const BluetoothServiceDataMap & map)15 BluetoothServiceDataMapIterationSource(const BluetoothServiceDataMap& map)
16 : map_(map), iterator_(map_->Map().begin()) {}
17
Next(ScriptState * script_state,String & map_key,Member<DOMDataView> & map_value,ExceptionState &)18 bool Next(ScriptState* script_state,
19 String& map_key,
20 Member<DOMDataView>& map_value,
21 ExceptionState&) override {
22 if (iterator_ == map_->Map().end())
23 return false;
24 map_key = iterator_->key;
25 map_value =
26 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(iterator_->value);
27 ++iterator_;
28 return true;
29 }
30
Trace(Visitor * visitor) const31 void Trace(Visitor* visitor) const override {
32 visitor->Trace(map_);
33 PairIterable<String, Member<DOMDataView>>::IterationSource::Trace(visitor);
34 }
35
36 private:
37 // Needs to be kept alive while we're iterating over it.
38 const Member<const BluetoothServiceDataMap> map_;
39 BluetoothServiceDataMap::MapType::const_iterator iterator_;
40 };
41
BluetoothServiceDataMap(const BluetoothServiceDataMap::MapType & map)42 BluetoothServiceDataMap::BluetoothServiceDataMap(
43 const BluetoothServiceDataMap::MapType& map)
44 : parameter_map_(map) {}
45
~BluetoothServiceDataMap()46 BluetoothServiceDataMap::~BluetoothServiceDataMap() {}
47
48 PairIterable<String, Member<DOMDataView>>::IterationSource*
StartIteration(ScriptState *,ExceptionState &)49 BluetoothServiceDataMap::StartIteration(ScriptState*, ExceptionState&) {
50 return MakeGarbageCollected<BluetoothServiceDataMapIterationSource>(*this);
51 }
52
GetMapEntry(ScriptState *,const String & key,Member<DOMDataView> & value,ExceptionState &)53 bool BluetoothServiceDataMap::GetMapEntry(ScriptState*,
54 const String& key,
55 Member<DOMDataView>& value,
56 ExceptionState&) {
57 auto it = parameter_map_.find(key);
58 if (it == parameter_map_.end())
59 return false;
60
61 DOMDataView* dom_data_view =
62 BluetoothRemoteGATTUtils::ConvertWTFVectorToDataView(it->value);
63
64 value = dom_data_view;
65 return true;
66 }
67
68 } // namespace blink
69