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 X(V8NodeFilterCondition, Filter) \ | |
64 SCRIPT_PROMISE_PROPERTIES(X, Promise) \ | |
65 SCRIPT_PROMISE_PROPERTIES(X, Resolver) | |
66 | |
67 // The getter's name for a private property. | |
68 #define V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, PrivateKeyName) \ | |
69 Get##InterfaceName##PrivateKeyName | |
70 | |
71 // The member variable's name for a private property. | |
72 #define V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, PrivateKeyName) \ | |
73 m_symbol##InterfaceName##PrivateKeyName | |
74 | |
75 // The string used to create a private symbol. Must be unique per V8 instance. | |
76 #define V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, PrivateKeyName) \ | |
77 #InterfaceName "#" #PrivateKeyName // NOLINT(whitespace/indent) | |
78 | |
79 // Provides access to V8's private properties. | |
80 // | |
81 // Usage 1) Fast path to use a pre-registered symbol. | |
82 // auto private = V8PrivateProperty::getMessageEventCachedData(isolate); | |
83 // v8::Local<v8::Object> object = ...; | |
84 // v8::Local<v8::Value> value = private.getOrUndefined(object); | |
85 // value = ...; | |
86 // private.set(object, value); | |
87 // | |
88 // Usage 2) Slow path to create a global private symbol. | |
89 // const char symbolName[] = "Interface#PrivateKeyName"; | |
90 // auto private = V8PrivateProperty::createSymbol(isolate, symbolName); | |
91 // ... | |
92 class CORE_EXPORT V8PrivateProperty { | |
93 USING_FAST_MALLOC(V8PrivateProperty); | |
94 WTF_MAKE_NONCOPYABLE(V8PrivateProperty); | |
95 | |
96 public: | |
97 // Provides fast access to V8's private properties. | |
98 // | |
99 // Retrieving/creating a global private symbol from a string is very | |
100 // expensive compared to get or set a private property. This class | |
101 // provides a way to cache a private symbol and re-use it. | |
102 class CORE_EXPORT Symbol { | |
103 STACK_ALLOCATED(); | |
104 | |
105 public: | |
106 bool HasValue(v8::Local<v8::Object> object) const { | |
107 return object->HasPrivate(GetContext(), private_symbol_).ToChecked(); | |
108 } | |
109 | |
110 // Returns the value of the private property if set, or undefined. | |
111 v8::Local<v8::Value> GetOrUndefined(v8::Local<v8::Object> object) const { | |
112 return object->GetPrivate(GetContext(), private_symbol_).ToLocalChecked(); | |
113 } | |
114 | |
115 // TODO(peria): Remove this method, and use getOrUndefined() instead. | |
116 // Returns the value of the private property if set, or an empty handle. | |
117 v8::Local<v8::Value> GetOrEmpty(v8::Local<v8::Object> object) const { | |
118 if (HasValue(object)) | |
119 return GetOrUndefined(object); | |
120 return v8::Local<v8::Value>(); | |
121 } | |
122 | |
123 bool Set(v8::Local<v8::Object> object, v8::Local<v8::Value> value) const { | |
124 return object->SetPrivate(GetContext(), private_symbol_, value) | |
125 .ToChecked(); | |
126 } | |
127 | |
128 bool DeleteProperty(v8::Local<v8::Object> object) const { | |
129 return object->DeletePrivate(GetContext(), private_symbol_).ToChecked(); | |
130 } | |
131 | |
132 v8::Local<v8::Private> GetPrivate() const { return private_symbol_; } | |
133 | |
134 private: | |
135 friend class V8PrivateProperty; | |
136 // The following classes are exceptionally allowed to call to | |
137 // getFromMainWorld. | |
138 friend class V8CustomEvent; | |
139 friend class V8ServiceWorkerMessageEventInternal; | |
140 | |
141 Symbol(v8::Isolate* isolate, v8::Local<v8::Private> private_symbol) | |
142 : private_symbol_(private_symbol), isolate_(isolate) {} | |
143 | |
144 // To get/set private property, we should use the current context. | |
145 v8::Local<v8::Context> GetContext() const { | |
146 return isolate_->GetCurrentContext(); | |
147 } | |
148 | |
149 // Only friend classes are allowed to use this API. | |
150 v8::Local<v8::Value> GetFromMainWorld(ScriptWrappable*); | |
151 | |
152 v8::Local<v8::Private> private_symbol_; | |
153 v8::Isolate* isolate_; | |
154 }; | |
155 | |
156 static std::unique_ptr<V8PrivateProperty> Create() { | |
157 return WTF::WrapUnique(new V8PrivateProperty()); | |
158 } | |
159 | |
160 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ | |
161 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(/* // NOLINT */ \ | |
162 InterfaceName, KeyName)( \ | |
163 v8::Isolate * isolate) { \ | |
164 V8PrivateProperty* private_prop = \ | |
165 V8PerIsolateData::From(isolate)->PrivateProperty(); \ | |
166 v8::Eternal<v8::Private>& property_handle = \ | |
167 private_prop->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName); \ | |
168 if (UNLIKELY(property_handle.IsEmpty())) { \ | |
169 property_handle.Set( \ | |
170 isolate, CreateV8Private(isolate, V8_PRIVATE_PROPERTY_SYMBOL_STRING( \ | |
171 InterfaceName, KeyName))); \ | |
172 } \ | |
173 return Symbol(isolate, property_handle.Get(isolate)); \ | |
174 } | |
175 | |
176 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER) | |
177 #undef V8_PRIVATE_PROPERTY_DEFINE_GETTER | |
178 | |
179 // TODO(peria): Do not use this specialized hack. See a TODO comment | |
180 // on m_symbolWindowDocumentCachedAccessor. | |
181 static Symbol GetWindowDocumentCachedAccessor(v8::Isolate* isolate) { | |
182 V8PrivateProperty* private_prop = | |
183 V8PerIsolateData::From(isolate)->PrivateProperty(); | |
184 if (UNLIKELY( | |
185 private_prop->symbol_window_document_cached_accessor_.IsEmpty())) { | |
186 private_prop->symbol_window_document_cached_accessor_.Set( | |
187 isolate, CreateCachedV8Private( | |
188 isolate, V8_PRIVATE_PROPERTY_SYMBOL_STRING( | |
189 "Window", "DocumentCachedAccessor"))); | |
190 } | |
191 return Symbol( | |
192 isolate, private_prop->symbol_window_document_cached_accessor_.NewLocal( | |
193 isolate)); | |
194 } | |
195 | |
196 static Symbol GetSymbol(v8::Isolate* isolate, const char* symbol) { | |
197 return Symbol(isolate, CreateCachedV8Private(isolate, symbol)); | |
198 } | |
199 | |
200 private: | |
201 V8PrivateProperty() {} | |
202 | |
203 static v8::Local<v8::Private> CreateV8Private(v8::Isolate*, | |
204 const char* symbol); | |
205 // TODO(peria): Remove this method. We should not use v8::Private::ForApi(). | |
206 static v8::Local<v8::Private> CreateCachedV8Private(v8::Isolate*, | |
207 const char* symbol); | |
208 | |
209 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ | |
210 v8::Eternal<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \ | |
211 InterfaceName, KeyName); // NOLINT(readability/naming/underscores) | |
212 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) | |
213 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER | |
214 | |
215 // TODO(peria): Do not use this specialized hack for | |
216 // Window#DocumentCachedAccessor. This is required to put v8::Private key in | |
217 // a snapshot, and it cannot be a v8::Eternal<> due to V8 serializer's | |
218 // requirement. | |
219 ScopedPersistent<v8::Private> symbol_window_document_cached_accessor_; | |
220 }; | |
221 | |
222 } // namespace blink | |
223 | |
224 #endif // V8PrivateProperty_h | |
OLD | NEW |