Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(124)

Unified Diff: third_party/WebKit/Source/modules/bluetooth/ManufacturerDataMap.h

Issue 1403323007: bluetooth: WIP Advertising Data (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bluetooth-characteristic-properties
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: third_party/WebKit/Source/modules/bluetooth/ManufacturerDataMap.h
diff --git a/third_party/WebKit/Source/modules/bluetooth/ManufacturerDataMap.h b/third_party/WebKit/Source/modules/bluetooth/ManufacturerDataMap.h
new file mode 100644
index 0000000000000000000000000000000000000000..a12d32c1844ead2cba0bfee0db2073a5d148a522
--- /dev/null
+++ b/third_party/WebKit/Source/modules/bluetooth/ManufacturerDataMap.h
@@ -0,0 +1,104 @@
+// Copyright 2015 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 ManufacturerDataMap_h
+#define ManufacturerDataMap_h
+
+#include "bindings/core/v8/Maplike.h"
+#include "bindings/core/v8/ScriptWrappable.h"
+#include "core/dom/DOMArrayBuffer.h"
+#include "platform/heap/Handle.h"
+
+namespace blink {
+
+class ExceptionState;
+class ScriptState;
+
+class ManufacturerDataMap final
+ : public GarbageCollected<ManufacturerDataMap>
+ , public ScriptWrappable
+ , public Maplike<unsigned short, RefPtr<DOMArrayBuffer>> {
+ DEFINE_WRAPPERTYPEINFO();
+public:
+ using MapEntry = std::pair<unsigned short, RefPtr<DOMArrayBuffer>>;
+
+ explicit ManufacturerDataMap()
+ : m_entries()
+ , m_keys()
+ {
+ }
+
+ // IDL attributes / methods
+ size_t size() const { return m_keys.size(); };
+
+ DEFINE_INLINE_TRACE()
+ {
+ visitor->trace(m_keys);
+ visitor->trace(m_entries);
+ }
+
+private:
+ // Required by the Iterable interface.
+ IterationSource* startIteration(ScriptState*, ExceptionState&) override
+ {
+ return new MapIterationSource(this);
+ }
+
+ // Required by the Maplike interface.
+ bool getMapEntry(ScriptState*, const unsigned short& key, RefPtr<DOMArrayBuffer>& value, ExceptionState&) override
+ {
+ auto it = m_keys.find(key);
+ if (it == m_keys.end()) {
+ return false;
+ }
+ value = m_entries[it->value].second;
+ return true;
+ }
+
+ // Map requires that the values are iterated over in the same order they
+ // were added so we keep a vector of the entries.
+ HeapVector<std::pair<unsigned short, RefPtr<DOMArrayBuffer>>> m_entries;
+ // Map requires sublinear access times so we keep a map of Company
+ // Identifier codes to the indices of the entries in the vector.
+ HeapHashMap<unsigned short, int> m_keys;
+
+ // Class used by Iterable to access the entries in the map.
+ // Note: This template class relies on the fact that m_map.m_entries will
+ // never be modified once it is created.
+ class MapIterationSource final : public PairIterable<unsigned short, RefPtr<DOMArrayBuffer>>::IterationSource {
+ public:
+ MapIterationSource(ManufacturerDataMap* map)
+ : m_map(map)
+ , m_current(0)
+ {
+ }
+
+ // Required by the IterationSource interface.
+ bool next(ScriptState*, unsigned short& key, RefPtr<DOMArrayBuffer>& value, ExceptionState&) override
+ {
+ if (m_current >= m_map->size()) {
+ return false;
+ }
+ const MapEntry& entry = m_map->m_entries[m_current];
+ key = entry.first;
+ value = entry.second;
+ m_current++;
+ return true;
+ }
+
+ DEFINE_INLINE_TRACE()
+ {
+ visitor->trace(m_map);
+ }
+ private:
+ // m_map needs to be kept alive while Javascript holds an iterator to it,
+ // so we make it a member.
+ const Member<ManufacturerDataMap> m_map;
+ size_t m_current;
+ };
+};
+
+} // namespace blink
+
+#endif // ManufacturerDataMap_h

Powered by Google App Engine
This is Rietveld 408576698