OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef V8PrivateProperty_h | 5 // This file has been moved to platform/bindings/V8PrivateProperty.h. |
6 #define V8PrivateProperty_h | 6 // TODO(adithyas): Remove this file. |
7 | 7 #include "platform/bindings/V8PrivateProperty.h" |
8 #include <memory> | |
9 | |
10 #include "bindings/core/v8/ScriptPromiseProperties.h" | |
11 #include "bindings/core/v8/V8BindingMacros.h" | |
12 #include "bindings/core/v8/V8PerIsolateData.h" | |
13 #include "core/CoreExport.h" | |
14 #include "platform/wtf/Allocator.h" | |
15 #include "platform/wtf/PtrUtil.h" | |
16 #include "v8/include/v8.h" | |
17 | |
18 namespace blink { | |
19 | |
20 class ScriptWrappable; | |
21 | |
22 // TODO(peria): Remove properties just to keep V8 objects alive. | |
23 // e.g. InternalBody.Buffer, InternalBody.Stream, IDBCursor.Request, | |
24 // FetchEvent.Request. | |
25 // Apply |X| for each pair of (InterfaceName, PrivateKeyName). | |
26 #define V8_PRIVATE_PROPERTY_FOR_EACH(X) \ | |
27 X(CustomElement, AdoptedCallback) \ | |
28 X(CustomElement, AttributeChangedCallback) \ | |
29 X(CustomElement, ConnectedCallback) \ | |
30 X(CustomElement, DisconnectedCallback) \ | |
31 X(CustomElement, Document) \ | |
32 X(CustomElement, IsInterfacePrototypeObject) \ | |
33 X(CustomElement, NamespaceURI) \ | |
34 X(CustomElement, RegistryMap) \ | |
35 X(CustomElement, TagName) \ | |
36 X(CustomElement, Type) \ | |
37 X(CustomElementLifecycle, AttachedCallback) \ | |
38 X(CustomElementLifecycle, AttributeChangedCallback) \ | |
39 X(CustomElementLifecycle, CreatedCallback) \ | |
40 X(CustomElementLifecycle, DetachedCallback) \ | |
41 X(CustomEvent, Detail) \ | |
42 X(DOMException, Error) \ | |
43 X(ErrorEvent, Error) \ | |
44 X(FetchEvent, Request) \ | |
45 X(Global, Event) \ | |
46 X(IDBCursor, Request) \ | |
47 X(IDBObserver, Callback) \ | |
48 X(InternalBody, Buffer) \ | |
49 X(InternalBody, Stream) \ | |
50 X(IntersectionObserver, Callback) \ | |
51 X(LazyEventListener, ToString) \ | |
52 X(MessageChannel, Port1) \ | |
53 X(MessageChannel, Port2) \ | |
54 X(MessageEvent, CachedData) \ | |
55 X(MutationObserver, Callback) \ | |
56 X(NamedConstructor, Initialized) \ | |
57 X(PerformanceObserver, Callback) \ | |
58 X(PopStateEvent, State) \ | |
59 X(SameObject, NotificationActions) \ | |
60 X(SameObject, NotificationData) \ | |
61 X(SameObject, NotificationVibrate) \ | |
62 X(SameObject, PerformanceLongTaskTimingAttribution) \ | |
63 SCRIPT_PROMISE_PROPERTIES(X, Promise) \ | |
64 SCRIPT_PROMISE_PROPERTIES(X, Resolver) | |
65 | |
66 // The getter's name for a private property. | |
67 #define V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, PrivateKeyName) \ | |
68 Get##InterfaceName##PrivateKeyName | |
69 | |
70 // The member variable's name for a private property. | |
71 #define V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, PrivateKeyName) \ | |
72 m_symbol##InterfaceName##PrivateKeyName | |
73 | |
74 // The string used to create a private symbol. Must be unique per V8 instance. | |
75 #define V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, PrivateKeyName) \ | |
76 #InterfaceName "#" #PrivateKeyName // NOLINT(whitespace/indent) | |
77 | |
78 // Provides access to V8's private properties. | |
79 // | |
80 // Usage 1) Fast path to use a pre-registered symbol. | |
81 // auto private = V8PrivateProperty::getMessageEventCachedData(isolate); | |
82 // v8::Local<v8::Object> object = ...; | |
83 // v8::Local<v8::Value> value = private.getOrUndefined(object); | |
84 // value = ...; | |
85 // private.set(object, value); | |
86 // | |
87 // Usage 2) Slow path to create a global private symbol. | |
88 // const char symbolName[] = "Interface#PrivateKeyName"; | |
89 // auto private = V8PrivateProperty::createSymbol(isolate, symbolName); | |
90 // ... | |
91 class CORE_EXPORT V8PrivateProperty { | |
92 USING_FAST_MALLOC(V8PrivateProperty); | |
93 WTF_MAKE_NONCOPYABLE(V8PrivateProperty); | |
94 | |
95 public: | |
96 // Provides fast access to V8's private properties. | |
97 // | |
98 // Retrieving/creating a global private symbol from a string is very | |
99 // expensive compared to get or set a private property. This class | |
100 // provides a way to cache a private symbol and re-use it. | |
101 class CORE_EXPORT Symbol { | |
102 STACK_ALLOCATED(); | |
103 | |
104 public: | |
105 bool HasValue(v8::Local<v8::Object> object) const { | |
106 return object->HasPrivate(GetContext(), private_symbol_).ToChecked(); | |
107 } | |
108 | |
109 // Returns the value of the private property if set, or undefined. | |
110 v8::Local<v8::Value> GetOrUndefined(v8::Local<v8::Object> object) const { | |
111 return object->GetPrivate(GetContext(), private_symbol_).ToLocalChecked(); | |
112 } | |
113 | |
114 // TODO(peria): Remove this method, and use getOrUndefined() instead. | |
115 // Returns the value of the private property if set, or an empty handle. | |
116 v8::Local<v8::Value> GetOrEmpty(v8::Local<v8::Object> object) const { | |
117 if (HasValue(object)) | |
118 return GetOrUndefined(object); | |
119 return v8::Local<v8::Value>(); | |
120 } | |
121 | |
122 bool Set(v8::Local<v8::Object> object, v8::Local<v8::Value> value) const { | |
123 return object->SetPrivate(GetContext(), private_symbol_, value) | |
124 .ToChecked(); | |
125 } | |
126 | |
127 bool DeleteProperty(v8::Local<v8::Object> object) const { | |
128 return object->DeletePrivate(GetContext(), private_symbol_).ToChecked(); | |
129 } | |
130 | |
131 v8::Local<v8::Private> GetPrivate() const { return private_symbol_; } | |
132 | |
133 private: | |
134 friend class V8PrivateProperty; | |
135 // The following classes are exceptionally allowed to call to | |
136 // getFromMainWorld. | |
137 friend class V8CustomEvent; | |
138 friend class V8ExtendableMessageEvent; | |
139 | |
140 Symbol(v8::Isolate* isolate, v8::Local<v8::Private> private_symbol) | |
141 : private_symbol_(private_symbol), isolate_(isolate) {} | |
142 | |
143 // To get/set private property, we should use the current context. | |
144 v8::Local<v8::Context> GetContext() const { | |
145 return isolate_->GetCurrentContext(); | |
146 } | |
147 | |
148 // Only friend classes are allowed to use this API. | |
149 v8::Local<v8::Value> GetFromMainWorld(ScriptWrappable*); | |
150 | |
151 v8::Local<v8::Private> private_symbol_; | |
152 v8::Isolate* isolate_; | |
153 }; | |
154 | |
155 static std::unique_ptr<V8PrivateProperty> Create() { | |
156 return WTF::WrapUnique(new V8PrivateProperty()); | |
157 } | |
158 | |
159 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ | |
160 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(/* // NOLINT */ \ | |
161 InterfaceName, KeyName)( \ | |
162 v8::Isolate * isolate) { \ | |
163 V8PrivateProperty* private_prop = \ | |
164 V8PerIsolateData::From(isolate)->PrivateProperty(); \ | |
165 v8::Eternal<v8::Private>& property_handle = \ | |
166 private_prop->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName); \ | |
167 if (UNLIKELY(property_handle.IsEmpty())) { \ | |
168 property_handle.Set( \ | |
169 isolate, CreateV8Private(isolate, V8_PRIVATE_PROPERTY_SYMBOL_STRING( \ | |
170 InterfaceName, KeyName))); \ | |
171 } \ | |
172 return Symbol(isolate, property_handle.Get(isolate)); \ | |
173 } | |
174 | |
175 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER) | |
176 #undef V8_PRIVATE_PROPERTY_DEFINE_GETTER | |
177 | |
178 // TODO(peria): Do not use this specialized hack. See a TODO comment | |
179 // on m_symbolWindowDocumentCachedAccessor. | |
180 static Symbol GetWindowDocumentCachedAccessor(v8::Isolate* isolate) { | |
181 V8PrivateProperty* private_prop = | |
182 V8PerIsolateData::From(isolate)->PrivateProperty(); | |
183 if (UNLIKELY( | |
184 private_prop->symbol_window_document_cached_accessor_.IsEmpty())) { | |
185 private_prop->symbol_window_document_cached_accessor_.Set( | |
186 isolate, CreateCachedV8Private( | |
187 isolate, V8_PRIVATE_PROPERTY_SYMBOL_STRING( | |
188 "Window", "DocumentCachedAccessor"))); | |
189 } | |
190 return Symbol( | |
191 isolate, private_prop->symbol_window_document_cached_accessor_.NewLocal( | |
192 isolate)); | |
193 } | |
194 | |
195 static Symbol GetSymbol(v8::Isolate* isolate, const char* symbol) { | |
196 return Symbol(isolate, CreateCachedV8Private(isolate, symbol)); | |
197 } | |
198 | |
199 private: | |
200 V8PrivateProperty() {} | |
201 | |
202 static v8::Local<v8::Private> CreateV8Private(v8::Isolate*, | |
203 const char* symbol); | |
204 // TODO(peria): Remove this method. We should not use v8::Private::ForApi(). | |
205 static v8::Local<v8::Private> CreateCachedV8Private(v8::Isolate*, | |
206 const char* symbol); | |
207 | |
208 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ | |
209 v8::Eternal<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \ | |
210 InterfaceName, KeyName); // NOLINT(readability/naming/underscores) | |
211 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) | |
212 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER | |
213 | |
214 // TODO(peria): Do not use this specialized hack for | |
215 // Window#DocumentCachedAccessor. This is required to put v8::Private key in | |
216 // a snapshot, and it cannot be a v8::Eternal<> due to V8 serializer's | |
217 // requirement. | |
218 ScopedPersistent<v8::Private> symbol_window_document_cached_accessor_; | |
219 }; | |
220 | |
221 } // namespace blink | |
222 | |
223 #endif // V8PrivateProperty_h | |
OLD | NEW |