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/WrapperTypeInfo.h"
37 #include "wtf/HashMap.h"
41 template<class KeyType>
44 explicit DOMWrapperMap(v8::Isolate* isolate)
50 v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate)
52 return m_map.Get(key);
55 bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key)
57 return m_map.SetReturnValue(key, returnValue);
60 void setReference(const v8::Persistent<v8::Object>& parent, KeyType* key, v8::Isolate* isolate)
62 m_map.SetReference(key, parent);
65 bool containsKey(KeyType* key)
67 return m_map.Contains(key);
70 void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperConfiguration& configuration)
72 ASSERT(static_cast<KeyType*>(toNative(wrapper)) == key);
73 RELEASE_ASSERT(!containsKey(key)); // See crbug.com/368095
74 v8::UniquePersistent<v8::Object> unique(m_isolate, wrapper);
75 configuration.configureWrapper(&unique);
76 m_map.Set(key, unique.Pass());
84 void removeAndDispose(KeyType* key)
86 ASSERT(containsKey(key));
91 class PersistentValueMapTraits {
94 typedef HashMap<KeyType*, v8::PersistentContainerValue> Impl;
95 typedef typename Impl::iterator Iterator;
96 static size_t Size(const Impl* impl) { return impl->size(); }
97 static bool Empty(Impl* impl) { return impl->isEmpty(); }
98 static void Swap(Impl& impl, Impl& other) { impl.swap(other); }
99 static Iterator Begin(Impl* impl) { return impl->begin(); }
100 static Iterator End(Impl* impl) { return impl->end(); }
101 static v8::PersistentContainerValue Value(Iterator& iter)
105 static KeyType* Key(Iterator& iter) { return iter->key; }
106 static v8::PersistentContainerValue Set(
107 Impl* impl, KeyType* key, v8::PersistentContainerValue value)
109 v8::PersistentContainerValue oldValue = Get(impl, key);
110 impl->add(key, value);
113 static v8::PersistentContainerValue Get(const Impl* impl, KeyType* key)
115 return impl->get(key);
118 static v8::PersistentContainerValue Remove(Impl* impl, KeyType* key)
120 return impl->take(key);
124 static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak;
125 typedef v8::PersistentValueMap<KeyType*, v8::Object, PersistentValueMapTraits> MapType;
126 typedef MapType WeakCallbackDataType;
128 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, KeyType* key, v8::Local<v8::Object>& value)
133 static void DisposeCallbackData(WeakCallbackDataType* callbackData) { }
135 static MapType* MapFromWeakCallbackData(
136 const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
138 return data.GetParameter();
141 static KeyType* KeyFromWeakCallbackData(
142 const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
144 return static_cast<KeyType*>(toNative(data.GetValue()));
148 // Generally nothing to do, but see below for a specialization for
149 // DomWrapperMap<void>.
150 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<v8::Object> value, KeyType* key) { }
153 v8::Isolate* m_isolate;
154 typename PersistentValueMapTraits::MapType m_map;
158 inline void DOMWrapperMap<void>::PersistentValueMapTraits::Dispose(
159 v8::Isolate* isolate,
160 v8::UniquePersistent<v8::Object> value,
163 RELEASE_ASSERT(!value.IsEmpty()); // See crbug.com/368095.
164 releaseObject(v8::Local<v8::Object>::New(isolate, value));
167 } // namespace WebCore
169 #endif // DOMWrapperMap_h