scoped_winrt_initializer.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Copyright 2017 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_WINRT_INITIALIZER_H_
  5. #define BASE_WIN_SCOPED_WINRT_INITIALIZER_H_
  6. #include <objbase.h>
  7. #include "base/base_export.h"
  8. #include "base/threading/thread_checker.h"
  9. #include "base/win/scoped_windows_thread_environment.h"
  10. namespace base {
  11. namespace win {
  12. // Initializes the Windows Runtime in the constructor and uninitalizes the
  13. // Windows Runtime in the destructor. As a side effect, COM is also initialized
  14. // as an MTA in the constructor and correspondingly uninitialized in the
  15. // destructor.
  16. //
  17. // Generally, you should only use this on Windows 8 or above. It is redundant
  18. // to use ScopedComInitializer in conjunction with ScopedWinrtInitializer.
  19. //
  20. // WARNING: This should only be used once per thread, ideally scoped to a
  21. // similar lifetime as the thread itself. You should not be using this in random
  22. // utility functions that make Windows Runtime calls -- instead ensure these
  23. // functions are running on a Windows Runtime supporting thread!
  24. class BASE_EXPORT ScopedWinrtInitializer
  25. : public ScopedWindowsThreadEnvironment {
  26. public:
  27. ScopedWinrtInitializer();
  28. ~ScopedWinrtInitializer() override;
  29. // ScopedWindowsThreadEnvironment:
  30. bool Succeeded() const override;
  31. private:
  32. const HRESULT hr_;
  33. THREAD_CHECKER(thread_checker_);
  34. DISALLOW_COPY_AND_ASSIGN(ScopedWinrtInitializer);
  35. };
  36. } // namespace win
  37. } // namespace base
  38. #endif // BASE_WIN_SCOPED_WINRT_INITIALIZER_H_