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 <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 { |
|
haraken
2017/03/30 13:53:34
Not related to this CL, I might want to remove thi
Yuki
2017/03/30 14:18:14
I'd like to do the opposite. As V8 never returns
haraken
2017/03/30 14:21:37
I'm fine as long as the two methods are unified in
| |
| 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 |
| 102 // Returns the value of the private property if set, or an empty handle. | 99 // Returns the value of the private property if set, or an empty handle. |
| 103 v8::Local<v8::Value> get(v8::Local<v8::Context> context, | 100 v8::Local<v8::Value> getOrEmpty(v8::Local<v8::Object> object) const { |
| 104 v8::Local<v8::Object> object) const { | 101 if (hasValue(object)) |
| 105 if (!v8CallBoolean(object->HasPrivate(context, m_privateSymbol))) | 102 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>(); | 103 return v8::Local<v8::Value>(); |
| 111 } | 104 } |
| 112 | 105 |
| 113 bool set(v8::Local<v8::Context> context, | 106 bool set(v8::Local<v8::Object> object, v8::Local<v8::Value> value) const { |
| 114 v8::Local<v8::Object> object, | 107 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 } | 108 } |
| 118 | 109 |
| 119 v8::Local<v8::Private> getPrivate() { return m_privateSymbol; } | 110 v8::Local<v8::Private> getPrivate() const { return m_privateSymbol; } |
| 120 | 111 |
| 121 private: | 112 private: |
| 122 friend class V8PrivateProperty; | 113 friend class V8PrivateProperty; |
| 123 // The following classes are exceptionally allowed to call to | 114 // The following classes are exceptionally allowed to call to |
| 124 // getFromMainWorld. | 115 // getFromMainWorld. |
| 125 friend class V8CustomEvent; | 116 friend class V8CustomEvent; |
| 126 friend class V8ServiceWorkerMessageEventInternal; | 117 friend class V8ServiceWorkerMessageEventInternal; |
| 127 | 118 |
| 128 explicit Symbol(v8::Local<v8::Private> privateSymbol) | 119 Symbol(v8::Isolate* isolate, v8::Local<v8::Private> privateSymbol) |
| 129 : m_privateSymbol(privateSymbol) {} | 120 : m_privateSymbol(privateSymbol), m_isolate(isolate) {} |
| 121 | |
| 122 v8::Local<v8::Context> context() const { | |
|
haraken
2017/03/30 13:53:34
I'd prefer removing this method to prevent someone
Yuki
2017/03/30 14:18:14
This method is introduced in order to prevent some
haraken
2017/03/30 14:21:37
OK, then I'd like to rename it to currentContext()
Yuki
2017/03/30 15:02:43
context() is a short and good name in this case, I
peria
2017/03/30 15:53:24
Thank you Shiino-san for the explanation.
I'll put
| |
| 123 return m_isolate->GetCurrentContext(); | |
| 124 } | |
| 130 | 125 |
| 131 // Only friend classes are allowed to use this API. | 126 // Only friend classes are allowed to use this API. |
| 132 v8::Local<v8::Value> getFromMainWorld(ScriptState*, ScriptWrappable*); | 127 v8::Local<v8::Value> getFromMainWorld(ScriptWrappable*); |
| 133 | 128 |
| 134 v8::Local<v8::Private> m_privateSymbol; | 129 v8::Local<v8::Private> m_privateSymbol; |
| 130 v8::Isolate* m_isolate; | |
| 135 }; | 131 }; |
| 136 | 132 |
| 137 static std::unique_ptr<V8PrivateProperty> create() { | 133 static std::unique_ptr<V8PrivateProperty> create() { |
| 138 return WTF::wrapUnique(new V8PrivateProperty()); | 134 return WTF::wrapUnique(new V8PrivateProperty()); |
| 139 } | 135 } |
| 140 | 136 |
| 141 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ | 137 #define V8_PRIVATE_PROPERTY_DEFINE_GETTER(InterfaceName, KeyName) \ |
| 142 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(InterfaceName, KeyName)( \ | 138 static Symbol V8_PRIVATE_PROPERTY_GETTER_NAME(/* // NOLINT */ \ |
| 143 v8::Isolate * isolate) /* NOLINT(readability/naming/underscores) */ \ | 139 InterfaceName, KeyName)( \ |
| 144 { \ | 140 v8::Isolate * isolate) { \ |
| 145 V8PrivateProperty* privateProp = \ | 141 V8PrivateProperty* privateProp = \ |
| 146 V8PerIsolateData::from(isolate)->privateProperty(); \ | 142 V8PerIsolateData::from(isolate)->privateProperty(); \ |
| 147 if (UNLIKELY(privateProp \ | 143 v8::Eternal<v8::Private>& propertyHandle = \ |
| 148 ->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ | 144 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName); \ |
| 149 .IsEmpty())) { \ | 145 if (UNLIKELY(propertyHandle.IsEmpty())) { \ |
| 150 privateProp->V8_PRIVATE_PROPERTY_MEMBER_NAME(InterfaceName, KeyName) \ | 146 static constexpr char kSymbolName[] = \ |
| 151 .Set( \ | 147 V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName); \ |
| 152 isolate, \ | 148 propertyHandle.Set(isolate, createV8Private(isolate, kSymbolName, \ |
| 153 createV8Private( \ | 149 sizeof(kSymbolName))); \ |
| 154 isolate, \ | 150 } \ |
| 155 V8_PRIVATE_PROPERTY_SYMBOL_STRING(InterfaceName, KeyName), \ | 151 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 } | 152 } |
| 153 | |
| 164 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER) | 154 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DEFINE_GETTER) |
| 165 #undef V8_PRIVATE_PROPERTY_DEFINE_GETTER | 155 #undef V8_PRIVATE_PROPERTY_DEFINE_GETTER |
| 166 | 156 |
| 167 private: | 157 private: |
| 168 V8PrivateProperty() {} | 158 V8PrivateProperty() {} |
| 169 | 159 |
| 170 static v8::Local<v8::Private> createV8Private(v8::Isolate*, | 160 static v8::Local<v8::Private> createV8Private(v8::Isolate*, |
| 171 const char* symbol, | 161 const char* symbol, |
| 172 size_t length); | 162 size_t length); |
| 173 | 163 |
| 174 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ | 164 #define V8_PRIVATE_PROPERTY_DECLARE_MEMBER(InterfaceName, KeyName) \ |
| 175 v8::Eternal<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \ | 165 v8::Eternal<v8::Private> V8_PRIVATE_PROPERTY_MEMBER_NAME( \ |
| 176 InterfaceName, KeyName); // NOLINT(readability/naming/underscores) | 166 InterfaceName, KeyName); // NOLINT(readability/naming/underscores) |
| 177 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) | 167 V8_PRIVATE_PROPERTY_FOR_EACH(V8_PRIVATE_PROPERTY_DECLARE_MEMBER) |
| 178 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER | 168 #undef V8_PRIVATE_PROPERTY_DECLARE_MEMBER |
| 179 }; | 169 }; |
| 180 | 170 |
| 181 } // namespace blink | 171 } // namespace blink |
| 182 | 172 |
| 183 #endif // V8PrivateProperty_h | 173 #endif // V8PrivateProperty_h |
| OLD | NEW |