1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_mscom_Interceptor_h
8 #define mozilla_mscom_Interceptor_h
10 #include "mozilla/Move.h"
11 #include "mozilla/Mutex.h"
13 #include "mozilla/mscom/IHandlerProvider.h"
14 #include "mozilla/mscom/Ptr.h"
15 #include "mozilla/mscom/WeakRef.h"
16 #include "mozilla/RefPtr.h"
25 class LiveSetAutoLock
;
29 // {8831EB53-A937-42BC-9921-B3E1121FDF86}
30 DEFINE_GUID(IID_IInterceptorSink
, 0x8831eb53, 0xa937, 0x42bc, 0x99, 0x21, 0xb3,
31 0xe1, 0x12, 0x1f, 0xdf, 0x86);
33 struct IInterceptorSink
: public ICallFrameEvents
, public HandlerProvider
{
34 virtual STDMETHODIMP
SetInterceptor(IWeakReference
* aInterceptor
) = 0;
37 // {3710799B-ECA2-4165-B9B0-3FA1E4A9B230}
38 DEFINE_GUID(IID_IInterceptor
, 0x3710799b, 0xeca2, 0x4165, 0xb9, 0xb0, 0x3f,
39 0xa1, 0xe4, 0xa9, 0xb2, 0x30);
41 struct IInterceptor
: public IUnknown
{
42 virtual STDMETHODIMP
GetTargetForIID(
43 REFIID aIid
, InterceptorTargetPtr
<IUnknown
>& aTarget
) = 0;
44 virtual STDMETHODIMP
GetInterceptorForIID(REFIID aIid
,
45 void** aOutInterceptor
) = 0;
49 * The COM interceptor is the core functionality in mscom that allows us to
50 * redirect method calls to different threads. It emulates the vtable of a
51 * target interface. When a call is made on this emulated vtable, the call is
52 * packaged up into an instance of the ICallFrame interface which may be passed
53 * to other contexts for execution.
55 * In order to accomplish this, COM itself provides the CoGetInterceptor
56 * function, which instantiates an ICallInterceptor. Note, however, that
57 * ICallInterceptor only works on a single interface; we need to be able to
58 * interpose QueryInterface calls so that we can instantiate a new
59 * ICallInterceptor for each new interface that is requested.
61 * We accomplish this by using COM aggregation, which means that the
62 * ICallInterceptor delegates its IUnknown implementation to its outer object
63 * (the mscom::Interceptor we implement and control).
65 class Interceptor final
: public WeakReferenceSupport
,
66 public IStdMarshalInfo
,
70 static HRESULT
Create(STAUniquePtr
<IUnknown
> aTarget
, IInterceptorSink
* aSink
,
71 REFIID aInitialIid
, void** aOutInterface
);
74 * Disconnect all remote clients for a given target.
75 * Because Interceptors disable COM garbage collection to improve
76 * performance, they never receive Release calls from remote clients. If
77 * the object can be shut down while clients still hold a reference, this
78 * function can be used to force COM to disconnect all remote connections
79 * (using CoDisconnectObject) and thus release the associated references to
80 * the Interceptor, its target and any objects associated with the
82 * Note that the specified target must be the same IUnknown pointer used to
83 * create the Interceptor. Where there is multiple inheritance, querying for
84 * IID_IUnknown and calling this function with that pointer alone will not
85 * disconnect remotes for all interfaces. If you expect that the same object
86 * may be fetched with different initial interfaces, you should call this
87 * function once for each possible IUnknown pointer.
88 * @return S_OK if there was an Interceptor for the given target,
89 * S_FALSE if there was not.
91 static HRESULT
DisconnectRemotesForTarget(IUnknown
* aTarget
);
94 STDMETHODIMP
QueryInterface(REFIID riid
, void** ppv
) override
;
95 STDMETHODIMP_(ULONG
) AddRef() override
;
96 STDMETHODIMP_(ULONG
) Release() override
;
99 STDMETHODIMP
GetClassForHandler(DWORD aDestContext
, void* aDestContextPtr
,
100 CLSID
* aHandlerClsid
) override
;
103 STDMETHODIMP
GetUnmarshalClass(REFIID riid
, void* pv
, DWORD dwDestContext
,
104 void* pvDestContext
, DWORD mshlflags
,
105 CLSID
* pCid
) override
;
106 STDMETHODIMP
GetMarshalSizeMax(REFIID riid
, void* pv
, DWORD dwDestContext
,
107 void* pvDestContext
, DWORD mshlflags
,
108 DWORD
* pSize
) override
;
109 STDMETHODIMP
MarshalInterface(IStream
* pStm
, REFIID riid
, void* pv
,
110 DWORD dwDestContext
, void* pvDestContext
,
111 DWORD mshlflags
) override
;
112 STDMETHODIMP
UnmarshalInterface(IStream
* pStm
, REFIID riid
,
113 void** ppv
) override
;
114 STDMETHODIMP
ReleaseMarshalData(IStream
* pStm
) override
;
115 STDMETHODIMP
DisconnectObject(DWORD dwReserved
) override
;
118 STDMETHODIMP
GetTargetForIID(
119 REFIID aIid
, InterceptorTargetPtr
<IUnknown
>& aTarget
) override
;
120 STDMETHODIMP
GetInterceptorForIID(REFIID aIid
,
121 void** aOutInterceptor
) override
;
125 MapEntry(REFIID aIid
, IUnknown
* aInterceptor
, IUnknown
* aTargetInterface
)
127 mInterceptor(aInterceptor
),
128 mTargetInterface(aTargetInterface
) {}
131 RefPtr
<IUnknown
> mInterceptor
;
132 IUnknown
* mTargetInterface
;
136 explicit Interceptor(IInterceptorSink
* aSink
);
138 HRESULT
GetInitialInterceptorForIID(detail::LiveSetAutoLock
& aLiveSetLock
,
140 STAUniquePtr
<IUnknown
> aTarget
,
141 void** aOutInterface
);
142 HRESULT
GetInterceptorForIID(REFIID aIid
, void** aOutInterceptor
,
143 MutexAutoLock
* aAlreadyLocked
);
144 MapEntry
* Lookup(REFIID aIid
);
145 HRESULT
QueryInterfaceTarget(REFIID aIid
, void** aOutput
,
146 TimeDuration
* aOutDuration
= nullptr);
147 HRESULT
WeakRefQueryInterface(REFIID aIid
, IUnknown
** aOutInterface
) override
;
148 HRESULT
CreateInterceptor(REFIID aIid
, IUnknown
* aOuter
, IUnknown
** aOutput
);
149 REFIID
MarshalAs(REFIID aIid
) const;
150 HRESULT
PublishTarget(detail::LiveSetAutoLock
& aLiveSetLock
,
151 RefPtr
<IUnknown
> aInterceptor
, REFIID aTargetIid
,
152 STAUniquePtr
<IUnknown
> aTarget
);
155 InterceptorTargetPtr
<IUnknown
> mTarget
;
156 RefPtr
<IInterceptorSink
> mEventSink
;
157 mozilla::Mutex mInterceptorMapMutex
; // Guards mInterceptorMap
158 // Using a nsTArray since the # of interfaces is not going to be very high
159 nsTArray
<MapEntry
> mInterceptorMap
;
160 mozilla::Mutex mStdMarshalMutex
; // Guards mStdMarshalUnk and mStdMarshal
161 RefPtr
<IUnknown
> mStdMarshalUnk
;
162 IMarshal
* mStdMarshal
; // WEAK
163 static MOZ_THREAD_LOCAL(bool) tlsCreatingStdMarshal
;
166 template <typename InterfaceT
>
167 inline HRESULT
CreateInterceptor(STAUniquePtr
<InterfaceT
> aTargetInterface
,
168 IInterceptorSink
* aEventSink
,
169 InterfaceT
** aOutInterface
) {
170 if (!aTargetInterface
|| !aEventSink
) {
174 REFIID iidTarget
= __uuidof(InterfaceT
);
176 STAUniquePtr
<IUnknown
> targetUnknown(aTargetInterface
.release());
177 return Interceptor::Create(std::move(targetUnknown
), aEventSink
, iidTarget
,
178 (void**)aOutInterface
);
182 } // namespace mozilla
184 #endif // mozilla_mscom_Interceptor_h