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