Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(788)

Side by Side Diff: Source/bindings/v8/DOMWrapperMap.h

Issue 180363004: Convert DOMWrapperMap to use PersistentValueMap (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Adapt to changes in v8-util.h Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | Source/bindings/v8/V8NPObject.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #ifndef DOMWrapperMap_h 31 #ifndef DOMWrapperMap_h
32 #define DOMWrapperMap_h 32 #define DOMWrapperMap_h
33 33
34 #include "bindings/v8/UnsafePersistent.h"
35 #include "bindings/v8/WrapperTypeInfo.h" 34 #include "bindings/v8/WrapperTypeInfo.h"
35 #include <v8-util.h>
36 #include <v8.h> 36 #include <v8.h>
37 #include "wtf/HashMap.h" 37 #include "wtf/HashMap.h"
38 38
39 namespace WebCore { 39 namespace WebCore {
40 40
41 template<class KeyType> 41 template<class KeyType>
42 class DOMWrapperMap { 42 class DOMWrapperMap {
43 public: 43 public:
44 typedef HashMap<KeyType*, UnsafePersistent<v8::Object> > MapType;
45
46 explicit DOMWrapperMap(v8::Isolate* isolate) 44 explicit DOMWrapperMap(v8::Isolate* isolate)
47 : m_isolate(isolate) 45 : m_isolate(isolate)
46 , m_map(isolate)
48 { 47 {
49 } 48 }
50 49
51 v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate) 50 v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate)
52 { 51 {
53 return m_map.get(key).newLocal(isolate); 52 return m_map.Get(key);
54 } 53 }
55 54
56 bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key ) 55 bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key )
57 { 56 {
58 typename MapType::iterator it = m_map.find(key); 57 return m_map.SetReturnValue(key, returnValue);
59 if (it == m_map.end())
60 return false;
61 returnValue.Set(*(it->value.persistent()));
62 return true;
63 } 58 }
64 59
65 void 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)
66 { 61 {
67 m_map.get(key).setReferenceFrom(parent, isolate); 62 m_map.SetReference(key, parent);
68 } 63 }
69 64
70 bool containsKey(KeyType* key) 65 bool containsKey(KeyType* key)
71 { 66 {
72 return m_map.find(key) != m_map.end(); 67 return m_map.Contains(key);
73 }
74
75 bool containsKeyAndValue(KeyType* key, v8::Handle<v8::Object> value)
76 {
77 typename MapType::iterator it = m_map.find(key);
78 if (it == m_map.end())
79 return false;
80 return *(it->value.persistent()) == value;
81 } 68 }
82 69
83 void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperConfigur ation& configuration) 70 void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperConfigur ation& configuration)
84 { 71 {
85 ASSERT(static_cast<KeyType*>(toNative(wrapper)) == key); 72 ASSERT(static_cast<KeyType*>(toNative(wrapper)) == key);
86 v8::Persistent<v8::Object> persistent(m_isolate, wrapper); 73 v8::UniquePersistent<v8::Object> unique(m_isolate, wrapper);
87 configuration.configureWrapper(&persistent); 74 configuration.configureWrapper(&unique);
88 persistent.SetWeak(this, &setWeakCallback); 75 m_map.Set(key, unique.Pass());
89 typename MapType::AddResult result = m_map.add(key, UnsafePersistent<v8: :Object>());
90 ASSERT(result.isNewEntry);
91 // FIXME: Stop handling this case once duplicate wrappers are guaranteed not to be created.
92 if (!result.isNewEntry)
93 result.storedValue->value.dispose();
94 result.storedValue->value = UnsafePersistent<v8::Object>(persistent);
95 } 76 }
96 77
97 void clear() 78 void clear()
98 { 79 {
99 v8::HandleScope scope(m_isolate); 80 m_map.Clear();
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 releaseObject(it->value.newLocal(m_isolate));
106 it->value.dispose();
107 }
108 }
109 } 81 }
110 82
111 void removeAndDispose(KeyType* key) 83 void removeAndDispose(KeyType* key)
112 { 84 {
113 typename MapType::iterator it = m_map.find(key); 85 m_map.Remove(key);
114 ASSERT_WITH_SECURITY_IMPLICATION(it != m_map.end());
115 it->value.dispose();
116 m_map.remove(it);
117 } 86 }
118 87
119 private: 88 private:
120 static void setWeakCallback(const v8::WeakCallbackData<v8::Object, DOMWrappe rMap<KeyType> >&); 89 class PersistentValueMapTraits {
90 public:
91 // Map traits:
92 typedef HashMap<KeyType*, v8::PersistentContainerValue> Impl;
93 typedef typename Impl::iterator Iterator;
94 static size_t Size(const Impl* impl) { return impl->size(); }
95 static bool Empty(Impl* impl) { return impl->isEmpty(); }
96 static void Swap(Impl& impl, Impl& other) { impl.swap(other); }
97 static Iterator Begin(Impl* impl) { return impl->begin(); }
98 static Iterator End(Impl* impl) { return impl->end(); }
99 static v8::PersistentContainerValue Value(Iterator& iter)
100 {
101 return iter->value;
102 }
103 static KeyType* Key(Iterator& iter) { return iter->key; }
104 static v8::PersistentContainerValue Set(
105 Impl* impl, KeyType* key, v8::PersistentContainerValue value)
106 {
107 v8::PersistentContainerValue oldValue = Get(impl, key);
108 impl->add(key, value);
109 return oldValue;
110 }
111 static v8::PersistentContainerValue Get(const Impl* impl, KeyType* key)
112 {
113 return impl->get(key);
114 }
115
116 static v8::PersistentContainerValue Remove(Impl* impl, KeyType* key)
117 {
118 return impl->take(key);
119 }
120
121 // Weak traits:
122 static const v8::PersistentContainerCallbackType kCallbackType = v8::kWe ak;
123 typedef v8::PersistentValueMap<KeyType*, v8::Object, PersistentValueMapT raits> MapType;
124 typedef MapType WeakCallbackDataType;
125
126 static WeakCallbackDataType* WeakCallbackParameter(MapType* map, KeyType * key, v8::Local<v8::Object>& value)
127 {
128 return map;
129 }
130
131 static void DisposeCallbackData(WeakCallbackDataType* callbackData) { }
132
133 static MapType* MapFromWeakCallbackData(
134 const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
135 {
136 return data.GetParameter();
137 }
138
139 static KeyType* KeyFromWeakCallbackData(
140 const v8::WeakCallbackData<v8::Object, WeakCallbackDataType>& data)
141 {
142 return static_cast<KeyType*>(toNative(data.GetValue()));
143 }
144
145 // Dispose traits:
146 // Generally nothing to do, but see below for a specialization for
147 // DomWrapperMap<void>.
148 static void Dispose(v8::Isolate* isolate, v8::UniquePersistent<v8::Objec t> value, KeyType* key) { }
149 };
121 150
122 v8::Isolate* m_isolate; 151 v8::Isolate* m_isolate;
123 MapType m_map; 152 typename PersistentValueMapTraits::MapType m_map;
124 }; 153 };
125 154
126 template<> 155 template <>
127 inline void DOMWrapperMap<void>::setWeakCallback(const v8::WeakCallbackData<v8:: Object, DOMWrapperMap<void> >& data) 156 inline void DOMWrapperMap<void>::PersistentValueMapTraits::Dispose(
157 v8::Isolate* isolate,
158 v8::UniquePersistent<v8::Object> value,
159 void* key)
128 { 160 {
129 void* key = static_cast<void*>(toNative(data.GetValue())); 161 releaseObject(v8::Local<v8::Object>::New(isolate, value));
130 ASSERT(*data.GetParameter()->m_map.get(key).persistent() == data.GetValue()) ;
131 data.GetParameter()->removeAndDispose(key);
132 releaseObject(data.GetValue());
133 } 162 }
134 163
135 } // namespace WebCore 164 } // namespace WebCore
136 165
137 #endif // DOMWrapperMap_h 166 #endif // DOMWrapperMap_h
OLDNEW
« no previous file with comments | « no previous file | Source/bindings/v8/V8NPObject.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698