1 /* 2 * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef RTC_BASE_WIN_SCOPED_COM_INITIALIZER_H_ 12 #define RTC_BASE_WIN_SCOPED_COM_INITIALIZER_H_ 13 14 #include <comdef.h> 15 16 namespace webrtc { 17 18 // Initializes COM in the constructor (STA or MTA), and uninitializes COM in the 19 // destructor. Taken from base::win::ScopedCOMInitializer. 20 // 21 // WARNING: This should only be used once per thread, ideally scoped to a 22 // similar lifetime as the thread itself. You should not be using this in 23 // random utility functions that make COM calls; instead ensure that these 24 // functions are running on a COM-supporting thread! 25 // See https://msdn.microsoft.com/en-us/library/ms809971.aspx for details. 26 class ScopedCOMInitializer { 27 public: 28 // Enum value provided to initialize the thread as an MTA instead of STA. 29 // There are two types of apartments, Single Threaded Apartments (STAs) 30 // and Multi Threaded Apartments (MTAs). Within a given process there can 31 // be multiple STA’s but there is only one MTA. STA is typically used by 32 // "GUI applications" and MTA by "worker threads" with no UI message loop. 33 enum SelectMTA { kMTA }; 34 35 // Constructor for STA initialization. 36 ScopedCOMInitializer(); 37 38 // Constructor for MTA initialization. 39 explicit ScopedCOMInitializer(SelectMTA mta); 40 41 ~ScopedCOMInitializer(); 42 43 ScopedCOMInitializer(const ScopedCOMInitializer&) = delete; 44 ScopedCOMInitializer& operator=(const ScopedCOMInitializer&) = delete; 45 Succeeded()46 bool Succeeded() { return SUCCEEDED(hr_); } 47 48 private: 49 void Initialize(COINIT init); 50 51 HRESULT hr_; 52 }; 53 54 } // namespace webrtc 55 56 #endif // RTC_BASE_WIN_SCOPED_COM_INITIALIZER_H_ 57