Bug 1261107: Add IsCurrentThreadMTA() to ipc/mscom/Utils; r=jimm
authorAaron Klotz <aklotz@mozilla.com>
Mon, 18 Jul 2016 13:47:34 -0600
changeset 348950 e108877f47de83c38c245c0683120b1cdee653c7
parent 348949 99cc6fc1ed3fe878644247449fcf2b62198f67f9
child 348951 e87f3cbb950fe22a5e0c80fda8494b8ea68a4203
push id1230
push userjlund@mozilla.com
push dateMon, 31 Oct 2016 18:13:35 +0000
treeherdermozilla-release@5e06e3766db2 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjimm
bugs1261107
milestone50.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1261107: Add IsCurrentThreadMTA() to ipc/mscom/Utils; r=jimm MozReview-Commit-ID: 3ezKIAmvZZM
ipc/mscom/Utils.cpp
ipc/mscom/Utils.h
ipc/mscom/moz.build
--- a/ipc/mscom/Utils.cpp
+++ b/ipc/mscom/Utils.cpp
@@ -1,23 +1,56 @@
 /* -*- 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/. */
 
+// We need Windows 7 headers
+#ifdef NTDDI_VERSION
+#undef NTDDI_VERSION
+#endif
+#define NTDDI_VERSION 0x06010000
+#ifdef _WIN32_WINNT
+#undef _WIN32_WINNT
+#endif
+#define _WIN32_WINNT 0x0601
+
+#include "DynamicallyLinkedFunctionPtr.h"
 #include "mozilla/mscom/Utils.h"
 #include "mozilla/RefPtr.h"
 
 #include <objidl.h>
 
 namespace mozilla {
 namespace mscom {
 
 bool
+IsCurrentThreadMTA()
+{
+  static DynamicallyLinkedFunctionPtr<decltype(&::CoGetApartmentType)>
+    pCoGetApartmentType(L"ole32.dll", "CoGetApartmentType");
+
+  // There isn't really a thread-safe way to query this on Windows XP. In that
+  // case, we'll just return false since that assumption does no harm.
+  if (!pCoGetApartmentType) {
+    return false;
+  }
+
+  APTTYPE aptType;
+  APTTYPEQUALIFIER aptTypeQualifier;
+  HRESULT hr = pCoGetApartmentType(&aptType, &aptTypeQualifier);
+  if (FAILED(hr)) {
+    return false;
+  }
+
+  return aptType == APTTYPE_MTA;
+}
+
+bool
 IsProxy(IUnknown* aUnknown)
 {
   if (!aUnknown) {
     return false;
   }
 
   // Only proxies implement this interface, so if it is present then we must
   // be dealing with a proxied object.
--- a/ipc/mscom/Utils.h
+++ b/ipc/mscom/Utils.h
@@ -7,15 +7,16 @@
 #ifndef mozilla_mscom_Utils_h
 #define mozilla_mscom_Utils_h
 
 struct IUnknown;
 
 namespace mozilla {
 namespace mscom {
 
+bool IsCurrentThreadMTA();
 bool IsProxy(IUnknown* aUnknown);
 
 } // namespace mscom
 } // namespace mozilla
 
 #endif // mozilla_mscom_Utils_h
 
--- a/ipc/mscom/moz.build
+++ b/ipc/mscom/moz.build
@@ -5,16 +5,19 @@
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 EXPORTS.mozilla.mscom += [
     'COMApartmentRegion.h',
     'MainThreadRuntime.h',
     'Utils.h',
 ]
 
+SOURCES += [
+    'Utils.cpp',
+]
+
 UNIFIED_SOURCES += [
     'MainThreadRuntime.cpp',
-    'Utils.cpp',
 ]
 
 include('/ipc/chromium/chromium-config.mozbuild')
 
 FINAL_LIBRARY = 'xul'