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