Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * Copyright (C) 2010 Google Inc. All rights reserved. | 2 * Copyright (C) 2010 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 21 matching lines...) Expand all Loading... | |
| 32 #define WebPrivatePtr_h | 32 #define WebPrivatePtr_h |
| 33 | 33 |
| 34 #include "WebCommon.h" | 34 #include "WebCommon.h" |
| 35 | 35 |
| 36 #if INSIDE_BLINK | 36 #if INSIDE_BLINK |
| 37 #include "platform/heap/Handle.h" | 37 #include "platform/heap/Handle.h" |
| 38 #include "wtf/PassRefPtr.h" | 38 #include "wtf/PassRefPtr.h" |
| 39 #include "wtf/TypeTraits.h" | 39 #include "wtf/TypeTraits.h" |
| 40 #endif | 40 #endif |
| 41 | 41 |
| 42 namespace WTF { | |
| 43 template<class T> class ThreadSafeRefCounted; | |
| 44 } | |
| 45 | |
| 42 namespace blink { | 46 namespace blink { |
| 43 | 47 |
| 48 // By default, the destruction of a WebPrivatePtr<> must happen on the same | |
| 49 // thread, but can be allowed to happen on another thread. | |
| 50 enum WebPrivatePtrDestruction { | |
| 51 SameThreadDestruction, | |
|
tkent
2015/07/23 23:28:09
The enum items should be WebPrivatePtrDestruction
sof
2015/07/24 05:25:21
Thanks, aligned. And preferable to export & use th
| |
| 52 AllowCrossThreadDestruction, | |
| 53 }; | |
| 54 | |
| 44 #if INSIDE_BLINK | 55 #if INSIDE_BLINK |
| 45 enum LifetimeManagementType { | 56 enum LifetimeManagementType { |
| 46 RefCountedLifetime, | 57 RefCountedLifetime, |
| 47 GarbageCollectedLifetime, | 58 GarbageCollectedLifetime, |
| 48 RefCountedGarbageCollectedLifetime | 59 RefCountedGarbageCollectedLifetime |
| 49 }; | 60 }; |
| 50 | 61 |
| 51 template<typename T> | 62 template<typename T> |
| 52 class LifetimeOf { | 63 class LifetimeOf { |
| 53 static const bool isGarbageCollected = WTF::IsSubclassOfTemplate<T, GarbageC ollected>::value || IsGarbageCollectedMixin<T>::value; | 64 static const bool isGarbageCollected = WTF::IsSubclassOfTemplate<T, GarbageC ollected>::value || IsGarbageCollectedMixin<T>::value; |
| 54 static const bool isRefCountedGarbageCollected = WTF::IsSubclassOfTemplate<T , RefCountedGarbageCollected>::value; | 65 static const bool isRefCountedGarbageCollected = WTF::IsSubclassOfTemplate<T , RefCountedGarbageCollected>::value; |
| 55 public: | 66 public: |
| 56 static const LifetimeManagementType value = | 67 static const LifetimeManagementType value = |
| 57 !isGarbageCollected ? RefCountedLifetime : | 68 !isGarbageCollected ? RefCountedLifetime : |
| 58 isRefCountedGarbageCollected ? RefCountedGarbageCollectedLifetime : Garb ageCollectedLifetime; | 69 isRefCountedGarbageCollected ? RefCountedGarbageCollectedLifetime : Garb ageCollectedLifetime; |
| 59 }; | 70 }; |
| 60 | 71 |
| 61 template<typename T, LifetimeManagementType lifetime> | 72 template<typename T, WebPrivatePtrDestruction crossThreadDestruction, LifetimeMa nagementType lifetime> |
| 62 class PtrStorageImpl; | 73 class PtrStorageImpl; |
| 63 | 74 |
| 64 template<typename T> | 75 template<typename T, WebPrivatePtrDestruction crossThreadDestruction> |
| 65 class PtrStorageImpl<T, RefCountedLifetime> { | 76 class PtrStorageImpl<T, crossThreadDestruction, RefCountedLifetime> { |
| 66 public: | 77 public: |
| 67 typedef PassRefPtr<T> BlinkPtrType; | 78 typedef PassRefPtr<T> BlinkPtrType; |
| 68 | 79 |
| 69 void assign(const BlinkPtrType& val) | 80 void assign(const BlinkPtrType& val) |
| 70 { | 81 { |
| 82 static_assert(crossThreadDestruction == SameThreadDestruction || WTF::Is SubclassOfTemplate<T, WTF::ThreadSafeRefCounted>::value, "Cross thread destructi ble class must derive from ThreadSafeRefCounted<>"); | |
| 71 release(); | 83 release(); |
| 72 m_ptr = val.leakRef(); | 84 m_ptr = val.leakRef(); |
| 73 } | 85 } |
| 74 | 86 |
| 75 void assign(const PtrStorageImpl& other) | 87 void assign(const PtrStorageImpl& other) |
| 76 { | 88 { |
| 77 release(); | 89 release(); |
| 78 T* val = other.get(); | 90 T* val = other.get(); |
| 79 WTF::refIfNotNull(val); | 91 WTF::refIfNotNull(val); |
| 80 m_ptr = val; | 92 m_ptr = val; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 92 void release() | 104 void release() |
| 93 { | 105 { |
| 94 WTF::derefIfNotNull(m_ptr); | 106 WTF::derefIfNotNull(m_ptr); |
| 95 m_ptr = 0; | 107 m_ptr = 0; |
| 96 } | 108 } |
| 97 | 109 |
| 98 private: | 110 private: |
| 99 T* m_ptr; | 111 T* m_ptr; |
| 100 }; | 112 }; |
| 101 | 113 |
| 102 template<typename T> | 114 template <typename T, WebPrivatePtrDestruction> |
| 103 class PtrStorageImpl<T, GarbageCollectedLifetime> { | 115 struct WebPrivatePtrPersistentStorageType { |
| 116 public: | |
| 117 using Type = Persistent<T>; | |
| 118 }; | |
| 119 | |
| 120 template <typename T> | |
| 121 struct WebPrivatePtrPersistentStorageType<T, AllowCrossThreadDestruction> { | |
| 122 public: | |
| 123 using Type = CrossThreadPersistent<T>; | |
| 124 }; | |
| 125 | |
| 126 template<typename T, WebPrivatePtrDestruction crossThreadDestruction> | |
| 127 class PtrStorageImpl<T, crossThreadDestruction, GarbageCollectedLifetime> { | |
| 104 public: | 128 public: |
| 105 void assign(const RawPtr<T>& val) | 129 void assign(const RawPtr<T>& val) |
| 106 { | 130 { |
| 107 if (!val) { | 131 if (!val) { |
| 108 release(); | 132 release(); |
| 109 return; | 133 return; |
| 110 } | 134 } |
| 111 | 135 |
| 112 if (!m_handle) | 136 if (!m_handle) |
| 113 m_handle = new Persistent<T>(); | 137 m_handle = new (typename WebPrivatePtrPersistentStorageType<T, cross ThreadDestruction>::Type)(); |
| 114 | 138 |
| 115 (*m_handle) = val; | 139 (*m_handle) = val; |
| 116 } | 140 } |
| 117 | 141 |
| 118 void assign(T* ptr) { assign(RawPtr<T>(ptr)); } | 142 void assign(T* ptr) { assign(RawPtr<T>(ptr)); } |
| 119 template<typename U> void assign(const RawPtr<U>& val) { assign(RawPtr<T>(va l)); } | 143 template<typename U> void assign(const RawPtr<U>& val) { assign(RawPtr<T>(va l)); } |
| 120 | 144 |
| 121 void assign(const PtrStorageImpl& other) { assign(other.get()); } | 145 void assign(const PtrStorageImpl& other) { assign(other.get()); } |
| 122 | 146 |
| 123 void moveFrom(PtrStorageImpl& other) | 147 void moveFrom(PtrStorageImpl& other) |
| 124 { | 148 { |
| 125 release(); | 149 release(); |
| 126 m_handle = other.m_handle; | 150 m_handle = other.m_handle; |
| 127 other.m_handle = 0; | 151 other.m_handle = 0; |
| 128 } | 152 } |
| 129 | 153 |
| 130 T* get() const { return m_handle ? m_handle->get() : 0; } | 154 T* get() const { return m_handle ? m_handle->get() : 0; } |
| 131 | 155 |
| 132 void release() | 156 void release() |
| 133 { | 157 { |
| 134 delete m_handle; | 158 delete m_handle; |
| 135 m_handle = 0; | 159 m_handle = 0; |
| 136 } | 160 } |
| 137 | 161 |
| 138 private: | 162 private: |
| 139 Persistent<T>* m_handle; | 163 typename WebPrivatePtrPersistentStorageType<T, crossThreadDestruction>::Type * m_handle; |
| 140 }; | 164 }; |
| 141 | 165 |
| 142 template<typename T> | 166 template<typename T, WebPrivatePtrDestruction crossThreadDestruction> |
| 143 class PtrStorageImpl<T, RefCountedGarbageCollectedLifetime> : public PtrStorageI mpl<T, GarbageCollectedLifetime> { | 167 class PtrStorageImpl<T, crossThreadDestruction, RefCountedGarbageCollectedLifeti me> : public PtrStorageImpl<T, crossThreadDestruction, GarbageCollectedLifetime> { |
| 144 public: | 168 public: |
| 145 void assign(const PassRefPtrWillBeRawPtr<T>& val) { PtrStorageImpl<T, Garbag eCollectedLifetime>::assign(val.get()); } | 169 void assign(const PassRefPtrWillBeRawPtr<T>& val) { PtrStorageImpl<T, crossT hreadDestruction, GarbageCollectedLifetime>::assign(val.get()); } |
| 146 | 170 |
| 147 void assign(const PtrStorageImpl& other) { PtrStorageImpl<T, GarbageCollecte dLifetime>::assign(other.get()); } | 171 void assign(const PtrStorageImpl& other) { PtrStorageImpl<T, crossThreadDest ruction, GarbageCollectedLifetime>::assign(other.get()); } |
| 148 }; | 172 }; |
| 149 | 173 |
| 150 template<typename T> | 174 template<typename T, WebPrivatePtrDestruction crossThreadDestruction> |
| 151 class PtrStorage : public PtrStorageImpl<T, LifetimeOf<T>::value> { | 175 class PtrStorage : public PtrStorageImpl<T, crossThreadDestruction, LifetimeOf<T >::value> { |
| 152 public: | 176 public: |
| 153 static PtrStorage& fromSlot(void** slot) | 177 static PtrStorage& fromSlot(void** slot) |
| 154 { | 178 { |
| 155 static_assert(sizeof(PtrStorage) == sizeof(void*), "PtrStorage must be t he size of a pointer"); | 179 static_assert(sizeof(PtrStorage) == sizeof(void*), "PtrStorage must be t he size of a pointer"); |
| 156 return *reinterpret_cast<PtrStorage*>(slot); | 180 return *reinterpret_cast<PtrStorage*>(slot); |
| 157 } | 181 } |
| 158 | 182 |
| 159 static const PtrStorage& fromSlot(void* const* slot) | 183 static const PtrStorage& fromSlot(void* const* slot) |
| 160 { | 184 { |
| 161 static_assert(sizeof(PtrStorage) == sizeof(void*), "PtrStorage must be t he size of a pointer"); | 185 static_assert(sizeof(PtrStorage) == sizeof(void*), "PtrStorage must be t he size of a pointer"); |
| 162 return *reinterpret_cast<const PtrStorage*>(slot); | 186 return *reinterpret_cast<const PtrStorage*>(slot); |
| 163 } | 187 } |
| 164 | 188 |
| 165 private: | 189 private: |
| 166 // Prevent construction via normal means. | 190 // Prevent construction via normal means. |
| 167 PtrStorage(); | 191 PtrStorage(); |
| 168 PtrStorage(const PtrStorage&); | 192 PtrStorage(const PtrStorage&); |
| 169 }; | 193 }; |
| 170 #endif | 194 #endif |
| 171 | 195 |
| 172 | |
| 173 // This class is an implementation detail of the Blink API. It exists to help | 196 // This class is an implementation detail of the Blink API. It exists to help |
| 174 // simplify the implementation of Blink interfaces that merely wrap a reference | 197 // simplify the implementation of Blink interfaces that merely wrap a reference |
| 175 // counted WebCore class. | 198 // counted WebCore class. |
| 176 // | 199 // |
| 177 // A typical implementation of a class which uses WebPrivatePtr might look like | 200 // A typical implementation of a class which uses WebPrivatePtr might look like |
| 178 // this: | 201 // this: |
| 179 // class WebFoo { | 202 // class WebFoo { |
| 180 // public: | 203 // public: |
| 181 // BLINK_EXPORT ~WebFoo(); | 204 // BLINK_EXPORT ~WebFoo(); |
| 182 // WebFoo() { } | 205 // WebFoo() { } |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 199 // #endif | 222 // #endif |
| 200 // | 223 // |
| 201 // private: | 224 // private: |
| 202 // WebPrivatePtr<Foo> m_private; | 225 // WebPrivatePtr<Foo> m_private; |
| 203 // }; | 226 // }; |
| 204 // | 227 // |
| 205 // // WebFoo.cpp | 228 // // WebFoo.cpp |
| 206 // WebFoo::~WebFoo() { m_private.reset(); } | 229 // WebFoo::~WebFoo() { m_private.reset(); } |
| 207 // void WebFoo::assign(const WebFoo& other) { ... } | 230 // void WebFoo::assign(const WebFoo& other) { ... } |
| 208 // | 231 // |
| 209 template <typename T> | 232 template <typename T, WebPrivatePtrDestruction crossThreadDestruction = SameThre adDestruction> |
| 210 class WebPrivatePtr { | 233 class WebPrivatePtr { |
| 211 public: | 234 public: |
| 212 WebPrivatePtr() : m_storage(0) { } | 235 WebPrivatePtr() : m_storage(0) { } |
| 213 ~WebPrivatePtr() | 236 ~WebPrivatePtr() |
| 214 { | 237 { |
| 215 // We don't destruct the object pointed by m_ptr here because we don't | 238 // We don't destruct the object pointed by m_ptr here because we don't |
| 216 // want to expose destructors of core classes to embedders. We should | 239 // want to expose destructors of core classes to embedders. We should |
| 217 // call reset() manually in destructors of classes with WebPrivatePtr | 240 // call reset() manually in destructors of classes with WebPrivatePtr |
| 218 // members. | 241 // members. |
| 219 BLINK_ASSERT(!m_storage); | 242 BLINK_ASSERT(!m_storage); |
| 220 } | 243 } |
| 221 | 244 |
| 222 bool isNull() const { return !m_storage; } | 245 bool isNull() const { return !m_storage; } |
| 223 | 246 |
| 224 #if INSIDE_BLINK | 247 #if INSIDE_BLINK |
| 225 template<typename U> | 248 template<typename U> |
| 226 WebPrivatePtr(const U& ptr) | 249 WebPrivatePtr(const U& ptr) |
| 227 : m_storage(0) | 250 : m_storage(0) |
| 228 { | 251 { |
| 229 storage().assign(ptr); | 252 storage().assign(ptr); |
| 230 } | 253 } |
| 231 | 254 |
| 232 void reset() { storage().release(); } | 255 void reset() { storage().release(); } |
| 233 | 256 |
| 234 WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other) | 257 WebPrivatePtr& operator=(const WebPrivatePtr& other) |
| 235 { | 258 { |
| 236 storage().assign(other.storage()); | 259 storage().assign(other.storage()); |
| 237 return *this; | 260 return *this; |
| 238 } | 261 } |
| 239 | 262 |
| 240 void moveFrom(WebPrivatePtr<T>& other) | 263 void moveFrom(WebPrivatePtr& other) |
| 241 { | 264 { |
| 242 storage().moveFrom(other.storage()); | 265 storage().moveFrom(other.storage()); |
| 243 return; | 266 return; |
| 244 } | 267 } |
| 245 | 268 |
| 246 template<typename U> | 269 template<typename U> |
| 247 WebPrivatePtr<T>& operator=(const U& ptr) | 270 WebPrivatePtr& operator=(const U& ptr) |
| 248 { | 271 { |
| 249 storage().assign(ptr); | 272 storage().assign(ptr); |
| 250 return *this; | 273 return *this; |
| 251 } | 274 } |
| 252 | 275 |
| 253 T* get() const { return storage().get(); } | 276 T* get() const { return storage().get(); } |
| 254 | 277 |
| 255 T& operator*() const | 278 T& operator*() const |
| 256 { | 279 { |
| 257 ASSERT(m_storage); | 280 ASSERT(m_storage); |
| 258 return *get(); | 281 return *get(); |
| 259 } | 282 } |
| 260 | 283 |
| 261 T* operator->() const | 284 T* operator->() const |
| 262 { | 285 { |
| 263 ASSERT(m_storage); | 286 ASSERT(m_storage); |
| 264 return get(); | 287 return get(); |
| 265 } | 288 } |
| 266 #endif | 289 #endif |
| 267 | 290 |
| 268 private: | 291 private: |
| 269 #if INSIDE_BLINK | 292 #if INSIDE_BLINK |
| 270 PtrStorage<T>& storage() { return PtrStorage<T>::fromSlot(&m_storage); } | 293 PtrStorage<T, crossThreadDestruction>& storage() { return PtrStorage<T, cros sThreadDestruction>::fromSlot(&m_storage); } |
| 271 const PtrStorage<T>& storage() const { return PtrStorage<T>::fromSlot(&m_sto rage); } | 294 const PtrStorage<T, crossThreadDestruction>& storage() const { return PtrSto rage<T, crossThreadDestruction>::fromSlot(&m_storage); } |
| 272 #endif | 295 #endif |
| 273 | 296 |
| 274 #if !INSIDE_BLINK | 297 #if !INSIDE_BLINK |
| 275 // Disable the assignment operator; we define it above for when | 298 // Disable the assignment operator; we define it above for when |
| 276 // INSIDE_BLINK is set, but we need to make sure that it is not | 299 // INSIDE_BLINK is set, but we need to make sure that it is not |
| 277 // used outside there; the compiler-provided version won't handle reference | 300 // used outside there; the compiler-provided version won't handle reference |
| 278 // counting properly. | 301 // counting properly. |
| 279 WebPrivatePtr<T>& operator=(const WebPrivatePtr<T>& other); | 302 WebPrivatePtr& operator=(const WebPrivatePtr& other); |
| 280 #endif | 303 #endif |
| 281 // Disable the copy constructor; classes that contain a WebPrivatePtr | 304 // Disable the copy constructor; classes that contain a WebPrivatePtr |
| 282 // should implement their copy constructor using assign(). | 305 // should implement their copy constructor using assign(). |
| 283 WebPrivatePtr(const WebPrivatePtr<T>&); | 306 WebPrivatePtr(const WebPrivatePtr&); |
| 284 | 307 |
| 285 void* m_storage; | 308 void* m_storage; |
| 286 }; | 309 }; |
| 287 | 310 |
| 288 } // namespace blink | 311 } // namespace blink |
| 289 | 312 |
| 290 #endif | 313 #endif |
| OLD | NEW |