| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WslCoreAdviseHandler.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains WSL Core COM Advise/Unadvise functions. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #pragma once |
| 16 | #include <vector> |
| 17 | #include <objbase.h> |
| 18 | #include <wil/com.h> |
| 19 | #include <wil/resource.h> |
| 20 | |
| 21 | namespace wsl::core { |
| 22 | // This class encapsulates the non-obvious and sometimes non-trivial calls to register for COM callbacks |
| 23 | // using the fairly standardized Advise ConnectionPoint interface |
| 24 | class WslCoreAdviseHandler |
| 25 | { |
| 26 | public: |
| 27 | WslCoreAdviseHandler() = default; |
| 28 | ~WslCoreAdviseHandler() = default; |
| 29 | |
| 30 | WslCoreAdviseHandler(const WslCoreAdviseHandler&) = delete; |
| 31 | WslCoreAdviseHandler& operator=(const WslCoreAdviseHandler&) = delete; |
| 32 | WslCoreAdviseHandler(WslCoreAdviseHandler&&) = delete; |
| 33 | WslCoreAdviseHandler& operator=(WslCoreAdviseHandler&&) = delete; |
| 34 | |
| 35 | // typename T is the connection point interface which is implemented |
| 36 | // typename C is the server object implementing IConnectionPoint to Advise() |
| 37 | // typename S is the client's sink object (must implement type T) |
| 38 | template <typename T, typename C, typename S> |
| 39 | void AdviseInProcObject(wil::com_ptr<C> sourceObject, _In_ S* connectionSink) |
| 40 | { |
| 41 | AdviseInstance newInstance; |
| 42 | |
| 43 | const wil::com_ptr<IConnectionPointContainer> pointContainer = sourceObject.template query<IConnectionPointContainer>(); |
| 44 | THROW_IF_FAILED(pointContainer->FindConnectionPoint(__uuidof(T), newInstance.m_connectionPoint.addressof())); |
| 45 | THROW_IF_FAILED(newInstance.m_connectionPoint->Advise(connectionSink, &newInstance.m_cookie)); |
| 46 | |
| 47 | m_adviseInstances.emplace_back(std::move(newInstance)); |
| 48 | } |
| 49 | |
| 50 | // typename C is the instantiated object implementing IConnectionPoint to Advise() |
| 51 | // typename S is the interface to register with Advise() |
| 52 | // Also sets the authentication information that will be used to make calls on the specified proxy. |
| 53 | template <typename T, typename C, typename S> |
| 54 | void AdviseProxyObject(wil::com_ptr<C> sourceObject, _In_ S* connectionSink) |
| 55 | { |
| 56 | AdviseInstance newInstance; |
| 57 | |
| 58 | const wil::com_ptr<IConnectionPointContainer> pointContainer = sourceObject.template query<IConnectionPointContainer>(); |
| 59 | THROW_IF_FAILED(CoSetProxyBlanket( |
| 60 | pointContainer.get(), RPC_C_AUTHN_DEFAULT, RPC_C_AUTHZ_NONE, COLE_DEFAULT_PRINCIPAL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_STATIC_CLOAKING)); |
| 61 | |
| 62 | THROW_IF_FAILED(pointContainer->FindConnectionPoint(__uuidof(T), newInstance.m_connectionPoint.addressof())); |
| 63 | THROW_IF_FAILED(CoSetProxyBlanket( |
| 64 | newInstance.m_connectionPoint.get(), RPC_C_AUTHN_DEFAULT, RPC_C_AUTHZ_NONE, COLE_DEFAULT_PRINCIPAL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_STATIC_CLOAKING)); |
| 65 | |
| 66 | THROW_IF_FAILED(newInstance.m_connectionPoint->Advise(static_cast<IUnknown*>(connectionSink), &newInstance.m_cookie)); |
| 67 | m_adviseInstances.emplace_back(std::move(newInstance)); |
| 68 | } |
| 69 | |
| 70 | void Reset() noexcept |
| 71 | { |
| 72 | m_adviseInstances.clear(); |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | struct AdviseInstance |
| 77 | { |
| 78 | wil::com_ptr<IConnectionPoint> m_connectionPoint{}; |
| 79 | DWORD m_cookie{0}; |
| 80 | |
| 81 | AdviseInstance() noexcept = default; |
| 82 | ~AdviseInstance() noexcept |
| 83 | { |
| 84 | if (m_connectionPoint && m_cookie != 0) |
| 85 | { |
| 86 | m_connectionPoint->Unadvise(m_cookie); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | AdviseInstance(const AdviseInstance&) = delete; |
| 91 | AdviseInstance& operator=(const AdviseInstance&) = delete; |
| 92 | AdviseInstance(AdviseInstance&&) = default; |
| 93 | AdviseInstance& operator=(AdviseInstance&&) = default; |
| 94 | }; |
| 95 | |
| 96 | std::vector<AdviseInstance> m_adviseInstances; |
| 97 | }; |
| 98 | } // namespace wsl::core |