| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | |
| 3 * | |
| 4 * Redistribution and use in source and binary forms, with or without | |
| 5 * modification, are permitted provided that the following conditions are | |
| 6 * met: | |
| 7 * | |
| 8 * * Redistributions of source code must retain the above copyright | |
| 9 * notice, this list of conditions and the following disclaimer. | |
| 10 * * Redistributions in binary form must reproduce the above | |
| 11 * copyright notice, this list of conditions and the following disclaimer | |
| 12 * in the documentation and/or other materials provided with the | |
| 13 * distribution. | |
| 14 * * Neither the name of Google Inc. nor the names of its | |
| 15 * contributors may be used to endorse or promote products derived from | |
| 16 * this software without specific prior written permission. | |
| 17 * | |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 29 */ | |
| 30 | |
| 31 #ifndef SKY_ENGINE_BINDINGS_CORE_V8_SCRIPTWRAPPABLE_H_ | |
| 32 #define SKY_ENGINE_BINDINGS_CORE_V8_SCRIPTWRAPPABLE_H_ | |
| 33 | |
| 34 #include "sky/engine/bindings/core/v8/WrapperTypeInfo.h" | |
| 35 #include "v8/include/v8.h" | |
| 36 | |
| 37 namespace blink { | |
| 38 | |
| 39 /** | |
| 40 * The base class of all wrappable objects. | |
| 41 * | |
| 42 * This class provides the internal pointer to be stored in the wrapper objects, | |
| 43 * and its conversions from / to the DOM instances. | |
| 44 * | |
| 45 * Note that this class must not have vtbl (any virtual function) or any member | |
| 46 * variable which increase the size of instances. Some of the classes sensitive | |
| 47 * to the size inherit from this class. So this class must be zero size. | |
| 48 */ | |
| 49 class ScriptWrappableBase { | |
| 50 public: | |
| 51 template<typename T> | |
| 52 T* toImpl() | |
| 53 { | |
| 54 // Check if T* is castable to ScriptWrappableBase*, which means T | |
| 55 // doesn't have two or more ScriptWrappableBase as superclasses. | |
| 56 // If T has two ScriptWrappableBase as superclasses, conversions | |
| 57 // from T* to ScriptWrappableBase* are ambiguous. | |
| 58 ASSERT(static_cast<ScriptWrappableBase*>(static_cast<T*>(this))); | |
| 59 // The internal pointers must be aligned to at least 4 byte alignment. | |
| 60 ASSERT((reinterpret_cast<intptr_t>(this) & 0x3) == 0); | |
| 61 return static_cast<T*>(this); | |
| 62 } | |
| 63 ScriptWrappableBase* toScriptWrappableBase() | |
| 64 { | |
| 65 // The internal pointers must be aligned to at least 4 byte alignment. | |
| 66 ASSERT((reinterpret_cast<intptr_t>(this) & 0x3) == 0); | |
| 67 return this; | |
| 68 } | |
| 69 | |
| 70 void assertWrapperSanity(v8::Local<v8::Object> object) | |
| 71 { | |
| 72 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(object.IsEmpty() | |
| 73 || object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectInde
x) == toScriptWrappableBase()); | |
| 74 } | |
| 75 }; | |
| 76 | |
| 77 /** | |
| 78 * ScriptWrappable wraps a V8 object and its WrapperTypeInfo. | |
| 79 * | |
| 80 * ScriptWrappable acts much like a v8::Persistent<> in that it keeps a | |
| 81 * V8 object alive. | |
| 82 * | |
| 83 * The state transitions are: | |
| 84 * - new: an empty ScriptWrappable. | |
| 85 * - setWrapper: install a v8::Persistent (or empty) | |
| 86 * - disposeWrapper (via setWeakCallback, triggered by V8 garbage collecter): | |
| 87 * remove v8::Persistent and become empty. | |
| 88 */ | |
| 89 class ScriptWrappable : public ScriptWrappableBase { | |
| 90 public: | |
| 91 ScriptWrappable() { } | |
| 92 | |
| 93 // Returns the WrapperTypeInfo of the instance. | |
| 94 // | |
| 95 // This method must be overridden by DEFINE_WRAPPERTYPEINFO macro. | |
| 96 virtual const WrapperTypeInfo* wrapperTypeInfo() const = 0; | |
| 97 | |
| 98 // Creates and returns a new wrapper object. | |
| 99 virtual v8::Handle<v8::Object> wrap(v8::Handle<v8::Object> creationContext,
v8::Isolate*); | |
| 100 | |
| 101 // Associates the instance with the existing wrapper. Returns |wrapper|. | |
| 102 virtual v8::Handle<v8::Object> associateWithWrapper(const WrapperTypeInfo*,
v8::Handle<v8::Object> wrapper, v8::Isolate*); | |
| 103 | |
| 104 void setWrapper(v8::Handle<v8::Object> wrapper, v8::Isolate* isolate, const
WrapperTypeInfo* wrapperTypeInfo) | |
| 105 { | |
| 106 ASSERT(!containsWrapper()); | |
| 107 if (!*wrapper) | |
| 108 return; | |
| 109 m_wrapper.Reset(isolate, wrapper); | |
| 110 wrapperTypeInfo->configureWrapper(&m_wrapper); | |
| 111 m_wrapper.SetWeak(this, &setWeakCallback); | |
| 112 ASSERT(containsWrapper()); | |
| 113 } | |
| 114 | |
| 115 v8::Local<v8::Object> newLocalWrapper(v8::Isolate* isolate) const | |
| 116 { | |
| 117 return v8::Local<v8::Object>::New(isolate, m_wrapper); | |
| 118 } | |
| 119 | |
| 120 bool isEqualTo(const v8::Local<v8::Object>& other) const | |
| 121 { | |
| 122 return m_wrapper == other; | |
| 123 } | |
| 124 | |
| 125 static bool wrapperCanBeStoredInObject(const void*) { return false; } | |
| 126 static bool wrapperCanBeStoredInObject(const ScriptWrappable*) { return true
; } | |
| 127 | |
| 128 static ScriptWrappable* fromObject(const void*) | |
| 129 { | |
| 130 ASSERT_NOT_REACHED(); | |
| 131 return 0; | |
| 132 } | |
| 133 | |
| 134 static ScriptWrappable* fromObject(ScriptWrappable* object) | |
| 135 { | |
| 136 return object; | |
| 137 } | |
| 138 | |
| 139 bool setReturnValue(v8::ReturnValue<v8::Value> returnValue) | |
| 140 { | |
| 141 returnValue.Set(m_wrapper); | |
| 142 return containsWrapper(); | |
| 143 } | |
| 144 | |
| 145 void markAsDependentGroup(ScriptWrappable* groupRoot, v8::Isolate* isolate) | |
| 146 { | |
| 147 ASSERT(containsWrapper()); | |
| 148 ASSERT(groupRoot && groupRoot->containsWrapper()); | |
| 149 | |
| 150 // FIXME: There has to be a better way. | |
| 151 v8::UniqueId groupId(*reinterpret_cast<intptr_t*>(&groupRoot->m_wrapper)
); | |
| 152 m_wrapper.MarkPartiallyDependent(); | |
| 153 isolate->SetObjectGroupId(v8::Persistent<v8::Value>::Cast(m_wrapper), gr
oupId); | |
| 154 } | |
| 155 | |
| 156 void setReference(const v8::Persistent<v8::Object>& parent, v8::Isolate* iso
late) | |
| 157 { | |
| 158 isolate->SetReference(parent, m_wrapper); | |
| 159 } | |
| 160 | |
| 161 template<typename V8T, typename T> | |
| 162 static void assertWrapperSanity(v8::Local<v8::Object> object, T* objectAsT) | |
| 163 { | |
| 164 ASSERT(objectAsT); | |
| 165 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(object.IsEmpty() | |
| 166 || object->GetAlignedPointerFromInternalField(v8DOMWrapperObjectInde
x) == V8T::toScriptWrappableBase(objectAsT)); | |
| 167 } | |
| 168 | |
| 169 template<typename V8T, typename T> | |
| 170 static void assertWrapperSanity(void* object, T* objectAsT) | |
| 171 { | |
| 172 ASSERT_NOT_REACHED(); | |
| 173 } | |
| 174 | |
| 175 template<typename V8T, typename T> | |
| 176 static void assertWrapperSanity(ScriptWrappable* object, T* objectAsT) | |
| 177 { | |
| 178 ASSERT(object); | |
| 179 ASSERT(objectAsT); | |
| 180 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(object->m_wrapper.IsEmpty() | |
| 181 || v8::Object::GetAlignedPointerFromInternalField(object->m_wrapper,
v8DOMWrapperObjectIndex) == V8T::toScriptWrappableBase(objectAsT)); | |
| 182 } | |
| 183 | |
| 184 using ScriptWrappableBase::assertWrapperSanity; | |
| 185 | |
| 186 bool containsWrapper() const { return !m_wrapper.IsEmpty(); } | |
| 187 | |
| 188 #if !ENABLE(OILPAN) | |
| 189 protected: | |
| 190 virtual ~ScriptWrappable() | |
| 191 { | |
| 192 // We must not get deleted as long as we contain a wrapper. If this happ
ens, we screwed up ref | |
| 193 // counting somewhere. Crash here instead of crashing during a later gc
cycle. | |
| 194 RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(!containsWrapper()); | |
| 195 } | |
| 196 #endif | |
| 197 // With Oilpan we don't need a ScriptWrappable destructor. | |
| 198 // | |
| 199 // - 'RELEASE_ASSERT_WITH_SECURITY_IMPLICATION(!containsWrapper())' is not n
eeded | |
| 200 // because Oilpan is not using reference counting at all. If containsWrapper
() is true, | |
| 201 // it means that ScriptWrappable still has a wrapper. In this case, the dest
ructor | |
| 202 // must not be called since the wrapper has a persistent handle back to this
ScriptWrappable object. | |
| 203 // Assuming that Oilpan's GC is correct (If we cannot assume this, a lot of
more things are | |
| 204 // already broken), we must not hit the RELEASE_ASSERT. | |
| 205 | |
| 206 private: | |
| 207 void disposeWrapper(v8::Local<v8::Object> wrapper) | |
| 208 { | |
| 209 ASSERT(containsWrapper()); | |
| 210 ASSERT(wrapper == m_wrapper); | |
| 211 m_wrapper.Reset(); | |
| 212 } | |
| 213 | |
| 214 static void setWeakCallback(const v8::WeakCallbackData<v8::Object, ScriptWra
ppable>& data) | |
| 215 { | |
| 216 data.GetParameter()->disposeWrapper(data.GetValue()); | |
| 217 | |
| 218 // FIXME: I noticed that 50%~ of minor GC cycle times can be consumed | |
| 219 // inside data.GetParameter()->deref(), which causes Node destructions.
We should | |
| 220 // make Node destructions incremental. | |
| 221 releaseObject(data.GetValue()); | |
| 222 } | |
| 223 | |
| 224 v8::Persistent<v8::Object> m_wrapper; | |
| 225 }; | |
| 226 | |
| 227 // Defines 'wrapperTypeInfo' virtual method which returns the WrapperTypeInfo of | |
| 228 // the instance. Also declares a static member of type WrapperTypeInfo, of which | |
| 229 // the definition is given by the IDL code generator. | |
| 230 // | |
| 231 // Every DOM Class T must meet either of the following conditions: | |
| 232 // - T.idl inherits from [NotScriptWrappable]. | |
| 233 // - T inherits from ScriptWrappable and has DEFINE_WRAPPERTYPEINFO(). | |
| 234 // | |
| 235 // If a DOM class T does not inherit from ScriptWrappable, you have to write | |
| 236 // [NotScriptWrappable] in the IDL file as an extended attribute in order to let | |
| 237 // IDL code generator know that T does not inherit from ScriptWrappable. Note | |
| 238 // that [NotScriptWrappable] is inheritable. | |
| 239 // | |
| 240 // All the derived classes of ScriptWrappable, regardless of directly or | |
| 241 // indirectly, must write this macro in the class definition. | |
| 242 #define DEFINE_WRAPPERTYPEINFO() \ | |
| 243 public: \ | |
| 244 virtual const WrapperTypeInfo* wrapperTypeInfo() const override \ | |
| 245 { \ | |
| 246 return &s_wrapperTypeInfo; \ | |
| 247 } \ | |
| 248 private: \ | |
| 249 static const WrapperTypeInfo& s_wrapperTypeInfo | |
| 250 | |
| 251 } // namespace blink | |
| 252 | |
| 253 #endif // SKY_ENGINE_BINDINGS_CORE_V8_SCRIPTWRAPPABLE_H_ | |
| OLD | NEW |