Bug 1263224: Smart pointers for passing COM interface pointers around in other apartments; r?jimm draft
authorAaron Klotz <aklotz@mozilla.com>
Tue, 19 Jul 2016 13:13:31 -0600
changeset 393334 0fdf654eee404ddca46d4a4987e28e95557bf0bd
parent 393333 675a8b7306ea6715470b753147f1f9ca334d3a3b
child 393335 ba95eef4285f1cebb575b0c071e52399416a27de
push id24290
push useraklotz@mozilla.com
push dateWed, 27 Jul 2016 18:19:09 +0000
reviewersjimm
bugs1263224
milestone50.0a1
Bug 1263224: Smart pointers for passing COM interface pointers around in other apartments; r?jimm MozReview-Commit-ID: 377VvQ5PMCJ
ipc/mscom/Ptr.h
ipc/mscom/moz.build
new file mode 100644
--- /dev/null
+++ b/ipc/mscom/Ptr.h
@@ -0,0 +1,111 @@
+/* -*- 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_Ptr_h
+#define mozilla_mscom_Ptr_h
+
+#include "mozilla/Assertions.h"
+#include "mozilla/DebugOnly.h"
+#include "mozilla/mscom/EnsureMTA.h"
+#include "mozilla/UniquePtr.h"
+#include "nsError.h"
+#include "nsThreadUtils.h"
+#include "nsXULAppAPI.h"
+
+/**
+ * The glue code in mozilla::mscom often needs to pass around interface pointers
+ * belonging to a different apartment from the current one. We must not touch
+ * the reference counts of those objects on the wrong apartment. By using these
+ * UniquePtr specializations, we may ensure that the reference counts are always
+ * handled correctly.
+ */
+
+namespace mozilla {
+namespace mscom {
+
+namespace detail {
+
+template <typename T>
+struct MainThreadRelease
+{
+  void operator()(T* aPtr)
+  {
+    if (!aPtr) {
+      return;
+    }
+    if (NS_IsMainThread()) {
+      aPtr->Release();
+      return;
+    }
+    DebugOnly<nsresult> rv =
+      NS_DispatchToMainThread(NewNonOwningRunnableMethod(aPtr,
+                                                         &T::Release));
+    MOZ_ASSERT(NS_SUCCEEDED(rv));
+  }
+};
+
+template <typename T>
+struct MTARelease
+{
+  void operator()(T* aPtr)
+  {
+    if (!aPtr) {
+      return;
+    }
+    EnsureMTA([&]() -> void
+    {
+      aPtr->Release();
+    });
+  }
+};
+
+template <typename T>
+struct MTAReleaseInChildProcess
+{
+  void operator()(T* aPtr)
+  {
+    if (!aPtr) {
+      return;
+    }
+    if (XRE_IsParentProcess()) {
+      MOZ_ASSERT(NS_IsMainThread());
+      aPtr->Release();
+      return;
+    }
+    EnsureMTA([&]() -> void
+    {
+      aPtr->Release();
+    });
+  }
+};
+
+struct InterceptorTargetDeleter
+{
+  void operator()(IUnknown* aPtr)
+  {
+    // We intentionally do not touch the refcounts of interceptor targets!
+  }
+};
+
+} // namespace detail
+
+template <typename T>
+using STAUniquePtr = mozilla::UniquePtr<T, detail::MainThreadRelease<T>>;
+
+template <typename T>
+using MTAUniquePtr = mozilla::UniquePtr<T, detail::MTARelease<T>>;
+
+template <typename T>
+using ProxyUniquePtr = mozilla::UniquePtr<T, detail::MTAReleaseInChildProcess<T>>;
+
+using InterceptorTargetPtr =
+  mozilla::UniquePtr<IUnknown, detail::InterceptorTargetDeleter>;
+
+} // namespace mscom
+} // namespace mozilla
+
+#endif // mozilla_mscom_Ptr_h
+
--- a/ipc/mscom/moz.build
+++ b/ipc/mscom/moz.build
@@ -6,16 +6,17 @@
 
 EXPORTS.mozilla.mscom += [
     'COMApartmentRegion.h',
     'COMPtrHolder.h',
     'EnsureMTA.h',
     'InterceptorLog.h',
     'MainThreadRuntime.h',
     'ProxyStream.h',
+    'Ptr.h',
     'Registration.h',
     'Utils.h',
 ]
 
 SOURCES += [
     'Registration.cpp',
     'Utils.cpp',
 ]