Bug 1869053 - [5/5] templatize and further simplify AgileReference r=handyman,win-reviewers

Lift `aIid` to compile-time, as a template parameter `InterfaceT`. This
simplifies the common case for using `Resolve()`, where the desired and
supplied interfaces are the same. (For the as-yet-unattested case where
they're not, retain the old functionality by means of a small family of
`ResolveAs()` functions.)

Additionally, to eliminate a swath of custom logic and magic-number
choices surrounding `mHResult`, eliminate `mHResult` itself as well.
Instead, since its value was derived from the creation of the underlying
`IAgileReference`, any callers that might care can acquire it as an
additional return value from a named-constructor function.

These collectively trim `AgileReference`'s footprint down to a single
`RefPtr`, with all its special member functions having only default
implementations.

Differential Revision: https://phabricator.services.mozilla.com/D196513
This commit is contained in:
Ray Kraesig 2023-12-21 19:46:55 +00:00
Родитель d1a2476c7a
Коммит 50c943690c
4 изменённых файлов: 125 добавлений и 145 удалений

Просмотреть файл

@ -7,10 +7,7 @@
#include "mozilla/mscom/AgileReference.h"
#include <utility>
#include "mozilla/Assertions.h"
#include "mozilla/DebugOnly.h"
#include "mozilla/DynamicallyLinkedFunctionPtr.h"
#include "mozilla/mscom/Utils.h"
#if defined(__MINGW32__)
@ -40,81 +37,28 @@ static const mozilla::StaticDynamicallyLinkedFunctionPtr<
#endif // defined(__MINGW32__)
namespace mozilla::mscom {
AgileReference::AgileReference() : mIid(), mHResult(E_NOINTERFACE) {}
AgileReference::AgileReference(REFIID aIid, IUnknown* aObject)
: mIid(aIid), mHResult(E_UNEXPECTED) {
AssignInternal(aObject);
}
AgileReference::AgileReference(AgileReference&& aOther) noexcept
: mIid(aOther.mIid),
mAgileRef(std::move(aOther.mAgileRef)),
mHResult(aOther.mHResult) {
aOther.mHResult = CO_E_RELEASED;
}
void AgileReference::Assign(REFIID aIid, IUnknown* aObject) {
Clear();
mIid = aIid;
AssignInternal(aObject);
}
void AgileReference::AssignInternal(IUnknown* aObject) {
// We expect mIid to already be set
DebugOnly<IID> zeroIid = {};
MOZ_ASSERT(mIid != zeroIid);
namespace mozilla::mscom::detail {
HRESULT AgileReference_CreateImpl(RefPtr<IAgileReference>& aRefPtr, REFIID riid,
IUnknown* aObject) {
MOZ_ASSERT(aObject);
mHResult = RoGetAgileReference(AGILEREFERENCE_DEFAULT, mIid, aObject,
getter_AddRefs(mAgileRef));
MOZ_ASSERT(IsCOMInitializedOnCurrentThread());
return ::RoGetAgileReference(AGILEREFERENCE_DEFAULT, riid, aObject,
getter_AddRefs(aRefPtr));
}
AgileReference::~AgileReference() { Clear(); }
void AgileReference::Clear() {
mIid = {};
mAgileRef = nullptr;
mHResult = E_NOINTERFACE;
}
AgileReference& AgileReference::operator=(const AgileReference& aOther) {
Clear();
mIid = aOther.mIid;
mAgileRef = aOther.mAgileRef;
mHResult = aOther.mHResult;
return *this;
}
AgileReference& AgileReference::operator=(AgileReference&& aOther) noexcept {
Clear();
mIid = aOther.mIid;
mAgileRef = std::move(aOther.mAgileRef);
mHResult = aOther.mHResult;
aOther.mHResult = CO_E_RELEASED;
return *this;
}
HRESULT
AgileReference::ResolveRaw(REFIID aIid, void** aOutInterface) const {
HRESULT AgileReference_ResolveImpl(RefPtr<IAgileReference> const& aRefPtr,
REFIID riid, void** aOutInterface) {
MOZ_ASSERT(aRefPtr);
MOZ_ASSERT(aOutInterface);
MOZ_ASSERT(mAgileRef);
MOZ_ASSERT(IsCOMInitializedOnCurrentThread());
if (!aOutInterface) {
if (!aRefPtr || !aOutInterface) {
return E_INVALIDARG;
}
*aOutInterface = nullptr;
if (mAgileRef) {
return mAgileRef->Resolve(aIid, aOutInterface);
}
return E_NOINTERFACE;
return aRefPtr->Resolve(riid, aOutInterface);
}
} // namespace mozilla::mscom
} // namespace mozilla::mscom::detail

Просмотреть файл

@ -9,95 +9,135 @@
#include "mozilla/Attributes.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Result.h"
#include "mozilla/Unused.h"
#include "nsDebug.h"
#include "nsISupportsImpl.h"
#include <objidl.h>
namespace mozilla::mscom {
namespace detail {
// Detemplatized implementation details of `AgileReference`.
HRESULT AgileReference_CreateImpl(RefPtr<IAgileReference>&, REFIID, IUnknown*);
HRESULT AgileReference_ResolveImpl(RefPtr<IAgileReference> const&, REFIID,
void**);
} // namespace detail
/**
* This class encapsulates an "agile reference." These are references that
* allow you to pass COM interfaces between apartments. When you have an
* interface that you would like to pass between apartments, you wrap that
* interface in an AgileReference and pass the agile reference instead. Then
* you unwrap the interface by calling AgileReference::Resolve.
* This class encapsulates an "agile reference". These are references that allow
* you to pass COM interfaces between apartments. When you have an interface
* that you would like to pass between apartments, you wrap that interface in an
* AgileReference and pass that instead. Then you can "unwrap" the interface by
* calling Resolve(), which will return a proxy object implementing the same
* interface.
*
* Sample usage:
*
* // In the multithreaded apartment, foo is an IFoo*
* auto myAgileRef = AgileReference(IID_IFoo, foo);
*
* // myAgileRef is passed to our main thread, which runs in a single-threaded
* // apartment:
*
* RefPtr<IFoo> foo;
* HRESULT hr = myAgileRef.Resolve(IID_IFoo, getter_AddRefs(foo));
* // Now foo may be called from the main thread
* ```
* // From a non-main thread, where `foo` is an `IFoo*` or `RefPtr<IFoo>`:
* auto myAgileRef = AgileReference(foo);
* NS_DispatchToMainThread([mar = std::move(myAgileRef)] {
* RefPtr<IFoo> foo = mar.Resolve();
* // Now methods may be invoked on `foo`
* });
* ```
*/
template <typename InterfaceT>
class AgileReference final {
static_assert(
std::is_base_of_v<IUnknown, InterfaceT>,
"template parameter of AgileReference must be a COM interface type");
public:
AgileReference();
template <typename InterfaceT>
explicit AgileReference(RefPtr<InterfaceT>& aObject)
: AgileReference(__uuidof(InterfaceT), aObject) {}
AgileReference(REFIID aIid, IUnknown* aObject);
AgileReference() = default;
~AgileReference() = default;
AgileReference(const AgileReference& aOther) = default;
AgileReference(AgileReference&& aOther) noexcept;
AgileReference(AgileReference&& aOther) noexcept = default;
~AgileReference();
AgileReference& operator=(const AgileReference& aOther) = default;
AgileReference& operator=(AgileReference&& aOther) noexcept = default;
explicit operator bool() const { return !!mAgileRef; }
HRESULT GetHResult() const { return mHResult; }
template <typename T>
void Assign(const RefPtr<T>& aOther) {
Assign(__uuidof(T), aOther);
}
// Raw version, and implementation, of Resolve(). Can be used directly if
// necessary, but in general, prefer one of the templated versions below
// (depending on whether or not you need the HRESULT).
HRESULT ResolveRaw(REFIID aIid, void** aOutInterface) const;
template <typename Interface>
HRESULT Resolve(RefPtr<Interface>& aOutInterface) const {
return this->ResolveRaw(__uuidof(Interface), getter_AddRefs(aOutInterface));
}
template <typename T>
RefPtr<T> Resolve() {
RefPtr<T> p;
Resolve<T>(p);
return p;
}
AgileReference& operator=(const AgileReference& aOther);
AgileReference& operator=(AgileReference&& aOther) noexcept;
AgileReference& operator=(decltype(nullptr)) {
Clear();
AgileReference& operator=(std::nullptr_t) {
mAgileRef = nullptr;
return *this;
}
void Clear();
// Create a new AgileReference from an existing COM object.
//
// These constructors do not provide the HRESULT on failure. If that's
// desired, use `AgileReference::Create()`, below.
explicit AgileReference(InterfaceT* aObject) {
HRESULT const hr = detail::AgileReference_CreateImpl(
mAgileRef, __uuidof(InterfaceT), aObject);
Unused << NS_WARN_IF(FAILED(hr));
}
explicit AgileReference(RefPtr<InterfaceT> const& aObject)
: AgileReference(aObject.get()) {}
// Create a new AgileReference from an existing COM object, or alternatively,
// return the HRESULT explaining why one couldn't be created.
//
// A convenience wrapper `MakeAgileReference()` which infers `InterfaceT` from
// the RefPtr's concrete type is provided below.
static Result<AgileReference<InterfaceT>, HRESULT> Create(
RefPtr<InterfaceT> const& aObject) {
AgileReference ret;
HRESULT const hr = detail::AgileReference_CreateImpl(
ret.mAgileRef, __uuidof(InterfaceT), aObject.get());
if (FAILED(hr)) {
return Err(hr);
}
return ret;
}
explicit operator bool() const { return !!mAgileRef; }
// Common case: resolve directly to the originally-specified interface-type.
RefPtr<InterfaceT> Resolve() const {
auto res = ResolveAs<InterfaceT>();
if (res.isErr()) return nullptr;
return res.unwrap();
}
// Uncommon cases: resolve directly to a different interface type, and/or
// provide IAgileReference::Resolve()'s HRESULT.
//
// When used in other COM apartments, `IAgileInterface::Resolve()` returns a
// proxy object which (at time of writing) is not documented to provide any
// interface other than the one for which it was instantiated. (Calling
// `QueryInterface` _might_ work, but isn't explicitly guaranteed.)
//
template <typename OtherInterface = InterfaceT>
Result<RefPtr<OtherInterface>, HRESULT> ResolveAs() const {
RefPtr<OtherInterface> p;
auto const hr = ResolveRaw(__uuidof(OtherInterface), getter_AddRefs(p));
if (FAILED(hr)) {
return Err(hr);
}
return p;
}
// Raw version of Resolve/ResolveAs. Rarely, if ever, preferable to the
// statically-typed versions.
HRESULT ResolveRaw(REFIID aIid, void** aOutInterface) const {
return detail::AgileReference_ResolveImpl(mAgileRef, aIid, aOutInterface);
}
private:
void Assign(REFIID aIid, IUnknown* aObject);
void AssignInternal(IUnknown* aObject);
private:
// The interface ID with which this reference was constructed.
IID mIid;
RefPtr<IAgileReference> mAgileRef;
// The result associated with this reference's construction. May be modified
// when mAgileRef changes, but is explicitly not touched by `Resolve`.
HRESULT mHResult;
};
// Attempt to create an AgileReference from a refcounted interface pointer,
// providing the HRESULT as a secondary return-value.
template <typename InterfaceT>
inline Result<AgileReference<InterfaceT>, HRESULT> MakeAgileReference(
RefPtr<InterfaceT> const& aObj) {
return AgileReference<InterfaceT>::Create(aObj);
}
} // namespace mozilla::mscom
#endif // mozilla_mscom_AgileReference_h

Просмотреть файл

@ -130,11 +130,6 @@ LegacyJumpListBuilder::LegacyJumpListBuilder()
observerService->AddObserver(this, TOPIC_PROFILE_BEFORE_CHANGE, false);
observerService->AddObserver(this, TOPIC_CLEAR_PRIVATE_DATA, false);
}
RefPtr jumpListMgr = mJumpListMgr.Resolve<ICustomDestinationList>();
if (!jumpListMgr) {
return;
}
}
LegacyJumpListBuilder::~LegacyJumpListBuilder() {
@ -146,7 +141,7 @@ NS_IMETHODIMP LegacyJumpListBuilder::SetAppUserModelID(
ReentrantMonitorAutoEnter lock(mMonitor);
if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;
RefPtr jumpListMgr = mJumpListMgr.Resolve<ICustomDestinationList>();
RefPtr<ICustomDestinationList> jumpListMgr = mJumpListMgr.Resolve();
if (!jumpListMgr) {
return NS_ERROR_NOT_AVAILABLE;
}
@ -187,7 +182,7 @@ NS_IMETHODIMP LegacyJumpListBuilder::GetMaxListItems(int16_t* aMaxItems) {
return NS_OK;
}
RefPtr jumpListMgr = mJumpListMgr.Resolve<ICustomDestinationList>();
RefPtr<ICustomDestinationList> jumpListMgr = mJumpListMgr.Resolve();
if (!jumpListMgr) {
return NS_ERROR_UNEXPECTED;
}
@ -260,7 +255,7 @@ void LegacyJumpListBuilder::DoInitListBuild(RefPtr<Promise>&& aPromise) {
}));
});
RefPtr jumpListMgr = mJumpListMgr.Resolve<ICustomDestinationList>();
RefPtr<ICustomDestinationList> jumpListMgr = mJumpListMgr.Resolve();
if (!jumpListMgr) {
return;
}
@ -335,7 +330,7 @@ NS_IMETHODIMP LegacyJumpListBuilder::AddListToBuild(int16_t aCatType,
ReentrantMonitorAutoEnter lock(mMonitor);
if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;
RefPtr jumpListMgr = mJumpListMgr.Resolve<ICustomDestinationList>();
RefPtr<ICustomDestinationList> jumpListMgr = mJumpListMgr.Resolve();
if (!jumpListMgr) {
return NS_ERROR_UNEXPECTED;
}
@ -459,7 +454,7 @@ NS_IMETHODIMP LegacyJumpListBuilder::AbortListBuild() {
ReentrantMonitorAutoEnter lock(mMonitor);
if (!mJumpListMgr) return NS_ERROR_NOT_AVAILABLE;
RefPtr jumpListMgr = mJumpListMgr.Resolve<ICustomDestinationList>();
RefPtr<ICustomDestinationList> jumpListMgr = mJumpListMgr.Resolve();
if (!jumpListMgr) {
return NS_ERROR_UNEXPECTED;
}
@ -508,7 +503,7 @@ void LegacyJumpListBuilder::DoCommitListBuild(
Unused << NS_DispatchToMainThread(aCallback);
});
RefPtr jumpListMgr = mJumpListMgr.Resolve<ICustomDestinationList>();
RefPtr<ICustomDestinationList> jumpListMgr = mJumpListMgr.Resolve();
if (!jumpListMgr) {
return;
}
@ -531,7 +526,7 @@ NS_IMETHODIMP LegacyJumpListBuilder::DeleteActiveList(bool* _retval) {
AbortListBuild();
}
RefPtr jumpListMgr = mJumpListMgr.Resolve<ICustomDestinationList>();
RefPtr<ICustomDestinationList> jumpListMgr = mJumpListMgr.Resolve();
if (!jumpListMgr) {
return NS_ERROR_UNEXPECTED;
}

Просмотреть файл

@ -46,7 +46,8 @@ class LegacyJumpListBuilder : public nsILegacyJumpListBuilder,
static Atomic<bool> sBuildingList;
private:
mscom::AgileReference mJumpListMgr MOZ_GUARDED_BY(mMonitor);
mscom::AgileReference<ICustomDestinationList> mJumpListMgr
MOZ_GUARDED_BY(mMonitor);
uint32_t mMaxItems MOZ_GUARDED_BY(mMonitor);
bool mHasCommit;
RefPtr<LazyIdleThread> mIOThread;