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

Unified 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: Removed unused (and possibly overlapping) operator= overload. 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « Source/web/WebSpeechGrammar.cpp ('k') | public/web/WebSpeechGrammar.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: public/platform/WebPrivateGarbageCollectedPtr.h
diff --git a/public/platform/WebPrivateGarbageCollectedPtr.h b/public/platform/WebPrivateGarbageCollectedPtr.h
new file mode 100644
index 0000000000000000000000000000000000000000..9bbaca77bbc80aefcd0f217f623ea20d6fda853e
--- /dev/null
+++ b/public/platform/WebPrivateGarbageCollectedPtr.h
@@ -0,0 +1,170 @@
+/* Copyright (c) 2014 The Chromium Authors. All rihts reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef WebPrivateGarbageCollectedPtr_h
Mads Ager (chromium) 2014/02/17 12:26:29 We had a chat about this just now. We can probably
sof 2014/02/17 12:45:03 There's a slight problem with that: uses of WebPri
Mads Ager (chromium) 2014/02/17 12:54:30 Yeah, I see that now. Hmm. I'll give it a bit more
sof 2014/02/17 14:09:00 I think we get out ahead by doing so, switched ove
+#define WebPrivateGarbageCollectedPtr_h
+
+#include "WebCommon.h"
+
+#if INSIDE_BLINK
+#include "wtf/PassRefPtr.h"
+
+namespace WebCore {
Mads Ager (chromium) 2014/02/17 13:31:31 We should be able to remove all of the forward dec
sof 2014/02/17 14:09:00 That would be cleaner, the reason why it's like th
+template<typename T> class Persistent;
+}
+
+namespace WTF {
+template<typename T> class RawPtr;
+}
+
+# if ENABLE(OILPAN)
Mads Ager (chromium) 2014/02/17 12:26:29 Please remove the spaces between # and if/define/e
sof 2014/02/17 14:09:00 Done.
+# define PassRefPtrWillBeRawPtr RawPtr
+# else
+# define PassRefPtrWillBeRawPtr PassRefPtr
+# endif
+#endif
+
+namespace blink {
+
+// A variant of WebPrivatePtr that holds a Blink object that's garbage
+// collected, without exposing the details of how (and with what
+// types.) If no support for having such objects (i.e., no Oilpan),
+// WebPrivateGarbageCollectedPtr is equal to WebPrivatePtr and the
+// object will be reference counted.
+//
+// See WebPrivatePtr.h comment for how to make use of
+// WebPrivateGarbageCollectedPtr; same pattern applies, but for one
+// detail. Instead of declaring & defining,
+//
+// #if INSIDE_BLINK
+// WebFoo(const WTF::PassRefPtr<WebCore::Foo>&);
+// #endif
+//
+// provide
+//
+// #if INSIDE_BLINK
+// WebFoo(const WTF::PassRefPtrWillBeRawPtr<WebCore::Foo>&);
+// #endif
+//
+// FIXME: oilpan: when transition types are no more, remove the
+// unseemly #if ENABLE(OILPAN) blocks.
+template <typename T>
+class WebPrivateGarbageCollectedPtr {
+public:
+ WebPrivateGarbageCollectedPtr() : m_ptr(0) { }
+ ~WebPrivateGarbageCollectedPtr()
+ {
+ // We don't destruct the object pointed by m_ptr here because we don't
+ // want to expose destructors of core classes to embedders. We should
+ // call reset() manually in destructors of classes with WebPrivateGarbageCollectedPtr
+ // members.
+ BLINK_ASSERT(!m_ptr);
+ }
+
+ bool isNull() const { return !m_ptr; }
+
+#if INSIDE_BLINK
+ WebPrivateGarbageCollectedPtr(const WTF::PassRefPtrWillBeRawPtr<T>& p)
+ {
+# if ENABLE(OILPAN)
+ assign(p);
+# else
+ m_ptr = p.leakRef();
+# endif
+ }
+
+ void reset()
+ {
+# if ENABLE(OILPAN)
+ if (m_ptr) {
+ delete reinterpret_cast<WebCore::Persistent<T>*>(m_ptr);
+ m_ptr = 0;
+ }
+# else
+ assign(0);
+# endif
+ }
+
+ WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollectedPtr<T>& other)
+ {
+# if ENABLE(OILPAN)
+ assign(other.get());
+# else
+ T* p = other.m_ptr;
+ if (p)
+ p->ref();
+ assign(p);
+# endif
+ return *this;
+ }
+
+ WebPrivateGarbageCollectedPtr<T>& operator=(const WTF::PassRefPtrWillBeRawPtr<T>& p)
+ {
+# if ENABLE(OILPAN)
+ assign(p);
+# else
+ assign(p.leakRef());
+# endif
+ return *this;
+ }
+
+ T* get() const
+ {
+# if ENABLE(OILPAN)
+ if (!m_ptr)
+ return 0;
+ return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get();
+# else
+ return m_ptr;
+# endif
+ }
+
+ T* operator->() const
+ {
+ ASSERT(m_ptr);
+# if ENABLE(OILPAN)
+ return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get();
+# else
+ return m_ptr;
+# endif
+ }
+#endif
+
+private:
+#if INSIDE_BLINK
+ void assign(T* p)
+ {
+# if ENABLE(OILPAN)
+ reset();
+ // Keep an opaque pointer object pointer to avoid exposing
+ // representation details. (Relatively speaking, it's
+ // acceptable to have this be as a T* rather than a
+ // generic void*.
+ m_ptr = reinterpret_cast<T*>(new WebCore::Persistent<T>(p));
Mads Ager (chromium) 2014/02/17 12:26:29 We really shouldn't have to do this. We should all
Mads Ager (chromium) 2014/02/17 13:31:31 In addition to reusing the persistent we should be
sof 2014/02/17 14:09:00 Oh yes, it completely failed to consider the null
+# else
+ // p is already ref'd for us by the caller
+ if (m_ptr)
+ m_ptr->deref();
+ m_ptr = p;
+# endif
+ }
+#else
+ // Disable the assignment operator; we define it above for when
+ // INSIDE_BLINK is set, but we need to make sure that it is not
+ // used outside there; the compiler-provided version won't handle reference
+ // counting properly.
+ WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollectedPtr<T>& other);
+#endif
+ // Disable the copy constructor; classes that contain a
+ // WebPrivateGarbageCollectedPtr should implement their copy
+ // constructor using assign().
+ WebPrivateGarbageCollectedPtr(const WebPrivateGarbageCollectedPtr<T>&);
+
+ T* m_ptr;
+};
+
+} // namespace blink
+
+#endif
« no previous file with comments | « Source/web/WebSpeechGrammar.cpp ('k') | public/web/WebSpeechGrammar.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698