| 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 <memory> | 8 #include <memory> |
| 9 | 9 |
| 10 #include "bindings/core/v8/ScriptPromiseProperties.h" | 10 #include "bindings/core/v8/ScriptPromiseProperties.h" |
| 11 #include "bindings/core/v8/V8BindingMacros.h" | 11 #include "bindings/core/v8/V8BindingMacros.h" |
| 12 #include "bindings/core/v8/V8PerIsolateData.h" | 12 #include "bindings/core/v8/V8PerIsolateData.h" |
| 13 #include "core/CoreExport.h" | 13 #include "core/CoreExport.h" |
| 14 #include "v8/include/v8.h" | 14 #include "v8/include/v8.h" |
| 15 #include "wtf/Allocator.h" | 15 #include "wtf/Allocator.h" |
| 16 #include "wtf/PtrUtil.h" | 16 #include "wtf/PtrUtil.h" |
| 17 | 17 |
| 18 namespace blink { | 18 namespace blink { |
| 19 | 19 |
| 20 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, AdoptedCallback) \ | 25 X(CustomElement, AdoptedCallback) \ |
| 27 X(CustomElement, AttributeChangedCallback) \ | 26 X(CustomElement, AttributeChangedCallback) \ |
| 28 X(CustomElement, ConnectedCallback) \ | 27 X(CustomElement, ConnectedCallback) \ |
| 29 X(CustomElement, DisconnectedCallback) \ | 28 X(CustomElement, DisconnectedCallback) \ |
| 30 X(CustomElement, Document) \ | 29 X(CustomElement, Document) \ |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 public: | 80 public: |
| 82 // Provides fast access to V8's private properties. | 81 // Provides fast access to V8's private properties. |
| 83 // | 82 // |
| 84 // Retrieving/creating a global private symbol from a string is very | 83 // Retrieving/creating a global private symbol from a string is very |
| 85 // expensive compared to get or set a private property. This class | 84 // expensive compared to get or set a private property. This class |
| 86 // provides a way to cache a private symbol and re-use it. | 85 // provides a way to cache a private symbol and re-use it. |
| 87 class CORE_EXPORT Symbol { | 86 class CORE_EXPORT Symbol { |
| 88 STACK_ALLOCATED(); | 87 STACK_ALLOCATED(); |
| 89 | 88 |
| 90 public: | 89 public: |
| 91 bool hasValue(v8::Local<v8::Context> context, | 90 bool hasValue(v8::Local<v8::Object> object) const { |
| 92 v8::Local<v8::Object> object) const { | 91 return object->HasPrivate(context(), m_privateSymbol).ToChecked(); |
| 93 return v8CallBoolean(object->HasPrivate(context, m_privateSymbol)); | |
| 94 } | 92 } |
| 95 | 93 |
| 96 // Returns the value of the private property if set, or undefined. | 94 // Returns the value of the private property if set, or undefined. |
| 97 v8::Local<v8::Value> getOrUndefined(v8::Local<v8::Context> context, | 95 v8::Local<v8::Value> getOrUndefined(v8::Local<v8::Object> object) const { |
| 98 v8::Local<v8::Object> object) const { | 96 return object->GetPrivate(context(), m_privateSymbol).ToLocalChecked(); |
| 99 return object->GetPrivate(context, m_privateSymbol).ToLocalChecked(); | |
| 100 } | 97 } |
| 101 | 98 |
| 99 // TODO(peria): Remove this method, and use getOrUndefined() instead. |
| 102 // Returns the value of the private property if set, or an empty handle. | 100 // Returns the value of the private property if set, or an empty handle. |
| 103 v8::Local<v8::Value> get(v8::Local<v8::Context> context, | 101 v8::Local<v8::Value> getOrEmpty(v8::Local<v8::Object> object) const { |
| 104 v8::Local<v8::Object> object) const { | 102 if (hasValue(object)) |
| 105 if (!v8CallBoolean(object->HasPrivate(context, m_privateSymbol))) | 103 return getOrUndefined(object); |
| 106 return v8::Local<v8::Value>(); | |
| 107 v8::Local<v8::Value> value; | |
| 108 if (v8Call(object->GetPrivate(context, m_privateSymbol), value)) | |
| 109 return value; | |
| 110 return v8::Local<v8::Value>(); | 104 return v8::Local<v8::Value>(); |
| 111 } | 105 } |
| 112 | 106 |
| 113 bool set(v8::Local<v8::Context> context, | 107 bool set(v8::Local<v8::Object> object, v8::Local<v8::Value> value) const { |
| 114 v8::Local<v8::Object> object, | 108 return object->SetPrivate(context(), m_privateSymbol, value).ToChecked(); |
| 115 v8::Local<v8::Value> value) const { | |
| 116 return v8CallBoolean(object->SetPrivate(context, m_privateSymbol, value)); | |
| 117 } | 109 } |
| 118 | 110 |
| 119 v8::Local<v8::Private> getPrivate() { return m_privateSymbol; } | 111 v8::Local<v8::Private> getPrivate() const { return m_privateSymbol; } |
| 120 | 112 |
| 121 private: | 113 private: |
| 122 friend class V8PrivateProperty; | 114 friend class V8PrivateProperty; |
| 123 // The following classes are exceptionally allowed to call to | 115 // The following classes are exceptionally allowed to call to |
| 124 // getFromMainWorld. | 116 // getFromMainWorld. |
| 125 friend class V8CustomEvent; | 117 friend class V8CustomEvent; |
| 126 friend class V8ServiceWorkerMessageEventInternal; | 118 friend class V8ServiceWorkerMessageEventInternal; |
| 127 | 119 |
| 128 explicit Symbol(v8::Local<v8::Private> privateSymbol) | 120 Symbol(v8::Isolate* isolate, v8::Local<v8::Private> privateSymbol) |
| 129 : m_privateSymbol(privateSymbol) {} | 121 : m_privateSymbol(privateSymbol), m_isolate(isolate) {} |
| 122 |
| 123 // To get/set private property, we should use the current context. |
| 124 v8::Local<v8::Context> context() const { |
| 125 return m_isolate->GetCurrentContext(); |
| 126 } |
| 130 | 127 |
| 131 // Only friend classes are allowed to use this API. | 128 // Only friend classes are allowed to use this API. |
| 132 v8::Local<v8::Value> getFromMainWorld(ScriptState*, ScriptWrappable*); | 129 v8::Local<v8::Value> getFromMainWorld(ScriptWrappable*); |
| 133 | 130 |
| 134 v8::Local<v8::Private> m_privateSymbol; | 131 v8::Local<v8::Private> m_privateSymbol; |
| 132 v8::Isolate* m_isolate; |
| 135 }; | 133 }; |
| 136 | 134 |
| 137 static std::unique_ptr<V8PrivateProperty> create() { | 135 static std::unique_ptr<V8PrivateProperty> create() { |
| 138 return WTF::wrapUnique(new V8PrivateProperty()); | 136 return WTF::wrapUnique(new V8PrivateProperty()); |
| 139 } | 137 } |
| 140 | 138 |
| 141 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ | 139 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ |
| 142 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, KeyName)( \ | 140 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(/* // NOLINT */ \ |
| 143 v8::Isolate * isolate) /* NOLINT(readability/naming/underscores) */ \ | 141 InterfaceName, KeyName)( \ |
| 144 { \ | 142 v8::Isolate * isolate) { \ |
| 145 V8PrivateProperty* privateProp = \ | 143 V8PrivateProperty* privateProp = \ |
| 146 V8PerIsolateData::from(isolate)->privateProperty(); \ | 144 V8PerIsolateData::from(isolate)->privateProperty(); \ |
| 147 if (UNLIKELY(privateProp \ | 145 v8::Eternal<v8::Private>& propertyHandle = \ |
| 148 ->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ | 146 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName); \ |
| 149 .IsEmpty())) { \ | 147 if (UNLIKELY(propertyHandle.IsEmpty())) { \ |
| 150 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ | 148 static constexpr char kSymbolName[] = \ |
| 151 .Set( \ | 149 V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName); \ |
| 152 isolate, \ | 150 propertyHandle.Set(isolate, createV8Private(isolate, kSymbolName, \ |
| 153 createV8Private( \ | 151 sizeof(kSymbolName))); \ |
| 154 isolate, \ | 152 } \ |
| 155 V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName), \ | 153 return Symbol(isolate, propertyHandle.Get(isolate)); \ |
| 156 sizeof V8_PRIVATE_PROPERTY_SYMBOL_STRING( \ | |
| 157 InterfaceName, \ | |
| 158 KeyName))); /* NOLINT(readability/naming/underscores) */ \ | |
| 159 } \ | |
| 160 return Symbol( \ | |
| 161 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ | |
| 162 .Get(isolate)); \ | |
| 163 } | 154 } |
| 155 |
| 164 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER) | 156 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER) |
| 165 #undef V8_PRIVATE_PROPERTY_DEFINE_GETTER | 157 #undef V8_PRIVATE_PROPERTY_DEFINE_GETTER |
| 166 | 158 |
| 167 private: | 159 private: |
| 168 V8PrivateProperty() {} | 160 V8PrivateProperty() {} |
| 169 | 161 |
| 170 static v8::Local<v8::Private> createV8Private(v8::Isolate*, | 162 static v8::Local<v8::Private> createV8Private(v8::Isolate*, |
| 171 const char* symbol, | 163 const char* symbol, |
| 172 size_t length); | 164 size_t length); |
| 173 | 165 |
| 174 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ | 166 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ |
| 175 v8::Eternal<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \ | 167 v8::Eternal<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \ |
| 176 InterfaceName, KeyName); // NOLINT(readability/naming/underscores) | 168 InterfaceName, KeyName); // NOLINT(readability/naming/underscores) |
| 177 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) | 169 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) |
| 178 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER | 170 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER |
| 179 }; | 171 }; |
| 180 | 172 |
| 181 } // namespace blink | 173 } // namespace blink |
| 182 | 174 |
| 183 #endif // V8PrivateProperty_h | 175 #endif // V8PrivateProperty_h |
| OLD | NEW |