| 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 16 matching lines...) Expand all Loading... |
| 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 DOMDataStore_h | 31 #ifndef DOMDataStore_h |
| 32 #define DOMDataStore_h | 32 #define DOMDataStore_h |
| 33 | 33 |
| 34 #include "DOMWrapperMap.h" | 34 #include "DOMWrapperMap.h" |
| 35 #include "DOMWrapperWorld.h" | 35 #include "DOMWrapperWorld.h" |
| 36 #include "Node.h" | 36 #include "Node.h" |
| 37 #include "V8GCController.h" | 37 #include "ScriptWrappable.h" |
| 38 #include "WrapperTypeInfo.h" | 38 #include "WrapperTypeInfo.h" |
| 39 #include <v8.h> | 39 #include <v8.h> |
| 40 #include <wtf/HashMap.h> | 40 #include <wtf/HashMap.h> |
| 41 #include <wtf/MainThread.h> | 41 #include <wtf/MainThread.h> |
| 42 #include <wtf/Noncopyable.h> | 42 #include <wtf/Noncopyable.h> |
| 43 #include <wtf/OwnPtr.h> | 43 #include <wtf/OwnPtr.h> |
| 44 #include <wtf/StdLibExtras.h> | 44 #include <wtf/StdLibExtras.h> |
| 45 #include <wtf/Threading.h> | 45 #include <wtf/Threading.h> |
| 46 #include <wtf/ThreadSpecific.h> | 46 #include <wtf/ThreadSpecific.h> |
| 47 #include <wtf/Vector.h> | 47 #include <wtf/Vector.h> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 59 template<typename T, typename HolderContainer, typename Wrappable> | 59 template<typename T, typename HolderContainer, typename Wrappable> |
| 60 static v8::Handle<v8::Object> getWrapperFast(T* object, const HolderContaine
r& container, Wrappable* holder) | 60 static v8::Handle<v8::Object> getWrapperFast(T* object, const HolderContaine
r& container, Wrappable* holder) |
| 61 { | 61 { |
| 62 // What we'd really like to check here is whether we're in the | 62 // What we'd really like to check here is whether we're in the |
| 63 // main world or in an isolated world. The fastest way to do that | 63 // main world or in an isolated world. The fastest way to do that |
| 64 // is to check that there is no isolated world and the 'object' | 64 // is to check that there is no isolated world and the 'object' |
| 65 // is an object that can exist in the main world. The second fastest | 65 // is an object that can exist in the main world. The second fastest |
| 66 // way is to check whether the wrappable's wrapper is the same as | 66 // way is to check whether the wrappable's wrapper is the same as |
| 67 // the holder. | 67 // the holder. |
| 68 if ((!DOMWrapperWorld::isolatedWorldsExist() && !canExistInWorker(object
)) || holderContainsWrapper(container, holder)) { | 68 if ((!DOMWrapperWorld::isolatedWorldsExist() && !canExistInWorker(object
)) || holderContainsWrapper(container, holder)) { |
| 69 if (mainWorldWrapperIsStoredInObject(object)) | 69 if (ScriptWrappable::wrapperCanBeStoredInObject(object)) |
| 70 return getWrapperFromObject(object); | 70 return ScriptWrappable::getWrapperFromObject(object); |
| 71 return mainWorldStore()->m_wrapperMap.get(object); | 71 return mainWorldStore()->m_wrapperMap.get(object); |
| 72 } | 72 } |
| 73 return current(container.GetIsolate())->get(object); | 73 return current(container.GetIsolate())->get(object); |
| 74 } | 74 } |
| 75 | 75 |
| 76 template<typename T> | 76 template<typename T> |
| 77 static v8::Handle<v8::Object> getWrapper(T* object, v8::Isolate* isolate) | 77 static v8::Handle<v8::Object> getWrapper(T* object, v8::Isolate* isolate) |
| 78 { | 78 { |
| 79 if (mainWorldWrapperIsStoredInObject(object) && !canExistInWorker(object
)) { | 79 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && !canExistInWo
rker(object)) { |
| 80 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist())) | 80 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist())) |
| 81 return getWrapperFromObject(object); | 81 return ScriptWrappable::getWrapperFromObject(object); |
| 82 } | 82 } |
| 83 return current(isolate)->get(object); | 83 return current(isolate)->get(object); |
| 84 } | 84 } |
| 85 | 85 |
| 86 template<typename T> | 86 template<typename T> |
| 87 static v8::Handle<v8::Object> getWrapperForMainWorld(T* object) | 87 static v8::Handle<v8::Object> getWrapperForMainWorld(T* object) |
| 88 { | 88 { |
| 89 if (mainWorldWrapperIsStoredInObject(object)) | 89 if (ScriptWrappable::wrapperCanBeStoredInObject(object)) |
| 90 return getWrapperFromObject(object); | 90 return ScriptWrappable::getWrapperFromObject(object); |
| 91 return mainWorldStore()->get(object); | 91 return mainWorldStore()->get(object); |
| 92 } | 92 } |
| 93 | 93 |
| 94 template<typename T> | 94 template<typename T> |
| 95 static void setWrapper(T* object, v8::Handle<v8::Object> wrapper, v8::Isolat
e* isolate, const WrapperConfiguration& configuration) | 95 static void setWrapper(T* object, v8::Handle<v8::Object> wrapper, v8::Isolat
e* isolate, const WrapperConfiguration& configuration) |
| 96 { | 96 { |
| 97 if (mainWorldWrapperIsStoredInObject(object) && !canExistInWorker(object
)) { | 97 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && !canExistInWo
rker(object)) { |
| 98 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist())) { | 98 if (LIKELY(!DOMWrapperWorld::isolatedWorldsExist())) { |
| 99 setWrapperInObject(object, wrapper, isolate, configuration); | 99 ScriptWrappable::setWrapperInObject(object, wrapper, isolate, co
nfiguration); |
| 100 return; | 100 return; |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 return current(isolate)->set(object, wrapper, isolate, configuration); | 103 return current(isolate)->set(object, wrapper, isolate, configuration); |
| 104 } | 104 } |
| 105 | 105 |
| 106 template<typename T> | 106 template<typename T> |
| 107 inline v8::Handle<v8::Object> get(T* object) | 107 inline v8::Handle<v8::Object> get(T* object) |
| 108 { | 108 { |
| 109 if (mainWorldWrapperIsStoredInObject(object) && m_type == MainWorld) | 109 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == Mai
nWorld) |
| 110 return getWrapperFromObject(object); | 110 return ScriptWrappable::getWrapperFromObject(object); |
| 111 return m_wrapperMap.get(object); | 111 return m_wrapperMap.get(object); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void reportMemoryUsage(MemoryObjectInfo*) const; | 114 void reportMemoryUsage(MemoryObjectInfo*) const; |
| 115 | 115 |
| 116 private: | 116 private: |
| 117 template<typename T> | 117 template<typename T> |
| 118 inline void set(T* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isol
ate, const WrapperConfiguration& configuration) | 118 inline void set(T* object, v8::Handle<v8::Object> wrapper, v8::Isolate* isol
ate, const WrapperConfiguration& configuration) |
| 119 { | 119 { |
| 120 ASSERT(!!object); | 120 ASSERT(!!object); |
| 121 ASSERT(!wrapper.IsEmpty()); | 121 ASSERT(!wrapper.IsEmpty()); |
| 122 if (mainWorldWrapperIsStoredInObject(object) && m_type == MainWorld) { | 122 if (ScriptWrappable::wrapperCanBeStoredInObject(object) && m_type == Mai
nWorld) { |
| 123 setWrapperInObject(object, wrapper, isolate, configuration); | 123 ScriptWrappable::setWrapperInObject(object, wrapper, isolate, config
uration); |
| 124 return; | 124 return; |
| 125 } | 125 } |
| 126 m_wrapperMap.set(object, wrapper, configuration); | 126 m_wrapperMap.set(object, wrapper, configuration); |
| 127 } | 127 } |
| 128 | 128 |
| 129 static DOMDataStore* mainWorldStore(); | 129 static DOMDataStore* mainWorldStore(); |
| 130 | 130 |
| 131 static bool mainWorldWrapperIsStoredInObject(void*) { return false; } | |
| 132 static bool mainWorldWrapperIsStoredInObject(ScriptWrappable*) { return true
; } | |
| 133 | |
| 134 static bool canExistInWorker(void*) { return true; } | 131 static bool canExistInWorker(void*) { return true; } |
| 135 static bool canExistInWorker(Node*) { return false; } | 132 static bool canExistInWorker(Node*) { return false; } |
| 136 | 133 |
| 137 template<typename HolderContainer> | 134 template<typename HolderContainer> |
| 138 static bool holderContainsWrapper(const HolderContainer&, void*) | 135 static bool holderContainsWrapper(const HolderContainer&, void*) |
| 139 { | 136 { |
| 140 return false; | 137 return false; |
| 141 } | 138 } |
| 139 |
| 142 template<typename HolderContainer> | 140 template<typename HolderContainer> |
| 143 static bool holderContainsWrapper(const HolderContainer& container, ScriptWr
appable* wrappable) | 141 static bool holderContainsWrapper(const HolderContainer& container, ScriptWr
appable* wrappable) |
| 144 { | 142 { |
| 145 // Verify our assumptions about the main world. | 143 // Verify our assumptions about the main world. |
| 146 ASSERT(wrappable->wrapper().IsEmpty() || container.Holder() != wrappable
->wrapper() || current(v8::Isolate::GetCurrent())->m_type == MainWorld); | 144 ASSERT(wrappable->wrapper().IsEmpty() || container.Holder() != wrappable
->wrapper() || current(v8::Isolate::GetCurrent())->m_type == MainWorld); |
| 147 return container.Holder() == wrappable->wrapper(); | 145 return container.Holder() == wrappable->wrapper(); |
| 148 } | 146 } |
| 149 | 147 |
| 150 static v8::Handle<v8::Object> getWrapperFromObject(void*) | |
| 151 { | |
| 152 ASSERT_NOT_REACHED(); | |
| 153 return v8::Handle<v8::Object>(); | |
| 154 } | |
| 155 static v8::Handle<v8::Object> getWrapperFromObject(ScriptWrappable* object) | |
| 156 { | |
| 157 return object->wrapper(); | |
| 158 } | |
| 159 | |
| 160 static void setWrapperInObject(void*, v8::Handle<v8::Object>, v8::Isolate*,
const WrapperConfiguration&) | |
| 161 { | |
| 162 ASSERT_NOT_REACHED(); | |
| 163 } | |
| 164 static void setWrapperInObject(ScriptWrappable* object, v8::Handle<v8::Objec
t> wrapper, v8::Isolate* isolate, const WrapperConfiguration& configuration) | |
| 165 { | |
| 166 object->setWrapper(wrapper, isolate, configuration); | |
| 167 } | |
| 168 | |
| 169 WrapperWorldType m_type; | 148 WrapperWorldType m_type; |
| 170 DOMWrapperMap<void> m_wrapperMap; | 149 DOMWrapperMap<void> m_wrapperMap; |
| 171 }; | 150 }; |
| 172 | 151 |
| 173 template<> | 152 template<> |
| 174 inline void WeakHandleListener<DOMWrapperMap<void> >::callback(v8::Isolate* isol
ate, v8::Persistent<v8::Value> value, DOMWrapperMap<void>* map) | 153 inline void WeakHandleListener<DOMWrapperMap<void> >::callback(v8::Isolate* isol
ate, v8::Persistent<v8::Value> value, DOMWrapperMap<void>* map) |
| 175 { | 154 { |
| 176 ASSERT(value->IsObject()); | 155 ASSERT(value->IsObject()); |
| 177 v8::Persistent<v8::Object> wrapper = v8::Persistent<v8::Object>::Cast(value)
; | 156 v8::Persistent<v8::Object> wrapper = v8::Persistent<v8::Object>::Cast(value)
; |
| 178 WrapperTypeInfo* type = toWrapperTypeInfo(wrapper); | 157 WrapperTypeInfo* type = toWrapperTypeInfo(wrapper); |
| 179 ASSERT(type->derefObjectFunction); | 158 ASSERT(type->derefObjectFunction); |
| 180 void* key = static_cast<void*>(toNative(wrapper)); | 159 void* key = static_cast<void*>(toNative(wrapper)); |
| 181 map->removeAndDispose(key, wrapper, isolate); | 160 map->removeAndDispose(key, wrapper, isolate); |
| 182 type->derefObject(key); | 161 type->derefObject(key); |
| 183 } | 162 } |
| 184 | 163 |
| 185 } // namespace WebCore | 164 } // namespace WebCore |
| 186 | 165 |
| 187 #endif // DOMDataStore_h | 166 #endif // DOMDataStore_h |
| OLD | NEW |