Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / DOMWrapperMap.h
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/core/v8/WrapperTypeInfo.h"
35 #include "platform/ScriptForbiddenScope.h"
36 #include "wtf/HashMap.h"
37 #include <v8-util.h>
38 #include <v8.h>
39
40 namespace blink {
41
42 template<class KeyType>
43 class DOMWrapperMap {
44 public:
45     explicit DOMWrapperMap(v8::Isolate* isolate)
46         : m_isolate(isolate)
47         , m_map(isolate)
48     {
49     }
50
51     v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate)
52     {
53         return m_map.Get(key);
54     }
55
56     bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key)
57     {
58         return m_map.SetReturnValue(key, returnValue);
59     }
60
61     void setReference(const v8::Persistent<v8::Object>& parent, KeyType* key, v8::Isolate* isolate)
62     {
63         m_map.SetReference(key, parent);
64     }
65
66     bool containsKey(KeyType* key)
67     {
68         return m_map.Contains(key);
69     }
70
71     void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperTypeInfo* wrapperTypeInfo)
72     {
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());
78     }
79
80     void clear()
81     {
82         m_map.Clear();
83     }
84
85     void removeAndDispose(KeyType* key)
86     {
87         ASSERT(containsKey(key));
88         m_map.Remove(key);
89     }
90
91 private:
92     class PersistentValueMapTraits {
93     public:
94         // Map traits:
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)
103         {
104             return iter->value;
105         }
106         static KeyType* Key(Iterator& iter) { return iter->key; }
107         static v8::PersistentContainerValue Set(
108             Impl* impl, KeyType* key, v8::PersistentContainerValue value)
109         {
110             v8::PersistentContainerValue oldValue = Get(impl, key);
111             impl->set(key, value);
112             return oldValue;
113         }
114         static v8::PersistentContainerValue Get(const Impl* impl, KeyType* key)
115         {
116             return impl->get(key);
117         }
118
119         static v8::PersistentContainerValue Remove(Impl* impl, KeyType* key)
120         {
121             return impl->take(key);
122         }
123
124         // Weak traits:
125         static const v8::PersistentContainerCallbackType kCallbackType = v8::kWeak;
126         typedef v8::PersistentValueMap<KeyType*, v8::Object, PersistentValueMapTraits> MapType;
127         typedef MapType WeakCallbackDataType;
128
129         static WeakCallbackDataType* WeakCallbackParameter(MapType* map, KeyType* key, v8::Local<v8::Object>& value)
130         {
131             return map;
132         }
133
134         static void DisposeCallbackData(WeakCallbackDataType* callbackData) { }
135
136         static MapType* MapFromWeakCallbackData(
137             const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
138         {
139             return data.GetParameter();
140         }
141
142         static KeyType* KeyFromWeakCallbackData(
143             const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
144         {
145             return reinterpret_cast<KeyType*>(toScriptWrappableBase(data.GetValue()));
146         }
147
148         static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<v8::Object> value, KeyType* key) { }
149     };
150
151     v8::Isolate* m_isolate;
152     typename PersistentValueMapTraits::MapType m_map;
153 };
154
155 } // namespace blink
156
157 #endif // DOMWrapperMap_h