Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Side by Side Diff: extensions/renderer/api_binding.h

Issue 2947463002: [Extensions Bindings] Add a bindings/ subdirectory under renderer (Closed)
Patch Set: . Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
34 namespace binding {
35 enum class RequestThread;
36 }
37
38 // 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 // The APIBinding then matches the calling arguments against an expected method
41 // 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 // each can vend multiple v8::Objects for different contexts.
44 // This object is designed to be one-per-isolate, but used across separate
45 // contexts.
46 class APIBinding {
47 public:
48 using CreateCustomType = base::Callback<v8::Local<v8::Object>(
49 v8::Isolate* isolate,
50 const std::string& type_name,
51 const std::string& property_name,
52 const base::ListValue* property_values)>;
53
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.
60 using HandlerCallback = base::Callback<void(gin::Arguments*)>;
61
62 // The APITypeReferenceMap is required to outlive this object.
63 // |function_definitions|, |type_definitions| and |event_definitions|
64 // may be null if the API does not specify any of that category.
65 APIBinding(const std::string& name,
66 const base::ListValue* function_definitions,
67 const base::ListValue* type_definitions,
68 const base::ListValue* event_definitions,
69 const base::DictionaryValue* property_definitions,
70 const CreateCustomType& create_custom_type,
71 const AvailabilityCallback& is_available,
72 std::unique_ptr<APIBindingHooks> binding_hooks,
73 APITypeReferenceMap* type_refs,
74 APIRequestHandler* request_handler,
75 APIEventHandler* event_handler);
76 ~APIBinding();
77
78 // Returns a new v8::Object for the API this APIBinding represents.
79 v8::Local<v8::Object> CreateInstance(v8::Local<v8::Context> context);
80
81 APIBindingHooks* hooks() { return binding_hooks_.get(); }
82
83 private:
84 // Initializes the object_template_ for this API. Called lazily when the
85 // first instance is created.
86 void InitializeTemplate(v8::Isolate* isolate);
87
88 // Decorates |object_template| with the properties specified by |properties|.
89 void DecorateTemplateWithProperties(
90 v8::Isolate* isolate,
91 v8::Local<v8::ObjectTemplate> object_template,
92 const base::DictionaryValue& properties);
93
94 // Handler for getting the v8::Object associated with an event on the API.
95 static void GetEventObject(v8::Local<v8::Name>,
96 const v8::PropertyCallbackInfo<v8::Value>& info);
97
98 // Handler for getting the v8::Object associated with a custom property on the
99 // API.
100 static void GetCustomPropertyObject(
101 v8::Local<v8::Name> property,
102 const v8::PropertyCallbackInfo<v8::Value>& info);
103
104 // Handles calling of an API method with the given |name| on the given
105 // |thread| and matches the arguments against |signature|.
106 void HandleCall(const std::string& name,
107 const APISignature* signature,
108 const binding::RequestThread thread,
109 gin::Arguments* args);
110
111 // The root name of the API, e.g. "tabs" for chrome.tabs.
112 std::string api_name_;
113
114 // A map from method name to method data.
115 struct MethodData;
116 std::map<std::string, std::unique_ptr<MethodData>> methods_;
117
118 // The events associated with this API.
119 struct EventData;
120 std::vector<std::unique_ptr<EventData>> events_;
121
122 // The custom properties on the API; these are rare.
123 struct CustomPropertyData;
124 std::vector<std::unique_ptr<CustomPropertyData>> custom_properties_;
125
126 // The pair for enum entry is <original, js-ified>. JS enum entries use
127 // SCREAMING_STYLE (whereas our API enums are just inconsistent).
128 using EnumEntry = std::pair<std::string, std::string>;
129 // A map of <name, values> for the enums on this API.
130 std::map<std::string, std::vector<EnumEntry>> enums_;
131
132 // The associated properties of the API, if any.
133 const base::DictionaryValue* property_definitions_;
134
135 // The callback for constructing a custom type.
136 CreateCustomType create_custom_type_;
137
138 // The callback for checking availability of an API feature.
139 AvailabilityCallback is_available_;
140
141 // The registered hooks for this API.
142 std::unique_ptr<APIBindingHooks> binding_hooks_;
143
144 // The reference map for all known types; required to outlive this object.
145 APITypeReferenceMap* type_refs_;
146
147 // The associated request handler, shared between this and other bindings.
148 // Required to outlive this object.
149 APIRequestHandler* request_handler_;
150
151 // The associated event handler, shared between this and other bindings.
152 // Required to outlive this object.
153 APIEventHandler* event_handler_;
154
155 // 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
157 // unavailable are removed after object instantiation.
158 v8::Eternal<v8::ObjectTemplate> object_template_;
159
160 base::WeakPtrFactory<APIBinding> weak_factory_;
161
162 DISALLOW_COPY_AND_ASSIGN(APIBinding);
163 };
164
165 } // namespace extensions
166
167 #endif // EXTENSIONS_RENDERER_API_BINDING_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698