1 // Copyright 2020 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 THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_IDL_MEMBER_INSTALLER_H_
6 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_IDL_MEMBER_INSTALLER_H_
7 
8 #include "base/containers/span.h"
9 #include "third_party/blink/renderer/platform/platform_export.h"
10 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
11 #include "v8/include/v8-fast-api-calls.h"
12 #include "v8/include/v8.h"
13 
14 namespace blink {
15 
16 class DOMWrapperWorld;
17 
18 namespace bindings {
19 
20 // IDLMemberInstaller is a set of utility functions to define IDL members as
21 // ES properties.
22 class PLATFORM_EXPORT IDLMemberInstaller final {
23   STATIC_ONLY(IDLMemberInstaller);
24 
25  public:
26   // On which object the property is defined
27   enum class FlagLocation {
28     kInstance,
29     kPrototype,
30     kInterface,
31   };
32   // In which world the property is defined
33   enum class FlagWorld {
34     kMainWorld = 1 << 0,
35     kNonMainWorlds = 1 << 1,
36     kAllWorlds = kMainWorld | kNonMainWorlds,
37   };
38   // v8::Signature check against the receiver object
39   enum class FlagReceiverCheck {
40     kCheck,
41     kDoNotCheck,
42   };
43   // Cross origin access check
44   enum class FlagCrossOriginCheck {
45     kCheck,
46     kDoNotCheck,
47   };
48 
49   // Web IDL attribute
50   struct AttributeConfig {
51     AttributeConfig& operator=(const AttributeConfig&) = delete;
52 
53     const char* name;
54     v8::FunctionCallback callback_for_get;
55     v8::FunctionCallback callback_for_set;
56     unsigned v8_property_attribute : 3;       // v8::PropertyAttribute
57     unsigned location : 2;                    // FlagLocation
58     unsigned world : 2;                       // FlagWorld
59     unsigned receiver_check : 1;              // FlagReceiverCheck
60     unsigned cross_origin_check_for_get : 1;  // FlagCrossOriginCheck
61     unsigned cross_origin_check_for_set : 1;  // FlagCrossOriginCheck
62     unsigned v8_side_effect : 2;              // v8::SideEffectType
63     unsigned v8_cached_accessor : 2;  // V8PrivateProperty::CachedAccessor
64   };
65   static void InstallAttributes(v8::Isolate* isolate,
66                                 const DOMWrapperWorld& world,
67                                 v8::Local<v8::Template> instance_template,
68                                 v8::Local<v8::Template> prototype_template,
69                                 v8::Local<v8::Template> interface_template,
70                                 v8::Local<v8::Signature> signature,
71                                 base::span<const AttributeConfig> configs);
72   static void InstallAttributes(v8::Isolate* isolate,
73                                 const DOMWrapperWorld& world,
74                                 v8::Local<v8::Object> instance_object,
75                                 v8::Local<v8::Object> prototype_object,
76                                 v8::Local<v8::Object> interface_object,
77                                 v8::Local<v8::Signature> signature,
78                                 base::span<const AttributeConfig> configs);
79 
80   // Web IDL constant
81   struct ConstantCallbackConfig {
82     ConstantCallbackConfig& operator=(const ConstantCallbackConfig&) = delete;
83 
84     const char* name;
85     v8::AccessorNameGetterCallback callback;
86   };
87   static void InstallConstants(
88       v8::Isolate* isolate,
89       const DOMWrapperWorld& world,
90       v8::Local<v8::Template> instance_template,
91       v8::Local<v8::Template> prototype_template,
92       v8::Local<v8::Template> interface_template,
93       v8::Local<v8::Signature> signature,
94       base::span<const ConstantCallbackConfig> configs);
95 
96   struct ConstantValueConfig {
97     ConstantValueConfig& operator=(const ConstantValueConfig&) = delete;
98 
99     const char* name;
100     int64_t value;
101   };
102   static void InstallConstants(v8::Isolate* isolate,
103                                const DOMWrapperWorld& world,
104                                v8::Local<v8::Template> instance_template,
105                                v8::Local<v8::Template> prototype_template,
106                                v8::Local<v8::Template> interface_template,
107                                v8::Local<v8::Signature> signature,
108                                base::span<const ConstantValueConfig> configs);
109 
110   // Web IDL operation
111   struct OperationConfig {
112     OperationConfig& operator=(const OperationConfig&) = delete;
113 
114     const char* name;
115     v8::FunctionCallback callback;
116     unsigned length : 8;
117     unsigned v8_property_attribute : 3;  // v8::PropertyAttribute
118     unsigned location : 2;               // FlagLocation
119     unsigned world : 2;                  // FlagWorld
120     unsigned receiver_check : 1;         // FlagReceiverCheck
121     unsigned cross_origin_check : 1;     // FlagCrossOriginCheck
122     unsigned v8_side_effect : 2;         // v8::SideEffectType
123   };
124   static void InstallOperations(v8::Isolate* isolate,
125                                 const DOMWrapperWorld& world,
126                                 v8::Local<v8::Template> instance_template,
127                                 v8::Local<v8::Template> prototype_template,
128                                 v8::Local<v8::Template> interface_template,
129                                 v8::Local<v8::Signature> signature,
130                                 base::span<const OperationConfig> configs);
131   static void InstallOperations(v8::Isolate* isolate,
132                                 const DOMWrapperWorld& world,
133                                 v8::Local<v8::Object> instance_object,
134                                 v8::Local<v8::Object> prototype_object,
135                                 v8::Local<v8::Object> interface_object,
136                                 v8::Local<v8::Signature> signature,
137                                 base::span<const OperationConfig> configs);
138 
139   struct NoAllocDirectCallOperationConfig {
140     OperationConfig operation_config;
141     v8::CFunction v8_c_function;
142   };
143   static void InstallOperations(
144       v8::Isolate* isolate,
145       const DOMWrapperWorld& world,
146       v8::Local<v8::Template> instance_template,
147       v8::Local<v8::Template> prototype_template,
148       v8::Local<v8::Template> interface_template,
149       v8::Local<v8::Signature> signature,
150       base::span<const NoAllocDirectCallOperationConfig> configs);
151 
152   // Global property reference
153   // https://heycam.github.io/webidl/#define-the-global-property-references
154   // [LegacyNamespace]
155   // https://heycam.github.io/webidl/#LegacyNamespace
156   struct ExposedConstructConfig {
157     ExposedConstructConfig& operator=(const ExposedConstructConfig&) = delete;
158 
159     const char* name;
160     v8::AccessorNameGetterCallback callback;
161   };
162   static void InstallExposedConstructs(
163       v8::Isolate* isolate,
164       const DOMWrapperWorld& world,
165       v8::Local<v8::Template> instance_template,
166       v8::Local<v8::Template> prototype_template,
167       v8::Local<v8::Template> interface_template,
168       v8::Local<v8::Signature> signature,
169       base::span<const ExposedConstructConfig> configs);
170   static void InstallExposedConstructs(
171       v8::Isolate* isolate,
172       const DOMWrapperWorld& world,
173       v8::Local<v8::Object> instance_object,
174       v8::Local<v8::Object> prototype_object,
175       v8::Local<v8::Object> interface_object,
176       v8::Local<v8::Signature> signature,
177       base::span<const ExposedConstructConfig> configs);
178 };
179 
180 }  // namespace bindings
181 
182 }  // namespace blink
183 
184 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_IDL_MEMBER_INSTALLER_H_
185