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|, where |type_name| is the fully-qualified type (e.g. |
| 82 // storage.StorageArea). |
| 83 void RegisterCustomType(const std::string& type_name, |
| 84 const CustomTypeHandler& function); |
| 85 |
75 private: | 86 private: |
76 // Creates a new APIBinding for the given |api_name|. | 87 // Creates a new APIBinding for the given |api_name|. |
77 std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name); | 88 std::unique_ptr<APIBinding> CreateNewAPIBinding(const std::string& api_name); |
78 | 89 |
79 // Callback for the APITypeReferenceMap in order to initialize an unknown | 90 // Callback for the APITypeReferenceMap in order to initialize an unknown |
80 // type. | 91 // type. |
81 void InitializeType(const std::string& name); | 92 void InitializeType(const std::string& name); |
82 | 93 |
| 94 // Handles creating the type for the specified property. |
| 95 v8::Local<v8::Object> CreateCustomType(v8::Local<v8::Context> context, |
| 96 const std::string& type_name, |
| 97 const std::string& property_name); |
| 98 |
83 // The map of cached API reference types. | 99 // The map of cached API reference types. |
84 APITypeReferenceMap type_reference_map_; | 100 APITypeReferenceMap type_reference_map_; |
85 | 101 |
86 // The request handler associated with the system. | 102 // The request handler associated with the system. |
87 APIRequestHandler request_handler_; | 103 APIRequestHandler request_handler_; |
88 | 104 |
89 // The event handler associated with the system. | 105 // The event handler associated with the system. |
90 APIEventHandler event_handler_; | 106 APIEventHandler event_handler_; |
91 | 107 |
92 // A map from api_name -> APIBinding for constructed APIs. APIBindings are | 108 // A map from api_name -> APIBinding for constructed APIs. APIBindings are |
93 // created lazily. | 109 // created lazily. |
94 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_; | 110 std::map<std::string, std::unique_ptr<APIBinding>> api_bindings_; |
95 | 111 |
96 // A map from api_name -> APIBindingHooks for registering custom hooks. | 112 // 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 | 113 // TODO(devlin): This map is pretty pointer-y. Is that going to be a |
98 // performance concern? | 114 // performance concern? |
99 std::map<std::string, std::unique_ptr<APIBindingHooks>> binding_hooks_; | 115 std::map<std::string, std::unique_ptr<APIBindingHooks>> binding_hooks_; |
100 | 116 |
| 117 std::map<std::string, CustomTypeHandler> custom_types_; |
| 118 |
101 binding::RunJSFunction call_js_; | 119 binding::RunJSFunction call_js_; |
102 | 120 |
103 binding::RunJSFunctionSync call_js_sync_; | 121 binding::RunJSFunctionSync call_js_sync_; |
104 | 122 |
105 // The method to retrieve the DictionaryValue describing a given extension | 123 // The method to retrieve the DictionaryValue describing a given extension |
106 // API. Curried in for testing purposes so we can use fake APIs. | 124 // API. Curried in for testing purposes so we can use fake APIs. |
107 GetAPISchemaMethod get_api_schema_; | 125 GetAPISchemaMethod get_api_schema_; |
108 | 126 |
109 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem); | 127 DISALLOW_COPY_AND_ASSIGN(APIBindingsSystem); |
110 }; | 128 }; |
111 | 129 |
112 } // namespace | 130 } // namespace |
113 | 131 |
114 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ | 132 #endif // EXTENSIONS_RENDERER_API_BINDINGS_SYSTEM_H_ |
OLD | NEW |