Utils.h (3770B)
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 #ifndef mozilla_mscom_Utils_h 8 #define mozilla_mscom_Utils_h 9 10 #if defined(MOZILLA_INTERNAL_API) 11 # include "nsString.h" 12 #endif // defined(MOZILLA_INTERNAL_API) 13 14 #include "mozilla/Attributes.h" 15 #include <guiddef.h> 16 #include <stdint.h> 17 18 struct IStream; 19 struct IUnknown; 20 21 namespace mozilla { 22 namespace mscom { 23 namespace detail { 24 25 enum class GuidType { 26 CLSID, 27 AppID, 28 }; 29 30 long BuildRegGuidPath(REFGUID aGuid, const GuidType aGuidType, wchar_t* aBuf, 31 const size_t aBufLen); 32 33 } // namespace detail 34 35 bool IsCOMInitializedOnCurrentThread(); 36 bool IsCurrentThreadMTA(); 37 bool IsCurrentThreadExplicitMTA(); 38 bool IsCurrentThreadImplicitMTA(); 39 #if defined(MOZILLA_INTERNAL_API) 40 bool IsCurrentThreadNonMainMTA(); 41 #endif // defined(MOZILLA_INTERNAL_API) 42 bool IsProxy(IUnknown* aUnknown); 43 bool IsValidGUID(REFGUID aCheckGuid); 44 uintptr_t GetContainingModuleHandle(); 45 46 template <size_t N> 47 inline long BuildAppidPath(REFGUID aAppId, wchar_t (&aPath)[N]) { 48 return detail::BuildRegGuidPath(aAppId, detail::GuidType::AppID, aPath, N); 49 } 50 51 template <size_t N> 52 inline long BuildClsidPath(REFCLSID aClsid, wchar_t (&aPath)[N]) { 53 return detail::BuildRegGuidPath(aClsid, detail::GuidType::CLSID, aPath, N); 54 } 55 56 /** 57 * Given a buffer, create a new IStream object. 58 * @param aBuf Buffer containing data to initialize the stream. This parameter 59 * may be nullptr, causing the stream to be created with aBufLen 60 * bytes of uninitialized data. 61 * @param aBufLen Length of data in aBuf, or desired stream size if aBuf is 62 * nullptr. 63 * @param aOutStream Outparam to receive the newly created stream. 64 * @return HRESULT error code. 65 */ 66 long CreateStream(const uint8_t* aBuf, const uint32_t aBufLen, 67 IStream** aOutStream); 68 69 /** 70 * Length of a stringified GUID as formatted for the registry, i.e. including 71 * curly-braces and dashes. 72 */ 73 constexpr size_t kGuidRegFormatCharLenInclNul = 39; 74 75 #if defined(MOZILLA_INTERNAL_API) 76 void GUIDToString(REFGUID aGuid, nsAString& aOutString); 77 78 /** 79 * Converts an IID to a human-readable string for the purposes of diagnostic 80 * tools such as the profiler. For some special cases, we output a friendly 81 * string that describes the purpose of the interface. If no such description 82 * exists, we simply fall back to outputting the IID as a string formatted by 83 * GUIDToString(). 84 */ 85 void DiagnosticNameForIID(REFIID aIid, nsACString& aOutString); 86 #else 87 void GUIDToString(REFGUID aGuid, 88 wchar_t (&aOutBuf)[kGuidRegFormatCharLenInclNul]); 89 #endif // defined(MOZILLA_INTERNAL_API) 90 91 /** 92 * Execute cleanup code when going out of scope if a condition is met. 93 * This is useful when, for example, particular cleanup needs to be performed 94 * whenever a call returns a failure HRESULT. 95 * Both the condition and cleanup code are provided as functions (usually 96 * lambdas). 97 */ 98 template <typename CondFnT, typename ExeFnT> 99 class MOZ_RAII ExecuteWhen final { 100 public: 101 ExecuteWhen(CondFnT& aCondFn, ExeFnT& aExeFn) 102 : mCondFn(aCondFn), mExeFn(aExeFn) {} 103 104 ~ExecuteWhen() { 105 if (mCondFn()) { 106 mExeFn(); 107 } 108 } 109 110 ExecuteWhen(const ExecuteWhen&) = delete; 111 ExecuteWhen(ExecuteWhen&&) = delete; 112 ExecuteWhen& operator=(const ExecuteWhen&) = delete; 113 ExecuteWhen& operator=(ExecuteWhen&&) = delete; 114 115 private: 116 CondFnT& mCondFn; 117 ExeFnT& mExeFn; 118 }; 119 120 } // namespace mscom 121 } // namespace mozilla 122 123 #endif // mozilla_mscom_Utils_h