scoped_com_initializer.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 "base/base_export.h"
  8. #include "base/macros.h"
  9. #include "base/threading/thread_checker.h"
  10. #include "base/win/scoped_windows_thread_environment.h"
  11. namespace base {
  12. namespace win {
  13. // Initializes COM in the constructor (STA or MTA), and uninitializes COM in the
  14. // destructor.
  15. //
  16. // WARNING: This should only be used once per thread, ideally scoped to a
  17. // similar lifetime as the thread itself. You should not be using this in
  18. // random utility functions that make COM calls -- instead ensure these
  19. // functions are running on a COM-supporting thread!
  20. class BASE_EXPORT ScopedCOMInitializer : public ScopedWindowsThreadEnvironment {
  21. public:
  22. // Enum value provided to initialize the thread as an MTA instead of STA.
  23. enum SelectMTA { kMTA };
  24. // Constructor for STA initialization.
  25. ScopedCOMInitializer();
  26. // Constructor for MTA initialization.
  27. explicit ScopedCOMInitializer(SelectMTA mta);
  28. ~ScopedCOMInitializer() override;
  29. // ScopedWindowsThreadEnvironment:
  30. bool Succeeded() const override;
  31. private:
  32. void Initialize(COINIT init);
  33. HRESULT hr_;
  34. THREAD_CHECKER(thread_checker_);
  35. DISALLOW_COPY_AND_ASSIGN(ScopedCOMInitializer);
  36. };
  37. } // namespace win
  38. } // namespace base
  39. #endif // BASE_WIN_SCOPED_COM_INITIALIZER_H_