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 V8DOMMap_h 32 #define V8DOMMap_h 33 34 #include <wtf/HashMap.h> 35 #include <wtf/OwnPtr.h> 36 #include <v8.h> 37 38 namespace WebCore { 39 class Node; 40 #if ENABLE(SVG) 41 class SVGElementInstance; 42 #endif 43 44 // A table of wrappers with weak pointers. 45 // This table allows us to avoid track wrapped objects for debugging 46 // and for ensuring that we don't double wrap the same object. 47 template<class KeyType, class ValueType> class WeakReferenceMap { 48 public: WeakReferenceMap(v8::WeakReferenceCallback callback)49 WeakReferenceMap(v8::WeakReferenceCallback callback) : m_weakReferenceCallback(callback) { } ~WeakReferenceMap()50 virtual ~WeakReferenceMap() 51 { 52 #ifndef NDEBUG 53 if (m_map.size() > 0) 54 fprintf(stderr, "Leak %d JS wrappers.\n", m_map.size()); 55 #endif 56 } 57 58 // Get the JS wrapper object of an object. get(KeyType * obj)59 virtual v8::Persistent<ValueType> get(KeyType* obj) 60 { 61 ValueType* wrapper = m_map.get(obj); 62 return wrapper ? v8::Persistent<ValueType>(wrapper) : v8::Persistent<ValueType>(); 63 } 64 set(KeyType * obj,v8::Persistent<ValueType> wrapper)65 virtual void set(KeyType* obj, v8::Persistent<ValueType> wrapper) 66 { 67 ASSERT(!m_map.contains(obj)); 68 wrapper.MakeWeak(obj, m_weakReferenceCallback); 69 m_map.set(obj, *wrapper); 70 } 71 forget(KeyType * obj)72 virtual void forget(KeyType* obj) 73 { 74 ASSERT(obj); 75 ValueType* wrapper = m_map.take(obj); 76 if (!wrapper) 77 return; 78 79 v8::Persistent<ValueType> handle(wrapper); 80 handle.Dispose(); 81 handle.Clear(); 82 } 83 contains(KeyType * obj)84 bool contains(KeyType* obj) { return m_map.contains(obj); } 85 impl()86 HashMap<KeyType*, ValueType*>& impl() { return m_map; } 87 88 protected: 89 HashMap<KeyType*, ValueType*> m_map; 90 v8::WeakReferenceCallback m_weakReferenceCallback; 91 }; 92 93 template <class KeyType> class DOMWrapperMap : public WeakReferenceMap<KeyType, v8::Object> { 94 public: DOMWrapperMap(v8::WeakReferenceCallback callback)95 DOMWrapperMap(v8::WeakReferenceCallback callback) : WeakReferenceMap<KeyType, v8::Object>(callback) { } 96 97 class Visitor { 98 public: 99 virtual void visitDOMWrapper(KeyType* key, v8::Persistent<v8::Object> object) = 0; ~Visitor()100 virtual ~Visitor() { } 101 }; 102 }; 103 104 // An opaque class that represents a set of DOM wrappers. 105 class DOMDataStore; 106 107 // A utility class to manage the lifetime of set of DOM wrappers. 108 class DOMDataStoreHandle { 109 public: 110 DOMDataStoreHandle(); 111 ~DOMDataStoreHandle(); 112 getStore()113 DOMDataStore* getStore() const { return m_store.get(); } 114 115 private: 116 OwnPtr<DOMDataStore> m_store; 117 }; 118 119 // A map from DOM node to its JS wrapper. 120 DOMWrapperMap<Node>& getDOMNodeMap(); 121 void visitDOMNodesInCurrentThread(DOMWrapperMap<Node>::Visitor*); 122 123 // A map from a DOM object (non-node) to its JS wrapper. This map does not contain the DOM objects which can have pending activity (active dom objects). 124 DOMWrapperMap<void>& getDOMObjectMap(); 125 void visitDOMObjectsInCurrentThread(DOMWrapperMap<void>::Visitor*); 126 127 // A map from a DOM object to its JS wrapper for DOM objects which can have pending activity. 128 DOMWrapperMap<void>& getActiveDOMObjectMap(); 129 void visitActiveDOMObjectsInCurrentThread(DOMWrapperMap<void>::Visitor*); 130 131 // This should be called to remove all DOM objects associated with the current thread when it is tearing down. 132 void removeAllDOMObjectsInCurrentThread(); 133 134 #if ENABLE(SVG) 135 // A map for SVGElementInstances to its JS wrapper. 136 DOMWrapperMap<SVGElementInstance>& getDOMSVGElementInstanceMap(); 137 void visitSVGElementInstancesInCurrentThread(DOMWrapperMap<SVGElementInstance>::Visitor*); 138 139 // Map of SVG objects with contexts to V8 objects. 140 DOMWrapperMap<void>& getDOMSVGObjectWithContextMap(); 141 void visitDOMSVGObjectsInCurrentThread(DOMWrapperMap<void>::Visitor*); 142 #endif 143 } // namespace WebCore 144 145 #endif // V8DOMMap_h 146