| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2009 Google Inc. All rights reserved. | 2 * Copyright (C) 2009 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 15 matching lines...) Expand all Loading... |
| 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 V8EventListenerList_h | 31 #ifndef V8EventListenerList_h |
| 32 #define V8EventListenerList_h | 32 #define V8EventListenerList_h |
| 33 | 33 |
| 34 #include <v8.h> | 34 #include <v8.h> |
| 35 #include <wtf/Vector.h> | 35 #include <wtf/Vector.h> |
| 36 #include <wtf/HashMap.h> |
| 36 | 37 |
| 37 namespace WebCore { | 38 namespace WebCore { |
| 38 class V8EventListener; | 39 class V8EventListener; |
| 40 class V8EventListenerListIterator; |
| 39 | 41 |
| 40 // This is a container for V8EventListener objects that also does some cachi
ng to speed up finding entries by v8::Object. | 42 // This is a container for V8EventListener objects that uses the identity ha
sh of the v8::Object to |
| 43 // speed up lookups |
| 41 class V8EventListenerList { | 44 class V8EventListenerList { |
| 42 public: | 45 public: |
| 43 static const size_t maxKeyNameLength = 254; | 46 // Because v8::Object identity hashes are not guaranteed to be unique, w
e unfortunately can't just map |
| 47 // an int to V8EventListener. Instead we define a HashMap of int to Vect
or of V8EventListener |
| 48 // called a ListenerMultiMap. |
| 49 typedef Vector<V8EventListener*>* Values; |
| 50 struct ValuesTraits : HashTraits<Values> { |
| 51 static const bool needsDestruction = true; |
| 52 }; |
| 53 typedef HashMap<int, Values, DefaultHash<int>::Hash, HashTraits<int>, Va
luesTraits> ListenerMultiMap; |
| 44 | 54 |
| 45 // The name should be distinct from any other V8EventListenerList within
the same process, and <= maxKeyNameLength characters. | 55 V8EventListenerList(); |
| 46 explicit V8EventListenerList(const char* name); | |
| 47 ~V8EventListenerList(); | 56 ~V8EventListenerList(); |
| 48 | 57 |
| 49 typedef Vector<V8EventListener*>::iterator iterator; | 58 friend class V8EventListenerListIterator; |
| 50 V8EventListenerList::iterator begin(); | 59 typedef V8EventListenerListIterator iterator; |
| 60 |
| 61 iterator begin(); |
| 51 iterator end(); | 62 iterator end(); |
| 52 | 63 |
| 53 // In addition to adding the listener to this list, this also caches the
V8EventListener as a hidden property on its wrapped | |
| 54 // v8 listener object, so we can quickly look it up later. | |
| 55 void add(V8EventListener*); | 64 void add(V8EventListener*); |
| 56 void remove(V8EventListener*); | 65 void remove(V8EventListener*); |
| 57 V8EventListener* find(v8::Local<v8::Object>, bool isInline); | 66 V8EventListener* find(v8::Local<v8::Object>, bool isAttribute); |
| 58 void clear(); | 67 void clear(); |
| 59 size_t size() { return m_list.size(); } | 68 size_t size() { return m_table.size(); } |
| 60 | 69 |
| 61 private: | 70 private: |
| 62 v8::Handle<v8::String> getKey(bool isInline); | 71 ListenerMultiMap m_table; |
| 63 v8::Persistent<v8::String> m_inlineKey; | 72 |
| 64 v8::Persistent<v8::String> m_nonInlineKey; | 73 // we also keep a reverse mapping of V8EventListener to v8::Object ident
ity hash, |
| 65 Vector<V8EventListener*> m_list; | 74 // in order to speed up removal by V8EventListener |
| 75 HashMap<V8EventListener*, int> m_reverseTable; |
| 76 }; |
| 77 |
| 78 class V8EventListenerListIterator { |
| 79 public: |
| 80 ~V8EventListenerListIterator(); |
| 81 void operator++(); |
| 82 bool operator==(const V8EventListenerListIterator&); |
| 83 bool operator!=(const V8EventListenerListIterator&); |
| 84 V8EventListener* operator*(); |
| 85 private: |
| 86 friend class V8EventListenerList; |
| 87 explicit V8EventListenerListIterator(V8EventListenerList*); |
| 88 V8EventListenerListIterator(V8EventListenerList*, bool shouldSeekToEnd); |
| 89 void seekToEnd(); |
| 90 |
| 91 V8EventListenerList* m_list; |
| 92 V8EventListenerList::ListenerMultiMap::iterator m_iter; |
| 93 size_t m_vectorIndex; |
| 66 }; | 94 }; |
| 67 | 95 |
| 68 } // namespace WebCore | 96 } // namespace WebCore |
| 69 | 97 |
| 70 #endif // V8EventListenerList_h | 98 #endif // V8EventListenerList_h |
| OLD | NEW |