scoped_com_initializer.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_WIN_SCOPED_COM_INITIALIZER_H_
  5. #define BASE_WIN_SCOPED_COM_INITIALIZER_H_
  6. #include <objbase.h>
  7. #include <wrl/client.h>
  8. #include "base/base_export.h"
  9. #include "base/macros.h"
  10. #include "base/threading/thread_checker.h"
  11. #include "base/win/com_init_balancer.h"
  12. #include "base/win/scoped_windows_thread_environment.h"
  13. namespace base {
  14. namespace win {
  15. // Initializes COM in the constructor (STA or MTA), and uninitializes COM in the
  16. // destructor.
  17. //
  18. // It is strongly encouraged to block premature uninitialization of the COM
  19. // libraries in threads that execute third-party code, as a way to protect
  20. // against unbalanced CoInitialize/CoUninitialize pairs.
  21. //
  22. // WARNING: This should only be used once per thread, ideally scoped to a
  23. // similar lifetime as the thread itself. You should not be using this in
  24. // random utility functions that make COM calls -- instead ensure these
  25. // functions are running on a COM-supporting thread!
  26. class BASE_EXPORT ScopedCOMInitializer : public ScopedWindowsThreadEnvironment {
  27. public:
  28. // Enum value provided to initialize the thread as an MTA instead of STA.
  29. enum SelectMTA { kMTA };
  30. // Enum values which enumerates uninitialization modes for the COM library.
  31. enum class Uninitialization {
  32. // Default value. Used in threads where no third-party code is executed.
  33. kAllow,
  34. // Blocks premature uninitialization of the COM libraries before going out
  35. // of scope. Used in threads where third-party code is executed.
  36. kBlockPremature,
  37. };
  38. // Constructors for STA initialization.
  39. explicit ScopedCOMInitializer(
  40. Uninitialization uninitialization = Uninitialization::kAllow);
  41. // Constructors for MTA initialization.
  42. explicit ScopedCOMInitializer(
  43. SelectMTA mta,
  44. Uninitialization uninitialization = Uninitialization::kAllow);
  45. ~ScopedCOMInitializer() override;
  46. // ScopedWindowsThreadEnvironment:
  47. bool Succeeded() const override;
  48. // Used for testing. Returns the COM balancer's apartment thread ref count.
  49. DWORD GetCOMBalancerReferenceCountForTesting() const;
  50. private:
  51. void Initialize(COINIT init, Uninitialization uninitialization);
  52. HRESULT hr_ = S_OK;
  53. Microsoft::WRL::ComPtr<internal::ComInitBalancer> com_balancer_;
  54. THREAD_CHECKER(thread_checker_);
  55. DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer);
  56. };
  57. } // namespace win
  58. } // namespace base
  59. #endif // BASE_WIN_SCOPED_COM_INITIALIZER_H_