blob: f15e9aa0cccb0d3b8d54679deab6a3839adfa8a3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "WinContentSystemParameters.h"
#include "WinUtils.h"
#include "nsUXThemeData.h"
#include "mozilla/Mutex.h"
#include "mozilla/dom/ContentChild.h"
#include "mozilla/dom/ContentParent.h"
namespace mozilla {
namespace widget {
using dom::SystemParameterKVPair;
static_assert(uint8_t(SystemParameterId::Count) < 32,
"Too many SystemParameterId for "
"WinContentSystemParameters::Detail::validCachedValueBitfield");
struct WinContentSystemParameters::Detail {
OffTheBooksMutex mutex{"WinContentSystemParameters::Detail::mutex"};
// A bitfield indicating which cached ids are valid
uint32_t validCachedValueBitfield{0};
bool cachedIsPerMonitorDPIAware{false};
float cachedSystemDPI{0.0f};
bool cachedFlatMenusEnabled{false};
// Almost-always true in Windows 7, always true starting in Windows 8
bool cachedIsAppThemed{true};
};
// static
WinContentSystemParameters* WinContentSystemParameters::GetSingleton() {
static WinContentSystemParameters sInstance{};
return &sInstance;
}
WinContentSystemParameters::WinContentSystemParameters()
: mDetail(std::make_unique<Detail>()) {}
WinContentSystemParameters::~WinContentSystemParameters() {}
bool WinContentSystemParameters::IsPerMonitorDPIAware() {
MOZ_ASSERT(XRE_IsContentProcess());
OffTheBooksMutexAutoLock lock(mDetail->mutex);
MOZ_RELEASE_ASSERT(
IsCachedValueValid(SystemParameterId::IsPerMonitorDPIAware));
return mDetail->cachedIsPerMonitorDPIAware;
}
float WinContentSystemParameters::SystemDPI() {
MOZ_ASSERT(XRE_IsContentProcess());
OffTheBooksMutexAutoLock lock(mDetail->mutex);
MOZ_RELEASE_ASSERT(IsCachedValueValid(SystemParameterId::SystemDPI));
return mDetail->cachedSystemDPI;
}
bool WinContentSystemParameters::AreFlatMenusEnabled() {
MOZ_ASSERT(XRE_IsContentProcess());
OffTheBooksMutexAutoLock lock(mDetail->mutex);
MOZ_RELEASE_ASSERT(IsCachedValueValid(SystemParameterId::FlatMenusEnabled));
return mDetail->cachedFlatMenusEnabled;
}
bool WinContentSystemParameters::IsAppThemed() {
MOZ_ASSERT(XRE_IsContentProcess());
OffTheBooksMutexAutoLock lock(mDetail->mutex);
MOZ_RELEASE_ASSERT(IsCachedValueValid(SystemParameterId::IsAppThemed));
return mDetail->cachedIsAppThemed;
}
void WinContentSystemParameters::SetContentValueInternal(
const SystemParameterKVPair& aKVPair) {
MOZ_ASSERT(XRE_IsContentProcess());
MOZ_ASSERT(NS_IsMainThread());
MOZ_RELEASE_ASSERT(aKVPair.id() < uint8_t(SystemParameterId::Count));
SetCachedValueValid(SystemParameterId(aKVPair.id()), true);
mDetail->mutex.AssertCurrentThreadOwns();
switch (SystemParameterId(aKVPair.id())) {
case SystemParameterId::IsPerMonitorDPIAware:
mDetail->cachedIsPerMonitorDPIAware = aKVPair.value();
return;
case SystemParameterId::SystemDPI:
mDetail->cachedSystemDPI = aKVPair.value();
return;
case SystemParameterId::FlatMenusEnabled:
mDetail->cachedFlatMenusEnabled = aKVPair.value();
return;
case SystemParameterId::IsAppThemed:
mDetail->cachedIsAppThemed = aKVPair.value();
return;
case SystemParameterId::Count:
MOZ_CRASH("Invalid SystemParameterId");
}
MOZ_CRASH("Unhandled SystemParameterId");
}
void WinContentSystemParameters::SetContentValues(
const nsTArray<dom::SystemParameterKVPair>& values) {
MOZ_ASSERT(XRE_IsContentProcess());
MOZ_ASSERT(NS_IsMainThread());
OffTheBooksMutexAutoLock lock(mDetail->mutex);
for (auto& kvPair : values) {
SetContentValueInternal(kvPair);
}
}
bool WinContentSystemParameters::GetParentValueInternal(
SystemParameterId aId, SystemParameterKVPair* aKVPair) {
MOZ_ASSERT(XRE_IsParentProcess());
MOZ_ASSERT(aKVPair);
aKVPair->id() = uint8_t(aId);
switch (aId) {
case SystemParameterId::IsPerMonitorDPIAware:
aKVPair->value() = WinUtils::IsPerMonitorDPIAware();
return true;
case SystemParameterId::SystemDPI:
aKVPair->value() = WinUtils::SystemDPI();
return true;
case SystemParameterId::FlatMenusEnabled:
aKVPair->value() = nsUXThemeData::AreFlatMenusEnabled();
return true;
case SystemParameterId::IsAppThemed:
aKVPair->value() = nsUXThemeData::IsAppThemed();
return true;
case SystemParameterId::Count:
MOZ_CRASH("Invalid SystemParameterId");
}
MOZ_CRASH("Unhandled SystemParameterId");
}
nsTArray<dom::SystemParameterKVPair>
WinContentSystemParameters::GetParentValues() {
MOZ_ASSERT(XRE_IsParentProcess());
nsTArray<dom::SystemParameterKVPair> results;
for (uint8_t i = 0; i < uint8_t(SystemParameterId::Count); ++i) {
dom::SystemParameterKVPair kvPair{};
GetParentValueInternal(SystemParameterId(i), &kvPair);
results.AppendElement(std::move(kvPair));
}
return results;
}
void WinContentSystemParameters::OnThemeChanged() {
MOZ_ASSERT(XRE_IsParentProcess());
nsTArray<dom::SystemParameterKVPair> updates;
{
dom::SystemParameterKVPair kvPair{};
GetParentValueInternal(SystemParameterId::FlatMenusEnabled, &kvPair);
updates.AppendElement(std::move(kvPair));
}
{
dom::SystemParameterKVPair kvPair{};
GetParentValueInternal(SystemParameterId::IsAppThemed, &kvPair);
updates.AppendElement(std::move(kvPair));
}
nsTArray<dom::ContentParent*> contentProcesses{};
dom::ContentParent::GetAll(contentProcesses);
for (auto contentProcess : contentProcesses) {
Unused << contentProcess->SendUpdateSystemParameters(updates);
}
}
bool WinContentSystemParameters::IsCachedValueValid(SystemParameterId aId) {
MOZ_ASSERT(XRE_IsContentProcess());
MOZ_ASSERT(uint8_t(aId) < uint8_t(SystemParameterId::Count));
mDetail->mutex.AssertCurrentThreadOwns();
uint32_t mask = uint32_t(1) << uint8_t(aId);
return (mDetail->validCachedValueBitfield & mask) != 0;
}
void WinContentSystemParameters::SetCachedValueValid(SystemParameterId aId,
bool aIsValid) {
MOZ_ASSERT(XRE_IsContentProcess());
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(uint8_t(aId) < uint8_t(SystemParameterId::Count));
mDetail->mutex.AssertCurrentThreadOwns();
uint32_t mask = uint32_t(1) << uint8_t(aId);
if (aIsValid) {
mDetail->validCachedValueBitfield |= mask;
} else {
mDetail->validCachedValueBitfield &= ~mask;
}
}
} // namespace widget
} // namespace mozilla
|