1 // Copyright 2020 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/font_access/font_table_map.h"
6 
7 #include "third_party/blink/renderer/platform/bindings/script_state.h"
8 
9 namespace blink {
10 
Trace(Visitor * visitor) const11 void FontTableMap::Trace(Visitor* visitor) const {
12   visitor->Trace(table_map_);
13   ScriptWrappable::Trace(visitor);
14 }
15 
16 class FontTableMapIterationSource final
17     : public PairIterable<String, Member<Blob>>::IterationSource {
18  public:
FontTableMapIterationSource(const FontTableMap::MapType & map)19   explicit FontTableMapIterationSource(const FontTableMap::MapType& map) {
20     for (const auto& table_name : map.Keys()) {
21       table_names_.push_back(table_name);
22       table_data_.push_back(map.at(table_name));
23     }
24   }
25 
Next(ScriptState * script_state,String & map_key,Member<Blob> & map_value,ExceptionState &)26   bool Next(ScriptState* script_state,
27             String& map_key,
28             Member<Blob>& map_value,
29             ExceptionState&) override {
30     if (current_index_ == table_names_.size())
31       return false;
32 
33     map_key = table_names_[current_index_];
34     map_value = table_data_[current_index_];
35 
36     ++current_index_;
37     return true;
38   }
39 
Trace(Visitor * visitor) const40   void Trace(Visitor* visitor) const override {
41     visitor->Trace(table_data_);
42     PairIterable<String, Member<Blob>>::IterationSource::Trace(visitor);
43   }
44 
45  private:
46   Vector<String> table_names_;
47   HeapVector<Member<Blob>> table_data_;
48   unsigned current_index_;
49 };
50 
51 PairIterable<String, Member<Blob>>::IterationSource*
StartIteration(ScriptState *,ExceptionState &)52 FontTableMap::StartIteration(ScriptState*, ExceptionState&) {
53   return MakeGarbageCollected<FontTableMapIterationSource>(table_map_);
54 }
55 
GetMapEntry(ScriptState *,const String & key,Member<Blob> & value,ExceptionState &)56 bool FontTableMap::GetMapEntry(ScriptState*,
57                                const String& key,
58                                Member<Blob>& value,
59                                ExceptionState&) {
60   if (table_map_.Contains(key)) {
61     value = table_map_.at(key);
62     return true;
63   }
64 
65   return false;
66 }
67 
68 }  // namespace blink
69