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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
ash / system / input_device_settings / input_device_settings_policy_handler.cc [blame]
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/system/input_device_settings/input_device_settings_policy_handler.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/public/mojom/input_device_settings.mojom.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
namespace ash {
namespace {
mojom::InputDeviceSettingsPolicyPtr GetBooleanPreferencePolicy(
const PrefService* pref_service,
const std::string& pref_name) {
mojom::InputDeviceSettingsPolicyPtr policy;
CHECK(pref_service);
const auto* pref = pref_service->FindPreference(pref_name);
if (!pref) {
return policy;
}
if (pref->IsManaged()) {
auto* value = pref->GetValue();
CHECK(value && value->is_bool());
policy = mojom::InputDeviceSettingsPolicy::New(
mojom::PolicyStatus::kManaged, value->GetBool());
// Prefs with recommended values must use `GetRecommendedValue` instead
// of `IsRecommended` as `IsRecommended` may return false even if the pref
// has a recommended value.
} else if (auto* value = pref->GetRecommendedValue(); value) {
CHECK(value->is_bool());
policy = mojom::InputDeviceSettingsPolicy::New(
mojom::PolicyStatus::kRecommended, value->GetBool());
}
return policy;
}
mojom::InputDeviceSettingsFkeyPolicyPtr GetFkeyPreferencePolicy(
const PrefService* pref_service,
const std::string& pref_name) {
mojom::InputDeviceSettingsFkeyPolicyPtr policy;
const auto* pref = pref_service->FindPreference(pref_name);
if (!pref) {
return policy;
}
if (pref->IsManaged()) {
auto* value = pref->GetValue();
CHECK(value && value->is_int());
policy = mojom::InputDeviceSettingsFkeyPolicy::New(
mojom::PolicyStatus::kManaged,
static_cast<ui::mojom::ExtendedFkeysModifier>(value->GetInt()));
// Prefs with recommended values must use `GetRecommendedValue` instead
// of `IsRecommended` as `IsRecommended` may return false even if the pref
// has a recommended value.
} else if (auto* value = pref->GetRecommendedValue(); value) {
CHECK(value->is_int());
policy = mojom::InputDeviceSettingsFkeyPolicy::New(
mojom::PolicyStatus::kRecommended,
static_cast<ui::mojom::ExtendedFkeysModifier>(value->GetInt()));
}
return policy;
}
mojom::InputDeviceSettingsSixPackKeyPolicyPtr GetSixPackKeyPreferencePolicy(
const PrefService* pref_service,
const std::string& pref_name) {
mojom::InputDeviceSettingsSixPackKeyPolicyPtr policy;
const auto* pref = pref_service->FindPreference(pref_name);
if (!pref) {
return policy;
}
if (pref->IsManaged()) {
auto* value = pref->GetValue();
CHECK(value && value->is_int());
if (pref_name == prefs::kInsertKeyModifier) {
CHECK(static_cast<ui::mojom::SixPackShortcutModifier>(value->GetInt()) !=
ui::mojom::SixPackShortcutModifier::kAlt);
}
policy = mojom::InputDeviceSettingsSixPackKeyPolicy::New(
mojom::PolicyStatus::kManaged,
static_cast<ui::mojom::SixPackShortcutModifier>(value->GetInt()));
// Prefs with recommended values must use `GetRecommendedValue` instead
// of `IsRecommended` as `IsRecommended` may return false even if the pref
// has a recommended value.
} else if (auto* value = pref->GetRecommendedValue(); value) {
CHECK(value->is_int());
if (pref_name == prefs::kInsertKeyModifier) {
CHECK(static_cast<ui::mojom::SixPackShortcutModifier>(value->GetInt()) !=
ui::mojom::SixPackShortcutModifier::kAlt);
}
policy = mojom::InputDeviceSettingsSixPackKeyPolicy::New(
mojom::PolicyStatus::kRecommended,
static_cast<ui::mojom::SixPackShortcutModifier>(value->GetInt()));
}
return policy;
}
} // namespace
InputDeviceSettingsPolicyHandler::InputDeviceSettingsPolicyHandler(
EnterprisePolicyCallback keyboard_policy_callback,
EnterprisePolicyCallback mouse_policy_callback)
: keyboard_policy_callback_(std::move(keyboard_policy_callback)),
mouse_policy_callback_(std::move(mouse_policy_callback)) {}
InputDeviceSettingsPolicyHandler::~InputDeviceSettingsPolicyHandler() = default;
void InputDeviceSettingsPolicyHandler::Initialize(PrefService* local_state,
PrefService* pref_service) {
has_user_prefs_ = pref_service != nullptr;
if (local_state) {
pref_change_registrar_local_state_.Init(local_state);
// Add pref observer for kOwnerPrimaryMouseButtonRight only when there is
// no user pref_service.
if (!pref_service) {
pref_change_registrar_local_state_.Add(
prefs::kOwnerPrimaryMouseButtonRight,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnMousePoliciesChanged,
base::Unretained(this)));
}
pref_change_registrar_local_state_.Add(
prefs::kDeviceSwitchFunctionKeysBehaviorEnabled,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnKeyboardPoliciesChanged,
base::Unretained(this)));
}
if (pref_service) {
pref_change_registrar_.Init(pref_service);
pref_change_registrar_.Add(
prefs::kPrimaryMouseButtonRight,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnMousePoliciesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kSendFunctionKeys,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnKeyboardPoliciesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kF11KeyModifier,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnKeyboardPoliciesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kF12KeyModifier,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnKeyboardPoliciesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kHomeAndEndKeysModifier,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnKeyboardPoliciesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kPageUpAndPageDownKeysModifier,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnKeyboardPoliciesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kDeleteKeyModifier,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnKeyboardPoliciesChanged,
base::Unretained(this)));
pref_change_registrar_.Add(
prefs::kInsertKeyModifier,
base::BindRepeating(
&InputDeviceSettingsPolicyHandler::OnKeyboardPoliciesChanged,
base::Unretained(this)));
}
RefreshKeyboardPolicies(/*notify=*/false);
RefreshMousePolicies(/*notify=*/false);
}
void InputDeviceSettingsPolicyHandler::RefreshKeyboardPolicies(bool notify) {
if (has_user_prefs_) {
keyboard_policies_.top_row_are_fkeys_policy = GetBooleanPreferencePolicy(
pref_change_registrar_.prefs(), prefs::kSendFunctionKeys);
keyboard_policies_.f11_key_policy = GetFkeyPreferencePolicy(
pref_change_registrar_.prefs(), prefs::kF11KeyModifier);
keyboard_policies_.f12_key_policy = GetFkeyPreferencePolicy(
pref_change_registrar_.prefs(), prefs::kF12KeyModifier);
keyboard_policies_.home_and_end_keys_policy = GetSixPackKeyPreferencePolicy(
pref_change_registrar_.prefs(), prefs::kHomeAndEndKeysModifier);
keyboard_policies_.page_up_and_page_down_keys_policy =
GetSixPackKeyPreferencePolicy(pref_change_registrar_.prefs(),
prefs::kPageUpAndPageDownKeysModifier);
keyboard_policies_.delete_key_policy = GetSixPackKeyPreferencePolicy(
pref_change_registrar_.prefs(), prefs::kDeleteKeyModifier);
keyboard_policies_.insert_key_policy = GetSixPackKeyPreferencePolicy(
pref_change_registrar_.prefs(), prefs::kInsertKeyModifier);
}
if (pref_change_registrar_local_state_.prefs()) {
keyboard_policies_.enable_meta_fkey_rewrites_policy =
GetBooleanPreferencePolicy(
pref_change_registrar_local_state_.prefs(),
prefs::kDeviceSwitchFunctionKeysBehaviorEnabled);
}
if (notify) {
keyboard_policy_callback_.Run();
}
}
void InputDeviceSettingsPolicyHandler::RefreshMousePolicies(bool notify) {
if (has_user_prefs_) {
mouse_policies_.swap_right_policy = GetBooleanPreferencePolicy(
pref_change_registrar_.prefs(), prefs::kPrimaryMouseButtonRight);
} else {
mouse_policies_.swap_right_policy =
GetBooleanPreferencePolicy(pref_change_registrar_local_state_.prefs(),
prefs::kOwnerPrimaryMouseButtonRight);
}
if (notify) {
mouse_policy_callback_.Run();
}
}
void InputDeviceSettingsPolicyHandler::OnKeyboardPoliciesChanged(
const std::string& pref_name) {
RefreshKeyboardPolicies(/*notify=*/true);
}
void InputDeviceSettingsPolicyHandler::OnMousePoliciesChanged(
const std::string& pref_name) {
RefreshMousePolicies(/*notify=*/true);
}
} // namespace ash