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 MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_HASH_MAP_H_ 6 #define MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_HASH_MAP_H_ 7 8 #include "base/logging.h" 9 #include "mojo/public/cpp/bindings/map_traits.h" 10 #include "third_party/WebKit/Source/wtf/HashMap.h" 11 12 namespace mojo { 13 14 template <typename K, typename V> 15 struct MapTraits<WTF::HashMap<K, V>> { 16 using Key = K; 17 using Value = V; 18 using Iterator = typename WTF::HashMap<K, V>::iterator; 19 using ConstIterator = typename WTF::HashMap<K, V>::const_iterator; 20 21 static bool IsNull(const WTF::HashMap<K, V>& input) { 22 // WTF::HashMap<> is always converted to non-null mojom map. 23 return false; 24 } 25 26 static void SetToNull(WTF::HashMap<K, V>* output) { 27 // WTF::HashMap<> doesn't support null state. Set it to empty instead. 28 output->clear(); 29 } 30 31 static size_t GetSize(const WTF::HashMap<K, V>& input) { 32 return input.size(); 33 } 34 35 static ConstIterator GetBegin(const WTF::HashMap<K, V>& input) { 36 return input.begin(); 37 } 38 static Iterator GetBegin(WTF::HashMap<K, V>& input) { return input.begin(); } 39 40 static void AdvanceIterator(ConstIterator& iterator) { ++iterator; } 41 static void AdvanceIterator(Iterator& iterator) { ++iterator; } 42 43 static const K& GetKey(Iterator& iterator) { return iterator->key; } 44 static const K& GetKey(ConstIterator& iterator) { return iterator->key; } 45 46 static V& GetValue(Iterator& iterator) { return iterator->value; } 47 static const V& GetValue(ConstIterator& iterator) { return iterator->value; } 48 49 static bool Insert(WTF::HashMap<K, V>& input, const K& key, V&& value) { 50 if (!WTF::HashMap<K, V>::isValidKey(key)) { 51 LOG(ERROR) << "The key value is disallowed by WTF::HashMap: " << key; 52 return false; 53 } 54 input.add(key, std::forward<V>(value)); 55 return true; 56 } 57 static bool Insert(WTF::HashMap<K, V>& input, const K& key, const V& value) { 58 if (!WTF::HashMap<K, V>::isValidKey(key)) { 59 LOG(ERROR) << "The key value is disallowed by WTF::HashMap: " << key; 60 return false; 61 } 62 input.add(key, value); 63 return true; 64 } 65 66 static void SetToEmpty(WTF::HashMap<K, V>* output) { output->clear(); } 67 }; 68 69 } // namespace mojo 70 71 #endif // MOJO_PUBLIC_CPP_BINDINGS_MAP_TRAITS_WTF_HASH_MAP_H_ 72