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