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/core/v8/WrapperTypeInfo.h"
35 #include "platform/ScriptForbiddenScope.h"
36 #include "wtf/HashMap.h"
42 template<class KeyType>
45 explicit DOMWrapperMap(v8::Isolate* isolate)
51 v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate)
53 return m_map.Get(key);
56 bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key)
58 return m_map.SetReturnValue(key, returnValue);
61 void setReference(const v8::Persistent<v8::Object>& parent, KeyType* key, v8::Isolate* isolate)
63 m_map.SetReference(key, parent);
66 bool containsKey(KeyType* key)
68 return m_map.Contains(key);
71 void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo)
73 ASSERT(reinterpret_cast<KeyType*>(toScriptWrappableBase(wrapper)) == key);
74 RELEASE_ASSERT(!containsKey(key)); // See crbug.com/368095
75 v8::UniquePersistent<v8::Object> unique(m_isolate, wrapper);
76 wrapperTypeInfo->configureWrapper(&unique);
77 m_map.Set(key, unique.Pass());
85 void removeAndDispose(KeyType* key)
87 ASSERT(containsKey(key));
92 class PersistentValueMapTraits {
95 typedef HashMap<KeyType*, v8::PersistentContainerValue> Impl;
96 typedef typename Impl::iterator Iterator;
97 static size_t Size(const Impl* impl) { return impl->size(); }
98 static bool Empty(Impl* impl) { return impl->isEmpty(); }
99 static void Swap(Impl& impl, Impl& other) { impl.swap(other); }
100 static Iterator Begin(Impl* impl) { return impl->begin(); }
101 static Iterator End(Impl* impl) { return impl->end(); }
102 static v8::PersistentContainerValue Value(Iterator& iter)
106 static KeyType* Key(Iterator& iter) { return iter->key; }
107 static v8::PersistentContainerValue Set(
108 Impl* impl, KeyType* key, v8::PersistentContainerValue value)
110 v8::PersistentContainerValue oldValue = Get(impl, key);
111 impl->set(key, value);
114 static v8::PersistentContainerValue Get(const Impl* impl, KeyType* key)
116 return impl->get(key);
119 static v8::PersistentContainerValue Remove(Impl* impl, KeyType* key)
121 return impl->take(key);
125 static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak;
126 typedef v8::PersistentValueMap<KeyType*, v8::Object, PersistentValueMapTraits> MapType;
127 typedef MapType WeakCallbackDataType;
129 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, KeyType* key, v8::Local<v8::Object>& value)
134 static void DisposeCallbackData(WeakCallbackDataType* callbackData) { }
136 static MapType* MapFromWeakCallbackData(
137 const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
139 return data.GetParameter();
142 static KeyType* KeyFromWeakCallbackData(
143 const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
145 return reinterpret_cast<KeyType*>(toScriptWrappableBase(data.GetValue()));
148 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<v8::Object> value, KeyType* key) { }
151 v8::Isolate* m_isolate;
152 typename PersistentValueMapTraits::MapType m_map;
157 #endif // DOMWrapperMap_h