2016-07-27 17:44:56 +00: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/. */
|
|
|
|
|
2017-02-18 00:30:03 +00:00
|
|
|
#ifndef mozilla_mscom_Interceptor_h
|
|
|
|
#define mozilla_mscom_Interceptor_h
|
2016-07-27 17:44:56 +00:00
|
|
|
|
|
|
|
#include "mozilla/Move.h"
|
|
|
|
#include "mozilla/Mutex.h"
|
|
|
|
#include "nsTArray.h"
|
2017-02-18 00:30:03 +00:00
|
|
|
#include "mozilla/mscom/IHandlerPayload.h"
|
2016-07-27 17:44:56 +00:00
|
|
|
#include "mozilla/mscom/Ptr.h"
|
|
|
|
#include "mozilla/mscom/WeakRef.h"
|
|
|
|
#include "mozilla/RefPtr.h"
|
|
|
|
|
2017-02-18 00:30:03 +00:00
|
|
|
#include <objidl.h>
|
2016-07-27 17:44:56 +00:00
|
|
|
#include <callobj.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace mscom {
|
|
|
|
|
|
|
|
// {8831EB53-A937-42BC-9921-B3E1121FDF86}
|
|
|
|
DEFINE_GUID(IID_IInterceptorSink,
|
|
|
|
0x8831eb53, 0xa937, 0x42bc, 0x99, 0x21, 0xb3, 0xe1, 0x12, 0x1f, 0xdf, 0x86);
|
|
|
|
|
|
|
|
struct IInterceptorSink : public ICallFrameEvents
|
2017-02-18 00:30:03 +00:00
|
|
|
, public HandlerPayload
|
2016-07-27 17:44:56 +00:00
|
|
|
{
|
|
|
|
virtual STDMETHODIMP SetInterceptor(IWeakReference* aInterceptor) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
// {3710799B-ECA2-4165-B9B0-3FA1E4A9B230}
|
|
|
|
DEFINE_GUID(IID_IInterceptor,
|
|
|
|
0x3710799b, 0xeca2, 0x4165, 0xb9, 0xb0, 0x3f, 0xa1, 0xe4, 0xa9, 0xb2, 0x30);
|
|
|
|
|
|
|
|
struct IInterceptor : public IUnknown
|
|
|
|
{
|
2017-03-07 00:22:43 +00:00
|
|
|
virtual STDMETHODIMP GetTargetForIID(REFIID aIid,
|
|
|
|
InterceptorTargetPtr<IUnknown>& aTarget) = 0;
|
2016-07-27 17:44:56 +00:00
|
|
|
virtual STDMETHODIMP GetInterceptorForIID(REFIID aIid,
|
|
|
|
void** aOutInterceptor) = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The COM interceptor is the core functionality in mscom that allows us to
|
|
|
|
* redirect method calls to different threads. It emulates the vtable of a
|
|
|
|
* target interface. When a call is made on this emulated vtable, the call is
|
|
|
|
* packaged up into an instance of the ICallFrame interface which may be passed
|
|
|
|
* to other contexts for execution.
|
|
|
|
*
|
|
|
|
* In order to accomplish this, COM itself provides the CoGetInterceptor
|
|
|
|
* function, which instantiates an ICallInterceptor. Note, however, that
|
|
|
|
* ICallInterceptor only works on a single interface; we need to be able to
|
|
|
|
* interpose QueryInterface calls so that we can instantiate a new
|
|
|
|
* ICallInterceptor for each new interface that is requested.
|
|
|
|
*
|
|
|
|
* We accomplish this by using COM aggregation, which means that the
|
|
|
|
* ICallInterceptor delegates its IUnknown implementation to its outer object
|
|
|
|
* (the mscom::Interceptor we implement and control).
|
|
|
|
*/
|
|
|
|
class Interceptor final : public WeakReferenceSupport
|
2017-02-18 00:30:03 +00:00
|
|
|
, public IStdMarshalInfo
|
|
|
|
, public IMarshal
|
2016-07-27 17:44:56 +00:00
|
|
|
, public IInterceptor
|
|
|
|
{
|
|
|
|
public:
|
2016-08-26 16:03:17 +00:00
|
|
|
static HRESULT Create(STAUniquePtr<IUnknown> aTarget, IInterceptorSink* aSink,
|
2016-07-27 17:44:56 +00:00
|
|
|
REFIID aIid, void** aOutput);
|
|
|
|
|
|
|
|
// IUnknown
|
|
|
|
STDMETHODIMP QueryInterface(REFIID riid, void** ppv) override;
|
|
|
|
STDMETHODIMP_(ULONG) AddRef() override;
|
|
|
|
STDMETHODIMP_(ULONG) Release() override;
|
|
|
|
|
2017-02-18 00:30:03 +00:00
|
|
|
// IStdMarshalInfo
|
|
|
|
STDMETHODIMP GetClassForHandler(DWORD aDestContext, void* aDestContextPtr,
|
|
|
|
CLSID* aHandlerClsid) override;
|
|
|
|
|
|
|
|
// IMarshal
|
|
|
|
STDMETHODIMP GetUnmarshalClass(REFIID riid, void* pv, DWORD dwDestContext,
|
|
|
|
void* pvDestContext, DWORD mshlflags,
|
|
|
|
CLSID* pCid) override;
|
|
|
|
STDMETHODIMP GetMarshalSizeMax(REFIID riid, void* pv, DWORD dwDestContext,
|
|
|
|
void* pvDestContext, DWORD mshlflags,
|
|
|
|
DWORD* pSize) override;
|
|
|
|
STDMETHODIMP MarshalInterface(IStream* pStm, REFIID riid, void* pv,
|
|
|
|
DWORD dwDestContext, void* pvDestContext,
|
|
|
|
DWORD mshlflags) override;
|
|
|
|
STDMETHODIMP UnmarshalInterface(IStream* pStm, REFIID riid,
|
|
|
|
void** ppv) override;
|
|
|
|
STDMETHODIMP ReleaseMarshalData(IStream* pStm) override;
|
|
|
|
STDMETHODIMP DisconnectObject(DWORD dwReserved) override;
|
|
|
|
|
2016-07-27 17:44:56 +00:00
|
|
|
// IInterceptor
|
2017-03-07 00:22:43 +00:00
|
|
|
STDMETHODIMP GetTargetForIID(REFIID aIid,
|
|
|
|
InterceptorTargetPtr<IUnknown>& aTarget) override;
|
2016-07-27 17:44:56 +00:00
|
|
|
STDMETHODIMP GetInterceptorForIID(REFIID aIid, void** aOutInterceptor) override;
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct MapEntry
|
|
|
|
{
|
|
|
|
MapEntry(REFIID aIid, IUnknown* aInterceptor, IUnknown* aTargetInterface)
|
|
|
|
: mIID(aIid)
|
|
|
|
, mInterceptor(aInterceptor)
|
|
|
|
, mTargetInterface(aTargetInterface)
|
|
|
|
{}
|
|
|
|
IID mIID;
|
2016-12-14 06:34:26 +00:00
|
|
|
RefPtr<IUnknown> mInterceptor;
|
2016-07-27 17:44:56 +00:00
|
|
|
IUnknown* mTargetInterface;
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2016-08-26 16:03:17 +00:00
|
|
|
Interceptor(STAUniquePtr<IUnknown> aTarget, IInterceptorSink* aSink);
|
2016-07-27 17:44:56 +00:00
|
|
|
~Interceptor();
|
|
|
|
MapEntry* Lookup(REFIID aIid);
|
|
|
|
HRESULT QueryInterfaceTarget(REFIID aIid, void** aOutput);
|
|
|
|
HRESULT ThreadSafeQueryInterface(REFIID aIid,
|
|
|
|
IUnknown** aOutInterface) override;
|
|
|
|
HRESULT CreateInterceptor(REFIID aIid, IUnknown* aOuter, IUnknown** aOutput);
|
|
|
|
|
|
|
|
private:
|
|
|
|
STAUniquePtr<IUnknown> mTarget;
|
|
|
|
RefPtr<IInterceptorSink> mEventSink;
|
|
|
|
mozilla::Mutex mMutex; // Guards mInterceptorMap
|
|
|
|
// Using a nsTArray since the # of interfaces is not going to be very high
|
|
|
|
nsTArray<MapEntry> mInterceptorMap;
|
2017-02-18 00:30:03 +00:00
|
|
|
RefPtr<IUnknown> mStdMarshalUnk;
|
|
|
|
IMarshal* mStdMarshal; // WEAK
|
2016-07-27 17:44:56 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename InterfaceT>
|
|
|
|
inline HRESULT
|
2016-08-26 16:03:17 +00:00
|
|
|
CreateInterceptor(STAUniquePtr<InterfaceT> aTargetInterface,
|
2016-07-27 17:44:56 +00:00
|
|
|
IInterceptorSink* aEventSink,
|
|
|
|
InterfaceT** aOutInterface)
|
|
|
|
{
|
|
|
|
if (!aTargetInterface || !aEventSink) {
|
|
|
|
return E_INVALIDARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
REFIID iidTarget = __uuidof(aTargetInterface);
|
|
|
|
|
|
|
|
STAUniquePtr<IUnknown> targetUnknown(aTargetInterface.release());
|
2016-08-26 16:03:17 +00:00
|
|
|
return Interceptor::Create(Move(targetUnknown), aEventSink, iidTarget,
|
2016-07-27 17:44:56 +00:00
|
|
|
(void**)aOutInterface);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mscom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2017-02-18 00:30:03 +00:00
|
|
|
#endif // mozilla_mscom_Interceptor_h
|