| OLD | NEW |
| 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 Loading... |
| 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" |
| 36 #include <v8.h> | 35 #include <v8.h> |
| 37 #include "wtf/HashMap.h" | 36 #include "wtf/HashMap.h" |
| 38 | 37 |
| 39 namespace WebCore { | 38 namespace WebCore { |
| 40 | 39 |
| 41 template<class KeyType> | 40 template<class KeyType> |
| 42 class DOMWrapperMap { | 41 class DOMWrapperMap { |
| 43 public: | 42 public: |
| 44 typedef HashMap<KeyType*, UnsafePersistent<v8::Object> > MapType; | |
| 45 | |
| 46 explicit DOMWrapperMap(v8::Isolate* isolate) | 43 explicit DOMWrapperMap(v8::Isolate* isolate) |
| 47 : m_isolate(isolate) | 44 : m_isolate(isolate) |
| 45 , m_map(isolate) |
| 48 { | 46 { |
| 49 } | 47 } |
| 50 | 48 |
| 51 v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate) | 49 v8::Handle<v8::Object> newLocal(KeyType* key, v8::Isolate* isolate) |
| 52 { | 50 { |
| 53 return m_map.get(key).newLocal(isolate); | 51 return m_map.Get(key); |
| 54 } | 52 } |
| 55 | 53 |
| 56 bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key
) | 54 bool setReturnValueFrom(v8::ReturnValue<v8::Value> returnValue, KeyType* key
) |
| 57 { | 55 { |
| 58 typename MapType::iterator it = m_map.find(key); | 56 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 } | 57 } |
| 64 | 58 |
| 65 void setReference(const v8::Persistent<v8::Object>& parent, KeyType* key, v8
::Isolate* isolate) | 59 void setReference(const v8::Persistent<v8::Object>& parent, KeyType* key, v8
::Isolate* isolate) |
| 66 { | 60 { |
| 67 m_map.get(key).setReferenceFrom(parent, isolate); | 61 m_map.SetReference(key, parent); |
| 68 } | 62 } |
| 69 | 63 |
| 70 bool containsKey(KeyType* key) | 64 bool containsKey(KeyType* key) |
| 71 { | 65 { |
| 72 return m_map.find(key) != m_map.end(); | 66 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 } | 67 } |
| 82 | 68 |
| 83 void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperConfigur
ation& configuration) | 69 void set(KeyType* key, v8::Handle<v8::Object> wrapper, const WrapperConfigur
ation& configuration) |
| 84 { | 70 { |
| 85 ASSERT(static_cast<KeyType*>(toNative(wrapper)) == key); | 71 ASSERT(static_cast<KeyType*>(toNative(wrapper)) == key); |
| 86 v8::Persistent<v8::Object> persistent(m_isolate, wrapper); | 72 v8::UniquePersistent<v8::Object> unique(m_isolate, wrapper); |
| 87 configuration.configureWrapper(&persistent); | 73 configuration.configureWrapper(&unique); |
| 88 persistent.SetWeak(this, &setWeakCallback); | 74 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 } | 75 } |
| 96 | 76 |
| 97 void clear() | 77 void clear() |
| 98 { | 78 { |
| 99 v8::HandleScope scope(m_isolate); | 79 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 } | 80 } |
| 110 | 81 |
| 111 void removeAndDispose(KeyType* key) | 82 void removeAndDispose(KeyType* key) |
| 112 { | 83 { |
| 113 typename MapType::iterator it = m_map.find(key); | 84 m_map.Remove(key); |
| 114 ASSERT_WITH_SECURITY_IMPLICATION(it != m_map.end()); | |
| 115 it->value.dispose(); | |
| 116 m_map.remove(it); | |
| 117 } | 85 } |
| 118 | 86 |
| 119 private: | 87 private: |
| 120 static void setWeakCallback(const v8::WeakCallbackData<v8::Object, DOMWrappe
rMap<KeyType> >&); | 88 class PersistentValueMapTraits { |
| 89 public: |
| 90 typedef HashMap<KeyType*, v8::PersistentContainerValue> Impl; |
| 91 typedef typename Impl::iterator Iterator; |
| 92 typedef Impl WeakCallbackDataType; |
| 93 |
| 94 static const bool kIsWeak = true; |
| 95 |
| 96 // Container API: Size/Empty/Swap/Begin/End/Value/Set/Get/Remove |
| 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 |
| 101 static Iterator Begin(Impl* impl) { return impl->begin(); } |
| 102 static Iterator End(Impl* impl) { return impl->end(); } |
| 103 static v8::PersistentContainerValue Value(Iterator& iter) |
| 104 { |
| 105 return iter->value; |
| 106 } |
| 107 static KeyType* Key(Iterator& iter) { return iter->key; } |
| 108 |
| 109 static v8::PersistentContainerValue Set( |
| 110 Impl* impl, KeyType* key, v8::PersistentContainerValue value) |
| 111 { |
| 112 v8::PersistentContainerValue oldValue = Get(impl, key); |
| 113 impl->add(key, value); |
| 114 return oldValue; |
| 115 } |
| 116 |
| 117 static v8::PersistentContainerValue Get(const Impl* impl, KeyType* key) |
| 118 { |
| 119 return impl->get(key); |
| 120 } |
| 121 |
| 122 static v8::PersistentContainerValue Remove(Impl* impl, KeyType* key) |
| 123 { |
| 124 return impl->take(key); |
| 125 } |
| 126 |
| 127 // Dispose callbacks: |
| 128 static void Dispose(v8::UniquePersistent<v8::Object> value, v8::Isolate*
isolate, Impl* impl, KeyType* key) { } |
| 129 |
| 130 // WeakCallbackDataType, create/dispose, conversion, callback. |
| 131 static WeakCallbackDataType* WeakCallbackParameter(Impl* impl, KeyType*
key, v8::Local<v8::Object>& value) |
| 132 { |
| 133 return impl; |
| 134 } |
| 135 |
| 136 static void DisposeCallbackData(WeakCallbackDataType* callbackData) { } |
| 137 |
| 138 static Impl* ImplFromWeakCallbackData( |
| 139 v8::WeakCallbackData<v8::Object, WeakCallbackDataType> data) |
| 140 { |
| 141 return data.GetParameter(); |
| 142 } |
| 143 |
| 144 static KeyType* KeyFromWeakCallbackData( |
| 145 v8::WeakCallbackData<v8::Object, WeakCallbackDataType> data) |
| 146 { |
| 147 return static_cast<KeyType*>(toNative(data.GetValue())); |
| 148 } |
| 149 }; |
| 121 | 150 |
| 122 v8::Isolate* m_isolate; | 151 v8::Isolate* m_isolate; |
| 123 MapType m_map; | 152 v8::PersistentValueMap<KeyType*, v8::Object, PersistentValueMapTraits> 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::UniquePersistent<v8::Object> value, |
| 158 v8::Isolate* isolate, |
| 159 Impl* impl, |
| 160 void* key) |
| 128 { | 161 { |
| 129 void* key = static_cast<void*>(toNative(data.GetValue())); | 162 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 } | 163 } |
| 134 | 164 |
| 135 } // namespace WebCore | 165 } // namespace WebCore |
| 136 | 166 |
| 137 #endif // DOMWrapperMap_h | 167 #endif // DOMWrapperMap_h |
| OLD | NEW |