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/platform_export.h"
36 #include "third_party/blink/renderer/platform/wtf/allocator/allocator.h"
37 #include "third_party/blink/renderer/platform/wtf/assertions.h"
38 #include "v8/include/v8.h"
39 
40 namespace blink {
41 
42 class ActiveScriptWrappableBase;
43 class CustomWrappable;
44 class DOMWrapperWorld;
45 class ScriptWrappable;
46 class Visitor;
47 
48 ScriptWrappable* ToScriptWrappable(
49     const v8::PersistentBase<v8::Object>& wrapper);
50 ScriptWrappable* ToScriptWrappable(v8::Local<v8::Object> wrapper);
51 
52 static const int kV8DOMWrapperTypeIndex =
53     static_cast<int>(gin::kWrapperInfoIndex);
54 static const int kV8DOMWrapperObjectIndex =
55     static_cast<int>(gin::kEncodedValueIndex);
56 static const int kV8DefaultWrapperInternalFieldCount =
57     static_cast<int>(gin::kNumberOfInternalFields);
58 // The value of the following field isn't used (only its presence), hence no
59 // corresponding Index constant exists for it.
60 static const int kV8PrototypeInternalFieldcount = 1;
61 
62 typedef v8::Local<v8::FunctionTemplate> (
63     *DomTemplateFunction)(v8::Isolate*, const DOMWrapperWorld&);
64 typedef ActiveScriptWrappableBase* (*ToActiveScriptWrappableFunction)(
65     v8::Local<v8::Object>);
66 typedef void (*ResolveWrapperReachabilityFunction)(
67     v8::Isolate*,
68     ScriptWrappable*,
69     const v8::Persistent<v8::Object>&);
70 typedef void (*InstallConditionalFeaturesFunction)(
71     v8::Local<v8::Context>,
72     const DOMWrapperWorld&,
73     v8::Local<v8::Object>,
74     v8::Local<v8::Object>,
75     v8::Local<v8::Function>,
76     v8::Local<v8::FunctionTemplate>);
77 
78 // This struct provides a way to store a bunch of information that is helpful
79 // when unwrapping v8 objects. Each v8 bindings class has exactly one static
80 // WrapperTypeInfo member, so comparing pointers is a safe way to determine if
81 // types match.
82 struct WrapperTypeInfo {
83   DISALLOW_NEW();
84 
85   enum WrapperTypePrototype {
86     kWrapperTypeObjectPrototype,
87     kWrapperTypeNoPrototype,  // For legacy callback interface
88   };
89 
90   enum WrapperClassId {
91     kNodeClassId = 1,  // NodeClassId must be smaller than ObjectClassId.
92     kObjectClassId,
93     kCustomWrappableId,
94   };
95 
96   enum ActiveScriptWrappableInheritance {
97     kNotInheritFromActiveScriptWrappable,
98     kInheritFromActiveScriptWrappable,
99   };
100 
UnwrapWrapperTypeInfo101   static const WrapperTypeInfo* Unwrap(v8::Local<v8::Value> type_info_wrapper) {
102     return reinterpret_cast<const WrapperTypeInfo*>(
103         v8::External::Cast(*type_info_wrapper)->Value());
104   }
105 
106   PLATFORM_EXPORT static void WrapperCreated();
107   PLATFORM_EXPORT static void WrapperDestroyed();
108 
EqualsWrapperTypeInfo109   bool Equals(const WrapperTypeInfo* that) const { return this == that; }
110 
IsSubclassWrapperTypeInfo111   bool IsSubclass(const WrapperTypeInfo* that) const {
112     for (const WrapperTypeInfo* current = this; current;
113          current = current->parent_class) {
114       if (current == that)
115         return true;
116     }
117 
118     return false;
119   }
120 
ConfigureWrapperWrapperTypeInfo121   void ConfigureWrapper(v8::PersistentBase<v8::Object>* wrapper) const {
122     wrapper->SetWrapperClassId(wrapper_class_id);
123   }
124 
ConfigureWrapperWrapperTypeInfo125   void ConfigureWrapper(v8::TracedReference<v8::Object>* wrapper) const {
126     wrapper->SetWrapperClassId(wrapper_class_id);
127   }
128 
DomTemplateWrapperTypeInfo129   v8::Local<v8::FunctionTemplate> DomTemplate(
130       v8::Isolate* isolate,
131       const DOMWrapperWorld& world) const {
132     return dom_template_function(isolate, world);
133   }
134 
InstallConditionalFeaturesWrapperTypeInfo135   void InstallConditionalFeatures(
136       v8::Local<v8::Context> context,
137       const DOMWrapperWorld& world,
138       v8::Local<v8::Object> instance_object,
139       v8::Local<v8::Object> prototype_object,
140       v8::Local<v8::Function> interface_object,
141       v8::Local<v8::FunctionTemplate> interface_template) const {
142     if (install_conditional_features_function) {
143       install_conditional_features_function(context, world, instance_object,
144                                             prototype_object, interface_object,
145                                             interface_template);
146     }
147   }
148 
IsActiveScriptWrappableWrapperTypeInfo149   bool IsActiveScriptWrappable() const {
150     return active_script_wrappable_inheritance ==
151            kInheritFromActiveScriptWrappable;
152   }
153 
154   // Garbage collection support for when the type depends the WrapperTypeInfo
155   // object.
156   PLATFORM_EXPORT void Trace(Visitor*, const void*) const;
157 
158   // This field must be the first member of the struct WrapperTypeInfo.
159   // See also static_assert() in .cpp file.
160   const gin::GinEmbedder gin_embedder;
161 
162   DomTemplateFunction dom_template_function;
163   InstallConditionalFeaturesFunction install_conditional_features_function;
164   const char* const interface_name;
165   const WrapperTypeInfo* parent_class;
166   const unsigned wrapper_type_prototype : 2;  // WrapperTypePrototype
167   const unsigned wrapper_class_id : 2;        // WrapperClassId
168   const unsigned  // ActiveScriptWrappableInheritance
169       active_script_wrappable_inheritance : 1;
170 };
171 
172 template <typename T, int offset>
GetInternalField(const v8::PersistentBase<v8::Object> & persistent)173 inline T* GetInternalField(const v8::PersistentBase<v8::Object>& persistent) {
174   DCHECK_LT(offset, v8::Object::InternalFieldCount(persistent));
175   return reinterpret_cast<T*>(
176       v8::Object::GetAlignedPointerFromInternalField(persistent, offset));
177 }
178 
179 template <typename T, int offset>
GetInternalField(const v8::TracedReference<v8::Object> & global)180 inline T* GetInternalField(const v8::TracedReference<v8::Object>& global) {
181   DCHECK_LT(offset, v8::Object::InternalFieldCount(global));
182   return reinterpret_cast<T*>(
183       v8::Object::GetAlignedPointerFromInternalField(global, offset));
184 }
185 
186 template <typename T, int offset>
GetInternalField(v8::Local<v8::Object> wrapper)187 inline T* GetInternalField(v8::Local<v8::Object> wrapper) {
188   DCHECK_LT(offset, wrapper->InternalFieldCount());
189   return reinterpret_cast<T*>(
190       wrapper->GetAlignedPointerFromInternalField(offset));
191 }
192 
193 // The return value can be null if |wrapper| is a global proxy, which points to
194 // nothing while a navigation.
ToScriptWrappable(const v8::PersistentBase<v8::Object> & wrapper)195 inline ScriptWrappable* ToScriptWrappable(
196     const v8::PersistentBase<v8::Object>& wrapper) {
197   return GetInternalField<ScriptWrappable, kV8DOMWrapperObjectIndex>(wrapper);
198 }
199 
ToScriptWrappable(const v8::TracedReference<v8::Object> & wrapper)200 inline ScriptWrappable* ToScriptWrappable(
201     const v8::TracedReference<v8::Object>& wrapper) {
202   return GetInternalField<ScriptWrappable, kV8DOMWrapperObjectIndex>(wrapper);
203 }
204 
ToScriptWrappable(v8::Local<v8::Object> wrapper)205 inline ScriptWrappable* ToScriptWrappable(v8::Local<v8::Object> wrapper) {
206   return GetInternalField<ScriptWrappable, kV8DOMWrapperObjectIndex>(wrapper);
207 }
208 
ToCustomWrappable(const v8::PersistentBase<v8::Object> & wrapper)209 inline CustomWrappable* ToCustomWrappable(
210     const v8::PersistentBase<v8::Object>& wrapper) {
211   return GetInternalField<CustomWrappable, kV8DOMWrapperObjectIndex>(wrapper);
212 }
213 
ToCustomWrappable(v8::Local<v8::Object> wrapper)214 inline CustomWrappable* ToCustomWrappable(v8::Local<v8::Object> wrapper) {
215   return GetInternalField<CustomWrappable, kV8DOMWrapperObjectIndex>(wrapper);
216 }
217 
ToUntypedWrappable(const v8::PersistentBase<v8::Object> & wrapper)218 inline void* ToUntypedWrappable(const v8::PersistentBase<v8::Object>& wrapper) {
219   return GetInternalField<void, kV8DOMWrapperObjectIndex>(wrapper);
220 }
221 
ToUntypedWrappable(const v8::TracedReference<v8::Object> & wrapper)222 inline void* ToUntypedWrappable(
223     const v8::TracedReference<v8::Object>& wrapper) {
224   return GetInternalField<void, kV8DOMWrapperObjectIndex>(wrapper);
225 }
226 
ToUntypedWrappable(v8::Local<v8::Object> wrapper)227 inline void* ToUntypedWrappable(v8::Local<v8::Object> wrapper) {
228   return GetInternalField<void, kV8DOMWrapperObjectIndex>(wrapper);
229 }
230 
ToWrapperTypeInfo(const v8::PersistentBase<v8::Object> & wrapper)231 inline const WrapperTypeInfo* ToWrapperTypeInfo(
232     const v8::PersistentBase<v8::Object>& wrapper) {
233   return GetInternalField<WrapperTypeInfo, kV8DOMWrapperTypeIndex>(wrapper);
234 }
235 
ToWrapperTypeInfo(const v8::TracedReference<v8::Object> & wrapper)236 inline const WrapperTypeInfo* ToWrapperTypeInfo(
237     const v8::TracedReference<v8::Object>& wrapper) {
238   return GetInternalField<WrapperTypeInfo, kV8DOMWrapperTypeIndex>(wrapper);
239 }
240 
ToWrapperTypeInfo(v8::Local<v8::Object> wrapper)241 inline const WrapperTypeInfo* ToWrapperTypeInfo(v8::Local<v8::Object> wrapper) {
242   return GetInternalField<WrapperTypeInfo, kV8DOMWrapperTypeIndex>(wrapper);
243 }
244 
245 }  // namespace blink
246 
247 #endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_BINDINGS_WRAPPER_TYPE_INFO_H_
248