Bug 1888033 - [Menu Redesign] Add a secret setting and feature flag for the menu...
[gecko.git] / toolkit / xre / DllPrefetchExperimentRegistryInfo.cpp
blob829999ac45960aafbe6b6d45bc232af558c0afac
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 #include "DllPrefetchExperimentRegistryInfo.h"
9 #include "mozilla/Assertions.h"
10 #include "mozilla/NativeNt.h"
11 #include "mozilla/ResultExtensions.h"
13 namespace mozilla {
15 const wchar_t DllPrefetchExperimentRegistryInfo::kExperimentSubKeyPath[] =
16 L"SOFTWARE"
17 L"\\" MOZ_APP_VENDOR L"\\" MOZ_APP_BASENAME L"\\DllPrefetchExperiment";
19 Result<Ok, nsresult> DllPrefetchExperimentRegistryInfo::Open() {
20 if (!!mRegKey) {
21 return Ok();
24 DWORD disposition;
25 HKEY rawKey;
26 LSTATUS result = ::RegCreateKeyExW(
27 HKEY_CURRENT_USER, kExperimentSubKeyPath, 0, nullptr,
28 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr, &rawKey, &disposition);
30 if (result != ERROR_SUCCESS) {
31 return Err(NS_ERROR_UNEXPECTED);
34 mRegKey.own(rawKey);
36 if (disposition == REG_CREATED_NEW_KEY ||
37 disposition == REG_OPENED_EXISTING_KEY) {
38 return Ok();
41 MOZ_ASSERT_UNREACHABLE("Invalid disposition from RegCreateKeyExW");
42 return Err(NS_ERROR_UNEXPECTED);
45 Result<Ok, nsresult> DllPrefetchExperimentRegistryInfo::ReflectPrefToRegistry(
46 int32_t aVal) {
47 MOZ_TRY(Open());
49 mPrefetchMode = aVal;
51 LSTATUS status =
52 ::RegSetValueExW(mRegKey.get(), mBinPath.get(), 0, REG_DWORD,
53 reinterpret_cast<PBYTE>(&aVal), sizeof(aVal));
55 if (status != ERROR_SUCCESS) {
56 return Err(NS_ERROR_UNEXPECTED);
59 return Ok();
62 Result<Ok, nsresult> DllPrefetchExperimentRegistryInfo::ReadRegistryValueData(
63 DWORD expectedType) {
64 MOZ_TRY(Open());
66 int32_t data;
67 DWORD dataLen = sizeof((ULONG)data);
68 DWORD type;
69 LSTATUS status =
70 ::RegQueryValueExW(mRegKey.get(), mBinPath.get(), nullptr, &type,
71 reinterpret_cast<PBYTE>(&data), &dataLen);
73 if (status == ERROR_FILE_NOT_FOUND) {
74 // The registry key has not been created, set to default 0
75 mPrefetchMode = 0;
76 return Ok();
79 if (status != ERROR_SUCCESS) {
80 return Err(NS_ERROR_UNEXPECTED);
83 if (type != expectedType) {
84 return Err(NS_ERROR_UNEXPECTED);
87 mPrefetchMode = data;
88 return Ok();
91 AlteredDllPrefetchMode
92 DllPrefetchExperimentRegistryInfo::GetAlteredDllPrefetchMode() {
93 Result<Ok, nsresult> result = ReadRegistryValueData(REG_DWORD);
94 if (!result.isOk()) {
95 MOZ_ASSERT(false);
96 return AlteredDllPrefetchMode::CurrentPrefetch;
99 switch (mPrefetchMode) {
100 case 0:
101 return AlteredDllPrefetchMode::CurrentPrefetch;
102 case 1:
103 return AlteredDllPrefetchMode::NoPrefetch;
104 case 2:
105 return AlteredDllPrefetchMode::OptimizedPrefetch;
106 default:
107 MOZ_ASSERT(false);
108 return AlteredDllPrefetchMode::CurrentPrefetch;
112 } // namespace mozilla