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
ash / system / privacy_hub / geolocation_privacy_switch_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/geolocation_privacy_switch_controller.h"
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/constants/geolocation_access_level.h"
#include "ash/public/cpp/session/session_observer.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.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_controller.h"
#include "base/notreached.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
namespace ash {
GeolocationPrivacySwitchController::GeolocationPrivacySwitchController()
: session_observation_(this) {
session_observation_.Observe(Shell::Get()->session_controller());
}
GeolocationPrivacySwitchController::~GeolocationPrivacySwitchController() =
default;
// static
GeolocationPrivacySwitchController* GeolocationPrivacySwitchController::Get() {
PrivacyHubController* privacy_hub_controller =
Shell::Get()->privacy_hub_controller();
return privacy_hub_controller
? privacy_hub_controller->geolocation_controller()
: nullptr;
}
void GeolocationPrivacySwitchController::OnActiveUserPrefServiceChanged(
PrefService* pref_service) {
pref_change_registrar_ = std::make_unique<PrefChangeRegistrar>();
pref_change_registrar_->Init(pref_service);
pref_change_registrar_->Add(
prefs::kUserGeolocationAccessLevel,
base::BindRepeating(
&GeolocationPrivacySwitchController::OnPreferenceChanged,
base::Unretained(this)));
// Establish the initial value for the cached_access_level_.
cached_access_level_ = static_cast<GeolocationAccessLevel>(
pref_change_registrar_->prefs()->GetInteger(
prefs::kUserGeolocationAccessLevel));
if (features::IsCrosPrivacyHubLocationEnabled()) {
UpdateNotification();
}
}
void GeolocationPrivacySwitchController::OnPreferenceChanged() {
VLOG(1) << "Privacy Hub: Geolocation switch state = "
<< static_cast<int>(AccessLevel());
if (features::IsCrosPrivacyHubLocationEnabled()) {
CHECK(pref_change_registrar_);
const GeolocationAccessLevel new_access_level =
static_cast<GeolocationAccessLevel>(
pref_change_registrar_->prefs()->GetInteger(
prefs::kUserGeolocationAccessLevel));
CHECK(cached_access_level_.has_value());
if (new_access_level != *cached_access_level_) {
// update the pref that tracks the previous access level.
pref_change_registrar_->prefs()->SetInteger(
prefs::kUserPreviousGeolocationAccessLevel,
static_cast<int>(*cached_access_level_));
cached_access_level_ = new_access_level;
}
UpdateNotification();
} else {
// Feature disabled means geolocation is always allowed
CHECK(pref_change_registrar_);
pref_change_registrar_->prefs()->SetInteger(
prefs::kUserGeolocationAccessLevel,
static_cast<int>(GeolocationAccessLevel::kAllowed));
}
}
void GeolocationPrivacySwitchController::TrackGeolocationAttempted(
const std::string& app_name) {
++usage_per_app_[app_name];
++usage_cnt_;
UpdateNotification();
}
void GeolocationPrivacySwitchController::TrackGeolocationRelinquished(
const std::string& app_name) {
--usage_per_app_[app_name];
--usage_cnt_;
if (usage_per_app_[app_name] < 0 || usage_cnt_ < 0) {
LOG(ERROR) << "Geolocation usage termination without start: count("
<< app_name << ") = " << usage_per_app_[app_name]
<< ", total count = " << usage_cnt_;
NOTREACHED();
}
UpdateNotification();
}
bool GeolocationPrivacySwitchController::IsGeolocationUsageAllowedForApps() {
switch (AccessLevel()) {
case GeolocationAccessLevel::kAllowed:
return true;
case GeolocationAccessLevel::kOnlyAllowedForSystem:
case GeolocationAccessLevel::kDisallowed:
return false;
}
}
std::vector<std::u16string> GeolocationPrivacySwitchController::GetActiveApps(
size_t max_count) const {
std::vector<std::u16string> apps;
for (const auto& [name, cnt] : usage_per_app_) {
if (cnt > 0) {
apps.push_back(base::UTF8ToUTF16(name));
if (apps.size() == max_count) {
break;
}
}
}
return apps;
}
GeolocationAccessLevel GeolocationPrivacySwitchController::AccessLevel() const {
CHECK(cached_access_level_.has_value());
return *cached_access_level_;
}
GeolocationAccessLevel GeolocationPrivacySwitchController::PreviousAccessLevel()
const {
CHECK(pref_change_registrar_);
const GeolocationAccessLevel previous_level =
static_cast<GeolocationAccessLevel>(
pref_change_registrar_->prefs()->GetInteger(
prefs::kUserPreviousGeolocationAccessLevel));
// Previous level should be distinct.
CHECK_NE(previous_level, AccessLevel());
return previous_level;
}
void GeolocationPrivacySwitchController::SetAccessLevel(
GeolocationAccessLevel access_level) {
if (!features::IsCrosPrivacyHubLocationEnabled()) {
return;
}
CHECK(pref_change_registrar_);
pref_change_registrar_->prefs()->SetInteger(
prefs::kUserGeolocationAccessLevel, static_cast<int>(access_level));
}
void GeolocationPrivacySwitchController::UpdateNotification() {
PrivacyHubNotificationController* notification_controller =
PrivacyHubNotificationController::Get();
if (!notification_controller) {
return;
}
if (usage_cnt_ == 0 || IsGeolocationUsageAllowedForApps()) {
notification_controller->RemoveSoftwareSwitchNotification(
SensorDisabledNotificationDelegate::Sensor::kLocation);
return;
}
notification_controller->ShowSoftwareSwitchNotification(
SensorDisabledNotificationDelegate::Sensor::kLocation);
}
void GeolocationPrivacySwitchController::ApplyArcLocationUpdate(
bool geolocation_enabled) {
if (!features::IsCrosPrivacyHubLocationEnabled()) {
return;
}
if (geolocation_enabled &&
AccessLevel() != GeolocationAccessLevel::kAllowed) {
SetAccessLevel(GeolocationAccessLevel::kAllowed);
} else if (!geolocation_enabled &&
AccessLevel() == ash::GeolocationAccessLevel::kAllowed) {
// Restore previous location level, which is blocking.
SetAccessLevel(PreviousAccessLevel());
}
}
} // namespace ash