| 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> |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 namespace extensions { | 26 namespace extensions { |
| 27 class APIRequestHandler; | 27 class APIRequestHandler; |
| 28 | 28 |
| 29 // A class encompassing the necessary pieces to construct the JS entry points | 29 // 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 | 30 // for Extension APIs. Designed to be used on a single thread, but safe between |
| 31 // multiple v8::Contexts. | 31 // multiple v8::Contexts. |
| 32 class APIBindingsSystem { | 32 class APIBindingsSystem { |
| 33 public: | 33 public: |
| 34 using GetAPISchemaMethod = | 34 using GetAPISchemaMethod = |
| 35 base::Callback<const base::DictionaryValue&(const std::string&)>; | 35 base::Callback<const base::DictionaryValue&(const std::string&)>; |
| 36 using CustomTypeHandler = |
| 37 base::Callback<v8::Local<v8::Object>(v8::Local<v8::Context> context, |
| 38 const std::string& property_name, |
| 39 APIRequestHandler* request_handler, |
| 40 APITypeReferenceMap* type_refs)>; |
| 36 | 41 |
| 37 APIBindingsSystem(const binding::RunJSFunction& call_js, | 42 APIBindingsSystem(const binding::RunJSFunction& call_js, |
| 38 const binding::RunJSFunctionSync& call_js_sync, | 43 const binding::RunJSFunctionSync& call_js_sync, |
| 39 const GetAPISchemaMethod& get_api_schema, | 44 const GetAPISchemaMethod& get_api_schema, |
| 40 const APIRequestHandler::SendRequestMethod& send_request, | 45 const APIRequestHandler::SendRequestMethod& send_request, |
| 41 const APIEventHandler::EventListenersChangedMethod& | 46 const APIEventHandler::EventListenersChangedMethod& |
| 42 event_listeners_changed, | 47 event_listeners_changed, |
| 43 APILastError last_error); | 48 APILastError last_error); |
| 44 ~APIBindingsSystem(); | 49 ~APIBindingsSystem(); |
| 45 | 50 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 65 | 70 |
| 66 // Returns the APIBindingHooks object for the given api to allow for | 71 // Returns the APIBindingHooks object for the given api to allow for |
| 67 // registering custom hooks. These must be registered *before* the | 72 // registering custom hooks. These must be registered *before* the |
| 68 // binding is instantiated. | 73 // binding is instantiated. |
| 69 // TODO(devlin): It's a little weird that we don't just expose a | 74 // TODO(devlin): It's a little weird that we don't just expose a |
| 70 // RegisterHooks-type method. Depending on how complex the hook interface | 75 // RegisterHooks-type method. Depending on how complex the hook interface |
| 71 // is, maybe we should rethink this. Downside would be that it's less | 76 // is, maybe we should rethink this. Downside would be that it's less |
| 72 // efficient to register multiple hooks for the same API. | 77 // efficient to register multiple hooks for the same API. |
| 73 APIBindingHooks* GetHooksForAPI(const std::string& api_name); | 78 APIBindingHooks* GetHooksForAPI(const std::string& api_name); |
| 74 | 79 |
| 80 // Registers the handler for creating a custom type with the given |
| 81 // |type_name|. |
| 82 void RegisterCustomType(const std::string& type_name, |
| 83 const CustomTypeHandler& function); |
| 84 |
| 75 private: | 85 private: |
| 76 // Creates a new APIBinding for the given |api_name|. | 86 // Creates a new APIBinding for the given |api_name|. |
| 77 std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name); | 87 std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name); |
| 78 | 88 |
| 79 // Callback for the APITypeReferenceMap in order to initialize an unknown | 89 // Callback for the APITypeReferenceMap in order to initialize an unknown |
| 80 // type. | 90 // type. |
| 81 void InitializeType(const std::string& name); | 91 void InitializeType(const std::string& name); |
| 82 | 92 |
| 93 // Handles creating the type for the specified property. |
| 94 v8::Local<v8::Object> CreateCustomType(v8::Local<v8::Context> context, |
| 95 const std::string& type_name, |
| 96 const std::string& property_name); |
| 97 |
| 83 // The map of cached API reference types. | 98 // The map of cached API reference types. |
| 84 APITypeReferenceMap type_reference_map_; | 99 APITypeReferenceMap type_reference_map_; |
| 85 | 100 |
| 86 // The request handler associated with the system. | 101 // The request handler associated with the system. |
| 87 APIRequestHandler request_handler_; | 102 APIRequestHandler request_handler_; |
| 88 | 103 |
| 89 // The event handler associated with the system. | 104 // The event handler associated with the system. |
| 90 APIEventHandler event_handler_; | 105 APIEventHandler event_handler_; |
| 91 | 106 |
| 92 // A map from api_name -> APIBinding for constructed APIs. APIBindings are | 107 // A map from api_name -> APIBinding for constructed APIs. APIBindings are |
| 93 // created lazily. | 108 // created lazily. |
| 94 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_; | 109 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_; |
| 95 | 110 |
| 96 // A map from api_name -> APIBindingHooks for registering custom hooks. | 111 // A map from api_name -> APIBindingHooks for registering custom hooks. |
| 97 // TODO(devlin): This map is pretty pointer-y. Is that going to be a | 112 // TODO(devlin): This map is pretty pointer-y. Is that going to be a |
| 98 // performance concern? | 113 // performance concern? |
| 99 std::map<std::string, std::unique_ptr<APIBindingHooks>> binding_hooks_; | 114 std::map<std::string, std::unique_ptr<APIBindingHooks>> binding_hooks_; |
| 100 | 115 |
| 116 std::map<std::string, CustomTypeHandler> custom_types_; |
| 117 |
| 101 binding::RunJSFunction call_js_; | 118 binding::RunJSFunction call_js_; |
| 102 | 119 |
| 103 binding::RunJSFunctionSync call_js_sync_; | 120 binding::RunJSFunctionSync call_js_sync_; |
| 104 | 121 |
| 105 // The method to retrieve the DictionaryValue describing a given extension | 122 // The method to retrieve the DictionaryValue describing a given extension |
| 106 // API. Curried in for testing purposes so we can use fake APIs. | 123 // API. Curried in for testing purposes so we can use fake APIs. |
| 107 GetAPISchemaMethod get_api_schema_; | 124 GetAPISchemaMethod get_api_schema_; |
| 108 | 125 |
| 109 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem); | 126 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem); |
| 110 }; | 127 }; |
| 111 | 128 |
| 112 } // namespace | 129 } // namespace |
| 113 | 130 |
| 114 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ | 131 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ |
| OLD | NEW |