|
| 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/. */ |
| 6 | + |
| 7 | +#include "mozilla/mscom/AgileReference.h" |
| 8 | + |
| 9 | +#include "DynamicallyLinkedFunctionPtr.h" |
| 10 | +#include "mozilla/DebugOnly.h" |
| 11 | +#include "mozilla/Assertions.h" |
| 12 | +#include "mozilla/Move.h" |
| 13 | + |
| 14 | +#if NTDDI_VERSION < NTDDI_WINBLUE |
| 15 | + |
| 16 | +// Declarations from Windows SDK specific to Windows 8.1 |
| 17 | + |
| 18 | +enum AgileReferenceOptions |
| 19 | +{ |
| 20 | + AGILEREFERENCE_DEFAULT = 0, |
| 21 | + AGILEREFERENCE_DELAYEDMARSHAL = 1, |
| 22 | +}; |
| 23 | + |
| 24 | +HRESULT WINAPI RoGetAgileReference(AgileReferenceOptions options, |
| 25 | + REFIID riid, IUnknown* pUnk, |
| 26 | + IAgileReference** ppAgileReference); |
| 27 | + |
| 28 | +#endif // NTDDI_VERSION < NTDDI_WINBLUE |
| 29 | + |
| 30 | +namespace mozilla { |
| 31 | +namespace mscom { |
| 32 | + |
| 33 | +AgileReference::AgileReference(REFIID aIid, IUnknown* aObject) |
| 34 | + : mIid(aIid) |
| 35 | + , mGitCookie(0) |
| 36 | +{ |
| 37 | + /* |
| 38 | + * There are two possible techniques for creating agile references. Starting |
| 39 | + * with Windows 8.1, we may use the RoGetAgileReference API, which is faster. |
| 40 | + * If that API is not available, we fall back to using the Global Interface |
| 41 | + * Table. |
| 42 | + */ |
| 43 | + static const DynamicallyLinkedFunctionPtr<decltype(&::RoGetAgileReference)> |
| 44 | + pRoGetAgileReference(L"ole32.dll", "RoGetAgileReference"); |
| 45 | + |
| 46 | + MOZ_ASSERT(aObject); |
| 47 | + |
| 48 | + if (pRoGetAgileReference && |
| 49 | + SUCCEEDED(pRoGetAgileReference(AGILEREFERENCE_DEFAULT, aIid, aObject, |
| 50 | + getter_AddRefs(mAgileRef)))) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + IGlobalInterfaceTable* git = ObtainGit(); |
| 55 | + MOZ_ASSERT(git); |
| 56 | + if (!git) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + DebugOnly<HRESULT> hr = git->RegisterInterfaceInGlobal(aObject, aIid, |
| 61 | + &mGitCookie); |
| 62 | + MOZ_ASSERT(SUCCEEDED(hr)); |
| 63 | +} |
| 64 | + |
| 65 | +AgileReference::AgileReference(AgileReference&& aOther) |
| 66 | + : mIid(aOther.mIid) |
| 67 | + , mAgileRef(Move(aOther.mAgileRef)) |
| 68 | + , mGitCookie(aOther.mGitCookie) |
| 69 | +{ |
| 70 | + aOther.mGitCookie = 0; |
| 71 | +} |
| 72 | + |
| 73 | +AgileReference::~AgileReference() |
| 74 | +{ |
| 75 | + if (!mGitCookie) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + IGlobalInterfaceTable* git = ObtainGit(); |
| 80 | + MOZ_ASSERT(git); |
| 81 | + if (!git) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + DebugOnly<HRESULT> hr = git->RevokeInterfaceFromGlobal(mGitCookie); |
| 86 | + MOZ_ASSERT(SUCCEEDED(hr)); |
| 87 | +} |
| 88 | + |
| 89 | +HRESULT |
| 90 | +AgileReference::Resolve(REFIID aIid, void** aOutInterface) |
| 91 | +{ |
| 92 | + MOZ_ASSERT(aOutInterface); |
| 93 | + MOZ_ASSERT(mAgileRef || mGitCookie); |
| 94 | + |
| 95 | + if (!aOutInterface) { |
| 96 | + return E_INVALIDARG; |
| 97 | + } |
| 98 | + |
| 99 | + *aOutInterface = nullptr; |
| 100 | + |
| 101 | + if (mAgileRef) { |
| 102 | + // IAgileReference lets you directly resolve the interface you want... |
| 103 | + return mAgileRef->Resolve(aIid, aOutInterface); |
| 104 | + } |
| 105 | + |
| 106 | + if (!mGitCookie) { |
| 107 | + return E_UNEXPECTED; |
| 108 | + } |
| 109 | + |
| 110 | + IGlobalInterfaceTable* git = ObtainGit(); |
| 111 | + MOZ_ASSERT(git); |
| 112 | + if (!git) { |
| 113 | + return E_UNEXPECTED; |
| 114 | + } |
| 115 | + |
| 116 | + RefPtr<IUnknown> originalInterface; |
| 117 | + HRESULT hr = git->GetInterfaceFromGlobal(mGitCookie, mIid, |
| 118 | + getter_AddRefs(originalInterface)); |
| 119 | + if (FAILED(hr)) { |
| 120 | + return hr; |
| 121 | + } |
| 122 | + |
| 123 | + if (aIid == mIid) { |
| 124 | + originalInterface.forget(aOutInterface); |
| 125 | + return S_OK; |
| 126 | + } |
| 127 | + |
| 128 | + // ...Whereas the GIT requires us to obtain the same interface that we |
| 129 | + // requested and then QI for the desired interface afterward. |
| 130 | + return originalInterface->QueryInterface(aIid, aOutInterface); |
| 131 | +} |
| 132 | + |
| 133 | +IGlobalInterfaceTable* |
| 134 | +AgileReference::ObtainGit() |
| 135 | +{ |
| 136 | + // Internally to COM, the Global Interface Table is a singleton, therefore we |
| 137 | + // don't worry about holding onto this reference indefinitely. |
| 138 | + static IGlobalInterfaceTable * const sGit = []() -> IGlobalInterfaceTable * const { |
| 139 | + IGlobalInterfaceTable* result = nullptr; |
| 140 | + DebugOnly<HRESULT> hr = |
| 141 | + ::CoCreateInstance(CLSID_StdGlobalInterfaceTable, nullptr, |
| 142 | + CLSCTX_INPROC_SERVER, IID_IGlobalInterfaceTable, |
| 143 | + reinterpret_cast<void**>(&result)); |
| 144 | + MOZ_ASSERT(SUCCEEDED(hr)); |
| 145 | + return result; |
| 146 | + }(); |
| 147 | + |
| 148 | + return sGit; |
| 149 | +} |
| 150 | + |
| 151 | +} // namespace mscom |
| 152 | +} // namespace mozilla |
0 commit comments