Backed out changeset e9d46c179688 (bug 1855759) for causing build bustages CLOSED...
[gecko.git] / ipc / mscom / Utils.h
blob214b15044d3ac220f39f78007c097ed18e2ce3c8
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_Utils_h
8 #define mozilla_mscom_Utils_h
10 #if defined(MOZILLA_INTERNAL_API)
11 # include "nsString.h"
12 #endif // defined(MOZILLA_INTERNAL_API)
14 #include "mozilla/Attributes.h"
15 #include <guiddef.h>
16 #include <stdint.h>
18 struct IStream;
19 struct IUnknown;
21 namespace mozilla {
22 namespace mscom {
23 namespace detail {
25 enum class GuidType {
26 CLSID,
27 AppID,
30 long BuildRegGuidPath(REFGUID aGuid, const GuidType aGuidType, wchar_t* aBuf,
31 const size_t aBufLen);
33 } // namespace detail
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();
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);
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);
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.
66 long CreateStream(const uint8_t* aBuf, const uint32_t aBufLen,
67 IStream** aOutStream);
69 /**
70 * Length of a stringified GUID as formatted for the registry, i.e. including
71 * curly-braces and dashes.
73 constexpr size_t kGuidRegFormatCharLenInclNul = 39;
75 #if defined(MOZILLA_INTERNAL_API)
76 /**
77 * Checks the registry to see if |aClsid| is a thread-aware in-process server.
79 * In DCOM, an in-process server is a server that is implemented inside a DLL
80 * that is loaded into the client's process for execution. If |aClsid| declares
81 * itself to be a local server (that is, a server that resides in another
82 * process), this function returns false.
84 * For the server to be thread-aware, its registry entry must declare a
85 * ThreadingModel that is one of "Free", "Both", or "Neutral". If the threading
86 * model is "Apartment" or some other, invalid value, the class is treated as
87 * being single-threaded.
89 * NB: This function cannot check CLSIDs that were registered via manifests,
90 * as unfortunately there is not a documented API available to query for those.
91 * This should not be an issue for most CLSIDs that Gecko is interested in, as
92 * we typically instantiate system CLSIDs which are available in the registry.
94 * @param aClsid The CLSID of the COM class to be checked.
95 * @return true if the class meets the above criteria, otherwise false.
97 bool IsClassThreadAwareInprocServer(REFCLSID aClsid);
99 void GUIDToString(REFGUID aGuid, nsAString& aOutString);
102 * Converts an IID to a human-readable string for the purposes of diagnostic
103 * tools such as the profiler. For some special cases, we output a friendly
104 * string that describes the purpose of the interface. If no such description
105 * exists, we simply fall back to outputting the IID as a string formatted by
106 * GUIDToString().
108 void DiagnosticNameForIID(REFIID aIid, nsACString& aOutString);
109 #else
110 void GUIDToString(REFGUID aGuid,
111 wchar_t (&aOutBuf)[kGuidRegFormatCharLenInclNul]);
112 #endif // defined(MOZILLA_INTERNAL_API)
115 * Execute cleanup code when going out of scope if a condition is met.
116 * This is useful when, for example, particular cleanup needs to be performed
117 * whenever a call returns a failure HRESULT.
118 * Both the condition and cleanup code are provided as functions (usually
119 * lambdas).
121 template <typename CondFnT, typename ExeFnT>
122 class MOZ_RAII ExecuteWhen final {
123 public:
124 ExecuteWhen(CondFnT& aCondFn, ExeFnT& aExeFn)
125 : mCondFn(aCondFn), mExeFn(aExeFn) {}
127 ~ExecuteWhen() {
128 if (mCondFn()) {
129 mExeFn();
133 ExecuteWhen(const ExecuteWhen&) = delete;
134 ExecuteWhen(ExecuteWhen&&) = delete;
135 ExecuteWhen& operator=(const ExecuteWhen&) = delete;
136 ExecuteWhen& operator=(ExecuteWhen&&) = delete;
138 private:
139 CondFnT& mCondFn;
140 ExeFnT& mExeFn;
143 } // namespace mscom
144 } // namespace mozilla
146 #endif // mozilla_mscom_Utils_h