| Index: public/platform/WebWillBePrivateGarbageCollectedPtr.h
|
| diff --git a/public/platform/WebWillBePrivateGarbageCollectedPtr.h b/public/platform/WebWillBePrivateGarbageCollectedPtr.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..892c17fd5d311639caa6d524afb2be636fdc790f
|
| --- /dev/null
|
| +++ b/public/platform/WebWillBePrivateGarbageCollectedPtr.h
|
| @@ -0,0 +1,173 @@
|
| +/* 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 WebWillBePrivateGarbageCollectedPtr_h
|
| +#define WebWillBePrivateGarbageCollectedPtr_h
|
| +
|
| +#include "WebCommon.h"
|
| +
|
| +#if BLINK_IMPLEMENTATION
|
| +#include "heap/Handle.h"
|
| +#include "wtf/PassRefPtr.h"
|
| +#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., Oilpan not
|
| +// enabled), WebWillBePrivateGarbageCollectedPtr is equal to
|
| +// WebPrivatePtr and the object will be reference counted.
|
| +//
|
| +// See WebPrivatePtr.h comment for how to make use of
|
| +// WebWillBePrivateGarbageCollectedPtr; same pattern applies, but for
|
| +// one detail. Instead of declaring & defining,
|
| +//
|
| +// #if BLINK_IMPLEMENTATION
|
| +// WebFoo(const WTF::PassRefPtr<WebCore::Foo>&);
|
| +// #endif
|
| +//
|
| +// provide
|
| +//
|
| +// #if BLINK_IMPLEMENTATION
|
| +// WebFoo(const PassRefPtrWillBeRawPtr<WebCore::Foo>&);
|
| +// #endif
|
| +//
|
| +template <typename T>
|
| +class WebWillBePrivateGarbageCollectedPtr {
|
| +public:
|
| + WebWillBePrivateGarbageCollectedPtr() : m_ptr(0) { }
|
| + ~WebWillBePrivateGarbageCollectedPtr()
|
| + {
|
| + // 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 WebWillBePrivateGarbageCollectedPtr
|
| + // members.
|
| + BLINK_ASSERT(!m_ptr);
|
| + }
|
| +
|
| + bool isNull() const { return !m_ptr; }
|
| +
|
| +#if BLINK_IMPLEMENTATION
|
| +#if ENABLE(OILPAN)
|
| + WebWillBePrivateGarbageCollectedPtr(const PassRefPtrWillBeRawPtr<T>& p)
|
| + {
|
| + assign(p);
|
| + }
|
| +
|
| + void reset()
|
| + {
|
| + if (m_ptr) {
|
| + delete reinterpret_cast<WebCore::Persistent<T>*>(m_ptr);
|
| + m_ptr = 0;
|
| + }
|
| + }
|
| +
|
| + WebWillBePrivateGarbageCollectedPtr<T>& operator=(const WebWillBePrivateGarbageCollectedPtr<T>& other)
|
| + {
|
| + assign(other.get());
|
| + return *this;
|
| + }
|
| +
|
| + WebWillBePrivateGarbageCollectedPtr<T>& operator=(const 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
|
| + WebWillBePrivateGarbageCollectedPtr(const PassRefPtrWillBeRawPtr<T>& p)
|
| + {
|
| + m_ptr = p.leakRef();
|
| + }
|
| +
|
| + void reset()
|
| + {
|
| + assign(0);
|
| + }
|
| +
|
| + WebWillBePrivateGarbageCollectedPtr<T>& operator=(const WebWillBePrivateGarbageCollectedPtr<T>& other)
|
| + {
|
| + T* p = other.m_ptr;
|
| + if (p)
|
| + p->ref();
|
| + assign(p);
|
| + return *this;
|
| + }
|
| +
|
| + WebWillBePrivateGarbageCollectedPtr<T>& operator=(const 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 BLINK_IMPLEMENTATION
|
| +#if ENABLE(OILPAN)
|
| + void assign(T* p)
|
| + {
|
| + if (!p) {
|
| + reset();
|
| + return;
|
| + }
|
| + // Keep an opaque 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
|
| + // BLINK_IMPLEMENTATION 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.
|
| + WebWillBePrivateGarbageCollectedPtr<T>& operator=(const WebWillBePrivateGarbageCollectedPtr<T>& other);
|
| +#endif
|
| + // Disable the copy constructor; classes that contain a
|
| + // WebPrivateGarbageCollectedPtr should implement their copy
|
| + // constructor using assign().
|
| + WebWillBePrivateGarbageCollectedPtr(const WebWillBePrivateGarbageCollectedPtr<T>&);
|
| +
|
| + T* m_ptr;
|
| +};
|
| +
|
| +} // namespace blink
|
| +
|
| +#endif
|
|
|