OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "extensions/renderer/api_type_reference_map.h" | 5 #include "extensions/renderer/api_type_reference_map.h" |
6 | 6 |
| 7 #include "extensions/renderer/api_signature.h" |
7 #include "extensions/renderer/argument_spec.h" | 8 #include "extensions/renderer/argument_spec.h" |
8 | 9 |
9 namespace extensions { | 10 namespace extensions { |
10 | 11 |
11 APITypeReferenceMap::APITypeReferenceMap( | 12 APITypeReferenceMap::APITypeReferenceMap( |
12 const InitializeTypeCallback& initialize_type) | 13 const InitializeTypeCallback& initialize_type) |
13 : initialize_type_(initialize_type) {} | 14 : initialize_type_(initialize_type) {} |
14 APITypeReferenceMap::~APITypeReferenceMap() = default; | 15 APITypeReferenceMap::~APITypeReferenceMap() = default; |
15 | 16 |
16 void APITypeReferenceMap::AddSpec(const std::string& name, | 17 void APITypeReferenceMap::AddSpec(const std::string& name, |
17 std::unique_ptr<ArgumentSpec> spec) { | 18 std::unique_ptr<ArgumentSpec> spec) { |
18 DCHECK(type_refs_.find(name) == type_refs_.end()); | 19 DCHECK(type_refs_.find(name) == type_refs_.end()); |
19 type_refs_[name] = std::move(spec); | 20 type_refs_[name] = std::move(spec); |
20 } | 21 } |
21 | 22 |
22 const ArgumentSpec* APITypeReferenceMap::GetSpec( | 23 const ArgumentSpec* APITypeReferenceMap::GetSpec( |
23 const std::string& name) const { | 24 const std::string& name) const { |
24 auto iter = type_refs_.find(name); | 25 auto iter = type_refs_.find(name); |
25 if (iter == type_refs_.end()) { | 26 if (iter == type_refs_.end()) { |
26 initialize_type_.Run(name); | 27 initialize_type_.Run(name); |
27 iter = type_refs_.find(name); | 28 iter = type_refs_.find(name); |
28 } | 29 } |
29 return iter == type_refs_.end() ? nullptr : iter->second.get(); | 30 return iter == type_refs_.end() ? nullptr : iter->second.get(); |
30 } | 31 } |
31 | 32 |
| 33 void APITypeReferenceMap::AddTypeMethodSignature( |
| 34 const std::string& name, |
| 35 std::unique_ptr<APISignature> signature) { |
| 36 DCHECK(type_methods_.find(name) == type_methods_.end()) |
| 37 << "Cannot re-register signature for: " << name; |
| 38 type_methods_[name] = std::move(signature); |
| 39 } |
| 40 |
| 41 const APISignature* APITypeReferenceMap::GetTypeMethodSignature( |
| 42 const std::string& name) const { |
| 43 auto iter = type_methods_.find(name); |
| 44 if (iter == type_methods_.end()) { |
| 45 // Find the type name by stripping away the method suffix. |
| 46 std::string::size_type dot = name.rfind('.'); |
| 47 DCHECK_NE(std::string::npos, dot); |
| 48 DCHECK_LT(dot, name.size() - 1); |
| 49 std::string type_name = name.substr(0, dot); |
| 50 initialize_type_.Run(type_name); |
| 51 iter = type_methods_.find(name); |
| 52 } |
| 53 return iter == type_methods_.end() ? nullptr : iter->second.get(); |
| 54 } |
| 55 |
32 } // namespace extensions | 56 } // namespace extensions |
OLD | NEW |