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