Chromium Code Reviews| Index: third_party/WebKit/Source/bindings/core/v8/V8PrivateProperty.h |
| diff --git a/third_party/WebKit/Source/bindings/core/v8/V8PrivateProperty.h b/third_party/WebKit/Source/bindings/core/v8/V8PrivateProperty.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4804786070e82be4c898a27491927800085136ca |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/bindings/core/v8/V8PrivateProperty.h |
| @@ -0,0 +1,138 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef V8PrivateProperty_h |
| +#define V8PrivateProperty_h |
| + |
| +#include "bindings/core/v8/ScopedPersistent.h" |
| +#include "bindings/core/v8/ScriptPromiseProperties.h" |
| +#include "bindings/core/v8/V8BindingMacros.h" |
| +#include "bindings/core/v8/V8PerIsolateData.h" |
| +#include "core/CoreExport.h" |
| +#include "wtf/Allocator.h" |
| +#include <memory> |
| +#include <v8.h> |
| + |
| +namespace blink { |
| + |
| +class ScriptState; |
| +class ScriptWrappable; |
| + |
| +// Apply |X| for each pair of (InterfaceName, PrivateKeyName). |
| +#define V8_PRIVATE_PROPERTY_FOR_EACH(X) \ |
| + X(MessageEvent, CachedData) |
| + |
| +// The getter's name for a private property. |
| +#define V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, PrivateKeyName) \ |
| + get##InterfaceName##PrivateKeyName |
| + |
| +// The member variable's name for a private property. |
| +#define V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, PrivateKeyName) \ |
| + m_symbol##InterfaceName##PrivateKeyName |
| + |
| +// The string used to create a private symbol. Must be unique per V8 instance. |
| +#define V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, PrivateKeyName) \ |
| + #InterfaceName"#"#PrivateKeyName // NOLINT(whitespace/indent) |
| + |
| +// Provides access to V8's private properties. |
| +// |
| +// Usage 1) Fast path to use a pre-registered symbol. |
| +// auto private = V8PrivateProperty::getMessageEventCachedData(isolate); |
| +// v8::Local<v8::Context> context = ...; |
| +// v8::Local<v8::Object> object = ...; |
| +// v8::Local<v8::Value> value = private.get(context, object); |
| +// value = ...; |
| +// private.set(context, object, value); |
| +// |
| +// Usage 2) Slow path to create a global private symbol. |
| +// const char symbolName[] = "Interface#PrivateKeyName"; |
| +// auto private = V8PrivateProperty::createSymbol(isolate, symbolName, sizeof symbolName); |
| +// ... |
| +class CORE_EXPORT V8PrivateProperty { |
| + USING_FAST_MALLOC(V8PrivateProperty); |
| + WTF_MAKE_NONCOPYABLE(V8PrivateProperty); |
| +public: |
| + // Provides fast access to V8's private properties. |
| + // |
| + // Retrieving/creating a global private symbol from a string is very |
| + // expensive compared to get or set a private property. This class |
| + // provides a way to cache a private symbol and re-use it. |
| + class CORE_EXPORT Symbol { |
| + STACK_ALLOCATED(); |
| + public: |
| + v8::Local<v8::Value> get(v8::Local<v8::Context> context, v8::Local<v8::Object> object) |
| + { |
| + v8::Local<v8::Value> value; |
| + if (object->GetPrivate(context, m_privateSymbol).ToLocal(&value)) |
| + return value; |
| + return v8::Local<v8::Value>(); |
| + } |
| + v8::Local<v8::Value> get(v8::Local<v8::Object> object) |
| + { |
| + return get(m_isolate->GetCurrentContext(), object); |
| + } |
| + |
| + bool set(v8::Local<v8::Context> context, v8::Local<v8::Object> object, v8::Local<v8::Value> value) |
| + { |
| + return v8CallBoolean(object->SetPrivate(context, m_privateSymbol, value)); |
| + } |
| + bool set(v8::Local<v8::Object> object, v8::Local<v8::Value> value) |
| + { |
| + return set(m_isolate->GetCurrentContext(), object, value); |
| + } |
| + |
| + 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.
|
| + |
| + private: |
| + friend class V8PrivateProperty; |
| + |
| + Symbol(v8::Isolate* isolate, v8::Local<v8::Private> privateSymbol) |
| + : m_isolate(isolate) |
| + , m_privateSymbol(privateSymbol) { } |
| + |
| + 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::
|
| + v8::Local<v8::Private> m_privateSymbol; |
| + }; |
| + |
| + static std::unique_ptr<V8PrivateProperty> create() |
| + { |
| + return std::unique_ptr<V8PrivateProperty>(new V8PrivateProperty()); |
|
haraken
2016/05/24 17:33:53
base::wrapUnique ?
Yuki
2016/05/26 10:52:11
Done.
|
| + } |
| + |
| +#define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ |
| + static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, KeyName)(v8::Isolate* isolate) /* NOLINT(readability/naming/underscores) */ \ |
| + { \ |
| + V8PrivateProperty* privateProp = V8PerIsolateData::from(isolate)->privateProperty(); \ |
| + 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.
|
| + privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName).set( \ |
| + isolate, \ |
| + createV8Private( \ |
| + isolate, \ |
| + V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName), \ |
| + sizeof V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName))); /* NOLINT(readability/naming/underscores) */ \ |
| + } \ |
| + return Symbol(isolate, privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName).newLocal(isolate)); \ |
| + } |
| + V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER) |
| +#undef V8_PRIVATE_PROPERTY_DEFINE_GETTER |
| + |
| + static Symbol createSymbol(v8::Isolate* isolate, const char* symbol, size_t length) |
| + { |
| + return Symbol(isolate, createV8Private(isolate, symbol, length)); |
| + } |
| + |
| +private: |
| + V8PrivateProperty() { } |
| + |
| + static v8::Local<v8::Private> createV8Private(v8::Isolate*, const char* symbol, size_t length); |
| + |
| +#define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ |
| + ScopedPersistent<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName); // NOLINT(readability/naming/underscores) |
| + V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) |
| +#undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER |
| +}; |
| + |
| +} // namespace blink |
| + |
| +#endif // V8PrivateProperty_h |