2016-07-19 22:36:55 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_mscom_WeakRef_h
|
|
|
|
#define mozilla_mscom_WeakRef_h
|
|
|
|
|
|
|
|
#include <guiddef.h>
|
2016-11-04 17:40:41 +03:00
|
|
|
#include <unknwn.h>
|
2016-07-19 22:36:55 +03:00
|
|
|
|
2017-02-18 03:35:01 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Atomics.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
2016-07-19 22:36:55 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Thread-safe weak references for COM that works pre-Windows 8 and do not
|
|
|
|
* require WinRT.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace mscom {
|
|
|
|
|
2017-06-08 20:52:53 +03:00
|
|
|
struct IWeakReferenceSource;
|
2017-02-18 03:35:01 +03:00
|
|
|
class WeakReferenceSupport;
|
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
class SharedRef final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit SharedRef(WeakReferenceSupport* aSupport);
|
|
|
|
void Lock();
|
|
|
|
void Unlock();
|
|
|
|
|
2017-06-08 20:52:53 +03:00
|
|
|
HRESULT ToStrongRef(IWeakReferenceSource** aOutStringReference);
|
2017-02-18 03:35:01 +03:00
|
|
|
HRESULT Resolve(REFIID aIid, void** aOutStrongReference);
|
|
|
|
void Clear();
|
|
|
|
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedRef)
|
|
|
|
|
|
|
|
SharedRef(const SharedRef&) = delete;
|
|
|
|
SharedRef(SharedRef&&) = delete;
|
|
|
|
SharedRef& operator=(const SharedRef&) = delete;
|
|
|
|
SharedRef& operator=(SharedRef&&) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
~SharedRef();
|
|
|
|
|
|
|
|
private:
|
|
|
|
CRITICAL_SECTION mCS;
|
|
|
|
WeakReferenceSupport* mSupport;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2016-07-19 22:36:55 +03:00
|
|
|
// {F841AEFA-064C-49A4-B73D-EBD14A90F012}
|
|
|
|
DEFINE_GUID(IID_IWeakReference,
|
|
|
|
0xf841aefa, 0x64c, 0x49a4, 0xb7, 0x3d, 0xeb, 0xd1, 0x4a, 0x90, 0xf0, 0x12);
|
|
|
|
|
|
|
|
struct IWeakReference : public IUnknown
|
|
|
|
{
|
2017-06-08 20:52:53 +03:00
|
|
|
virtual STDMETHODIMP ToStrongRef(IWeakReferenceSource** aOutStrongReference) = 0;
|
2017-02-18 03:35:01 +03:00
|
|
|
virtual STDMETHODIMP Resolve(REFIID aIid, void** aOutStrongReference) = 0;
|
2016-07-19 22:36:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// {87611F0C-9BBB-4F78-9D43-CAC5AD432CA1}
|
|
|
|
DEFINE_GUID(IID_IWeakReferenceSource,
|
|
|
|
0x87611f0c, 0x9bbb, 0x4f78, 0x9d, 0x43, 0xca, 0xc5, 0xad, 0x43, 0x2c, 0xa1);
|
|
|
|
|
|
|
|
struct IWeakReferenceSource : public IUnknown
|
|
|
|
{
|
|
|
|
virtual STDMETHODIMP GetWeakReference(IWeakReference** aOutWeakRef) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
class WeakRef;
|
|
|
|
|
|
|
|
class WeakReferenceSupport : public IWeakReferenceSource
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum class Flags
|
|
|
|
{
|
|
|
|
eNone = 0,
|
|
|
|
eDestroyOnMainThread = 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// IUnknown
|
|
|
|
STDMETHODIMP QueryInterface(REFIID riid, void** ppv) override;
|
|
|
|
STDMETHODIMP_(ULONG) AddRef() override;
|
|
|
|
STDMETHODIMP_(ULONG) Release() override;
|
|
|
|
|
|
|
|
// IWeakReferenceSource
|
|
|
|
STDMETHODIMP GetWeakReference(IWeakReference** aOutWeakRef) override;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
explicit WeakReferenceSupport(Flags aFlags);
|
2018-01-13 00:17:18 +03:00
|
|
|
virtual ~WeakReferenceSupport() = default;
|
2016-07-19 22:36:55 +03:00
|
|
|
|
2018-01-13 00:17:18 +03:00
|
|
|
virtual HRESULT WeakRefQueryInterface(REFIID aIid,
|
|
|
|
IUnknown** aOutInterface) = 0;
|
2017-08-31 01:34:43 +03:00
|
|
|
|
2018-01-25 23:45:21 +03:00
|
|
|
class MOZ_RAII StabilizeRefCount final
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit StabilizeRefCount(WeakReferenceSupport& aObject);
|
|
|
|
~StabilizeRefCount();
|
|
|
|
|
|
|
|
StabilizeRefCount(const StabilizeRefCount&) = delete;
|
|
|
|
StabilizeRefCount(StabilizeRefCount&&) = delete;
|
|
|
|
StabilizeRefCount& operator=(const StabilizeRefCount&) = delete;
|
|
|
|
StabilizeRefCount& operator=(StabilizeRefCount&&) = delete;
|
|
|
|
|
|
|
|
private:
|
|
|
|
WeakReferenceSupport& mObject;
|
|
|
|
};
|
|
|
|
|
|
|
|
friend class StabilizeRefCount;
|
|
|
|
|
2016-07-19 22:36:55 +03:00
|
|
|
private:
|
2017-02-18 03:35:01 +03:00
|
|
|
RefPtr<detail::SharedRef> mSharedRef;
|
|
|
|
ULONG mRefCnt;
|
|
|
|
Flags mFlags;
|
2016-07-19 22:36:55 +03:00
|
|
|
};
|
|
|
|
|
2017-02-27 21:44:45 +03:00
|
|
|
class WeakRef final : public IWeakReference
|
2016-07-19 22:36:55 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
// IUnknown
|
|
|
|
STDMETHODIMP QueryInterface(REFIID riid, void** ppv) override;
|
|
|
|
STDMETHODIMP_(ULONG) AddRef() override;
|
|
|
|
STDMETHODIMP_(ULONG) Release() override;
|
|
|
|
|
|
|
|
// IWeakReference
|
2017-06-08 20:52:53 +03:00
|
|
|
STDMETHODIMP ToStrongRef(IWeakReferenceSource** aOutStrongReference) override;
|
2016-07-19 22:36:55 +03:00
|
|
|
STDMETHODIMP Resolve(REFIID aIid, void** aOutStrongReference) override;
|
|
|
|
|
2017-02-18 03:35:01 +03:00
|
|
|
explicit WeakRef(RefPtr<detail::SharedRef>& aSharedRef);
|
2016-07-19 22:36:55 +03:00
|
|
|
|
|
|
|
private:
|
2017-02-27 21:44:45 +03:00
|
|
|
~WeakRef() = default;
|
|
|
|
|
2017-02-18 03:35:01 +03:00
|
|
|
Atomic<ULONG> mRefCnt;
|
|
|
|
RefPtr<detail::SharedRef> mSharedRef;
|
2016-07-19 22:36:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mscom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_mscom_WeakRef_h
|
|
|
|
|