api_type_reference_map.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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
  3. // found in the LICENSE file.
  4. #include "extensions/renderer/bindings/api_type_reference_map.h"
  5. #include <ostream>
  6. #include "extensions/renderer/bindings/api_signature.h"
  7. #include "extensions/renderer/bindings/argument_spec.h"
  8. namespace extensions {
  9. APITypeReferenceMap::APITypeReferenceMap(InitializeTypeCallback initialize_type)
  10. : initialize_type_(std::move(initialize_type)) {}
  11. APITypeReferenceMap::~APITypeReferenceMap() = default;
  12. void APITypeReferenceMap::AddSpec(const std::string& name,
  13. std::unique_ptr<ArgumentSpec> spec) {
  14. DCHECK(type_refs_.find(name) == type_refs_.end());
  15. type_refs_[name] = std::move(spec);
  16. }
  17. const ArgumentSpec* APITypeReferenceMap::GetSpec(
  18. const std::string& name) const {
  19. auto iter = type_refs_.find(name);
  20. if (iter == type_refs_.end()) {
  21. initialize_type_.Run(name);
  22. iter = type_refs_.find(name);
  23. }
  24. return iter == type_refs_.end() ? nullptr : iter->second.get();
  25. }
  26. void APITypeReferenceMap::AddAPIMethodSignature(
  27. const std::string& name,
  28. std::unique_ptr<APISignature> signature) {
  29. DCHECK(api_methods_.find(name) == api_methods_.end())
  30. << "Cannot re-register signature for: " << name;
  31. api_methods_[name] = std::move(signature);
  32. }
  33. const APISignature* APITypeReferenceMap::GetAPIMethodSignature(
  34. const std::string& name) const {
  35. auto iter = api_methods_.find(name);
  36. if (iter == api_methods_.end()) {
  37. initialize_type_.Run(name);
  38. iter = api_methods_.find(name);
  39. }
  40. return iter == api_methods_.end() ? nullptr : iter->second.get();
  41. }
  42. void APITypeReferenceMap::AddTypeMethodSignature(
  43. const std::string& name,
  44. std::unique_ptr<APISignature> signature) {
  45. DCHECK(type_methods_.find(name) == type_methods_.end())
  46. << "Cannot re-register signature for: " << name;
  47. type_methods_[name] = std::move(signature);
  48. }
  49. const APISignature* APITypeReferenceMap::GetTypeMethodSignature(
  50. const std::string& name) const {
  51. auto iter = type_methods_.find(name);
  52. if (iter == type_methods_.end()) {
  53. // Find the type name by stripping away the method suffix.
  54. std::string::size_type dot = name.rfind('.');
  55. DCHECK_NE(std::string::npos, dot);
  56. DCHECK_LT(dot, name.size() - 1);
  57. std::string type_name = name.substr(0, dot);
  58. initialize_type_.Run(type_name);
  59. iter = type_methods_.find(name);
  60. }
  61. return iter == type_methods_.end() ? nullptr : iter->second.get();
  62. }
  63. bool APITypeReferenceMap::HasTypeMethodSignature(
  64. const std::string& name) const {
  65. return type_methods_.find(name) != type_methods_.end();
  66. }
  67. const APISignature* APITypeReferenceMap::GetAsyncResponseSignature(
  68. const std::string& name) const {
  69. auto iter = api_methods_.find(name);
  70. return iter == api_methods_.end() ? nullptr : iter->second.get();
  71. }
  72. void APITypeReferenceMap::AddCustomSignature(
  73. const std::string& name,
  74. std::unique_ptr<APISignature> signature) {
  75. DCHECK(custom_signatures_.find(name) == custom_signatures_.end())
  76. << "Cannot re-register signature for: " << name;
  77. custom_signatures_[name] = std::move(signature);
  78. }
  79. const APISignature* APITypeReferenceMap::GetCustomSignature(
  80. const std::string& name) const {
  81. auto iter = custom_signatures_.find(name);
  82. return iter != custom_signatures_.end() ? iter->second.get() : nullptr;
  83. }
  84. void APITypeReferenceMap::AddEventSignature(
  85. const std::string& event_name,
  86. std::unique_ptr<APISignature> signature) {
  87. DCHECK(event_signatures_.find(event_name) == event_signatures_.end())
  88. << "Cannot re-register signature for: " << event_name;
  89. event_signatures_[event_name] = std::move(signature);
  90. }
  91. const APISignature* APITypeReferenceMap::GetEventSignature(
  92. const std::string& event_name) const {
  93. auto iter = event_signatures_.find(event_name);
  94. return iter != event_signatures_.end() ? iter->second.get() : nullptr;
  95. }
  96. } // namespace extensions