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/keyboard/keyboard_layout_map.h"
6 
7 namespace blink {
8 
9 class KeyboardLayoutMapIterationSource final
10     : public PairIterable<String, String>::IterationSource {
11  public:
KeyboardLayoutMapIterationSource(const KeyboardLayoutMap & map)12   KeyboardLayoutMapIterationSource(const KeyboardLayoutMap& map)
13       : map_(map), iterator_(map_->Map().begin()) {}
14 
Next(ScriptState * script_state,String & map_key,String & map_value,ExceptionState &)15   bool Next(ScriptState* script_state,
16             String& map_key,
17             String& map_value,
18             ExceptionState&) override {
19     if (iterator_ == map_->Map().end())
20       return false;
21     map_key = iterator_->key;
22     map_value = iterator_->value;
23     ++iterator_;
24     return true;
25   }
26 
Trace(Visitor * visitor)27   void Trace(Visitor* visitor) override {
28     visitor->Trace(map_);
29     PairIterable<String, String>::IterationSource::Trace(visitor);
30   }
31 
32  private:
33   // Needs to be kept alive while we're iterating over it.
34   const Member<const KeyboardLayoutMap> map_;
35   HashMap<String, String>::const_iterator iterator_;
36 };
37 
KeyboardLayoutMap(const HashMap<String,String> & map)38 KeyboardLayoutMap::KeyboardLayoutMap(const HashMap<String, String>& map)
39     : layout_map_(map) {}
40 
41 PairIterable<String, String>::IterationSource*
StartIteration(ScriptState *,ExceptionState &)42 KeyboardLayoutMap::StartIteration(ScriptState*, ExceptionState&) {
43   return MakeGarbageCollected<KeyboardLayoutMapIterationSource>(*this);
44 }
45 
GetMapEntry(ScriptState *,const String & key,String & value,ExceptionState &)46 bool KeyboardLayoutMap::GetMapEntry(ScriptState*,
47                                     const String& key,
48                                     String& value,
49                                     ExceptionState&) {
50   auto it = layout_map_.find(key);
51   if (it == layout_map_.end())
52     return false;
53 
54   value = it->value;
55   return true;
56 }
57 
58 }  // namespace blink
59