Chromium Code Reviews| Index: public/platform/WebPrivateGarbageCollectedPtr.h |
| diff --git a/public/platform/WebPrivateGarbageCollectedPtr.h b/public/platform/WebPrivateGarbageCollectedPtr.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e7b62cc956dfa5c748ab92431926184ac16922a2 |
| --- /dev/null |
| +++ b/public/platform/WebPrivateGarbageCollectedPtr.h |
| @@ -0,0 +1,182 @@ |
| +/* 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 |
| +#define WebPrivateGarbageCollectedPtr_h |
| + |
| +#include "WebCommon.h" |
| + |
| +#if INSIDE_BLINK |
| +#include "heap/Handle.h" |
| +#include "wtf/PassRefPtr.h" |
| + |
| +// 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
|
| +// used below map to CPP #defines, move the type names it can expand |
| +// to into the WebCore namespace. When Oilpan is always enabled, this |
| +// block can be removed. |
| +namespace WebCore { |
| +using WTF::RawPtr; |
| +using WTF::PassRefPtr; |
| +} |
| +#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 WebCore::PassRefPtrWillBeRawPtr<WebCore::Foo>&); |
| +// #endif |
| +// |
| +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 |
| +#if ENABLE(OILPAN) |
| + WebPrivateGarbageCollectedPtr(const WebCore::PassRefPtrWillBeRawPtr<T>& p) |
| + { |
| + assign(p); |
| + } |
| + |
| + void reset() |
| + { |
| + if (m_ptr) { |
| + delete reinterpret_cast<WebCore::Persistent<T>*>(m_ptr); |
| + m_ptr = 0; |
| + } |
| + } |
| + |
| + WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollectedPtr<T>& other) |
| + { |
| + assign(other.get()); |
| + return *this; |
| + } |
| + |
| + WebPrivateGarbageCollectedPtr<T>& operator=(const WebCore::PassRefPtrWillBeRawPtr<T>& p) |
| + { |
| + assign(p); |
| + return *this; |
| + } |
| + |
| + T* get() const |
| + { |
| + if (!m_ptr) |
| + return 0; |
| + return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get(); |
| + } |
| + |
| + T* operator->() const |
| + { |
| + ASSERT(m_ptr); |
| + return (reinterpret_cast<WebCore::Persistent<T>* >(m_ptr))->get(); |
| + } |
| +#else |
| + WebPrivateGarbageCollectedPtr(const WebCore::PassRefPtrWillBeRawPtr<T>& p) |
| + { |
| + m_ptr = p.leakRef(); |
| + } |
| + |
| + void reset() |
| + { |
| + assign(0); |
| + } |
| + |
| + WebPrivateGarbageCollectedPtr<T>& operator=(const WebPrivateGarbageCollectedPtr<T>& other) |
| + { |
| + T* p = other.m_ptr; |
| + if (p) |
| + p->ref(); |
| + assign(p); |
| + return *this; |
| + } |
| + |
| + WebPrivateGarbageCollectedPtr<T>& operator=(const WebCore::PassRefPtrWillBeRawPtr<T>& p) |
| + { |
| + assign(p.leakRef()); |
| + return *this; |
| + } |
| + |
| + T* get() const |
| + { |
| + return m_ptr; |
| + } |
| + |
| + T* operator->() const |
| + { |
| + ASSERT(m_ptr); |
| + return m_ptr; |
| + } |
| +#endif |
| +#endif |
| + |
| +private: |
| +#if INSIDE_BLINK |
| +#if ENABLE(OILPAN) |
| + void assign(T* p) |
| + { |
| + if (!p) { |
| + reset(); |
| + return; |
| + } |
| + // Keep an opaque pointer object pointer to avoid exposing |
| + // representation details. |
| + if (!m_ptr) |
| + m_ptr = reinterpret_cast<T*>(new WebCore::Persistent<T>(p)); |
| + else |
| + *reinterpret_cast<WebCore::Persistent<T>*>(m_ptr) = p; |
| + } |
| +#else |
| + void assign(T* p) |
| + { |
| + // 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 |