api_bindings_system.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // Copyright 2016 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_BINDINGS_SYSTEM_H_
  5. #define EXTENSIONS_RENDERER_BINDINGS_API_BINDINGS_SYSTEM_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "extensions/common/mojom/event_dispatcher.mojom-forward.h"
  11. #include "extensions/renderer/bindings/api_binding.h"
  12. #include "extensions/renderer/bindings/api_binding_types.h"
  13. #include "extensions/renderer/bindings/api_event_handler.h"
  14. #include "extensions/renderer/bindings/api_last_error.h"
  15. #include "extensions/renderer/bindings/api_request_handler.h"
  16. #include "extensions/renderer/bindings/api_type_reference_map.h"
  17. #include "extensions/renderer/bindings/binding_access_checker.h"
  18. #include "extensions/renderer/bindings/exception_handler.h"
  19. namespace base {
  20. class DictionaryValue;
  21. class ListValue;
  22. }
  23. namespace extensions {
  24. class APIBindingHooks;
  25. class InteractionProvider;
  26. // A class encompassing the necessary pieces to construct the JS entry points
  27. // for Extension APIs. Designed to be used on a single thread, but safe between
  28. // multiple v8::Contexts.
  29. class APIBindingsSystem {
  30. public:
  31. using GetAPISchemaMethod =
  32. base::RepeatingCallback<const base::DictionaryValue&(const std::string&)>;
  33. using CustomTypeHandler = base::RepeatingCallback<v8::Local<v8::Object>(
  34. v8::Isolate* isolate,
  35. const std::string& property_name,
  36. const base::ListValue* property_values,
  37. APIRequestHandler* request_handler,
  38. APIEventHandler* event_handler,
  39. APITypeReferenceMap* type_refs,
  40. const BindingAccessChecker* access_checker)>;
  41. APIBindingsSystem(
  42. GetAPISchemaMethod get_api_schema,
  43. BindingAccessChecker::APIAvailabilityCallback api_available,
  44. BindingAccessChecker::PromiseAvailabilityCallback promises_available,
  45. APIRequestHandler::SendRequestMethod send_request,
  46. std::unique_ptr<InteractionProvider> interaction_provider,
  47. APIEventListeners::ListenersUpdated event_listeners_changed,
  48. APIEventHandler::ContextOwnerIdGetter context_owner_getter,
  49. APIBinding::OnSilentRequest on_silent_request,
  50. binding::AddConsoleError add_console_error,
  51. APILastError last_error);
  52. APIBindingsSystem(const APIBindingsSystem&) = delete;
  53. APIBindingsSystem& operator=(const APIBindingsSystem&) = delete;
  54. ~APIBindingsSystem();
  55. // Returns a new v8::Object representing the api specified by |api_name|.
  56. v8::Local<v8::Object> CreateAPIInstance(
  57. const std::string& api_name,
  58. v8::Local<v8::Context> context,
  59. APIBindingHooks** hooks_out);
  60. // Responds to the request with the given |request_id|, calling the callback
  61. // with |response|. If |error| is non-empty, sets the last error.
  62. void CompleteRequest(int request_id,
  63. const base::Value::List& response,
  64. const std::string& error);
  65. // Notifies the APIEventHandler to fire the corresponding event, notifying
  66. // listeners.
  67. void FireEventInContext(const std::string& event_name,
  68. v8::Local<v8::Context> context,
  69. const base::Value::List& response,
  70. mojom::EventFilteringInfoPtr filter);
  71. // Returns the APIBindingHooks object for the given api to allow for
  72. // registering custom hooks. These must be registered *before* the
  73. // binding is instantiated.
  74. // TODO(devlin): It's a little weird that we don't just expose a
  75. // RegisterHooks-type method. Depending on how complex the hook interface
  76. // is, maybe we should rethink this. Downside would be that it's less
  77. // efficient to register multiple hooks for the same API.
  78. APIBindingHooks* GetHooksForAPI(const std::string& api_name);
  79. // Registers the handler for creating a custom type with the given
  80. // |type_name|, where |type_name| is the fully-qualified type (e.g.
  81. // storage.StorageArea).
  82. void RegisterCustomType(const std::string& type_name,
  83. CustomTypeHandler function);
  84. // Handles any cleanup necessary before releasing the given |context|.
  85. void WillReleaseContext(v8::Local<v8::Context> context);
  86. InteractionProvider* interaction_provider() {
  87. return interaction_provider_.get();
  88. }
  89. APIRequestHandler* request_handler() { return &request_handler_; }
  90. APIEventHandler* event_handler() { return &event_handler_; }
  91. APITypeReferenceMap* type_reference_map() { return &type_reference_map_; }
  92. ExceptionHandler* exception_handler() { return &exception_handler_; }
  93. private:
  94. // Creates a new APIBinding for the given |api_name|.
  95. std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name);
  96. // Callback for the APITypeReferenceMap in order to initialize an unknown
  97. // type.
  98. void InitializeType(const std::string& name);
  99. // Handles creating the type for the specified property.
  100. v8::Local<v8::Object> CreateCustomType(
  101. v8::Isolate* isolate,
  102. const std::string& type_name,
  103. const std::string& property_name,
  104. const base::ListValue* property_values);
  105. // The map of cached API reference types.
  106. APITypeReferenceMap type_reference_map_;
  107. // The exception handler for the system.
  108. ExceptionHandler exception_handler_;
  109. // The interaction provider for the system.
  110. std::unique_ptr<InteractionProvider> interaction_provider_;
  111. // The request handler associated with the system.
  112. APIRequestHandler request_handler_;
  113. // The event handler associated with the system.
  114. APIEventHandler event_handler_;
  115. // The access checker associated with the system.
  116. BindingAccessChecker access_checker_;
  117. // A map from api_name -> APIBinding for constructed APIs. APIBindings are
  118. // created lazily.
  119. std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_;
  120. // A map from api_name -> APIBindingHooks for registering custom hooks.
  121. // TODO(devlin): This map is pretty pointer-y. Is that going to be a
  122. // performance concern?
  123. std::map<std::string, std::unique_ptr<APIBindingHooks>> binding_hooks_;
  124. std::map<std::string, CustomTypeHandler> custom_types_;
  125. // The method to retrieve the DictionaryValue describing a given extension
  126. // API. Curried in for testing purposes so we can use fake APIs.
  127. GetAPISchemaMethod get_api_schema_;
  128. // The method to call when the system silently handles an API request without
  129. // notifying the browser.
  130. APIBinding::OnSilentRequest on_silent_request_;
  131. };
  132. } // namespace
  133. #endif // EXTENSIONS_RENDERER_BINDINGS_API_BINDINGS_SYSTEM_H_