Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / 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/v8/UnsafePersistent.h"
35 #include "bindings/v8/V8Utilities.h"
36 #include "bindings/v8/WrapperTypeInfo.h"
37 #include <v8.h>
38 #include "wtf/HashMap.h"
39
40 namespace WebCore {
41
42 template<class KeyType>
43 class DOMWrapperMap {
44 public:
45     typedef HashMap<KeyType*, UnsafePersistent<v8::Object> > MapType;
46
47     explicit DOMWrapperMap(v8::Isolate* isolate)
48         : m_isolate(isolate)
49     {
50     }
51
52     v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate)
53     {
54         return m_map.get(key).newLocal(isolate);
55     }
56
57     bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key)
58     {
59         typename MapType::iterator it = m_map.find(key);
60         if (it == m_map.end())
61             return false;
62         returnValue.Set(*(it->value.persistent()));
63         return true;
64     }
65
66     void setReference(const v8::Persistent<v8::Object>& parent, KeyType* key, v8::Isolate* isolate)
67     {
68         m_map.get(key).setReferenceFrom(parent, isolate);
69     }
70
71     bool containsKey(KeyType* key)
72     {
73         return m_map.find(key) != m_map.end();
74     }
75
76     bool containsKeyAndValue(KeyType* key, v8::Handle<v8::Object> value)
77     {
78         typename MapType::iterator it = m_map.find(key);
79         if (it == m_map.end())
80             return false;
81         return *(it->value.persistent()) == value;
82     }
83
84     void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperConfiguration& configuration)
85     {
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);
96     }
97
98     void clear()
99     {
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.
102             MapType map;
103             map.swap(m_map);
104             for (typename MapType::iterator it = map.begin(); it != map.end(); ++it) {
105                 toWrapperTypeInfo(*(it->value.persistent()))->derefObject(it->key);
106                 it->value.dispose();
107             }
108         }
109     }
110
111     void removeAndDispose(KeyType* key)
112     {
113         typename MapType::iterator it = m_map.find(key);
114         ASSERT_WITH_SECURITY_IMPLICATION(it != m_map.end());
115         it->value.dispose();
116         m_map.remove(it);
117     }
118
119 private:
120     static void setWeakCallback(const v8::WeakCallbackData<v8::Object, DOMWrapperMap<KeyType> >&);
121
122     v8::Isolate* m_isolate;
123     MapType m_map;
124 };
125
126 template<>
127 inline void DOMWrapperMap<void>::setWeakCallback(const v8::WeakCallbackData<v8::Object, DOMWrapperMap<void> >& data)
128 {
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);
135 }
136
137 } // namespace WebCore
138
139 #endif // DOMWrapperMap_h