2016-07-18 22:49:28 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef mozilla_mscom_EnsureMTA_h
|
|
|
|
#define mozilla_mscom_EnsureMTA_h
|
|
|
|
|
|
|
|
#include "MainThreadUtils.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/DebugOnly.h"
|
2019-09-04 18:36:42 +03:00
|
|
|
#include "mozilla/MozPromise.h"
|
2016-12-14 11:34:12 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2019-09-04 18:36:42 +03:00
|
|
|
#include "mozilla/mscom/AgileReference.h"
|
2016-08-30 04:55:37 +03:00
|
|
|
#include "mozilla/mscom/Utils.h"
|
2019-09-04 18:36:42 +03:00
|
|
|
#include "mozilla/RefPtr.h"
|
2016-07-18 22:49:28 +03:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIThread.h"
|
|
|
|
#include "nsThreadUtils.h"
|
2017-06-09 00:55:55 +03:00
|
|
|
#include "nsWindowsHelpers.h"
|
2016-07-18 22:49:28 +03:00
|
|
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace mscom {
|
2017-06-09 00:55:55 +03:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
// Forward declarations
|
|
|
|
template <typename T>
|
|
|
|
struct MTADelete;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct MTARelease;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct MTAReleaseInChildProcess;
|
|
|
|
|
2017-07-19 21:07:45 +03:00
|
|
|
struct PreservedStreamDeleter;
|
|
|
|
|
2017-06-09 00:55:55 +03:00
|
|
|
} // namespace detail
|
2016-07-18 22:49:28 +03:00
|
|
|
|
2021-06-15 00:53:18 +03:00
|
|
|
class ProcessRuntime;
|
|
|
|
|
2016-07-18 22:49:28 +03:00
|
|
|
// This class is OK to use as a temporary on the stack.
|
2017-06-09 00:55:55 +03:00
|
|
|
class MOZ_STACK_CLASS EnsureMTA final {
|
2016-07-18 22:49:28 +03:00
|
|
|
public:
|
2019-04-23 05:17:17 +03:00
|
|
|
enum class Option {
|
|
|
|
Default,
|
2021-06-15 00:53:18 +03:00
|
|
|
// Forcibly dispatch to the thread returned by GetPersistentMTAThread(),
|
|
|
|
// even if the current thread is already inside a MTA.
|
|
|
|
ForceDispatchToPersistentThread,
|
2019-04-23 05:17:17 +03:00
|
|
|
};
|
|
|
|
|
2019-09-04 02:51:15 +03:00
|
|
|
/**
|
|
|
|
* Synchronously run |aClosure| on a thread living in the COM multithreaded
|
|
|
|
* apartment. If the current thread lives inside the COM MTA, then it runs
|
2021-06-15 00:53:18 +03:00
|
|
|
* |aClosure| immediately unless |aOpt| ==
|
|
|
|
* Option::ForceDispatchToPersistentThread.
|
2019-09-04 02:51:15 +03:00
|
|
|
*/
|
2016-07-18 22:49:28 +03:00
|
|
|
template <typename FuncT>
|
2019-09-04 02:51:15 +03:00
|
|
|
explicit EnsureMTA(FuncT&& aClosure, Option aOpt = Option::Default) {
|
2021-06-15 00:53:18 +03:00
|
|
|
if (aOpt != Option::ForceDispatchToPersistentThread &&
|
|
|
|
IsCurrentThreadMTA()) {
|
2016-07-18 22:49:28 +03:00
|
|
|
// We're already on the MTA, we can run aClosure directly
|
|
|
|
aClosure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In this case we need to run aClosure on a background thread in the MTA
|
2021-06-15 00:53:18 +03:00
|
|
|
nsCOMPtr<nsIRunnable> runnable(
|
|
|
|
NS_NewRunnableFunction("EnsureMTA::EnsureMTA", std::move(aClosure)));
|
|
|
|
SyncDispatch(std::move(runnable), aOpt);
|
2016-07-18 22:49:28 +03:00
|
|
|
}
|
|
|
|
|
2019-09-04 18:36:42 +03:00
|
|
|
using CreateInstanceAgileRefPromise =
|
|
|
|
MozPromise<AgileReference, HRESULT, false>;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* *** A MSCOM PEER SHOULD REVIEW ALL NEW USES OF THIS API! ***
|
|
|
|
*
|
|
|
|
* Asynchronously instantiate a new COM object from a MTA thread, unless the
|
|
|
|
* current thread is already living inside the multithreaded apartment, in
|
|
|
|
* which case the object is immediately instantiated.
|
|
|
|
*
|
|
|
|
* This function only supports the most common configurations for creating
|
|
|
|
* a new object, so it only supports in-process servers. Furthermore, this
|
|
|
|
* function does not support aggregation (ie. the |pUnkOuter| parameter to
|
|
|
|
* CoCreateInstance).
|
|
|
|
*
|
|
|
|
* Given that attempting to instantiate an Apartment-threaded COM object
|
|
|
|
* inside the MTA results in a *loss* of performance, we assert when that
|
|
|
|
* situation arises.
|
|
|
|
*
|
|
|
|
* The resulting promise, once resolved, provides an AgileReference that may
|
|
|
|
* be passed between any COM-initialized thread in the current process.
|
|
|
|
*
|
|
|
|
* *** A MSCOM PEER SHOULD REVIEW ALL NEW USES OF THIS API! ***
|
|
|
|
*
|
|
|
|
* WARNING:
|
|
|
|
* Some COM objects do not support creation in the multithreaded apartment,
|
|
|
|
* in which case this function is not available as an option. In this case,
|
|
|
|
* the promise will always be rejected. In debug builds we will assert.
|
|
|
|
*
|
|
|
|
* *** A MSCOM PEER SHOULD REVIEW ALL NEW USES OF THIS API! ***
|
|
|
|
*
|
|
|
|
* WARNING:
|
|
|
|
* Any in-process COM objects whose interfaces accept HWNDs are probably
|
|
|
|
* *not* safe to instantiate in the multithreaded apartment! Even if this
|
|
|
|
* function succeeds when creating such an object, you *MUST NOT* do so, as
|
|
|
|
* these failures might not become apparent until your code is running out in
|
|
|
|
* the wild on the release channel!
|
|
|
|
*
|
|
|
|
* *** A MSCOM PEER SHOULD REVIEW ALL NEW USES OF THIS API! ***
|
|
|
|
*
|
|
|
|
* WARNING:
|
|
|
|
* When you obtain an interface from the AgileReference, it may or may not be
|
|
|
|
* a proxy to the real object. This depends entirely on the implementation of
|
|
|
|
* the underlying class and the multithreading capabilities that the class
|
|
|
|
* declares to the COM runtime. If the interface is proxied, it might be
|
|
|
|
* expensive to invoke methods on that interface! *Always* test the
|
|
|
|
* performance of your method calls when calling interfaces that are resolved
|
|
|
|
* via this function!
|
|
|
|
*
|
|
|
|
* *** A MSCOM PEER SHOULD REVIEW ALL NEW USES OF THIS API! ***
|
|
|
|
*
|
|
|
|
* (Despite this myriad of warnings, it is still *much* safer to use this
|
|
|
|
* function to asynchronously create COM objects than it is to roll your own!)
|
|
|
|
*
|
|
|
|
* *** A MSCOM PEER SHOULD REVIEW ALL NEW USES OF THIS API! ***
|
|
|
|
*/
|
|
|
|
static RefPtr<CreateInstanceAgileRefPromise> CreateInstance(REFCLSID aClsid,
|
|
|
|
REFIID aIid);
|
|
|
|
|
2016-07-18 22:49:28 +03:00
|
|
|
private:
|
2019-09-04 18:36:42 +03:00
|
|
|
static RefPtr<CreateInstanceAgileRefPromise> CreateInstanceInternal(
|
|
|
|
REFCLSID aClsid, REFIID aIid);
|
|
|
|
|
2021-06-15 00:53:18 +03:00
|
|
|
static nsCOMPtr<nsIThread> GetPersistentMTAThread();
|
|
|
|
|
|
|
|
static void SyncDispatch(nsCOMPtr<nsIRunnable>&& aRunnable, Option aOpt);
|
|
|
|
static void SyncDispatchToPersistentThread(nsIRunnable* aRunnable);
|
2017-06-09 00:55:55 +03:00
|
|
|
|
|
|
|
// The following function is private in order to force any consumers to be
|
|
|
|
// declared as friends of EnsureMTA. The intention is to prevent
|
|
|
|
// AsyncOperation from becoming some kind of free-for-all mechanism for
|
|
|
|
// asynchronously executing work on a background thread.
|
|
|
|
template <typename FuncT>
|
2019-09-04 02:51:15 +03:00
|
|
|
static void AsyncOperation(FuncT&& aClosure) {
|
2017-06-09 00:55:55 +03:00
|
|
|
if (IsCurrentThreadMTA()) {
|
|
|
|
aClosure();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:53:18 +03:00
|
|
|
nsCOMPtr<nsIThread> thread(GetPersistentMTAThread());
|
2017-06-09 00:55:55 +03:00
|
|
|
MOZ_ASSERT(thread);
|
|
|
|
if (!thread) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DebugOnly<nsresult> rv = thread->Dispatch(
|
2019-09-04 02:51:15 +03:00
|
|
|
NS_NewRunnableFunction("mscom::EnsureMTA::AsyncOperation",
|
|
|
|
std::move(aClosure)),
|
2017-06-12 22:34:10 +03:00
|
|
|
NS_DISPATCH_NORMAL);
|
2017-06-09 00:55:55 +03:00
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
}
|
|
|
|
|
2021-06-15 00:53:18 +03:00
|
|
|
/**
|
|
|
|
* This constructor just ensures that the MTA is up and running. This should
|
|
|
|
* only be called by ProcessRuntime.
|
|
|
|
*/
|
|
|
|
EnsureMTA();
|
|
|
|
|
|
|
|
friend class mozilla::mscom::ProcessRuntime;
|
|
|
|
|
2017-06-09 00:55:55 +03:00
|
|
|
template <typename T>
|
|
|
|
friend struct mozilla::mscom::detail::MTADelete;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
friend struct mozilla::mscom::detail::MTARelease;
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
friend struct mozilla::mscom::detail::MTAReleaseInChildProcess;
|
2017-07-19 21:07:45 +03:00
|
|
|
|
|
|
|
friend struct mozilla::mscom::detail::PreservedStreamDeleter;
|
2016-07-18 22:49:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace mscom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
|
|
|
#endif // mozilla_mscom_EnsureMTA_h
|