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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
ash / system / privacy_hub / privacy_hub_notification_controller.cc [blame]
// Copyright 2022 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/privacy_hub/privacy_hub_notification_controller.h"
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/geolocation_access_level.h"
#include "ash/public/cpp/new_window_delegate.h"
#include "ash/public/cpp/system_tray_client.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/strings/grit/ash_strings.h"
#include "ash/system/model/system_tray_model.h"
#include "ash/system/privacy_hub/camera_privacy_switch_controller.h"
#include "ash/system/privacy_hub/geolocation_privacy_switch_controller.h"
#include "ash/system/privacy_hub/microphone_privacy_switch_controller.h"
#include "ash/system/privacy_hub/privacy_hub_controller.h"
#include "ash/system/privacy_hub/privacy_hub_metrics.h"
#include "ash/system/privacy_hub/privacy_hub_notification.h"
#include "ash/system/privacy_hub/sensor_disabled_notification_delegate.h"
#include "ash/system/system_notification_controller.h"
#include "base/containers/ring_buffer.h"
#include "base/notreached.h"
#include "base/time/time.h"
#include "chromeos/ash/components/audio/cras_audio_handler.h"
#include "components/prefs/pref_service.h"
#include "ui/message_center/message_center.h"
namespace ash {
using Sensor = SensorDisabledNotificationDelegate::Sensor;
namespace {
constexpr char kLearnMoreUrl[] =
"https://support.google.com/chromebook/?p=privacy_hub";
void LogInvalidSensor(const Sensor sensor) {
NOTREACHED() << "Invalid sensor: "
<< static_cast<std::underlying_type_t<Sensor>>(sensor);
}
// Throttler for geolocation notification. Limits notification to be displayed
// no more than once in an hour and no more that 3x in 24 hours.
class GeolocationThrottler : public PrivacyHubNotification::Throttler {
public:
// PrivacyHubNotification::Throttler
bool ShouldThrottle() final {
if (dismissals_.CurrentIndex() == 0) {
// No dismissals recorded yet. -> No suppression.
return false;
}
const base::TimeTicks current_time = base::TimeTicks::Now();
const base::TimeDelta time_since_last_dismissal =
current_time - **dismissals_.End();
if (time_since_last_dismissal < base::Hours(1)) {
// There has been a dismissal recorded within the last hour. ->
// Notification suppressed.
return true;
}
// Now we should suppress if there have been at least 3 accesses within last
// 24 hours.
if (dismissals_.CurrentIndex() < dismissals_.BufferSize()) {
// Buffer is not full. -> There have not been 3 accesses. -> No
// suppression.
return false;
}
// There are 3 recorded accesses.
const base::TimeDelta time_since_oldest_dismissal =
current_time - **dismissals_.Begin();
if (time_since_oldest_dismissal >= base::Hours(24)) {
// The oldest one is older than 24 hours. -> No suppression.
return false;
}
// All 3 must be within the 24 hours window. -> Notification suppressed.
return true;
}
void RecordDismissalByUser() final {
dismissals_.SaveToBuffer(base::TimeTicks::Now());
}
private:
// Contains the times of up to the last three dismissals (in order).
base::RingBuffer<base::TimeTicks, 3u> dismissals_;
};
} // namespace
PrivacyHubNotificationController::PrivacyHubNotificationController() {
PrivacyHubController* privacy_hub_controller = PrivacyHubController::Get();
CHECK(privacy_hub_controller);
// If privacy hub is on and the camera fallback mechanism is active, we need
// to use a different set of messages.
// TODO(b/289510726): remove when all cameras fully support the software
// switch.
// Note: if the privacy hub is not enabled, this object may still exist as it
// is used by privacy indicators as well.
std::vector<int> camera_messages;
std::vector<int> combined_messages;
if (privacy_hub_controller->UsingCameraLEDFallback()) {
camera_messages = {
IDS_PRIVACY_HUB_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_DISCLAIMER,
IDS_PRIVACY_HUB_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_ONE_APP_NAME_WITH_DISCLAIMER,
IDS_PRIVACY_HUB_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_TWO_APP_NAMES_WITH_DISCLAIMER};
combined_messages = {
IDS_PRIVACY_HUB_MICROPHONE_AND_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_DISCLAIMER,
IDS_PRIVACY_HUB_MICROPHONE_AND_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_ONE_APP_NAME_WITH_DISCLAIMER,
IDS_PRIVACY_HUB_MICROPHONE_AND_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_TWO_APP_NAMES_WITH_DISCLAIMER};
} else {
camera_messages = {
IDS_PRIVACY_HUB_CAMERA_OFF_NOTIFICATION_MESSAGE,
IDS_PRIVACY_HUB_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_ONE_APP_NAME,
IDS_PRIVACY_HUB_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_TWO_APP_NAMES};
combined_messages = {
IDS_PRIVACY_HUB_MICROPHONE_AND_CAMERA_OFF_NOTIFICATION_MESSAGE,
IDS_PRIVACY_HUB_MICROPHONE_AND_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_ONE_APP_NAME,
IDS_PRIVACY_HUB_MICROPHONE_AND_CAMERA_OFF_NOTIFICATION_MESSAGE_WITH_TWO_APP_NAMES};
}
std::vector<int> microphone_messages = {
IDS_MICROPHONE_MUTED_NOTIFICATION_MESSAGE,
IDS_MICROPHONE_MUTED_NOTIFICATION_MESSAGE_WITH_ONE_APP_NAME,
IDS_MICROPHONE_MUTED_NOTIFICATION_MESSAGE_WITH_TWO_APP_NAMES};
auto camera_notification_descriptor = PrivacyHubNotificationDescriptor(
SensorSet{Sensor::kCamera}, IDS_PRIVACY_HUB_CAMERA_OFF_NOTIFICATION_TITLE,
std::vector<int>{IDS_PRIVACY_HUB_TURN_ON_CAMERA_ACTION_BUTTON},
camera_messages);
auto microphone_notification_descriptor = PrivacyHubNotificationDescriptor(
SensorSet{Sensor::kMicrophone},
IDS_MICROPHONE_MUTED_BY_SW_SWITCH_NOTIFICATION_TITLE,
std::vector<int>{IDS_MICROPHONE_MUTED_NOTIFICATION_ACTION_BUTTON},
microphone_messages);
auto combined_notification_descriptor = PrivacyHubNotificationDescriptor(
SensorSet{Sensor::kCamera, Sensor::kMicrophone},
IDS_PRIVACY_HUB_MICROPHONE_AND_CAMERA_OFF_NOTIFICATION_TITLE,
std::vector<int>{
IDS_PRIVACY_HUB_MICROPHONE_AND_CAMERA_OFF_NOTIFICATION_BUTTON,
IDS_PRIVACY_HUB_OPEN_SETTINGS_PAGE_BUTTON},
combined_messages);
combined_notification_descriptor.delegate()->SetSecondButtonCallback(
base::BindRepeating(
&PrivacyHubNotificationController::OpenPrivacyHubSettingsPage));
combined_notification_ = std::make_unique<PrivacyHubNotification>(
kCombinedNotificationId, NotificationCatalogName::kPrivacyHubMicAndCamera,
std::vector<PrivacyHubNotificationDescriptor>{
camera_notification_descriptor, microphone_notification_descriptor,
combined_notification_descriptor});
microphone_hw_switch_notification_ = std::make_unique<PrivacyHubNotification>(
kMicrophoneHardwareSwitchNotificationId,
NotificationCatalogName::kMicrophoneMute,
PrivacyHubNotificationDescriptor{
SensorSet{Sensor::kMicrophone},
IDS_MICROPHONE_MUTED_BY_HW_SWITCH_NOTIFICATION_TITLE,
std::vector<int>{IDS_ASH_LEARN_MORE},
std::vector<int>{
IDS_MICROPHONE_MUTED_NOTIFICATION_MESSAGE,
IDS_MICROPHONE_MUTED_NOTIFICATION_MESSAGE_WITH_ONE_APP_NAME,
IDS_MICROPHONE_MUTED_NOTIFICATION_MESSAGE_WITH_TWO_APP_NAMES},
base::MakeRefCounted<PrivacyHubNotificationClickDelegate>(
base::BindRepeating(
PrivacyHubNotificationController::OpenSupportUrl,
Sensor::kMicrophone))});
PrivacyHubNotificationDescriptor geolocation_notification_descriptor(
SensorSet{Sensor::kLocation},
IDS_PRIVACY_HUB_GEOLOCATION_OFF_NOTIFICATION_TITLE,
std::vector<int>{IDS_PRIVACY_HUB_TURN_ON_GEOLOCATION_ACTION_BUTTON,
IDS_PRIVACY_HUB_TURN_ON_GEOLOCATION_LEARN_MORE_BUTTON},
std::vector<int>{
IDS_PRIVACY_HUB_GEOLOCATION_OFF_NOTIFICATION_MESSAGE,
IDS_PRIVACY_HUB_GEOLOCATION_OFF_NOTIFICATION_MESSAGE_WITH_ONE_APP_NAME,
IDS_PRIVACY_HUB_GEOLOCATION_OFF_NOTIFICATION_MESSAGE_WITH_TWO_APP_NAMES});
geolocation_notification_descriptor.delegate()->SetSecondButtonCallback(
base::BindRepeating(PrivacyHubNotificationController::OpenSupportUrl,
Sensor::kLocation));
geolocation_notification_ = std::make_unique<PrivacyHubNotification>(
kGeolocationSwitchNotificationId,
NotificationCatalogName::kGeolocationSwitch,
std::move(geolocation_notification_descriptor));
geolocation_notification_->SetThrottler(
std::make_unique<GeolocationThrottler>());
}
PrivacyHubNotificationController::~PrivacyHubNotificationController() = default;
// static
PrivacyHubNotificationController* PrivacyHubNotificationController::Get() {
SystemNotificationController* system_notification_controller =
Shell::Get()->system_notification_controller();
return system_notification_controller
? system_notification_controller->privacy_hub()
: nullptr;
}
void PrivacyHubNotificationController::ShowSoftwareSwitchNotification(
const Sensor sensor) {
switch (sensor) {
case Sensor::kMicrophone: {
// Microphone software switch notification will be displayed now. If the
// hardware switch notification is still not cleared, clear it first.
microphone_hw_switch_notification_->Hide();
[[fallthrough]];
}
case Sensor::kCamera: {
AddSensor(sensor);
combined_notification_->Show();
break;
}
case Sensor::kLocation: {
geolocation_notification_->Show();
break;
}
default: {
LogInvalidSensor(sensor);
break;
}
}
}
void PrivacyHubNotificationController::RemoveSoftwareSwitchNotification(
const Sensor sensor) {
switch (sensor) {
case Sensor::kCamera: {
[[fallthrough]];
}
case Sensor::kMicrophone: {
RemoveSensor(sensor);
if (!sensors_.empty()) {
combined_notification_->Update();
} else {
combined_notification_->Hide();
}
break;
}
case Sensor::kLocation: {
geolocation_notification_->Hide();
break;
}
default: {
LogInvalidSensor(sensor);
break;
}
}
}
void PrivacyHubNotificationController::UpdateSoftwareSwitchNotification(
const Sensor sensor) {
switch (sensor) {
case Sensor::kCamera: {
[[fallthrough]];
}
case Sensor::kMicrophone: {
combined_notification_->Update();
break;
}
case Sensor::kLocation: {
geolocation_notification_->Update();
break;
}
default: {
LogInvalidSensor(sensor);
break;
}
}
}
// TODO(janlanik): Does this support geolocation?
bool PrivacyHubNotificationController::
IsSoftwareSwitchNotificationDisplayedForSensor(Sensor sensor) {
return combined_notification_->IsShown() && sensors_.Has(sensor);
}
void PrivacyHubNotificationController::
SetPriorityForMicrophoneHardwareNotification(
message_center::NotificationPriority priority) {
microphone_hw_switch_notification_->SetPriority(priority);
}
void PrivacyHubNotificationController::ShowHardwareSwitchNotification(
const Sensor sensor) {
switch (sensor) {
case Sensor::kMicrophone: {
RemoveSensor(sensor);
if (!sensors_.empty()) {
combined_notification_->Update();
} else {
// As the hardware switch notification for microphone will be displayed
// now, remove the sw switch notification.
combined_notification_->Hide();
}
microphone_hw_switch_notification_->Show();
break;
}
default: {
LogInvalidSensor(sensor);
break;
}
}
}
void PrivacyHubNotificationController::RemoveHardwareSwitchNotification(
const Sensor sensor) {
switch (sensor) {
case Sensor::kMicrophone: {
microphone_hw_switch_notification_->Hide();
break;
}
default: {
LogInvalidSensor(sensor);
break;
}
}
}
void PrivacyHubNotificationController::UpdateHardwareSwitchNotification(
const Sensor sensor) {
switch (sensor) {
case Sensor::kMicrophone: {
microphone_hw_switch_notification_->Update();
break;
}
default: {
LogInvalidSensor(sensor);
break;
}
}
}
void PrivacyHubNotificationController::OpenPrivacyHubSettingsPage() {
privacy_hub_metrics::LogPrivacyHubOpenedFromNotification();
Shell::Get()->system_tray_model()->client()->ShowPrivacyHubSettings();
}
void PrivacyHubNotificationController::OpenSupportUrl(Sensor sensor) {
switch (sensor) {
case Sensor::kMicrophone:
privacy_hub_metrics::LogPrivacyHubLearnMorePageOpened(
privacy_hub_metrics::PrivacyHubLearnMoreSensor::kMicrophone);
break;
case Sensor::kCamera:
privacy_hub_metrics::LogPrivacyHubLearnMorePageOpened(
privacy_hub_metrics::PrivacyHubLearnMoreSensor::kCamera);
break;
case Sensor::kLocation:
privacy_hub_metrics::LogPrivacyHubLearnMorePageOpened(
privacy_hub_metrics::PrivacyHubLearnMoreSensor::kGeolocation);
return;
}
NewWindowDelegate::GetPrimary()->OpenUrl(
GURL(kLearnMoreUrl), NewWindowDelegate::OpenUrlFrom::kUserInteraction,
NewWindowDelegate::Disposition::kNewForegroundTab);
}
// static
void PrivacyHubNotificationController::
SetAndLogSensorPreferenceFromNotification(Sensor sensor,
const bool enabled) {
PrefService* pref_service =
Shell::Get()->session_controller()->GetLastActiveUserPrefService();
if (!pref_service) {
return;
}
switch (sensor) {
case Sensor::kCamera: {
pref_service->SetBoolean(prefs::kUserCameraAllowed, enabled);
break;
}
case Sensor::kMicrophone: {
pref_service->SetBoolean(prefs::kUserMicrophoneAllowed, enabled);
break;
}
case Sensor::kLocation: {
// Geolocation notification asks user to allow geolocation for everything
// (not only system services).
if (auto* controller = ash::GeolocationPrivacySwitchController::Get()) {
controller->SetAccessLevel(enabled
? GeolocationAccessLevel::kAllowed
: GeolocationAccessLevel::kDisallowed);
}
break;
}
}
privacy_hub_metrics::LogSensorEnabledFromNotification(sensor, enabled);
}
std::unique_ptr<SensorDisabledNotificationDelegate>
PrivacyHubNotificationController::SetSensorDisabledNotificationDelegate(
std::unique_ptr<SensorDisabledNotificationDelegate> delegate) {
std::swap(sensor_disabled_notification_delegate_, delegate);
return delegate;
}
SensorDisabledNotificationDelegate*
PrivacyHubNotificationController::sensor_disabled_notification_delegate() {
SensorDisabledNotificationDelegate* delegate =
sensor_disabled_notification_delegate_.get();
CHECK(delegate);
return delegate;
}
void PrivacyHubNotificationController::AddSensor(Sensor sensor) {
sensors_.Put(sensor);
combined_notification_->SetSensors(sensors_);
}
void PrivacyHubNotificationController::RemoveSensor(Sensor sensor) {
sensors_.Remove(sensor);
combined_notification_->SetSensors(sensors_);
}
} // namespace ash