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 DOMDataStore_h 32 #define DOMDataStore_h 33 34 #include "DOMObjectsInclude.h" 35 36 #include <v8.h> 37 #include <wtf/HashMap.h> 38 #include <wtf/MainThread.h> 39 #include <wtf/Noncopyable.h> 40 #include <wtf/OwnPtr.h> 41 #include <wtf/StdLibExtras.h> 42 #include <wtf/Threading.h> 43 #include <wtf/ThreadSpecific.h> 44 #include <wtf/Vector.h> 45 46 namespace WebCore { 47 48 class DOMData; 49 50 typedef WTF::Vector<DOMDataStore*> DOMDataList; 51 52 // DOMDataStore 53 // 54 // DOMDataStore is the backing store that holds the maps between DOM objects 55 // and JavaScript objects. In general, each thread can have multiple backing 56 // stores, one per isolated world. 57 // 58 // This class doesn't manage the lifetime of the store. The data store 59 // lifetime is managed by subclasses. 60 // 61 class DOMDataStore : public Noncopyable { 62 public: 63 enum DOMWrapperMapType { 64 DOMNodeMap, 65 DOMObjectMap, 66 ActiveDOMObjectMap, 67 #if ENABLE(SVG) 68 DOMSVGElementInstanceMap, 69 DOMSVGObjectWithContextMap 70 #endif 71 }; 72 73 template <class KeyType> 74 class InternalDOMWrapperMap : public DOMWrapperMap<KeyType> { 75 public: InternalDOMWrapperMap(DOMData * domData,v8::WeakReferenceCallback callback)76 InternalDOMWrapperMap(DOMData* domData, v8::WeakReferenceCallback callback) 77 : DOMWrapperMap<KeyType>(callback), m_domData(domData) { } 78 forget(KeyType * object)79 virtual void forget(KeyType* object) 80 { 81 DOMWrapperMap<KeyType>::forget(object); 82 forgetDelayedObject(m_domData, object); 83 } 84 forgetOnly(KeyType * object)85 void forgetOnly(KeyType* object) { DOMWrapperMap<KeyType>::forget(object); } 86 87 private: 88 DOMData* m_domData; 89 }; 90 91 DOMDataStore(DOMData*); 92 virtual ~DOMDataStore(); 93 94 // A list of all DOMDataStore objects. Traversed during GC to find a thread-specific map that 95 // contains the object - so we can schedule the object to be deleted on the thread which created it. 96 static DOMDataList& allStores(); 97 // Mutex to protect against concurrent access of DOMDataList. 98 static WTF::Mutex& allStoresMutex(); 99 100 // Helper function to avoid circular includes. 101 static void forgetDelayedObject(DOMData*, void* object); 102 domData()103 DOMData* domData() const { return m_domData; } 104 105 void* getDOMWrapperMap(DOMWrapperMapType); 106 domNodeMap()107 InternalDOMWrapperMap<Node>& domNodeMap() { return *m_domNodeMap; } domObjectMap()108 InternalDOMWrapperMap<void>& domObjectMap() { return *m_domObjectMap; } activeDomObjectMap()109 InternalDOMWrapperMap<void>& activeDomObjectMap() { return *m_activeDomObjectMap; } 110 #if ENABLE(SVG) domSvgElementInstanceMap()111 InternalDOMWrapperMap<SVGElementInstance>& domSvgElementInstanceMap() { return *m_domSvgElementInstanceMap; } domSvgObjectWithContextMap()112 InternalDOMWrapperMap<void>& domSvgObjectWithContextMap() { return *m_domSvgObjectWithContextMap; } 113 #endif 114 115 // Need by V8GCController. 116 static void weakActiveDOMObjectCallback(v8::Persistent<v8::Value> v8Object, void* domObject); 117 118 protected: 119 static void weakNodeCallback(v8::Persistent<v8::Value> v8Object, void* domObject); 120 static void weakDOMObjectCallback(v8::Persistent<v8::Value> v8Object, void* domObject); 121 #if ENABLE(SVG) 122 static void weakSVGElementInstanceCallback(v8::Persistent<v8::Value> v8Object, void* domObject); 123 // SVG non-node elements may have a reference to a context node which should be notified when the element is change. 124 static void weakSVGObjectWithContextCallback(v8::Persistent<v8::Value> v8Object, void* domObject); 125 #endif 126 127 InternalDOMWrapperMap<Node>* m_domNodeMap; 128 InternalDOMWrapperMap<void>* m_domObjectMap; 129 InternalDOMWrapperMap<void>* m_activeDomObjectMap; 130 #if ENABLE(SVG) 131 InternalDOMWrapperMap<SVGElementInstance>* m_domSvgElementInstanceMap; 132 InternalDOMWrapperMap<void>* m_domSvgObjectWithContextMap; 133 #endif 134 135 private: 136 // A back-pointer to the DOMData to which we belong. 137 DOMData* m_domData; 138 }; 139 140 } // namespace WebCore 141 142 #endif // DOMDataStore_h 143