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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/WrapperTypeInfo.h

Issue 2843603002: Move ScriptWrappable and dependencies to platform/bindings (Closed)
Patch Set: Rebase and try again Created 3 years, 7 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
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 10 matching lines...) Expand all
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #ifndef WrapperTypeInfo_h 31 // This file has been moved to platform/bindings/WrapperTypeInfo.h.
32 #define WrapperTypeInfo_h 32 // TODO(adithyas): Remove this file.
33 33 #include "platform/bindings/WrapperTypeInfo.h"
34 #include "bindings/core/v8/ActiveScriptWrappable.h"
35 #include "gin/public/wrapper_info.h"
36 #include "platform/heap/Handle.h"
37 #include "platform/wtf/Allocator.h"
38 #include "platform/wtf/Assertions.h"
39 #include "v8/include/v8.h"
40
41 namespace blink {
42
43 class DOMWrapperWorld;
44 class ScriptWrappable;
45
46 ScriptWrappable* ToScriptWrappable(
47 const v8::PersistentBase<v8::Object>& wrapper);
48 ScriptWrappable* ToScriptWrappable(v8::Local<v8::Object> wrapper);
49
50 static const int kV8DOMWrapperTypeIndex =
51 static_cast<int>(gin::kWrapperInfoIndex);
52 static const int kV8DOMWrapperObjectIndex =
53 static_cast<int>(gin::kEncodedValueIndex);
54 static const int kV8DefaultWrapperInternalFieldCount =
55 static_cast<int>(gin::kNumberOfInternalFields);
56 static const int kV8PrototypeTypeIndex = 0;
57 static const int kV8PrototypeInternalFieldcount = 1;
58
59 typedef v8::Local<v8::FunctionTemplate> (
60 *DomTemplateFunction)(v8::Isolate*, const DOMWrapperWorld&);
61 typedef void (*TraceFunction)(Visitor*, ScriptWrappable*);
62 typedef void (*TraceWrappersFunction)(WrapperVisitor*, ScriptWrappable*);
63 typedef ActiveScriptWrappableBase* (*ToActiveScriptWrappableFunction)(
64 v8::Local<v8::Object>);
65 typedef void (*ResolveWrapperReachabilityFunction)(
66 v8::Isolate*,
67 ScriptWrappable*,
68 const v8::Persistent<v8::Object>&);
69 typedef void (*PreparePrototypeAndInterfaceObjectFunction)(
70 v8::Local<v8::Context>,
71 const DOMWrapperWorld&,
72 v8::Local<v8::Object>,
73 v8::Local<v8::Function>,
74 v8::Local<v8::FunctionTemplate>);
75
76 // This struct provides a way to store a bunch of information that is helpful
77 // when unwrapping v8 objects. Each v8 bindings class has exactly one static
78 // WrapperTypeInfo member, so comparing pointers is a safe way to determine if
79 // types match.
80 struct WrapperTypeInfo {
81 DISALLOW_NEW();
82
83 enum WrapperTypePrototype {
84 kWrapperTypeObjectPrototype,
85 kWrapperTypeExceptionPrototype,
86 };
87
88 enum WrapperClassId {
89 kNodeClassId = 1, // NodeClassId must be smaller than ObjectClassId.
90 kObjectClassId,
91 };
92
93 enum ActiveScriptWrappableInheritance {
94 kNotInheritFromActiveScriptWrappable,
95 kInheritFromActiveScriptWrappable,
96 };
97
98 enum Lifetime {
99 kDependent,
100 kIndependent,
101 };
102
103 static const WrapperTypeInfo* Unwrap(v8::Local<v8::Value> type_info_wrapper) {
104 return reinterpret_cast<const WrapperTypeInfo*>(
105 v8::External::Cast(*type_info_wrapper)->Value());
106 }
107
108 static void WrapperCreated() {
109 ThreadState::Current()->Heap().HeapStats().IncreaseWrapperCount(1);
110 }
111
112 static void WrapperDestroyed() {
113 ThreadHeapStats& heap_stats = ThreadState::Current()->Heap().HeapStats();
114 heap_stats.DecreaseWrapperCount(1);
115 heap_stats.IncreaseCollectedWrapperCount(1);
116 }
117
118 bool Equals(const WrapperTypeInfo* that) const { return this == that; }
119
120 bool IsSubclass(const WrapperTypeInfo* that) const {
121 for (const WrapperTypeInfo* current = this; current;
122 current = current->parent_class) {
123 if (current == that)
124 return true;
125 }
126
127 return false;
128 }
129
130 void ConfigureWrapper(v8::PersistentBase<v8::Object>* wrapper) const {
131 wrapper->SetWrapperClassId(wrapper_class_id);
132 if (lifetime == kIndependent)
133 wrapper->MarkIndependent();
134 }
135
136 v8::Local<v8::FunctionTemplate> domTemplate(
137 v8::Isolate* isolate,
138 const DOMWrapperWorld& world) const {
139 return dom_template_function(isolate, world);
140 }
141
142 void Trace(Visitor* visitor, ScriptWrappable* script_wrappable) const {
143 DCHECK(trace_function);
144 return trace_function(visitor, script_wrappable);
145 }
146
147 void TraceWrappers(WrapperVisitor* visitor,
148 ScriptWrappable* script_wrappable) const {
149 DCHECK(trace_wrappers_function);
150 return trace_wrappers_function(visitor, script_wrappable);
151 }
152
153 void PreparePrototypeAndInterfaceObject(
154 v8::Local<v8::Context> context,
155 const DOMWrapperWorld& world,
156 v8::Local<v8::Object> prototype_object,
157 v8::Local<v8::Function> interface_object,
158 v8::Local<v8::FunctionTemplate> interface_template) const {
159 if (prepare_prototype_and_interface_object_function)
160 prepare_prototype_and_interface_object_function(
161 context, world, prototype_object, interface_object,
162 interface_template);
163 }
164
165 bool IsActiveScriptWrappable() const {
166 return active_script_wrappable_inheritance ==
167 kInheritFromActiveScriptWrappable;
168 }
169
170 // This field must be the first member of the struct WrapperTypeInfo.
171 // See also static_assert() in .cpp file.
172 const gin::GinEmbedder gin_embedder;
173
174 DomTemplateFunction dom_template_function;
175 const TraceFunction trace_function;
176 const TraceWrappersFunction trace_wrappers_function;
177 PreparePrototypeAndInterfaceObjectFunction
178 prepare_prototype_and_interface_object_function;
179 const char* const interface_name;
180 const WrapperTypeInfo* parent_class;
181 const unsigned wrapper_type_prototype : 1; // WrapperTypePrototype
182 const unsigned wrapper_class_id : 2; // WrapperClassId
183 const unsigned // ActiveScriptWrappableInheritance
184 active_script_wrappable_inheritance : 1;
185 const unsigned lifetime : 1; // Lifetime
186 };
187
188 template <typename T, int offset>
189 inline T* GetInternalField(const v8::PersistentBase<v8::Object>& persistent) {
190 DCHECK_LT(offset, v8::Object::InternalFieldCount(persistent));
191 return reinterpret_cast<T*>(
192 v8::Object::GetAlignedPointerFromInternalField(persistent, offset));
193 }
194
195 template <typename T, int offset>
196 inline T* GetInternalField(v8::Local<v8::Object> wrapper) {
197 DCHECK_LT(offset, wrapper->InternalFieldCount());
198 return reinterpret_cast<T*>(
199 wrapper->GetAlignedPointerFromInternalField(offset));
200 }
201
202 // The return value can be null if |wrapper| is a global proxy, which points to
203 // nothing while a navigation.
204 inline ScriptWrappable* ToScriptWrappable(
205 const v8::PersistentBase<v8::Object>& wrapper) {
206 return GetInternalField<ScriptWrappable, kV8DOMWrapperObjectIndex>(wrapper);
207 }
208
209 inline ScriptWrappable* ToScriptWrappable(v8::Local<v8::Object> wrapper) {
210 return GetInternalField<ScriptWrappable, kV8DOMWrapperObjectIndex>(wrapper);
211 }
212
213 inline const WrapperTypeInfo* ToWrapperTypeInfo(
214 const v8::PersistentBase<v8::Object>& wrapper) {
215 return GetInternalField<WrapperTypeInfo, kV8DOMWrapperTypeIndex>(wrapper);
216 }
217
218 inline const WrapperTypeInfo* ToWrapperTypeInfo(v8::Local<v8::Object> wrapper) {
219 return GetInternalField<WrapperTypeInfo, kV8DOMWrapperTypeIndex>(wrapper);
220 }
221
222 } // namespace blink
223
224 #endif // WrapperTypeInfo_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698