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"
30 long BuildRegGuidPath(REFGUID aGuid
, const GuidType aGuidType
, wchar_t* aBuf
,
31 const size_t aBufLen
);
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();
47 inline long BuildAppidPath(REFGUID aAppId
, wchar_t (&aPath
)[N
]) {
48 return detail::BuildRegGuidPath(aAppId
, detail::GuidType::AppID
, aPath
, N
);
52 inline long BuildClsidPath(REFCLSID aClsid
, wchar_t (&aPath
)[N
]) {
53 return detail::BuildRegGuidPath(aClsid
, detail::GuidType::CLSID
, aPath
, N
);
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
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
);
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)
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
108 void DiagnosticNameForIID(REFIID aIid
, nsACString
& aOutString
);
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
121 template <typename CondFnT
, typename ExeFnT
>
122 class MOZ_RAII ExecuteWhen final
{
124 ExecuteWhen(CondFnT
& aCondFn
, ExeFnT
& aExeFn
)
125 : mCondFn(aCondFn
), mExeFn(aExeFn
) {}
133 ExecuteWhen(const ExecuteWhen
&) = delete;
134 ExecuteWhen(ExecuteWhen
&&) = delete;
135 ExecuteWhen
& operator=(const ExecuteWhen
&) = delete;
136 ExecuteWhen
& operator=(ExecuteWhen
&&) = delete;
144 } // namespace mozilla
146 #endif // mozilla_mscom_Utils_h