1 /* 2 * Copyright (C) 2009 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 DOMData_h 32 #define DOMData_h 33 34 #include "DOMDataStore.h" 35 #include "V8DOMWrapper.h" 36 37 namespace WebCore { 38 39 // DOMData 40 // 41 // DOMData represents the all the DOM wrappers for a given thread. In 42 // particular, DOMData holds wrappers for all the isolated worlds in the 43 // thread. The DOMData for the main thread and the DOMData for child threads 44 // use different subclasses. 45 // 46 class DOMData { 47 WTF_MAKE_NONCOPYABLE(DOMData); 48 public: 49 DOMData(); 50 virtual ~DOMData(); 51 52 static DOMData* getCurrent(); 53 virtual DOMDataStore& getStore() = 0; 54 55 template<typename T> 56 static void handleWeakObject(DOMDataStore::DOMWrapperMapType, v8::Persistent<v8::Object>, T* domObject); 57 58 template<typename T> 59 static void removeObjectsFromWrapperMap(DOMDataStore* store, AbstractWeakReferenceMap<T, v8::Object>& domMap); 60 owningThread()61 ThreadIdentifier owningThread() const { return m_owningThread; } 62 63 private: 64 static void derefObject(WrapperTypeInfo* type, void* domObject); 65 66 template<typename T> 67 class WrapperMapObjectRemover : public WeakReferenceMap<T, v8::Object>::Visitor { 68 public: visitDOMWrapper(DOMDataStore * store,T * domObject,v8::Persistent<v8::Object> v8Object)69 virtual void visitDOMWrapper(DOMDataStore* store, T* domObject, v8::Persistent<v8::Object> v8Object) 70 { 71 WrapperTypeInfo* type = V8DOMWrapper::domWrapperType(v8Object); 72 derefObject(type, domObject); 73 v8Object.Dispose(); 74 } 75 }; 76 77 ThreadIdentifier m_owningThread; 78 }; 79 80 template<typename T> handleWeakObject(DOMDataStore::DOMWrapperMapType mapType,v8::Persistent<v8::Object> v8Object,T * domObject)81 void DOMData::handleWeakObject(DOMDataStore::DOMWrapperMapType mapType, v8::Persistent<v8::Object> v8Object, T* domObject) 82 { 83 WrapperTypeInfo* type = V8DOMWrapper::domWrapperType(v8Object); 84 DOMDataList& list = DOMDataStore::allStores(); 85 bool found = false; 86 for (size_t i = 0; i < list.size(); ++i) { 87 DOMDataStore* store = list[i]; 88 ASSERT(store->domData()->owningThread() == WTF::currentThread()); 89 90 DOMWrapperMap<T>* domMap = static_cast<DOMWrapperMap<T>*>(store->getDOMWrapperMap(mapType)); 91 if (domMap->removeIfPresent(domObject, v8Object)) { 92 derefObject(type, domObject); 93 found = true; 94 } 95 } 96 97 // If not found, it means map for the wrapper has been already destroyed, just dispose the 98 // handle and deref the object to fight memory leak. 99 if (!found) { 100 v8Object.Dispose(); 101 derefObject(type, domObject); 102 } 103 } 104 105 template<typename T> removeObjectsFromWrapperMap(DOMDataStore * store,AbstractWeakReferenceMap<T,v8::Object> & domMap)106 void DOMData::removeObjectsFromWrapperMap(DOMDataStore* store, AbstractWeakReferenceMap<T, v8::Object>& domMap) 107 { 108 WrapperMapObjectRemover<T> remover; 109 domMap.visit(store, &remover); 110 domMap.clear(); 111 } 112 113 } // namespace WebCore 114 115 #endif // DOMData_h 116