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 29 matching lines...) Expand all Loading... |
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 = base::Callback<v8::Local<v8::Object>( | 44 using CreateCustomType = base::Callback<v8::Local<v8::Object>( |
45 v8::Isolate* isolate, | 45 v8::Isolate* isolate, |
46 const std::string& type_name, | 46 const std::string& type_name, |
47 const std::string& property_name, | 47 const std::string& property_name, |
48 const base::ListValue* property_values)>; | 48 const base::ListValue* property_values)>; |
49 | 49 |
50 // The callback for determining if a given API method (specified by |name|) | 50 // The callback for determining if a given API feature (specified by |name|) |
51 // is available. | 51 // is available in the given context. |
52 using AvailabilityCallback = base::Callback<bool(const std::string& name)>; | 52 using AvailabilityCallback = |
| 53 base::Callback<bool(v8::Local<v8::Context>, const std::string& name)>; |
53 | 54 |
54 // The callback type for handling an API call. | 55 // The callback type for handling an API call. |
55 using HandlerCallback = base::Callback<void(gin::Arguments*)>; | 56 using HandlerCallback = base::Callback<void(gin::Arguments*)>; |
56 | 57 |
57 // The APITypeReferenceMap is required to outlive this object. | 58 // The APITypeReferenceMap is required to outlive this object. |
58 // |function_definitions|, |type_definitions| and |event_definitions| | 59 // |function_definitions|, |type_definitions| and |event_definitions| |
59 // may be null if the API does not specify any of that category. | 60 // may be null if the API does not specify any of that category. |
60 APIBinding(const std::string& name, | 61 APIBinding(const std::string& name, |
61 const base::ListValue* function_definitions, | 62 const base::ListValue* function_definitions, |
62 const base::ListValue* type_definitions, | 63 const base::ListValue* type_definitions, |
63 const base::ListValue* event_definitions, | 64 const base::ListValue* event_definitions, |
64 const base::DictionaryValue* property_definitions, | 65 const base::DictionaryValue* property_definitions, |
65 const CreateCustomType& create_custom_type, | 66 const CreateCustomType& create_custom_type, |
| 67 const AvailabilityCallback& is_available, |
66 std::unique_ptr<APIBindingHooks> binding_hooks, | 68 std::unique_ptr<APIBindingHooks> binding_hooks, |
67 APITypeReferenceMap* type_refs, | 69 APITypeReferenceMap* type_refs, |
68 APIRequestHandler* request_handler, | 70 APIRequestHandler* request_handler, |
69 APIEventHandler* event_handler); | 71 APIEventHandler* event_handler); |
70 ~APIBinding(); | 72 ~APIBinding(); |
71 | 73 |
72 // Returns a new v8::Object for the API this APIBinding represents. | 74 // Returns a new v8::Object for the API this APIBinding represents. |
73 v8::Local<v8::Object> CreateInstance( | 75 v8::Local<v8::Object> CreateInstance(v8::Local<v8::Context> context); |
74 v8::Local<v8::Context> context, | |
75 const AvailabilityCallback& is_available); | |
76 | 76 |
77 APIBindingHooks* hooks() { return binding_hooks_.get(); } | 77 APIBindingHooks* hooks() { return binding_hooks_.get(); } |
78 | 78 |
79 private: | 79 private: |
80 // Initializes the object_template_ for this API. Called lazily when the | 80 // Initializes the object_template_ for this API. Called lazily when the |
81 // first instance is created. | 81 // first instance is created. |
82 void InitializeTemplate(v8::Isolate* isolate); | 82 void InitializeTemplate(v8::Isolate* isolate); |
83 | 83 |
84 // Decorates |object_template| with the properties specified by |properties|. | 84 // Decorates |object_template| with the properties specified by |properties|. |
85 void DecorateTemplateWithProperties( | 85 void DecorateTemplateWithProperties( |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
123 using EnumEntry = std::pair<std::string, std::string>; | 123 using EnumEntry = std::pair<std::string, std::string>; |
124 // A map of <name, values> for the enums on this API. | 124 // A map of <name, values> for the enums on this API. |
125 std::map<std::string, std::vector<EnumEntry>> enums_; | 125 std::map<std::string, std::vector<EnumEntry>> enums_; |
126 | 126 |
127 // The associated properties of the API, if any. | 127 // The associated properties of the API, if any. |
128 const base::DictionaryValue* property_definitions_; | 128 const base::DictionaryValue* property_definitions_; |
129 | 129 |
130 // The callback for constructing a custom type. | 130 // The callback for constructing a custom type. |
131 CreateCustomType create_custom_type_; | 131 CreateCustomType create_custom_type_; |
132 | 132 |
| 133 // The callback for checking availability of an API feature. |
| 134 AvailabilityCallback is_available_; |
| 135 |
133 // The registered hooks for this API. | 136 // The registered hooks for this API. |
134 std::unique_ptr<APIBindingHooks> binding_hooks_; | 137 std::unique_ptr<APIBindingHooks> binding_hooks_; |
135 | 138 |
136 // The reference map for all known types; required to outlive this object. | 139 // The reference map for all known types; required to outlive this object. |
137 APITypeReferenceMap* type_refs_; | 140 APITypeReferenceMap* type_refs_; |
138 | 141 |
139 // The associated request handler, shared between this and other bindings. | 142 // The associated request handler, shared between this and other bindings. |
140 // Required to outlive this object. | 143 // Required to outlive this object. |
141 APIRequestHandler* request_handler_; | 144 APIRequestHandler* request_handler_; |
142 | 145 |
143 // The associated event handler, shared between this and other bindings. | 146 // The associated event handler, shared between this and other bindings. |
144 // Required to outlive this object. | 147 // Required to outlive this object. |
145 APIEventHandler* event_handler_; | 148 APIEventHandler* event_handler_; |
146 | 149 |
147 // The template for this API. Note: some methods may only be available in | 150 // The template for this API. Note: some methods may only be available in |
148 // certain contexts, but this template contains all methods. Those that are | 151 // certain contexts, but this template contains all methods. Those that are |
149 // unavailable are removed after object instantiation. | 152 // unavailable are removed after object instantiation. |
150 v8::Eternal<v8::ObjectTemplate> object_template_; | 153 v8::Eternal<v8::ObjectTemplate> object_template_; |
151 | 154 |
152 base::WeakPtrFactory<APIBinding> weak_factory_; | 155 base::WeakPtrFactory<APIBinding> weak_factory_; |
153 | 156 |
154 DISALLOW_COPY_AND_ASSIGN(APIBinding); | 157 DISALLOW_COPY_AND_ASSIGN(APIBinding); |
155 }; | 158 }; |
156 | 159 |
157 } // namespace extensions | 160 } // namespace extensions |
158 | 161 |
159 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ | 162 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ |
OLD | NEW |