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_BINDING_H_ | 5 #ifndef EXTENSIONS_RENDERER_API_BINDING_H_ |
6 #define EXTENSIONS_RENDERER_API_BINDING_H_ | 6 #define EXTENSIONS_RENDERER_API_BINDING_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
(...skipping 23 matching lines...) Expand all Loading... |
34 // A class that vends v8::Objects for extension APIs. These APIs have function | 34 // A class that vends v8::Objects for extension APIs. These APIs have function |
35 // interceptors for all exposed methods, which call back into the APIBinding. | 35 // interceptors for all exposed methods, which call back into the APIBinding. |
36 // The APIBinding then matches the calling arguments against an expected method | 36 // The APIBinding then matches the calling arguments against an expected method |
37 // signature, throwing an error if they don't match. | 37 // signature, throwing an error if they don't match. |
38 // There should only need to be a single APIBinding object for each API, and | 38 // There should only need to be a single APIBinding object for each API, and |
39 // each can vend multiple v8::Objects for different contexts. | 39 // each can vend multiple v8::Objects for different contexts. |
40 // This object is designed to be one-per-isolate, but used across separate | 40 // This object is designed to be one-per-isolate, but used across separate |
41 // contexts. | 41 // contexts. |
42 class APIBinding { | 42 class APIBinding { |
43 public: | 43 public: |
| 44 using CreateCustomType = |
| 45 base::Callback<v8::Local<v8::Object>(v8::Local<v8::Context> context, |
| 46 const std::string& type_name, |
| 47 const std::string& property_name)>; |
| 48 |
44 // The callback for determining if a given API method (specified by |name|) | 49 // The callback for determining if a given API method (specified by |name|) |
45 // is available. | 50 // is available. |
46 using AvailabilityCallback = base::Callback<bool(const std::string& name)>; | 51 using AvailabilityCallback = base::Callback<bool(const std::string& name)>; |
47 | 52 |
48 // The callback type for handling an API call. | 53 // The callback type for handling an API call. |
49 using HandlerCallback = base::Callback<void(gin::Arguments*)>; | 54 using HandlerCallback = base::Callback<void(gin::Arguments*)>; |
50 | 55 |
51 // The APITypeReferenceMap is required to outlive this object. | 56 // The APITypeReferenceMap is required to outlive this object. |
52 // |function_definitions|, |type_definitions| and |event_definitions| | 57 // |function_definitions|, |type_definitions| and |event_definitions| |
53 // may be null if the API does not specify any of that category. | 58 // may be null if the API does not specify any of that category. |
54 APIBinding(const std::string& name, | 59 APIBinding(const std::string& name, |
55 const base::ListValue* function_definitions, | 60 const base::ListValue* function_definitions, |
56 const base::ListValue* type_definitions, | 61 const base::ListValue* type_definitions, |
57 const base::ListValue* event_definitions, | 62 const base::ListValue* event_definitions, |
58 const base::DictionaryValue* property_definitions, | 63 const base::DictionaryValue* property_definitions, |
| 64 const CreateCustomType& create_custom_type, |
59 std::unique_ptr<APIBindingHooks> binding_hooks, | 65 std::unique_ptr<APIBindingHooks> binding_hooks, |
60 APITypeReferenceMap* type_refs, | 66 APITypeReferenceMap* type_refs, |
61 APIRequestHandler* request_handler, | 67 APIRequestHandler* request_handler, |
62 APIEventHandler* event_handler); | 68 APIEventHandler* event_handler); |
63 ~APIBinding(); | 69 ~APIBinding(); |
64 | 70 |
65 // Returns a new v8::Object for the API this APIBinding represents. | 71 // Returns a new v8::Object for the API this APIBinding represents. |
66 v8::Local<v8::Object> CreateInstance( | 72 v8::Local<v8::Object> CreateInstance( |
67 v8::Local<v8::Context> context, | 73 v8::Local<v8::Context> context, |
68 v8::Isolate* isolate, | 74 v8::Isolate* isolate, |
69 const AvailabilityCallback& is_available); | 75 const AvailabilityCallback& is_available); |
70 | 76 |
71 // Returns the JS interface to use when registering hooks with legacy custom | 77 // Returns the JS interface to use when registering hooks with legacy custom |
72 // bindings. | 78 // bindings. |
73 v8::Local<v8::Object> GetJSHookInterface(v8::Local<v8::Context> context); | 79 v8::Local<v8::Object> GetJSHookInterface(v8::Local<v8::Context> context); |
74 | 80 |
75 private: | 81 private: |
76 // Initializes the object_template_ for this API. Called lazily when the | 82 // Initializes the object_template_ for this API. Called lazily when the |
77 // first instance is created. | 83 // first instance is created. |
78 void InitializeTemplate(v8::Isolate* isolate); | 84 void InitializeTemplate(v8::Isolate* isolate); |
79 | 85 |
| 86 // Decorates |object_template| with the properties specified by |properties|. |
| 87 void DecorateTemplateWithProperties( |
| 88 v8::Isolate* isolate, |
| 89 v8::Local<v8::ObjectTemplate> object_template, |
| 90 const base::DictionaryValue& properties); |
| 91 |
80 // Handler for getting the v8::Object associated with an event on the API. | 92 // Handler for getting the v8::Object associated with an event on the API. |
81 static void GetEventObject(v8::Local<v8::Name>, | 93 static void GetEventObject(v8::Local<v8::Name>, |
82 const v8::PropertyCallbackInfo<v8::Value>& info); | 94 const v8::PropertyCallbackInfo<v8::Value>& info); |
83 | 95 |
| 96 // Handler for getting the v8::Object associated with a custom property on the |
| 97 // API. |
| 98 static void GetCustomPropertyObject( |
| 99 v8::Local<v8::Name> property, |
| 100 const v8::PropertyCallbackInfo<v8::Value>& info); |
| 101 |
84 // Handles a call an API method with the given |name| and matches the | 102 // Handles a call an API method with the given |name| and matches the |
85 // arguments against |signature|. | 103 // arguments against |signature|. |
86 void HandleCall(const std::string& name, | 104 void HandleCall(const std::string& name, |
87 const APISignature* signature, | 105 const APISignature* signature, |
88 gin::Arguments* args); | 106 gin::Arguments* args); |
89 | 107 |
90 // The root name of the API, e.g. "tabs" for chrome.tabs. | 108 // The root name of the API, e.g. "tabs" for chrome.tabs. |
91 std::string api_name_; | 109 std::string api_name_; |
92 | 110 |
93 // A map from method name to method data. | 111 // A map from method name to method data. |
94 struct MethodData; | 112 struct MethodData; |
95 std::map<std::string, std::unique_ptr<MethodData>> methods_; | 113 std::map<std::string, std::unique_ptr<MethodData>> methods_; |
96 | 114 |
97 // The events associated with this API. | 115 // The events associated with this API. |
98 struct EventData; | 116 struct EventData; |
99 std::vector<std::unique_ptr<EventData>> events_; | 117 std::vector<std::unique_ptr<EventData>> events_; |
100 | 118 |
| 119 // The custom properties on the API; these are rare. |
| 120 struct CustomPropertyData; |
| 121 std::vector<std::unique_ptr<CustomPropertyData>> custom_properties_; |
| 122 |
101 // The pair for enum entry is <original, js-ified>. JS enum entries use | 123 // The pair for enum entry is <original, js-ified>. JS enum entries use |
102 // SCREAMING_STYLE (whereas our API enums are just inconsistent). | 124 // SCREAMING_STYLE (whereas our API enums are just inconsistent). |
103 using EnumEntry = std::pair<std::string, std::string>; | 125 using EnumEntry = std::pair<std::string, std::string>; |
104 // A map of <name, values> for the enums on this API. | 126 // A map of <name, values> for the enums on this API. |
105 std::map<std::string, std::vector<EnumEntry>> enums_; | 127 std::map<std::string, std::vector<EnumEntry>> enums_; |
106 | 128 |
107 // The associated properties of the API, if any. | 129 // The associated properties of the API, if any. |
108 const base::DictionaryValue* property_definitions_; | 130 const base::DictionaryValue* property_definitions_; |
109 | 131 |
| 132 // The callback for constructing a custom type. |
| 133 CreateCustomType create_custom_type_; |
| 134 |
110 // The registered hooks for this API. | 135 // The registered hooks for this API. |
111 std::unique_ptr<APIBindingHooks> binding_hooks_; | 136 std::unique_ptr<APIBindingHooks> binding_hooks_; |
112 | 137 |
113 // The reference map for all known types; required to outlive this object. | 138 // The reference map for all known types; required to outlive this object. |
114 const APITypeReferenceMap* type_refs_; | 139 const APITypeReferenceMap* type_refs_; |
115 | 140 |
116 // The associated request handler, shared between this and other bindings. | 141 // The associated request handler, shared between this and other bindings. |
117 // Required to outlive this object. | 142 // Required to outlive this object. |
118 APIRequestHandler* request_handler_; | 143 APIRequestHandler* request_handler_; |
119 | 144 |
120 // The template for this API. Note: some methods may only be available in | 145 // The template for this API. Note: some methods may only be available in |
121 // certain contexts, but this template contains all methods. Those that are | 146 // certain contexts, but this template contains all methods. Those that are |
122 // unavailable are removed after object instantiation. | 147 // unavailable are removed after object instantiation. |
123 v8::Eternal<v8::ObjectTemplate> object_template_; | 148 v8::Eternal<v8::ObjectTemplate> object_template_; |
124 | 149 |
125 base::WeakPtrFactory<APIBinding> weak_factory_; | 150 base::WeakPtrFactory<APIBinding> weak_factory_; |
126 | 151 |
127 DISALLOW_COPY_AND_ASSIGN(APIBinding); | 152 DISALLOW_COPY_AND_ASSIGN(APIBinding); |
128 }; | 153 }; |
129 | 154 |
130 } // namespace extensions | 155 } // namespace extensions |
131 | 156 |
132 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ | 157 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ |
OLD | NEW |