OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef V8PrivateProperty_h | |
6 #define V8PrivateProperty_h | |
7 | |
8 #include "bindings/core/v8/ScopedPersistent.h" | |
9 #include "bindings/core/v8/ScriptPromiseProperties.h" | |
10 #include "bindings/core/v8/V8BindingMacros.h" | |
11 #include "bindings/core/v8/V8PerIsolateData.h" | |
12 #include "core/CoreExport.h" | |
13 #include "wtf/Allocator.h" | |
14 #include <memory> | |
15 #include <v8.h> | |
16 | |
17 namespace blink { | |
18 | |
19 class ScriptState; | |
20 class ScriptWrappable; | |
21 | |
22 // Apply |X| for each pair of (InterfaceName, PrivateKeyName). | |
23 #define V8_PRIVATE_PROPERTY_FOR_EACH(X) \ | |
24 X(MessageEvent, CachedData) | |
25 | |
26 // The getter's name for a private property. | |
27 #define V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, PrivateKeyName) \ | |
28 get##InterfaceName##PrivateKeyName | |
29 | |
30 // The member variable's name for a private property. | |
31 #define V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, PrivateKeyName) \ | |
32 m_symbol##InterfaceName##PrivateKeyName | |
33 | |
34 // The string used to create a private symbol. Must be unique per V8 instance. | |
35 #define V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, PrivateKeyName) \ | |
36 #InterfaceName"#"#PrivateKeyName // NOLINT(whitespace/indent) | |
37 | |
38 // Provides access to V8's private properties. | |
39 // | |
40 // Usage 1) Fast path to use a pre-registered symbol. | |
41 // auto private = V8PrivateProperty::getMessageEventCachedData(isolate); | |
42 // v8::Local<v8::Context> context = ...; | |
43 // v8::Local<v8::Object> object = ...; | |
44 // v8::Local<v8::Value> value = private.get(context, object); | |
45 // value = ...; | |
46 // private.set(context, object, value); | |
47 // | |
48 // Usage 2) Slow path to create a global private symbol. | |
49 // const char symbolName[] = "Interface#PrivateKeyName"; | |
50 // auto private = V8PrivateProperty::createSymbol(isolate, symbolName, sizeof symbolName); | |
51 // ... | |
52 class CORE_EXPORT V8PrivateProperty { | |
53 USING_FAST_MALLOC(V8PrivateProperty); | |
54 WTF_MAKE_NONCOPYABLE(V8PrivateProperty); | |
55 public: | |
56 // Provides fast access to V8's private properties. | |
57 // | |
58 // Retrieving/creating a global private symbol from a string is very | |
59 // expensive compared to get or set a private property. This class | |
60 // provides a way to cache a private symbol and re-use it. | |
61 class CORE_EXPORT Symbol { | |
62 STACK_ALLOCATED(); | |
63 public: | |
64 v8::Local<v8::Value> get(v8::Local<v8::Context> context, v8::Local<v8::O bject> object) | |
65 { | |
66 v8::Local<v8::Value> value; | |
67 if (object->GetPrivate(context, m_privateSymbol).ToLocal(&value)) | |
68 return value; | |
69 return v8::Local<v8::Value>(); | |
70 } | |
71 v8::Local<v8::Value> get(v8::Local<v8::Object> object) | |
72 { | |
73 return get(m_isolate->GetCurrentContext(), object); | |
74 } | |
75 | |
76 bool set(v8::Local<v8::Context> context, v8::Local<v8::Object> object, v 8::Local<v8::Value> value) | |
77 { | |
78 return v8CallBoolean(object->SetPrivate(context, m_privateSymbol, va lue)); | |
79 } | |
80 bool set(v8::Local<v8::Object> object, v8::Local<v8::Value> value) | |
81 { | |
82 return set(m_isolate->GetCurrentContext(), object, value); | |
83 } | |
84 | |
85 v8::Local<v8::Value> getFromMainWorld(ScriptState*, ScriptWrappable*); | |
haraken
2016/05/24 17:33:53
Add a comment that this method should not be used
Yuki
2016/05/26 10:52:11
Made this one a private: member.
| |
86 | |
87 private: | |
88 friend class V8PrivateProperty; | |
89 | |
90 Symbol(v8::Isolate* isolate, v8::Local<v8::Private> privateSymbol) | |
91 : m_isolate(isolate) | |
92 , m_privateSymbol(privateSymbol) { } | |
93 | |
94 v8::Isolate* m_isolate; | |
haraken
2016/05/24 17:33:53
Instead of caching m_isolate, can we cache v8::Con
Yuki
2016/05/26 10:52:11
v8::Private itself is NOT associated with any v8::
| |
95 v8::Local<v8::Private> m_privateSymbol; | |
96 }; | |
97 | |
98 static std::unique_ptr<V8PrivateProperty> create() | |
99 { | |
100 return std::unique_ptr<V8PrivateProperty>(new V8PrivateProperty()); | |
haraken
2016/05/24 17:33:53
base::wrapUnique ?
Yuki
2016/05/26 10:52:11
Done.
| |
101 } | |
102 | |
103 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ | |
104 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, KeyName)(v8::Is olate* isolate) /* NOLINT(readability/naming/underscores) */ \ | |
105 { \ | |
106 V8PrivateProperty* privateProp = V8PerIsolateData::from(isolate)->privat eProperty(); \ | |
107 if (privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) .isEmpty()) { \ | |
haraken
2016/05/24 17:33:53
Add UNLIKELY?
Yuki
2016/05/26 10:52:11
Done.
| |
108 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) .set( \ | |
109 isolate, \ | |
110 createV8Private( \ | |
111 isolate, \ | |
112 V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName), \ | |
113 sizeof V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyN ame))); /* NOLINT(readability/naming/underscores) */ \ | |
114 } \ | |
115 return Symbol(isolate, privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(Inte rfaceName, KeyName).newLocal(isolate)); \ | |
116 } | |
117 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER) | |
118 #undef V8_PRIVATE_PROPERTY_DEFINE_GETTER | |
119 | |
120 static Symbol createSymbol(v8::Isolate* isolate, const char* symbol, size_t length) | |
121 { | |
122 return Symbol(isolate, createV8Private(isolate, symbol, length)); | |
123 } | |
124 | |
125 private: | |
126 V8PrivateProperty() { } | |
127 | |
128 static v8::Local<v8::Private> createV8Private(v8::Isolate*, const char* symb ol, size_t length); | |
129 | |
130 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ | |
131 ScopedPersistent<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName); // NOLINT(readability/naming/underscores) | |
132 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) | |
133 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER | |
134 }; | |
135 | |
136 } // namespace blink | |
137 | |
138 #endif // V8PrivateProperty_h | |
OLD | NEW |