2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
31 #ifndef DOMWrapperMap_h
32 #define DOMWrapperMap_h
34 #include "bindings/v8/UnsafePersistent.h"
35 #include "bindings/v8/V8Utilities.h"
36 #include "bindings/v8/WrapperTypeInfo.h"
38 #include "wtf/HashMap.h"
42 template<class KeyType>
45 typedef HashMap<KeyType*, UnsafePersistent<v8::Object> > MapType;
47 explicit DOMWrapperMap(v8::Isolate* isolate)
52 v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate)
54 return m_map.get(key).newLocal(isolate);
57 bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key)
59 typename MapType::iterator it = m_map.find(key);
60 if (it == m_map.end())
62 returnValue.Set(*(it->value.persistent()));
66 void setReference(const v8::Persistent<v8::Object>& parent, KeyType* key, v8::Isolate* isolate)
68 m_map.get(key).setReferenceFrom(parent, isolate);
71 bool containsKey(KeyType* key)
73 return m_map.find(key) != m_map.end();
76 bool containsKeyAndValue(KeyType* key, v8::Handle<v8::Object> value)
78 typename MapType::iterator it = m_map.find(key);
79 if (it == m_map.end())
81 return *(it->value.persistent()) == value;
84 void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperConfiguration& configuration)
86 ASSERT(static_cast<KeyType*>(toNative(wrapper)) == key);
87 v8::Persistent<v8::Object> persistent(m_isolate, wrapper);
88 configuration.configureWrapper(&persistent);
89 persistent.SetWeak(this, &setWeakCallback);
90 typename MapType::AddResult result = m_map.add(key, UnsafePersistent<v8::Object>());
91 ASSERT(result.isNewEntry);
92 // FIXME: Stop handling this case once duplicate wrappers are guaranteed not to be created.
93 if (!result.isNewEntry)
94 result.iterator->value.dispose();
95 result.iterator->value = UnsafePersistent<v8::Object>(persistent);
100 while (!m_map.isEmpty()) {
101 // Swap out m_map on each iteration to ensure any wrappers added due to side effects of the loop are cleared.
104 for (typename MapType::iterator it = map.begin(); it != map.end(); ++it) {
105 toWrapperTypeInfo(*(it->value.persistent()))->derefObject(it->key);
111 void removeAndDispose(KeyType* key)
113 typename MapType::iterator it = m_map.find(key);
114 ASSERT_WITH_SECURITY_IMPLICATION(it != m_map.end());
120 static void setWeakCallback(const v8::WeakCallbackData<v8::Object, DOMWrapperMap<KeyType> >&);
122 v8::Isolate* m_isolate;
127 inline void DOMWrapperMap<void>::setWeakCallback(const v8::WeakCallbackData<v8::Object, DOMWrapperMap<void> >& data)
129 const WrapperTypeInfo* type = toWrapperTypeInfo(data.GetValue());
130 ASSERT(type->derefObjectFunction);
131 void* key = static_cast<void*>(toNative(data.GetValue()));
132 ASSERT(*(data.GetParameter()->m_map.get(key).persistent()) == data.GetValue());
133 data.GetParameter()->removeAndDispose(key);
134 type->derefObject(key);
137 } // namespace WebCore
139 #endif // DOMWrapperMap_h