• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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 DOMWrapperMap_h
32 #define DOMWrapperMap_h
33 
34 #include "bindings/v8/WrapperTypeInfo.h"
35 #include <v8-util.h>
36 #include <v8.h>
37 #include "wtf/HashMap.h"
38 
39 namespace WebCore {
40 
41 template<class KeyType>
42 class DOMWrapperMap {
43 public:
DOMWrapperMap(v8::Isolate * isolate)44     explicit DOMWrapperMap(v8::Isolate* isolate)
45         : m_isolate(isolate)
46         , m_map(isolate)
47     {
48     }
49 
newLocal(KeyType * key,v8::Isolate * isolate)50     v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate)
51     {
52         return m_map.Get(key);
53     }
54 
setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue,KeyType * key)55     bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key)
56     {
57         return m_map.SetReturnValue(key, returnValue);
58     }
59 
setReference(const v8::Persistent<v8::Object> & parent,KeyType * key,v8::Isolate * isolate)60     void setReference(const v8::Persistent<v8::Object>& parent, KeyType* key, v8::Isolate* isolate)
61     {
62         m_map.SetReference(key, parent);
63     }
64 
containsKey(KeyType * key)65     bool containsKey(KeyType* key)
66     {
67         return m_map.Contains(key);
68     }
69 
set(KeyType * key,v8::Handle<v8::Object> wrapper,const WrapperConfiguration & configuration)70     void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperConfiguration& configuration)
71     {
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());
77     }
78 
clear()79     void clear()
80     {
81         m_map.Clear();
82     }
83 
removeAndDispose(KeyType * key)84     void removeAndDispose(KeyType* key)
85     {
86         ASSERT(containsKey(key));
87         m_map.Remove(key);
88     }
89 
90 private:
91     class PersistentValueMapTraits {
92     public:
93         // Map traits:
94         typedef HashMap<KeyType*, v8::PersistentContainerValue> Impl;
95         typedef typename Impl::iterator Iterator;
Size(const Impl * impl)96         static size_t Size(const Impl* impl) { return impl->size(); }
Empty(Impl * impl)97         static bool Empty(Impl* impl) { return impl->isEmpty(); }
Swap(Impl & impl,Impl & other)98         static void Swap(Impl& impl, Impl& other) { impl.swap(other); }
Begin(Impl * impl)99         static Iterator Begin(Impl* impl) { return impl->begin(); }
End(Impl * impl)100         static Iterator End(Impl* impl) { return impl->end(); }
Value(Iterator & iter)101         static v8::PersistentContainerValue Value(Iterator& iter)
102         {
103             return iter->value;
104         }
Key(Iterator & iter)105         static KeyType* Key(Iterator& iter) { return iter->key; }
Set(Impl * impl,KeyType * key,v8::PersistentContainerValue value)106         static v8::PersistentContainerValue Set(
107             Impl* impl, KeyType* key, v8::PersistentContainerValue value)
108         {
109             v8::PersistentContainerValue oldValue = Get(impl, key);
110             impl->set(key, value);
111             return oldValue;
112         }
Get(const Impl * impl,KeyType * key)113         static v8::PersistentContainerValue Get(const Impl* impl, KeyType* key)
114         {
115             return impl->get(key);
116         }
117 
Remove(Impl * impl,KeyType * key)118         static v8::PersistentContainerValue Remove(Impl* impl, KeyType* key)
119         {
120             return impl->take(key);
121         }
122 
123         // Weak traits:
124         static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak;
125         typedef v8::PersistentValueMap<KeyType*, v8::Object, PersistentValueMapTraits> MapType;
126         typedef MapType WeakCallbackDataType;
127 
WeakCallbackParameter(MapType * map,KeyType * key,v8::Local<v8::Object> & value)128         static WeakCallbackDataType* WeakCallbackParameter(MapType* map, KeyType* key, v8::Local<v8::Object>& value)
129         {
130             return map;
131         }
132 
DisposeCallbackData(WeakCallbackDataType * callbackData)133         static void DisposeCallbackData(WeakCallbackDataType* callbackData) { }
134 
MapFromWeakCallbackData(const v8::WeakCallbackData<v8::Object,WeakCallbackDataType> & data)135         static MapType* MapFromWeakCallbackData(
136             const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
137         {
138             return data.GetParameter();
139         }
140 
KeyFromWeakCallbackData(const v8::WeakCallbackData<v8::Object,WeakCallbackDataType> & data)141         static KeyType* KeyFromWeakCallbackData(
142             const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
143         {
144             return static_cast<KeyType*>(toNative(data.GetValue()));
145         }
146 
147         // Dispose traits:
148         // Generally nothing to do, but see below for a specialization for
149         // DomWrapperMap<void>.
Dispose(v8::Isolate * isolate,v8::UniquePersistent<v8::Object> value,KeyType * key)150         static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<v8::Object> value, KeyType* key) { }
151     };
152 
153     v8::Isolate* m_isolate;
154     typename PersistentValueMapTraits::MapType m_map;
155 };
156 
157 template <>
Dispose(v8::Isolate * isolate,v8::UniquePersistent<v8::Object> value,void * key)158 inline void DOMWrapperMap<void>::PersistentValueMapTraits::Dispose(
159     v8::Isolate* isolate,
160     v8::UniquePersistent<v8::Object> value,
161     void* key)
162 {
163     RELEASE_ASSERT(!value.IsEmpty()); // See crbug.com/368095.
164     releaseObject(v8::Local<v8::Object>::New(isolate, value));
165 }
166 
167 } // namespace WebCore
168 
169 #endif // DOMWrapperMap_h
170