1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_WRAPPER_TYPE_INFO_H_
32 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_WRAPPER_TYPE_INFO_H_
33 
34 #include "gin/public/wrapper_info.h"
35 #include "third_party/blink/renderer/platform/bindings/v8_interface_bridge_base.h"
36 #include "third_party/blink/renderer/platform/platform_export.h"
37 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
38 #include "third_party/blink/renderer/platform/wtf/assertions.h"
39 #include "v8/include/v8.h"
40 
41 namespace blink {
42 
43 class CustomWrappable;
44 class DOMWrapperWorld;
45 class ScriptWrappable;
46 class Visitor;
47 
48 static const int kV8DOMWrapperTypeIndex =
49     static_cast<int>(gin::kWrapperInfoIndex);
50 static const int kV8DOMWrapperObjectIndex =
51     static_cast<int>(gin::kEncodedValueIndex);
52 static const int kV8DefaultWrapperInternalFieldCount =
53     static_cast<int>(gin::kNumberOfInternalFields);
54 // The value of the following field isn't used (only its presence), hence no
55 // corresponding Index constant exists for it.
56 static const int kV8PrototypeInternalFieldcount = 1;
57 
58 // This struct provides a way to store a bunch of information that is helpful
59 // when unwrapping v8 objects. Each v8 bindings class has exactly one static
60 // WrapperTypeInfo member, so comparing pointers is a safe way to determine if
61 // types match.
62 struct PLATFORM_EXPORT WrapperTypeInfo final {
63   DISALLOW_NEW();
64 
65   enum WrapperTypePrototype {
66     kWrapperTypeObjectPrototype,
67     kWrapperTypeNoPrototype,  // For legacy callback interface
68   };
69 
70   enum WrapperClassId {
71     kNodeClassId = 1,  // NodeClassId must be smaller than ObjectClassId.
72     kObjectClassId,
73     kCustomWrappableId,
74   };
75 
76   enum ActiveScriptWrappableInheritance {
77     kNotInheritFromActiveScriptWrappable,
78     kInheritFromActiveScriptWrappable,
79   };
80 
81   enum IdlDefinitionKind {
82     kIdlInterface,
83     kIdlNamespace,
84     kIdlCallbackInterface,
85     kCustomWrappableKind,
86   };
87 
Unwrapfinal88   static const WrapperTypeInfo* Unwrap(v8::Local<v8::Value> type_info_wrapper) {
89     return reinterpret_cast<const WrapperTypeInfo*>(
90         v8::External::Cast(*type_info_wrapper)->Value());
91   }
92 
Equalsfinal93   bool Equals(const WrapperTypeInfo* that) const { return this == that; }
94 
IsSubclassfinal95   bool IsSubclass(const WrapperTypeInfo* that) const {
96     for (const WrapperTypeInfo* current = this; current;
97          current = current->parent_class) {
98       if (current == that)
99         return true;
100     }
101 
102     return false;
103   }
104 
ConfigureWrapperfinal105   void ConfigureWrapper(v8::TracedReference<v8::Object>* wrapper) const {
106     wrapper->SetWrapperClassId(wrapper_class_id);
107   }
108 
109   // Returns a v8::Template of interface object, namespace object, or the
110   // counterpart of the IDL definition.
111   //
112   // - kIdlInterface: v8::FunctionTemplate of interface object
113   // - kIdlNamespace: v8::ObjectTemplate of namespace object
114   // - kIdlCallbackInterface: v8::FunctionTemplate of legacy callback interface
115   //       object
116   // - kCustomWrappableKind: v8::FunctionTemplate
117   v8::Local<v8::Template> GetV8ClassTemplate(
118       v8::Isolate* isolate,
119       const DOMWrapperWorld& world) const;
120 
InstallConditionalFeaturesfinal121   void InstallConditionalFeatures(
122       v8::Local<v8::Context> context,
123       const DOMWrapperWorld& world,
124       v8::Local<v8::Object> instance_object,
125       v8::Local<v8::Object> prototype_object,
126       v8::Local<v8::Object> interface_object,
127       v8::Local<v8::Template> interface_template) const {
128     if (!install_context_dependent_props_func)
129       return;
130 
131     install_context_dependent_props_func(
132         context, world, instance_object, prototype_object, interface_object,
133         interface_template, bindings::V8InterfaceBridgeBase::FeatureSelector());
134   }
135 
IsActiveScriptWrappablefinal136   bool IsActiveScriptWrappable() const {
137     return active_script_wrappable_inheritance ==
138            kInheritFromActiveScriptWrappable;
139   }
140 
141   // Garbage collection support for when the type depends the WrapperTypeInfo
142   // object.
143   void Trace(Visitor*, const void*) const;
144 
145   // This field must be the first member of the struct WrapperTypeInfo.
146   // See also static_assert() in .cpp file.
147   const gin::GinEmbedder gin_embedder;
148 
149   bindings::V8InterfaceBridgeBase::InstallInterfaceTemplateFuncType
150       install_interface_template_func;
151   bindings::V8InterfaceBridgeBase::InstallContextDependentPropertiesFuncType
152       install_context_dependent_props_func;
153   const char* interface_name;
154   const WrapperTypeInfo* parent_class;
155   unsigned wrapper_type_prototype : 2;  // WrapperTypePrototype
156   unsigned wrapper_class_id : 2;        // WrapperClassId
157   unsigned                              // ActiveScriptWrappableInheritance
158       active_script_wrappable_inheritance : 1;
159   unsigned idl_definition_kind : 2;  // IdlDefinitionKind
160 };
161 
162 template <typename T, int offset>
GetInternalField(const v8::TracedReference<v8::Object> & global)163 inline T* GetInternalField(const v8::TracedReference<v8::Object>& global) {
164   DCHECK_LT(offset, v8::Object::InternalFieldCount(global));
165   return reinterpret_cast<T*>(
166       v8::Object::GetAlignedPointerFromInternalField(global, offset));
167 }
168 
169 template <typename T, int offset>
GetInternalField(v8::Local<v8::Object> wrapper)170 inline T* GetInternalField(v8::Local<v8::Object> wrapper) {
171   DCHECK_LT(offset, wrapper->InternalFieldCount());
172   return reinterpret_cast<T*>(
173       wrapper->GetAlignedPointerFromInternalField(offset));
174 }
175 
176 template <typename T, int offset>
GetInternalField(v8::Object * wrapper)177 inline T* GetInternalField(v8::Object* wrapper) {
178   DCHECK_LT(offset, wrapper->InternalFieldCount());
179   return reinterpret_cast<T*>(
180       wrapper->GetAlignedPointerFromInternalField(offset));
181 }
182 
183 // The return value can be null if |wrapper| is a global proxy, which points to
184 // nothing while a navigation.
ToScriptWrappable(const v8::TracedReference<v8::Object> & wrapper)185 inline ScriptWrappable* ToScriptWrappable(
186     const v8::TracedReference<v8::Object>& wrapper) {
187   return GetInternalField<ScriptWrappable, kV8DOMWrapperObjectIndex>(wrapper);
188 }
189 
ToScriptWrappable(v8::Local<v8::Object> wrapper)190 inline ScriptWrappable* ToScriptWrappable(v8::Local<v8::Object> wrapper) {
191   return GetInternalField<ScriptWrappable, kV8DOMWrapperObjectIndex>(wrapper);
192 }
193 
ToScriptWrappable(v8::Object * wrapper)194 inline ScriptWrappable* ToScriptWrappable(v8::Object* wrapper) {
195   return GetInternalField<ScriptWrappable, kV8DOMWrapperObjectIndex>(wrapper);
196 }
197 
ToCustomWrappable(v8::Local<v8::Object> wrapper)198 inline CustomWrappable* ToCustomWrappable(v8::Local<v8::Object> wrapper) {
199   return GetInternalField<CustomWrappable, kV8DOMWrapperObjectIndex>(wrapper);
200 }
201 
ToUntypedWrappable(const v8::TracedReference<v8::Object> & wrapper)202 inline void* ToUntypedWrappable(
203     const v8::TracedReference<v8::Object>& wrapper) {
204   return GetInternalField<void, kV8DOMWrapperObjectIndex>(wrapper);
205 }
206 
ToUntypedWrappable(v8::Local<v8::Object> wrapper)207 inline void* ToUntypedWrappable(v8::Local<v8::Object> wrapper) {
208   return GetInternalField<void, kV8DOMWrapperObjectIndex>(wrapper);
209 }
210 
ToWrapperTypeInfo(const v8::TracedReference<v8::Object> & wrapper)211 inline const WrapperTypeInfo* ToWrapperTypeInfo(
212     const v8::TracedReference<v8::Object>& wrapper) {
213   return GetInternalField<WrapperTypeInfo, kV8DOMWrapperTypeIndex>(wrapper);
214 }
215 
ToWrapperTypeInfo(v8::Local<v8::Object> wrapper)216 inline const WrapperTypeInfo* ToWrapperTypeInfo(v8::Local<v8::Object> wrapper) {
217   return GetInternalField<WrapperTypeInfo, kV8DOMWrapperTypeIndex>(wrapper);
218 }
219 
220 }  // namespace blink
221 
222 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_WRAPPER_TYPE_INFO_H_
223