api_type_reference_map.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef EXTENSIONS_RENDERER_BINDINGS_API_TYPE_REFERENCE_MAP_H_
  5. #define EXTENSIONS_RENDERER_BINDINGS_API_TYPE_REFERENCE_MAP_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. namespace extensions {
  11. class APISignature;
  12. class ArgumentSpec;
  13. // A map storing type specifications and method signatures for API definitions.
  14. class APITypeReferenceMap {
  15. public:
  16. // A callback used to initialize an unknown type, so that these can be
  17. // created lazily.
  18. using InitializeTypeCallback =
  19. base::RepeatingCallback<void(const std::string& name)>;
  20. explicit APITypeReferenceMap(InitializeTypeCallback initialize_type);
  21. APITypeReferenceMap(const APITypeReferenceMap&) = delete;
  22. APITypeReferenceMap& operator=(const APITypeReferenceMap&) = delete;
  23. ~APITypeReferenceMap();
  24. // Adds the |spec| to the map under the given |name|.
  25. void AddSpec(const std::string& name, std::unique_ptr<ArgumentSpec> spec);
  26. // Returns the spec for the given |name|.
  27. const ArgumentSpec* GetSpec(const std::string& name) const;
  28. // Adds the |signature| to the map under the given |name|. |name| is expected
  29. // to be fully qualified with the API and method name, e.g. tabs.create.
  30. void AddAPIMethodSignature(const std::string& name,
  31. std::unique_ptr<APISignature> signature);
  32. // Returns the signature for the given |name|. |name| is expected
  33. // to be fully qualified with the API and method name, e.g. tabs.create.
  34. const APISignature* GetAPIMethodSignature(const std::string& name) const;
  35. // Adds the |signature| to the map under the given |name|. |name| is expected
  36. // to be fully qualified with API, type, and method (e.g.
  37. // storage.StorageArea.get).
  38. void AddTypeMethodSignature(const std::string& name,
  39. std::unique_ptr<APISignature> signature);
  40. // Returns the signature for the given |name|. |name| is expected
  41. // to be fully qualified with API, type, and method (e.g.
  42. // storage.StorageArea.get).
  43. const APISignature* GetTypeMethodSignature(const std::string& name) const;
  44. // Returns true if the map has a signature for the given |name|. Unlike
  45. // GetTypeMethodSignature(), this will not try to fetch the type by loading
  46. // an API.
  47. bool HasTypeMethodSignature(const std::string& name) const;
  48. // Adds a custom signature for bindings to use.
  49. void AddCustomSignature(const std::string& name,
  50. std::unique_ptr<APISignature> signature);
  51. // Looks up a custom signature that was previously added.
  52. const APISignature* GetCustomSignature(const std::string& name) const;
  53. // Adds a signature for an API event with the given `event_name`.
  54. void AddEventSignature(const std::string& event_name,
  55. std::unique_ptr<APISignature> signature);
  56. // Retrieves a signature for an API event with the given `event_name`.
  57. const APISignature* GetEventSignature(const std::string& event_name) const;
  58. // Returns the associated APISignature for the given |name|. Logic differs
  59. // slightly from a normal GetAPIMethodSignature as we don't want to initialize
  60. // a new type if the signature is not found.
  61. const APISignature* GetAsyncResponseSignature(const std::string& name) const;
  62. bool empty() const { return type_refs_.empty(); }
  63. size_t size() const { return type_refs_.size(); }
  64. private:
  65. InitializeTypeCallback initialize_type_;
  66. std::map<std::string, std::unique_ptr<ArgumentSpec>> type_refs_;
  67. using SignatureMap = std::map<std::string, std::unique_ptr<APISignature>>;
  68. SignatureMap api_methods_;
  69. SignatureMap type_methods_;
  70. SignatureMap custom_signatures_;
  71. SignatureMap event_signatures_;
  72. };
  73. } // namespace extensions
  74. #endif // EXTENSIONS_RENDERER_BINDINGS_API_TYPE_REFERENCE_MAP_H_