| 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 12 matching lines...) Expand all Loading... |
| 23 namespace gin { | 23 namespace gin { |
| 24 class Arguments; | 24 class Arguments; |
| 25 } | 25 } |
| 26 | 26 |
| 27 namespace extensions { | 27 namespace extensions { |
| 28 class APIBindingHooks; | 28 class APIBindingHooks; |
| 29 class APIEventHandler; | 29 class APIEventHandler; |
| 30 class APIRequestHandler; | 30 class APIRequestHandler; |
| 31 class APISignature; | 31 class APISignature; |
| 32 class APITypeReferenceMap; | 32 class APITypeReferenceMap; |
| 33 class BindingAccessChecker; |
| 33 | 34 |
| 34 namespace binding { | 35 namespace binding { |
| 35 enum class RequestThread; | 36 enum class RequestThread; |
| 36 } | 37 } |
| 37 | 38 |
| 38 // A class that vends v8::Objects for extension APIs. These APIs have function | 39 // A class that vends v8::Objects for extension APIs. These APIs have function |
| 39 // interceptors for all exposed methods, which call back into the APIBinding. | 40 // interceptors for all exposed methods, which call back into the APIBinding. |
| 40 // The APIBinding then matches the calling arguments against an expected method | 41 // The APIBinding then matches the calling arguments against an expected method |
| 41 // signature, throwing an error if they don't match. | 42 // signature, throwing an error if they don't match. |
| 42 // There should only need to be a single APIBinding object for each API, and | 43 // There should only need to be a single APIBinding object for each API, and |
| 43 // each can vend multiple v8::Objects for different contexts. | 44 // each can vend multiple v8::Objects for different contexts. |
| 44 // This object is designed to be one-per-isolate, but used across separate | 45 // This object is designed to be one-per-isolate, but used across separate |
| 45 // contexts. | 46 // contexts. |
| 46 class APIBinding { | 47 class APIBinding { |
| 47 public: | 48 public: |
| 48 using CreateCustomType = base::Callback<v8::Local<v8::Object>( | 49 using CreateCustomType = base::Callback<v8::Local<v8::Object>( |
| 49 v8::Isolate* isolate, | 50 v8::Isolate* isolate, |
| 50 const std::string& type_name, | 51 const std::string& type_name, |
| 51 const std::string& property_name, | 52 const std::string& property_name, |
| 52 const base::ListValue* property_values)>; | 53 const base::ListValue* property_values)>; |
| 53 | 54 |
| 54 // The callback for determining if a given API feature (specified by |name|) | |
| 55 // is available in the given context. | |
| 56 using AvailabilityCallback = | |
| 57 base::Callback<bool(v8::Local<v8::Context>, const std::string& name)>; | |
| 58 | |
| 59 // The callback type for handling an API call. | 55 // The callback type for handling an API call. |
| 60 using HandlerCallback = base::Callback<void(gin::Arguments*)>; | 56 using HandlerCallback = base::Callback<void(gin::Arguments*)>; |
| 61 | 57 |
| 62 // The APITypeReferenceMap is required to outlive this object. | 58 // The APITypeReferenceMap is required to outlive this object. |
| 63 // |function_definitions|, |type_definitions| and |event_definitions| | 59 // |function_definitions|, |type_definitions| and |event_definitions| |
| 64 // 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. |
| 65 APIBinding(const std::string& name, | 61 APIBinding(const std::string& name, |
| 66 const base::ListValue* function_definitions, | 62 const base::ListValue* function_definitions, |
| 67 const base::ListValue* type_definitions, | 63 const base::ListValue* type_definitions, |
| 68 const base::ListValue* event_definitions, | 64 const base::ListValue* event_definitions, |
| 69 const base::DictionaryValue* property_definitions, | 65 const base::DictionaryValue* property_definitions, |
| 70 const CreateCustomType& create_custom_type, | 66 const CreateCustomType& create_custom_type, |
| 71 const AvailabilityCallback& is_available, | |
| 72 std::unique_ptr<APIBindingHooks> binding_hooks, | 67 std::unique_ptr<APIBindingHooks> binding_hooks, |
| 73 APITypeReferenceMap* type_refs, | 68 APITypeReferenceMap* type_refs, |
| 74 APIRequestHandler* request_handler, | 69 APIRequestHandler* request_handler, |
| 75 APIEventHandler* event_handler); | 70 APIEventHandler* event_handler, |
| 71 BindingAccessChecker* access_checker); |
| 76 ~APIBinding(); | 72 ~APIBinding(); |
| 77 | 73 |
| 78 // Returns a new v8::Object for the API this APIBinding represents. | 74 // Returns a new v8::Object for the API this APIBinding represents. |
| 79 v8::Local<v8::Object> CreateInstance(v8::Local<v8::Context> context); | 75 v8::Local<v8::Object> CreateInstance(v8::Local<v8::Context> context); |
| 80 | 76 |
| 81 APIBindingHooks* hooks() { return binding_hooks_.get(); } | 77 APIBindingHooks* hooks() { return binding_hooks_.get(); } |
| 82 | 78 |
| 83 private: | 79 private: |
| 84 // Initializes the object_template_ for this API. Called lazily when the | 80 // Initializes the object_template_ for this API. Called lazily when the |
| 85 // first instance is created. | 81 // first instance is created. |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 using EnumEntry = std::pair<std::string, std::string>; | 124 using EnumEntry = std::pair<std::string, std::string>; |
| 129 // A map of <name, values> for the enums on this API. | 125 // A map of <name, values> for the enums on this API. |
| 130 std::map<std::string, std::vector<EnumEntry>> enums_; | 126 std::map<std::string, std::vector<EnumEntry>> enums_; |
| 131 | 127 |
| 132 // The associated properties of the API, if any. | 128 // The associated properties of the API, if any. |
| 133 const base::DictionaryValue* property_definitions_; | 129 const base::DictionaryValue* property_definitions_; |
| 134 | 130 |
| 135 // The callback for constructing a custom type. | 131 // The callback for constructing a custom type. |
| 136 CreateCustomType create_custom_type_; | 132 CreateCustomType create_custom_type_; |
| 137 | 133 |
| 138 // The callback for checking availability of an API feature. | |
| 139 AvailabilityCallback is_available_; | |
| 140 | |
| 141 // The registered hooks for this API. | 134 // The registered hooks for this API. |
| 142 std::unique_ptr<APIBindingHooks> binding_hooks_; | 135 std::unique_ptr<APIBindingHooks> binding_hooks_; |
| 143 | 136 |
| 144 // The reference map for all known types; required to outlive this object. | 137 // The reference map for all known types; required to outlive this object. |
| 145 APITypeReferenceMap* type_refs_; | 138 APITypeReferenceMap* type_refs_; |
| 146 | 139 |
| 147 // The associated request handler, shared between this and other bindings. | 140 // The associated request handler, shared between this and other bindings. |
| 148 // Required to outlive this object. | 141 // Required to outlive this object. |
| 149 APIRequestHandler* request_handler_; | 142 APIRequestHandler* request_handler_; |
| 150 | 143 |
| 151 // The associated event handler, shared between this and other bindings. | 144 // The associated event handler, shared between this and other bindings. |
| 152 // Required to outlive this object. | 145 // Required to outlive this object. |
| 153 APIEventHandler* event_handler_; | 146 APIEventHandler* event_handler_; |
| 154 | 147 |
| 148 // The associated access checker; required to outlive this object. |
| 149 const BindingAccessChecker* const access_checker_; |
| 150 |
| 155 // The template for this API. Note: some methods may only be available in | 151 // The template for this API. Note: some methods may only be available in |
| 156 // certain contexts, but this template contains all methods. Those that are | 152 // certain contexts, but this template contains all methods. Those that are |
| 157 // unavailable are removed after object instantiation. | 153 // unavailable are removed after object instantiation. |
| 158 v8::Eternal<v8::ObjectTemplate> object_template_; | 154 v8::Eternal<v8::ObjectTemplate> object_template_; |
| 159 | 155 |
| 160 base::WeakPtrFactory<APIBinding> weak_factory_; | 156 base::WeakPtrFactory<APIBinding> weak_factory_; |
| 161 | 157 |
| 162 DISALLOW_COPY_AND_ASSIGN(APIBinding); | 158 DISALLOW_COPY_AND_ASSIGN(APIBinding); |
| 163 }; | 159 }; |
| 164 | 160 |
| 165 } // namespace extensions | 161 } // namespace extensions |
| 166 | 162 |
| 167 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ | 163 #endif // EXTENSIONS_RENDERER_API_BINDING_H_ |
| OLD | NEW |