Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 /* Copyright (c) 2014 The Chromium Authors. All rihts reserved. | |
| 2 * Use of this source code is governed by a BSD-style license that can be | |
| 3 * found in the LICENSE file. | |
| 4 */ | |
| 5 | |
| 6 #ifndef WebPrivateGarbageCollectedPtr_h | |
| 7 #define WebPrivateGarbageCollectedPtr_h | |
| 8 | |
| 9 #include "WebCommon.h" | |
| 10 | |
| 11 #if INSIDE_BLINK | |
| 12 #include "heap/Handle.h" | |
| 13 #include "wtf/PassRefPtr.h" | |
| 14 #endif | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 // A variant of WebPrivatePtr that holds a Blink object that's garbage | |
| 19 // collected, without exposing the details of how (and with what | |
| 20 // types.) If no support for having such objects (i.e., no Oilpan), | |
| 21 // WebPrivateGarbageCollectedPtr is equal to WebPrivatePtr and the | |
| 22 // object will be reference counted. | |
| 23 // | |
| 24 // See WebPrivatePtr.h comment for how to make use of | |
| 25 // WebPrivateGarbageCollectedPtr; same pattern applies, but for one | |
| 26 // detail. Instead of declaring & defining, | |
| 27 // | |
| 28 // #if INSIDE_BLINK | |
| 29 // WebFoo(const WTF::PassRefPtr<WebCore::Foo>&); | |
| 30 // #endif | |
| 31 // | |
| 32 // provide | |
| 33 // | |
| 34 // #if INSIDE_BLINK | |
| 35 // WebFoo(const WTF::PassRefPtrWillBeRawPtr<WebCore::Foo>&); | |
| 36 // #endif | |
| 37 // | |
| 38 template <typename T> | |
| 39 class WebPrivateGarbageCollectedPtr { | |
| 40 public: | |
| 41 WebPrivateGarbageCollectedPtr() : m_ptr(0) { } | |
| 42 ~WebPrivateGarbageCollectedPtr() | |
| 43 { | |
| 44 // We don't destruct the object pointed by m_ptr here because we don't | |
| 45 // want to expose destructors of core classes to embedders. We should | |
| 46 // call reset() manually in destructors of classes with WebPrivateGarbag eCollectedPtr | |
| 47 // members. | |
| 48 BLINK_ASSERT(!m_ptr); | |
| 49 } | |
| 50 | |
| 51 bool isNull() const { return !m_ptr; } | |
| 52 | |
| 53 #if INSIDE_BLINK | |
| 54 #if ENABLE(OILPAN) | |
| 55 WebPrivateGarbageCollectedPtr(const WTF::PassRefPtrWillBeRawPtr<T>& p) | |
|
Mads Ager (chromium)
2014/02/17 15:18:40
I think we have the transition types in the WebCor
| |
| 56 { | |
| 57 assign(p); | |
| 58 } | |
| 59 | |
| 60 void reset() | |
| 61 { | |
| 62 if (m_ptr) { | |
| 63 delete reinterpret_cast<WebCore::Persistent<T>*>(m_ptr); | |
| 64 m_ptr = 0; | |
| 65 } | |
| 66 } | |
| 67 | |
| 68 WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollected Ptr<T>& other) | |
| 69 { | |
| 70 assign(other.get()); | |
| 71 return *this; | |
| 72 } | |
| 73 | |
| 74 WebPrivateGarbageCollectedPtr<T>& operator=(const WTF::PassRefPtrWillBeRawPt r<T>& p) | |
| 75 { | |
| 76 assign(p); | |
| 77 return *this; | |
| 78 } | |
| 79 | |
| 80 T* get() const | |
| 81 { | |
| 82 if (!m_ptr) | |
| 83 return 0; | |
| 84 return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get(); | |
| 85 } | |
| 86 | |
| 87 T* operator->() const | |
| 88 { | |
| 89 ASSERT(m_ptr); | |
| 90 return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get(); | |
| 91 } | |
| 92 #else | |
| 93 WebPrivateGarbageCollectedPtr(const WTF::PassRefPtrWillBeRawPtr<T>& p) | |
| 94 { | |
| 95 m_ptr = p.leakRef(); | |
| 96 } | |
| 97 | |
| 98 void reset() | |
| 99 { | |
| 100 assign(0); | |
| 101 } | |
| 102 | |
| 103 WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollected Ptr<T>& other) | |
| 104 { | |
| 105 T* p = other.m_ptr; | |
| 106 if (p) | |
| 107 p->ref(); | |
| 108 assign(p); | |
| 109 return *this; | |
| 110 } | |
| 111 | |
| 112 WebPrivateGarbageCollectedPtr<T>& operator=(const WTF::PassRefPtrWillBeRawPt r<T>& p) | |
| 113 { | |
| 114 assign(p.leakRef()); | |
| 115 return *this; | |
| 116 } | |
| 117 | |
| 118 T* get() const | |
| 119 { | |
| 120 return m_ptr; | |
| 121 } | |
| 122 | |
| 123 T* operator->() const | |
| 124 { | |
| 125 ASSERT(m_ptr); | |
| 126 return m_ptr; | |
| 127 } | |
| 128 #endif | |
| 129 #endif | |
| 130 | |
| 131 private: | |
| 132 #if INSIDE_BLINK | |
| 133 #if ENABLE(OILPAN) | |
| 134 void assign(T* p) | |
| 135 { | |
| 136 if (!p) { | |
| 137 reset(); | |
| 138 return; | |
| 139 } | |
| 140 // Keep an opaque pointer object pointer to avoid exposing | |
|
Mads Ager (chromium)
2014/02/17 15:18:40
Too many occurrences of pointer? :)
| |
| 141 // representation details. | |
| 142 if (!m_ptr) | |
| 143 m_ptr = reinterpret_cast<T*>(new WebCore::Persistent<T>(p)); | |
| 144 else | |
| 145 *reinterpret_cast<WebCore::Persistent<T>*>(m_ptr) = p; | |
| 146 } | |
| 147 #else | |
| 148 void assign(T* p) | |
| 149 { | |
| 150 // p is already ref'd for us by the caller | |
| 151 if (m_ptr) | |
| 152 m_ptr->deref(); | |
| 153 m_ptr = p; | |
| 154 } | |
| 155 #endif | |
| 156 #else | |
| 157 // Disable the assignment operator; we define it above for when | |
| 158 // INSIDE_BLINK is set, but we need to make sure that it is not | |
| 159 // used outside there; the compiler-provided version won't handle reference | |
| 160 // counting properly. | |
| 161 WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollected Ptr<T>& other); | |
| 162 #endif | |
| 163 // Disable the copy constructor; classes that contain a | |
| 164 // WebPrivateGarbageCollectedPtr should implement their copy | |
| 165 // constructor using assign(). | |
| 166 WebPrivateGarbageCollectedPtr(const WebPrivateGarbageCollectedPtr<T>&); | |
| 167 | |
| 168 T* m_ptr; | |
| 169 }; | |
| 170 | |
| 171 } // namespace blink | |
| 172 | |
| 173 #endif | |
| OLD | NEW |