Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(331)

Side by Side Diff: public/platform/WebPrivateGarbageCollectedPtr.h

Issue 168963003: Make WebPrivatePtr capable of wrapping garbage collected objects. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Qualify PassRefPtrWillBeRawPtr by its namespace, WebCore. Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
15 // FIXME: oilpan: If the WebCore-qualified PassRefPtrWillBeRawPtr
sof 2014/02/17 15:19:10 Addressing some clang compilation breakage..referr
Mads Ager (chromium) 2014/02/17 15:23:35 How about having the define expand to a fully qual
sof 2014/02/17 15:27:40 I don't see how that would work with template alia
16 // used below map to CPP #defines, move the type names it can expand
17 // to into the WebCore namespace. When Oilpan is always enabled, this
18 // block can be removed.
19 namespace WebCore {
20 using WTF::RawPtr;
21 using WTF::PassRefPtr;
22 }
23 #endif
24
25 namespace blink {
26
27 // A variant of WebPrivatePtr that holds a Blink object that's garbage
28 // collected, without exposing the details of how (and with what
29 // types.) If no support for having such objects (i.e., no Oilpan),
30 // WebPrivateGarbageCollectedPtr is equal to WebPrivatePtr and the
31 // object will be reference counted.
32 //
33 // See WebPrivatePtr.h comment for how to make use of
34 // WebPrivateGarbageCollectedPtr; same pattern applies, but for one
35 // detail. Instead of declaring & defining,
36 //
37 // #if INSIDE_BLINK
38 // WebFoo(const WTF::PassRefPtr<WebCore::Foo>&);
39 // #endif
40 //
41 // provide
42 //
43 // #if INSIDE_BLINK
44 // WebFoo(const WebCore::PassRefPtrWillBeRawPtr<WebCore::Foo>&);
45 // #endif
46 //
47 template <typename T>
48 class WebPrivateGarbageCollectedPtr {
49 public:
50 WebPrivateGarbageCollectedPtr() : m_ptr(0) { }
51 ~WebPrivateGarbageCollectedPtr()
52 {
53 // We don't destruct the object pointed by m_ptr here because we don't
54 // want to expose destructors of core classes to embedders. We should
55 // call reset() manually in destructors of classes with WebPrivateGarbag eCollectedPtr
56 // members.
57 BLINK_ASSERT(!m_ptr);
58 }
59
60 bool isNull() const { return !m_ptr; }
61
62 #if INSIDE_BLINK
63 #if ENABLE(OILPAN)
64 WebPrivateGarbageCollectedPtr(const WebCore::PassRefPtrWillBeRawPtr<T>& p)
65 {
66 assign(p);
67 }
68
69 void reset()
70 {
71 if (m_ptr) {
72 delete reinterpret_cast<WebCore::Persistent<T>*>(m_ptr);
73 m_ptr = 0;
74 }
75 }
76
77 WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollected Ptr<T>& other)
78 {
79 assign(other.get());
80 return *this;
81 }
82
83 WebPrivateGarbageCollectedPtr<T>& operator=(const WebCore::PassRefPtrWillBeR awPtr<T>& p)
84 {
85 assign(p);
86 return *this;
87 }
88
89 T* get() const
90 {
91 if (!m_ptr)
92 return 0;
93 return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get();
94 }
95
96 T* operator->() const
97 {
98 ASSERT(m_ptr);
99 return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get();
100 }
101 #else
102 WebPrivateGarbageCollectedPtr(const WebCore::PassRefPtrWillBeRawPtr<T>& p)
103 {
104 m_ptr = p.leakRef();
105 }
106
107 void reset()
108 {
109 assign(0);
110 }
111
112 WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollected Ptr<T>& other)
113 {
114 T* p = other.m_ptr;
115 if (p)
116 p->ref();
117 assign(p);
118 return *this;
119 }
120
121 WebPrivateGarbageCollectedPtr<T>& operator=(const WebCore::PassRefPtrWillBeR awPtr<T>& p)
122 {
123 assign(p.leakRef());
124 return *this;
125 }
126
127 T* get() const
128 {
129 return m_ptr;
130 }
131
132 T* operator->() const
133 {
134 ASSERT(m_ptr);
135 return m_ptr;
136 }
137 #endif
138 #endif
139
140 private:
141 #if INSIDE_BLINK
142 #if ENABLE(OILPAN)
143 void assign(T* p)
144 {
145 if (!p) {
146 reset();
147 return;
148 }
149 // Keep an opaque pointer object pointer to avoid exposing
150 // representation details.
151 if (!m_ptr)
152 m_ptr = reinterpret_cast<T*>(new WebCore::Persistent<T>(p));
153 else
154 *reinterpret_cast<WebCore::Persistent<T>*>(m_ptr) = p;
155 }
156 #else
157 void assign(T* p)
158 {
159 // p is already ref'd for us by the caller
160 if (m_ptr)
161 m_ptr->deref();
162 m_ptr = p;
163 }
164 #endif
165 #else
166 // Disable the assignment operator; we define it above for when
167 // INSIDE_BLINK is set, but we need to make sure that it is not
168 // used outside there; the compiler-provided version won't handle reference
169 // counting properly.
170 WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollected Ptr<T>& other);
171 #endif
172 // Disable the copy constructor; classes that contain a
173 // WebPrivateGarbageCollectedPtr should implement their copy
174 // constructor using assign().
175 WebPrivateGarbageCollectedPtr(const WebPrivateGarbageCollectedPtr<T>&);
176
177 T* m_ptr;
178 };
179
180 } // namespace blink
181
182 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698