xref: /aosp_15_r20/external/cronet/base/test/scoped_os_info_override_win.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2018 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_TEST_SCOPED_OS_INFO_OVERRIDE_WIN_H_
6 #define BASE_TEST_SCOPED_OS_INFO_OVERRIDE_WIN_H_
7 
8 #include <memory>
9 
10 #include "base/memory/raw_ptr.h"
11 
12 namespace base {
13 namespace win {
14 class OSInfo;
15 }  // namespace win
16 }  // namespace base
17 
18 namespace base {
19 namespace test {
20 
21 // Helper class to override info returned by base::win::OSInfo::GetIntance()
22 // for the lifetime of this object. Upon destruction, the original info at time
23 // of object creation is restored.
24 class ScopedOSInfoOverride {
25  public:
26   // Types of windows machines that can be used for overriding.  Add new
27   // machine types as needed.
28   enum class Type {
29     kWin11Pro,
30     kWin11Home,
31     kWinServer2022,
32     kWin10Pro21H1,
33     kWin10Pro,
34     kWin10Home,
35     kWinServer2016,
36     kWin11HomeN,
37   };
38 
39   explicit ScopedOSInfoOverride(Type type);
40 
41   ScopedOSInfoOverride(const ScopedOSInfoOverride&) = delete;
42   ScopedOSInfoOverride& operator=(const ScopedOSInfoOverride&) = delete;
43 
44   ~ScopedOSInfoOverride();
45 
46  private:
47   using UniqueOsInfo =
48       std::unique_ptr<base::win::OSInfo, void (*)(base::win::OSInfo*)>;
49 
50   static UniqueOsInfo CreateInfoOfType(Type type);
51 
52   // The OSInfo taken by this instance at construction and restored at
53   // destruction.
54   raw_ptr<base::win::OSInfo> original_info_;
55 
56   // The OSInfo owned by this scoped object and which overrides
57   // base::win::OSInfo::GetIntance() for the lifespan of the object.
58   UniqueOsInfo overriding_info_;
59 
60   // Because the dtor of OSInfo is private, a custom deleter is needed to use
61   // unique_ptr.
62   static void deleter(base::win::OSInfo* info);
63 };
64 
65 }  // namespace test
66 }  // namespace base
67 
68 #endif  // BASE_TEST_SCOPED_OS_INFO_OVERRIDE_WIN_H_
69