| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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 #ifndef EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ |
| 6 #define EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ | 6 #define EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "extensions/renderer/api_binding.h" | 14 #include "extensions/renderer/api_binding.h" |
| 15 #include "extensions/renderer/api_binding_types.h" | 15 #include "extensions/renderer/api_binding_types.h" |
| 16 #include "extensions/renderer/api_event_handler.h" | 16 #include "extensions/renderer/api_event_handler.h" |
| 17 #include "extensions/renderer/api_last_error.h" | 17 #include "extensions/renderer/api_last_error.h" |
| 18 #include "extensions/renderer/api_request_handler.h" | 18 #include "extensions/renderer/api_request_handler.h" |
| 19 #include "extensions/renderer/api_type_reference_map.h" | 19 #include "extensions/renderer/api_type_reference_map.h" |
| 20 #include "extensions/renderer/binding_access_checker.h" |
| 20 | 21 |
| 21 namespace base { | 22 namespace base { |
| 22 class DictionaryValue; | 23 class DictionaryValue; |
| 23 class ListValue; | 24 class ListValue; |
| 24 } | 25 } |
| 25 | 26 |
| 26 namespace extensions { | 27 namespace extensions { |
| 27 class APIBindingHooks; | 28 class APIBindingHooks; |
| 28 | 29 |
| 29 // A class encompassing the necessary pieces to construct the JS entry points | 30 // A class encompassing the necessary pieces to construct the JS entry points |
| 30 // for Extension APIs. Designed to be used on a single thread, but safe between | 31 // for Extension APIs. Designed to be used on a single thread, but safe between |
| 31 // multiple v8::Contexts. | 32 // multiple v8::Contexts. |
| 32 class APIBindingsSystem { | 33 class APIBindingsSystem { |
| 33 public: | 34 public: |
| 34 using GetAPISchemaMethod = | 35 using GetAPISchemaMethod = |
| 35 base::Callback<const base::DictionaryValue&(const std::string&)>; | 36 base::Callback<const base::DictionaryValue&(const std::string&)>; |
| 36 using CustomTypeHandler = base::Callback<v8::Local<v8::Object>( | 37 using CustomTypeHandler = base::Callback<v8::Local<v8::Object>( |
| 37 v8::Isolate* isolate, | 38 v8::Isolate* isolate, |
| 38 const std::string& property_name, | 39 const std::string& property_name, |
| 39 const base::ListValue* property_values, | 40 const base::ListValue* property_values, |
| 40 APIRequestHandler* request_handler, | 41 APIRequestHandler* request_handler, |
| 41 APIEventHandler* event_handler, | 42 APIEventHandler* event_handler, |
| 42 APITypeReferenceMap* type_refs)>; | 43 APITypeReferenceMap* type_refs)>; |
| 43 | 44 |
| 44 APIBindingsSystem(const binding::RunJSFunction& call_js, | 45 APIBindingsSystem( |
| 45 const binding::RunJSFunctionSync& call_js_sync, | 46 const binding::RunJSFunction& call_js, |
| 46 const GetAPISchemaMethod& get_api_schema, | 47 const binding::RunJSFunctionSync& call_js_sync, |
| 47 const APIBinding::AvailabilityCallback& is_available, | 48 const GetAPISchemaMethod& get_api_schema, |
| 48 const APIRequestHandler::SendRequestMethod& send_request, | 49 const BindingAccessChecker::AvailabilityCallback& is_available, |
| 49 const APIEventHandler::EventListenersChangedMethod& | 50 const APIRequestHandler::SendRequestMethod& send_request, |
| 50 event_listeners_changed, | 51 const APIEventHandler::EventListenersChangedMethod& |
| 51 APILastError last_error); | 52 event_listeners_changed, |
| 53 APILastError last_error); |
| 52 ~APIBindingsSystem(); | 54 ~APIBindingsSystem(); |
| 53 | 55 |
| 54 // Returns a new v8::Object representing the api specified by |api_name|. | 56 // Returns a new v8::Object representing the api specified by |api_name|. |
| 55 v8::Local<v8::Object> CreateAPIInstance( | 57 v8::Local<v8::Object> CreateAPIInstance( |
| 56 const std::string& api_name, | 58 const std::string& api_name, |
| 57 v8::Local<v8::Context> context, | 59 v8::Local<v8::Context> context, |
| 58 APIBindingHooks** hooks_out); | 60 APIBindingHooks** hooks_out); |
| 59 | 61 |
| 60 // Responds to the request with the given |request_id|, calling the callback | 62 // Responds to the request with the given |request_id|, calling the callback |
| 61 // with |response|. If |error| is non-empty, sets the last error. | 63 // with |response|. If |error| is non-empty, sets the last error. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 111 |
| 110 // The map of cached API reference types. | 112 // The map of cached API reference types. |
| 111 APITypeReferenceMap type_reference_map_; | 113 APITypeReferenceMap type_reference_map_; |
| 112 | 114 |
| 113 // The request handler associated with the system. | 115 // The request handler associated with the system. |
| 114 APIRequestHandler request_handler_; | 116 APIRequestHandler request_handler_; |
| 115 | 117 |
| 116 // The event handler associated with the system. | 118 // The event handler associated with the system. |
| 117 APIEventHandler event_handler_; | 119 APIEventHandler event_handler_; |
| 118 | 120 |
| 121 // The access checker associated with the system. |
| 122 BindingAccessChecker access_checker_; |
| 123 |
| 119 // A map from api_name -> APIBinding for constructed APIs. APIBindings are | 124 // A map from api_name -> APIBinding for constructed APIs. APIBindings are |
| 120 // created lazily. | 125 // created lazily. |
| 121 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_; | 126 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_; |
| 122 | 127 |
| 123 // A map from api_name -> APIBindingHooks for registering custom hooks. | 128 // A map from api_name -> APIBindingHooks for registering custom hooks. |
| 124 // TODO(devlin): This map is pretty pointer-y. Is that going to be a | 129 // TODO(devlin): This map is pretty pointer-y. Is that going to be a |
| 125 // performance concern? | 130 // performance concern? |
| 126 std::map<std::string, std::unique_ptr<APIBindingHooks>> binding_hooks_; | 131 std::map<std::string, std::unique_ptr<APIBindingHooks>> binding_hooks_; |
| 127 | 132 |
| 128 std::map<std::string, CustomTypeHandler> custom_types_; | 133 std::map<std::string, CustomTypeHandler> custom_types_; |
| 129 | 134 |
| 130 binding::RunJSFunction call_js_; | 135 binding::RunJSFunction call_js_; |
| 131 | 136 |
| 132 binding::RunJSFunctionSync call_js_sync_; | 137 binding::RunJSFunctionSync call_js_sync_; |
| 133 | 138 |
| 134 // The method to retrieve the DictionaryValue describing a given extension | 139 // The method to retrieve the DictionaryValue describing a given extension |
| 135 // API. Curried in for testing purposes so we can use fake APIs. | 140 // API. Curried in for testing purposes so we can use fake APIs. |
| 136 GetAPISchemaMethod get_api_schema_; | 141 GetAPISchemaMethod get_api_schema_; |
| 137 | 142 |
| 138 APIBinding::AvailabilityCallback is_available_; | |
| 139 | |
| 140 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem); | 143 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem); |
| 141 }; | 144 }; |
| 142 | 145 |
| 143 } // namespace | 146 } // namespace |
| 144 | 147 |
| 145 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ | 148 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ |
| OLD | NEW |