Files
firefox/accessible/ipc/win/HandlerProvider.cpp
T
Aaron Klotz f9d4c14477 Bug 1303060: Changes to a11y to enable the serving of a COM handler; r=tbsaunde
MozReview-Commit-ID: GTQF3x1pBtX

A general outline of the COM handler (a.k.a. the "smart proxy"):

COM handlers are pieces of code that are loaded by the COM runtime along with
a proxy and are layered above that proxy. This enables the COM handler to
interpose itself between the caller and the proxy, thus providing the
opportunity for the handler to manipulate an interface's method calls before
those calls reach the proxy.

Handlers are regular COM components that live in DLLs and are declared in the
Windows registry. In order to allow for the specifying of a handler (and an
optional payload to be sent with the proxy), the mscom library allows its
clients to specify an implementation of the IHandlerProvider interface.

IHandlerProvider consists of 5 functions:

* GetHandler returns the CLSID of the component that should be loaded into
  the COM client's process. If GetHandler returns a failure code, then no
  handler is loaded.
* GetHandlerPayloadSize and WriteHandlerPayload are for obtaining the payload
  data. These calls are made on a background thread but need to do their work
  on the main thread. We declare the payload struct in IDL. MIDL generates two
  functions, IA2Payload_Encode and IA2Payload_Decode, which are used by
  mscom::StructToStream to read and write that struct to and from buffers.
* The a11y payload struct also includes an interface, IGeckoBackChannel, that
  allows the handler to communicate directly with Gecko. IGeckoBackChannel
  currently provides two methods: one to allow the handler to request fresh
  cache information, and the other to provide Gecko with its IHandlerControl
  interface.
* MarshalAs accepts an IID that specifies the interface that is about to be
  proxied. We may want to send a more sophisticated proxy than the one that
  is requested. The desired IID is returned by this function. In the case of
  a11y interfaces, we should always return IAccessible2_3 if we are asked for
  one of its parent interfaces. This allows us to eliminate round trips to
  resolve more sophisticated interfaces later on.
* NewInstance, which is needed to ensure that all descendent proxies are also
  imbued with the same handler code.

The main focus of this patch is as follows:

1. Provide an implementation of the IHandlerProvider interface;
2. Populate the handler payload (ie, the cache) with data;
3. Modify CreateHolderFromAccessible to specify the HandlerPayload object;
4. Receive the IHandlerControl interface from the handler DLL and move it
   into the chrome process.

Some more information about IHandlerControl:

There is one IHandlerControl per handler DLL instance. It is the interface that
we call in Gecko when we need to dispatch an event to the handler. In order to
ensure that events are dispatched in the correct order, we need to dispatch
those events from the chrome main thread so that they occur in sequential order
with calls to NotifyWinEvent.
2017-04-04 15:23:55 -06:00

264 lines
6.2 KiB
C++

/* -*- 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/a11y/HandlerProvider.h"
#include "Accessible2_3.h"
#include "HandlerData.h"
#include "HandlerData_i.c"
#include "mozilla/Assertions.h"
#include "mozilla/a11y/AccessibleWrap.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/Move.h"
#include "mozilla/mscom/AgileReference.h"
#include "mozilla/mscom/MainThreadInvoker.h"
#include "mozilla/mscom/Ptr.h"
#include "mozilla/mscom/StructStream.h"
#include "mozilla/mscom/Utils.h"
#include "nsThreadUtils.h"
#include <memory.h>
namespace mozilla {
namespace a11y {
HandlerProvider::HandlerProvider(REFIID aIid,
mscom::InterceptorTargetPtr<IUnknown> aTarget)
: mRefCnt(0)
, mMutex("mozilla::a11y::HandlerProvider::mMutex")
, mTargetUnkIid(aIid)
, mTargetUnk(Move(aTarget))
{
}
HRESULT
HandlerProvider::QueryInterface(REFIID riid, void** ppv)
{
if (!ppv) {
return E_INVALIDARG;
}
RefPtr<IUnknown> punk;
if (riid == IID_IUnknown || riid == IID_IGeckoBackChannel) {
punk = static_cast<IGeckoBackChannel*>(this);
}
if (!punk) {
return E_NOINTERFACE;
}
punk.forget(ppv);
return S_OK;
}
ULONG
HandlerProvider::AddRef()
{
return ++mRefCnt;
}
ULONG
HandlerProvider::Release()
{
ULONG result = --mRefCnt;
if (!result) {
delete this;
}
return result;
}
HRESULT
HandlerProvider::GetHandler(NotNull<CLSID*> aHandlerClsid)
{
if (!IsTargetInterfaceCacheable()) {
return E_NOINTERFACE;
}
*aHandlerClsid = CLSID_AccessibleHandler;
return S_OK;
}
void
HandlerProvider::GetAndSerializePayload(const MutexAutoLock&)
{
MOZ_ASSERT(mscom::IsCurrentThreadMTA());
if (mSerializer) {
return;
}
IA2Payload payload{};
if (!mscom::InvokeOnMainThread(this, &HandlerProvider::BuildIA2Data,
&payload.mData) ||
!payload.mData.mUniqueId) {
return;
}
// But we set mGeckoBackChannel on the current thread which resides in the
// MTA. This is important to ensure that COM always invokes
// IGeckoBackChannel methods in an MTA background thread.
RefPtr<IGeckoBackChannel> payloadRef(this);
// AddRef/Release pair for this reference is handled by payloadRef
payload.mGeckoBackChannel = this;
mSerializer = MakeUnique<mscom::StructToStream>(payload, &IA2Payload_Encode);
// Now that we have serialized payload, we should free any BSTRs that were
// allocated in BuildIA2Data.
ClearIA2Data(payload.mData);
}
HRESULT
HandlerProvider::GetHandlerPayloadSize(NotNull<DWORD*> aOutPayloadSize)
{
MOZ_ASSERT(mscom::IsCurrentThreadMTA());
if (!IsTargetInterfaceCacheable()) {
*aOutPayloadSize = mscom::StructToStream::GetEmptySize();
return S_OK;
}
MutexAutoLock lock(mMutex);
GetAndSerializePayload(lock);
if (!mSerializer) {
return E_FAIL;
}
*aOutPayloadSize = mSerializer->GetSize();
return S_OK;
}
void
HandlerProvider::BuildIA2Data(IA2Data* aOutIA2Data)
{
MOZ_ASSERT(aOutIA2Data);
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mTargetUnk);
MOZ_ASSERT(IsTargetInterfaceCacheable());
RefPtr<NEWEST_IA2_INTERFACE>
target(static_cast<NEWEST_IA2_INTERFACE*>(mTargetUnk.get()));
// NB: get_uniqueID should be the final property retrieved in this method,
// as its presence is used to determine whether the rest of this data
// retrieval was successful.
HRESULT hr = target->get_uniqueID(&aOutIA2Data->mUniqueId);
if (FAILED(hr)) {
ClearIA2Data(*aOutIA2Data);
}
}
void
HandlerProvider::ClearIA2Data(IA2Data& aData)
{
ZeroMemory(&aData, sizeof(IA2Data));
}
bool
HandlerProvider::IsTargetInterfaceCacheable()
{
return MarshalAs(mTargetUnkIid) == NEWEST_IA2_IID;
}
HRESULT
HandlerProvider::WriteHandlerPayload(NotNull<IStream*> aStream)
{
MutexAutoLock lock(mMutex);
if (!mSerializer) {
mscom::StructToStream emptyStruct;
return emptyStruct.Write(aStream);
}
HRESULT hr = mSerializer->Write(aStream);
mSerializer.reset();
return hr;
}
REFIID
HandlerProvider::MarshalAs(REFIID aIid)
{
static_assert(&NEWEST_IA2_IID == &IID_IAccessible2_3,
"You have modified NEWEST_IA2_IID. This code needs updating.");
if (aIid == IID_IDispatch || aIid == IID_IAccessible ||
aIid == IID_IAccessible2 || aIid == IID_IAccessible2_2 ||
aIid == IID_IAccessible2_3) {
// This should always be the newest IA2 interface ID
return NEWEST_IA2_IID;
}
// Otherwise we juse return the identity.
return aIid;
}
HRESULT
HandlerProvider::NewInstance(REFIID aIid,
mscom::InterceptorTargetPtr<IUnknown> aTarget,
NotNull<mscom::IHandlerProvider**> aOutNewPayload)
{
RefPtr<IHandlerProvider> newPayload(new HandlerProvider(aIid, Move(aTarget)));
newPayload.forget(aOutNewPayload.get());
return S_OK;
}
void
HandlerProvider::SetHandlerControlOnMainThread(DWORD aPid,
mscom::ProxyUniquePtr<IHandlerControl> aCtrl)
{
MOZ_ASSERT(NS_IsMainThread());
auto content = dom::ContentChild::GetSingleton();
MOZ_ASSERT(content);
IHandlerControlHolder holder(CreateHolderFromHandlerControl(Move(aCtrl)));
Unused << content->SendA11yHandlerControl(aPid, holder);
}
HRESULT
HandlerProvider::put_HandlerControl(long aPid, IHandlerControl* aCtrl)
{
MOZ_ASSERT(mscom::IsCurrentThreadMTA());
if (!aCtrl) {
return E_INVALIDARG;
}
auto ptrProxy = mscom::ToProxyUniquePtr(aCtrl);
if (!mscom::InvokeOnMainThread(this,
&HandlerProvider::SetHandlerControlOnMainThread,
static_cast<DWORD>(aPid), Move(ptrProxy))) {
return E_FAIL;
}
return S_OK;
}
HRESULT
HandlerProvider::Refresh(IA2Data* aOutData)
{
MOZ_ASSERT(mscom::IsCurrentThreadMTA());
if (!mscom::InvokeOnMainThread(this, &HandlerProvider::BuildIA2Data,
aOutData)) {
return E_FAIL;
}
return S_OK;
}
} // namespace a11y
} // namespace mozilla