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/. */
|
|
|
|
|
|
|
|
#define INITGUID
|
|
|
|
#include "mozilla/mscom/WeakRef.h"
|
|
|
|
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2017-02-18 03:35:01 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2016-07-19 22:36:55 +03:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsWindowsHelpers.h"
|
2017-02-16 22:38:09 +03:00
|
|
|
#include "nsProxyRelease.h"
|
2016-07-19 22:36:55 +03:00
|
|
|
|
2017-02-18 03:35:01 +03:00
|
|
|
static void InitializeCS(CRITICAL_SECTION& aCS) {
|
|
|
|
DWORD flags = 0;
|
|
|
|
#if defined(RELEASE_OR_BETA)
|
|
|
|
flags |= CRITICAL_SECTION_NO_DEBUG_INFO;
|
|
|
|
#endif
|
|
|
|
InitializeCriticalSectionEx(&aCS, 4000, flags);
|
|
|
|
}
|
|
|
|
|
2016-07-19 22:36:55 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace mscom {
|
|
|
|
|
2017-02-18 03:35:01 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
SharedRef::SharedRef(WeakReferenceSupport* aSupport) : mSupport(aSupport) {
|
|
|
|
::InitializeCS(mCS);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedRef::~SharedRef() { ::DeleteCriticalSection(&mCS); }
|
|
|
|
|
|
|
|
void SharedRef::Lock() { ::EnterCriticalSection(&mCS); }
|
|
|
|
|
|
|
|
void SharedRef::Unlock() { ::LeaveCriticalSection(&mCS); }
|
|
|
|
|
2017-06-08 20:52:53 +03:00
|
|
|
HRESULT
|
|
|
|
SharedRef::ToStrongRef(IWeakReferenceSource** aOutStrongReference) {
|
|
|
|
RefPtr<IWeakReferenceSource> strongRef;
|
|
|
|
|
|
|
|
{ // Scope for lock
|
|
|
|
AutoCriticalSection lock(&mCS);
|
|
|
|
if (!mSupport) {
|
|
|
|
return E_POINTER;
|
|
|
|
}
|
|
|
|
strongRef = mSupport;
|
|
|
|
}
|
|
|
|
|
|
|
|
strongRef.forget(aOutStrongReference);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2017-02-18 03:35:01 +03:00
|
|
|
HRESULT
|
|
|
|
SharedRef::Resolve(REFIID aIid, void** aOutStrongReference) {
|
|
|
|
RefPtr<WeakReferenceSupport> strongRef;
|
|
|
|
|
|
|
|
{ // Scope for lock
|
|
|
|
AutoCriticalSection lock(&mCS);
|
|
|
|
if (!mSupport) {
|
|
|
|
return E_POINTER;
|
|
|
|
}
|
|
|
|
strongRef = mSupport;
|
|
|
|
}
|
|
|
|
|
|
|
|
return strongRef->QueryInterface(aIid, aOutStrongReference);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SharedRef::Clear() {
|
|
|
|
AutoCriticalSection lock(&mCS);
|
|
|
|
mSupport = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2018-07-21 17:22:54 +03:00
|
|
|
typedef BaseAutoLock<detail::SharedRef&> SharedRefAutoLock;
|
|
|
|
typedef BaseAutoUnlock<detail::SharedRef&> SharedRefAutoUnlock;
|
2017-02-18 03:35:01 +03:00
|
|
|
|
2016-07-19 22:36:55 +03:00
|
|
|
WeakReferenceSupport::WeakReferenceSupport(Flags aFlags)
|
|
|
|
: mRefCnt(0), mFlags(aFlags) {
|
2017-02-18 03:35:01 +03:00
|
|
|
mSharedRef = new detail::SharedRef(this);
|
2017-08-31 01:34:43 +03:00
|
|
|
}
|
|
|
|
|
2016-07-19 22:36:55 +03:00
|
|
|
HRESULT
|
|
|
|
WeakReferenceSupport::QueryInterface(REFIID riid, void** ppv) {
|
|
|
|
RefPtr<IUnknown> punk;
|
|
|
|
if (!ppv) {
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
*ppv = nullptr;
|
|
|
|
|
|
|
|
// Raise the refcount for stabilization purposes during aggregation
|
2018-01-25 23:45:21 +03:00
|
|
|
StabilizeRefCount stabilize(*this);
|
2016-07-19 22:36:55 +03:00
|
|
|
|
|
|
|
if (riid == IID_IUnknown || riid == IID_IWeakReferenceSource) {
|
|
|
|
punk = static_cast<IUnknown*>(this);
|
|
|
|
} else {
|
2018-01-13 00:17:18 +03:00
|
|
|
HRESULT hr = WeakRefQueryInterface(riid, getter_AddRefs(punk));
|
2016-07-19 22:36:55 +03:00
|
|
|
if (FAILED(hr)) {
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!punk) {
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
punk.forget(ppv);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2018-01-25 23:45:21 +03:00
|
|
|
WeakReferenceSupport::StabilizeRefCount::StabilizeRefCount(
|
|
|
|
WeakReferenceSupport& aObject)
|
|
|
|
: mObject(aObject) {
|
|
|
|
SharedRefAutoLock lock(*mObject.mSharedRef);
|
|
|
|
++mObject.mRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
WeakReferenceSupport::StabilizeRefCount::~StabilizeRefCount() {
|
|
|
|
// We directly access these fields instead of calling Release() because we
|
|
|
|
// want to adjust the ref count without the other side effects (such as
|
|
|
|
// deleting this if the count drops back to zero, which may happen during
|
|
|
|
// an initial QI during object creation).
|
|
|
|
SharedRefAutoLock lock(*mObject.mSharedRef);
|
|
|
|
--mObject.mRefCnt;
|
|
|
|
}
|
|
|
|
|
2016-07-19 22:36:55 +03:00
|
|
|
ULONG
|
|
|
|
WeakReferenceSupport::AddRef() {
|
2017-02-18 03:35:01 +03:00
|
|
|
SharedRefAutoLock lock(*mSharedRef);
|
|
|
|
ULONG result = ++mRefCnt;
|
|
|
|
NS_LOG_ADDREF(this, result, "mscom::WeakReferenceSupport", sizeof(*this));
|
|
|
|
return result;
|
2016-07-19 22:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONG
|
|
|
|
WeakReferenceSupport::Release() {
|
|
|
|
ULONG newRefCnt;
|
|
|
|
{ // Scope for lock
|
2017-02-18 03:35:01 +03:00
|
|
|
SharedRefAutoLock lock(*mSharedRef);
|
2016-07-19 22:36:55 +03:00
|
|
|
newRefCnt = --mRefCnt;
|
|
|
|
if (newRefCnt == 0) {
|
2017-02-18 03:35:01 +03:00
|
|
|
mSharedRef->Clear();
|
2016-07-19 22:36:55 +03:00
|
|
|
}
|
|
|
|
}
|
2017-02-18 03:35:01 +03:00
|
|
|
NS_LOG_RELEASE(this, newRefCnt, "mscom::WeakReferenceSupport");
|
2016-07-19 22:36:55 +03:00
|
|
|
if (newRefCnt == 0) {
|
|
|
|
if (mFlags != Flags::eDestroyOnMainThread || NS_IsMainThread()) {
|
|
|
|
delete this;
|
|
|
|
} else {
|
2017-02-16 22:38:09 +03:00
|
|
|
// We need to delete this object on the main thread, but we aren't on the
|
|
|
|
// main thread right now, so we send a reference to ourselves to the main
|
|
|
|
// thread to be re-released there.
|
|
|
|
RefPtr<WeakReferenceSupport> self = this;
|
2017-06-14 04:27:17 +03:00
|
|
|
NS_ReleaseOnMainThreadSystemGroup("WeakReferenceSupport", self.forget());
|
2016-07-19 22:36:55 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return newRefCnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
WeakReferenceSupport::GetWeakReference(IWeakReference** aOutWeakRef) {
|
|
|
|
if (!aOutWeakRef) {
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
2017-02-18 03:35:01 +03:00
|
|
|
RefPtr<WeakRef> weakRef = MakeAndAddRef<WeakRef>(mSharedRef);
|
|
|
|
return weakRef->QueryInterface(IID_IWeakReference, (void**)aOutWeakRef);
|
2016-07-19 22:36:55 +03:00
|
|
|
}
|
|
|
|
|
2017-02-18 03:35:01 +03:00
|
|
|
WeakRef::WeakRef(RefPtr<detail::SharedRef>& aSharedRef)
|
|
|
|
: mRefCnt(0), mSharedRef(aSharedRef) {
|
|
|
|
MOZ_ASSERT(aSharedRef);
|
2016-07-19 22:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT
|
|
|
|
WeakRef::QueryInterface(REFIID riid, void** ppv) {
|
|
|
|
IUnknown* punk = nullptr;
|
|
|
|
if (!ppv) {
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (riid == IID_IUnknown || riid == IID_IWeakReference) {
|
|
|
|
punk = static_cast<IUnknown*>(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
*ppv = punk;
|
|
|
|
if (!punk) {
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
punk->AddRef();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ULONG
|
|
|
|
WeakRef::AddRef() {
|
2017-02-18 03:35:01 +03:00
|
|
|
ULONG result = ++mRefCnt;
|
|
|
|
NS_LOG_ADDREF(this, result, "mscom::WeakRef", sizeof(*this));
|
|
|
|
return result;
|
2016-07-19 22:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ULONG
|
|
|
|
WeakRef::Release() {
|
2017-02-18 03:35:01 +03:00
|
|
|
ULONG newRefCnt = --mRefCnt;
|
|
|
|
NS_LOG_RELEASE(this, newRefCnt, "mscom::WeakRef");
|
2016-07-19 22:36:55 +03:00
|
|
|
if (newRefCnt == 0) {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
return newRefCnt;
|
|
|
|
}
|
|
|
|
|
2017-06-08 20:52:53 +03:00
|
|
|
HRESULT
|
|
|
|
WeakRef::ToStrongRef(IWeakReferenceSource** aOutStrongReference) {
|
|
|
|
return mSharedRef->ToStrongRef(aOutStrongReference);
|
|
|
|
}
|
|
|
|
|
2016-07-19 22:36:55 +03:00
|
|
|
HRESULT
|
|
|
|
WeakRef::Resolve(REFIID aIid, void** aOutStrongReference) {
|
2017-02-18 03:35:01 +03:00
|
|
|
return mSharedRef->Resolve(aIid, aOutStrongReference);
|
2016-07-19 22:36:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mscom
|
|
|
|
} // namespace mozilla
|