Chromium Code Reviews| 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 #ifndef V8PrivateProperty_h |
| 6 #define V8PrivateProperty_h | 6 #define V8PrivateProperty_h |
| 7 | 7 |
| 8 #include <v8.h> | |
| 9 #include <memory> | |
| 8 #include "bindings/core/v8/ScopedPersistent.h" | 10 #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/V8BindingMacros.h" |
| 11 #include "bindings/core/v8/V8PerIsolateData.h" | 12 #include "bindings/core/v8/V8PerIsolateData.h" |
| 12 #include "core/CoreExport.h" | 13 #include "core/CoreExport.h" |
| 13 #include "wtf/Allocator.h" | 14 #include "wtf/Allocator.h" |
| 14 #include "wtf/PtrUtil.h" | 15 #include "wtf/PtrUtil.h" |
| 15 #include <memory> | |
| 16 #include <v8.h> | |
| 17 | 16 |
| 18 namespace blink { | 17 namespace blink { |
| 19 | 18 |
| 20 class ScriptState; | 19 class ScriptState; |
| 21 class ScriptWrappable; | 20 class ScriptWrappable; |
| 22 | 21 |
| 23 // Apply |X| for each pair of (InterfaceName, PrivateKeyName). | 22 // Apply |X| for each pair of (InterfaceName, PrivateKeyName). |
| 24 #define V8_PRIVATE_PROPERTY_FOR_EACH(X) \ | 23 #define V8_PRIVATE_PROPERTY_FOR_EACH(X) \ |
| 25 X(CustomEvent, Detail) \ | 24 X(CustomEvent, Detail) \ |
| 26 X(CustomElement, ConnectedCallback) \ | 25 X(CustomElement, ConnectedCallback) \ |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 // v8::Local<v8::Object> object = ...; | 59 // v8::Local<v8::Object> object = ...; |
| 61 // v8::Local<v8::Value> value = private.get(context, object); | 60 // v8::Local<v8::Value> value = private.get(context, object); |
| 62 // value = ...; | 61 // value = ...; |
| 63 // private.set(context, object, value); | 62 // private.set(context, object, value); |
| 64 // | 63 // |
| 65 // Usage 2) Slow path to create a global private symbol. | 64 // Usage 2) Slow path to create a global private symbol. |
| 66 // const char symbolName[] = "Interface#PrivateKeyName"; | 65 // const char symbolName[] = "Interface#PrivateKeyName"; |
| 67 // auto private = V8PrivateProperty::createSymbol(isolate, symbolName, | 66 // auto private = V8PrivateProperty::createSymbol(isolate, symbolName, |
| 68 // sizeof symbolName); | 67 // sizeof symbolName); |
| 69 // ... | 68 // ... |
| 70 class CORE_EXPORT V8PrivateProperty { | 69 class CORE_EXPORT V8PrivateProperty : public V8PerIsolateData::Data { |
| 71 USING_FAST_MALLOC(V8PrivateProperty); | 70 USING_FAST_MALLOC(V8PrivateProperty); |
| 72 WTF_MAKE_NONCOPYABLE(V8PrivateProperty); | 71 WTF_MAKE_NONCOPYABLE(V8PrivateProperty); |
| 73 | 72 |
| 74 public: | 73 public: |
| 75 // Provides fast access to V8's private properties. | 74 // Provides fast access to V8's private properties. |
| 76 // | 75 // |
| 77 // Retrieving/creating a global private symbol from a string is very | 76 // Retrieving/creating a global private symbol from a string is very |
| 78 // expensive compared to get or set a private property. This class | 77 // expensive compared to get or set a private property. This class |
| 79 // provides a way to cache a private symbol and re-use it. | 78 // provides a way to cache a private symbol and re-use it. |
| 80 class CORE_EXPORT Symbol { | 79 class CORE_EXPORT Symbol { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 120 | 119 |
| 121 explicit Symbol(v8::Local<v8::Private> privateSymbol) | 120 explicit Symbol(v8::Local<v8::Private> privateSymbol) |
| 122 : m_privateSymbol(privateSymbol) {} | 121 : m_privateSymbol(privateSymbol) {} |
| 123 | 122 |
| 124 // Only friend classes are allowed to use this API. | 123 // Only friend classes are allowed to use this API. |
| 125 v8::Local<v8::Value> getFromMainWorld(ScriptState*, ScriptWrappable*); | 124 v8::Local<v8::Value> getFromMainWorld(ScriptState*, ScriptWrappable*); |
| 126 | 125 |
| 127 v8::Local<v8::Private> m_privateSymbol; | 126 v8::Local<v8::Private> m_privateSymbol; |
| 128 }; | 127 }; |
| 129 | 128 |
| 130 static std::unique_ptr<V8PrivateProperty> create() { | 129 static void initialize(v8::Isolate* isolate) { |
| 131 return WTF::wrapUnique(new V8PrivateProperty()); | 130 V8PerIsolateData::from(isolate)->setPrivateProperty( |
| 131 new V8PrivateProperty()); | |
| 132 } | |
| 133 static V8PrivateProperty* from(v8::Isolate* isolate) { | |
|
Yuki
2017/02/15 10:00:40
No need to expose this method as public:.
Should b
| |
| 134 return static_cast<V8PrivateProperty*>( | |
| 135 V8PerIsolateData::from(isolate)->privateProperty()); | |
| 132 } | 136 } |
| 133 | 137 |
| 134 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ | 138 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ |
| 135 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, KeyName)( \ | 139 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, KeyName)( \ |
| 136 v8::Isolate * isolate) /* NOLINT(readability/naming/underscores) */ \ | 140 v8::Isolate * isolate) /* NOLINT(readability/naming/underscores) */ \ |
| 137 { \ | 141 { \ |
| 138 V8PrivateProperty* privateProp = \ | 142 V8PrivateProperty* privateProp = from(isolate); \ |
| 139 V8PerIsolateData::from(isolate)->privateProperty(); \ | |
| 140 if (UNLIKELY(privateProp \ | 143 if (UNLIKELY(privateProp \ |
| 141 ->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ | 144 ->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ |
| 142 .isEmpty())) { \ | 145 .isEmpty())) { \ |
| 143 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ | 146 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ |
| 144 .set( \ | 147 .set( \ |
| 145 isolate, \ | 148 isolate, \ |
| 146 createV8Private( \ | 149 createV8Private( \ |
| 147 isolate, \ | 150 isolate, \ |
| 148 V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName), \ | 151 V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName), \ |
| 149 sizeof V8_PRIVATE_PROPERTY_SYMBOL_STRING( \ | 152 sizeof V8_PRIVATE_PROPERTY_SYMBOL_STRING( \ |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 173 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ | 176 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ |
| 174 ScopedPersistent<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \ | 177 ScopedPersistent<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \ |
| 175 InterfaceName, KeyName); // NOLINT(readability/naming/underscores) | 178 InterfaceName, KeyName); // NOLINT(readability/naming/underscores) |
| 176 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) | 179 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) |
| 177 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER | 180 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER |
| 178 }; | 181 }; |
| 179 | 182 |
| 180 } // namespace blink | 183 } // namespace blink |
| 181 | 184 |
| 182 #endif // V8PrivateProperty_h | 185 #endif // V8PrivateProperty_h |
| OLD | NEW |