xref: /aosp_15_r20/external/cronet/base/win/scoped_winrt_initializer.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2017 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_WIN_SCOPED_WINRT_INITIALIZER_H_
6 #define BASE_WIN_SCOPED_WINRT_INITIALIZER_H_
7 
8 #include <objbase.h>
9 
10 #include "base/base_export.h"
11 #include "base/threading/thread_checker.h"
12 #include "base/win/scoped_windows_thread_environment.h"
13 
14 namespace base {
15 namespace win {
16 
17 // Initializes the Windows Runtime in the constructor and uninitalizes the
18 // Windows Runtime in the destructor. As a side effect, COM is also initialized
19 // as an MTA in the constructor and correspondingly uninitialized in the
20 // destructor.
21 //
22 // Generally, you should only use this on Windows 8 or above. It is redundant
23 // to use ScopedComInitializer in conjunction with ScopedWinrtInitializer.
24 //
25 // WARNING: This should only be used once per thread, ideally scoped to a
26 // similar lifetime as the thread itself. You should not be using this in random
27 // utility functions that make Windows Runtime calls -- instead ensure these
28 // functions are running on a Windows Runtime supporting thread!
29 class BASE_EXPORT ScopedWinrtInitializer
30     : public ScopedWindowsThreadEnvironment {
31  public:
32   ScopedWinrtInitializer();
33 
34   ScopedWinrtInitializer(const ScopedWinrtInitializer&) = delete;
35   ScopedWinrtInitializer& operator=(const ScopedWinrtInitializer&) = delete;
36 
37   ~ScopedWinrtInitializer() override;
38 
39   // ScopedWindowsThreadEnvironment:
40   bool Succeeded() const override;
41 
42  private:
43   const HRESULT hr_;
44   THREAD_CHECKER(thread_checker_);
45 };
46 
47 }  // namespace win
48 }  // namespace base
49 
50 #endif  // BASE_WIN_SCOPED_WINRT_INITIALIZER_H_
51