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
|
// 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 "chrome/browser/ash/privacy_hub/privacy_hub_util.h"
#include <optional>
#include <string>
#include "ash/constants/ash_features.h"
#include "ash/constants/ash_pref_names.h"
#include "ash/public/cpp/privacy_hub_delegate.h"
#include "ash/public/cpp/session/session_controller.h"
#include "ash/public/cpp/session/session_observer.h"
#include "ash/session/session_controller_impl.h"
#include "ash/shell.h"
#include "ash/system/geolocation/geolocation_controller.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_notification_controller.h"
#include "ash/system/privacy_hub/sensor_disabled_notification_delegate.h"
#include "ash/webui/settings/public/constants/routes.mojom-forward.h"
#include "base/check_deref.h"
#include "base/functional/callback.h"
#include "base/notreached.h"
#include "base/supports_user_data.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/ui/ash/app_access/app_access_notifier.h"
#include "chrome/browser/ui/settings_window_manager_chromeos.h"
#include "chromeos/ash/components/camera_presence_notifier/camera_presence_notifier.h"
#include "components/prefs/pref_change_registrar.h"
#include "components/prefs/pref_service.h"
namespace ash::privacy_hub_util {
namespace {
PrefService* ActivePrefService() {
auto* session_controller =
CHECK_DEREF(ash::Shell::Get()).session_controller();
return CHECK_DEREF(session_controller).GetActivePrefService();
}
ScopedUserPermissionPrefForTest::AccessLevel GetCurrentAccessLevel(
ContentType type) {
if (type == ContentType::MEDIASTREAM_CAMERA) {
const bool allowed =
CHECK_DEREF(ActivePrefService()).GetBoolean(prefs::kUserCameraAllowed);
return allowed ? ScopedUserPermissionPrefForTest::AccessLevel::kAllowed
: ScopedUserPermissionPrefForTest::AccessLevel::kDisallowed;
}
if (type == ContentType::MEDIASTREAM_MIC) {
const bool allowed = CHECK_DEREF(ActivePrefService())
.GetBoolean(prefs::kUserMicrophoneAllowed);
return allowed ? ScopedUserPermissionPrefForTest::AccessLevel::kAllowed
: ScopedUserPermissionPrefForTest::AccessLevel::kDisallowed;
}
CHECK(type == ContentType::GEOLOCATION);
return static_cast<ScopedUserPermissionPrefForTest::AccessLevel>(
CHECK_DEREF(ActivePrefService())
.GetInteger(prefs::kUserGeolocationAccessLevel));
}
void SetCurrentAccessLevel(
ContentType type,
ScopedUserPermissionPrefForTest::AccessLevel access_level) {
if (type == ContentType::MEDIASTREAM_CAMERA) {
CHECK(access_level !=
ScopedUserPermissionPrefForTest::AccessLevel::kOnlyAllowedForSystem);
CHECK_DEREF(ActivePrefService())
.SetBoolean(prefs::kUserCameraAllowed,
access_level ==
ScopedUserPermissionPrefForTest::AccessLevel::kAllowed);
return;
}
if (type == ContentType::MEDIASTREAM_MIC) {
CHECK(access_level !=
ScopedUserPermissionPrefForTest::AccessLevel::kOnlyAllowedForSystem);
CHECK_DEREF(ActivePrefService())
.SetBoolean(prefs::kUserMicrophoneAllowed,
access_level ==
ScopedUserPermissionPrefForTest::AccessLevel::kAllowed);
return;
}
CHECK(type == ContentType::GEOLOCATION);
CHECK_DEREF(ActivePrefService())
.SetInteger(prefs::kUserGeolocationAccessLevel,
static_cast<int>(access_level));
}
class ContentBlockObservationImpl : public ContentBlockObservation {
public:
// Access restricted constructor.
ContentBlockObservationImpl(SessionController* session_controller,
SystemPermissionChangedCallback callback)
: callback_(std::move(callback)) {
// Microphone and camera settings are following the active user's
// preferences. Subscribing to the active user's pref changes.
CHECK(Shell::Get()->session_controller());
active_user_pref_change_registrar_ =
std::make_unique<PrefChangeRegistrar>();
active_user_pref_change_registrar_->Init(
Shell::Get()->session_controller()->GetActivePrefService());
active_user_pref_change_registrar_->Add(
prefs::kUserCameraAllowed,
base::BindRepeating(&ContentBlockObservationImpl::OnPreferenceChanged,
base::Unretained(this)));
active_user_pref_change_registrar_->Add(
prefs::kUserMicrophoneAllowed,
base::BindRepeating(&ContentBlockObservationImpl::OnPreferenceChanged,
base::Unretained(this)));
// Geolocation setting is exclusively controlled by the primary user of the
// session. This is different from the camera and microphone implementation.
// Subscribing to the primary user's pref changes.
primary_user_pref_change_registrar_ =
std::make_unique<PrefChangeRegistrar>();
primary_user_pref_change_registrar_->Init(
Shell::Get()->session_controller()->GetPrimaryUserPrefService());
primary_user_pref_change_registrar_->Add(
prefs::kUserGeolocationAccessLevel,
base::BindRepeating(&ContentBlockObservationImpl::OnPreferenceChanged,
base::Unretained(this)));
}
~ContentBlockObservationImpl() override = default;
private:
// Handles changes in the user pref ( e.g. toggling the camera switch on
// Privacy Hub UI).
void OnPreferenceChanged(const std::string& pref_name) {
if (pref_name == prefs::kUserCameraAllowed) {
callback_.Run(
ContentType::MEDIASTREAM_CAMERA,
privacy_hub_util::ContentBlocked(ContentType::MEDIASTREAM_CAMERA));
return;
}
if (pref_name == prefs::kUserMicrophoneAllowed) {
callback_.Run(
ContentType::MEDIASTREAM_MIC,
privacy_hub_util::ContentBlocked(ContentType::MEDIASTREAM_MIC));
return;
}
if (pref_name == prefs::kUserGeolocationAccessLevel) {
callback_.Run(ContentType::GEOLOCATION,
privacy_hub_util::ContentBlocked(ContentType::GEOLOCATION));
return;
}
NOTREACHED();
}
SystemPermissionChangedCallback callback_;
std::unique_ptr<PrefChangeRegistrar> active_user_pref_change_registrar_;
std::unique_ptr<PrefChangeRegistrar> primary_user_pref_change_registrar_;
base::WeakPtrFactory<ContentBlockObservationImpl> weak_ptr_factory_{this};
};
} // namespace
void SetFrontend(PrivacyHubDelegate* ptr) {
PrivacyHubController* const controller = PrivacyHubController::Get();
if (controller != nullptr) {
// Controller may not be available when used from a test.
controller->SetFrontend(ptr);
}
}
bool MicrophoneSwitchState() {
return ui::MicrophoneMuteSwitchMonitor::Get()->microphone_mute_switch_on();
}
bool ShouldForceDisableCameraSwitch() {
PrivacyHubController* controller = PrivacyHubController::Get();
if (!controller || !controller->camera_controller()) {
return false;
}
return controller->camera_controller()->IsCameraAccessForceDisabled();
}
void SetUpCameraCountObserver() {
auto* camera_controller = CameraPrivacySwitchController::Get();
CHECK(camera_controller);
base::RepeatingCallback<void(int)> update_camera_count_in_privacy_hub =
base::BindRepeating(
[](CameraPrivacySwitchController* controller, int camera_count) {
controller->OnCameraCountChanged(camera_count);
},
camera_controller);
auto notifier = std::make_unique<CameraPresenceNotifier>(
std::move(update_camera_count_in_privacy_hub));
notifier->Start();
static const char kUserDataKey = '\0';
camera_controller->SetUserData(&kUserDataKey, std::move(notifier));
}
// Notifies the Privacy Hub controller.
void TrackGeolocationAttempted(const std::string& name) {
if (!features::IsCrosPrivacyHubLocationEnabled()) {
return;
}
GeolocationPrivacySwitchController* controller =
GeolocationPrivacySwitchController::Get();
// TODO(b/288854399): Remove this if.
if (controller) {
controller->TrackGeolocationAttempted(name);
}
}
// Notifies the Privacy Hub controller.
void TrackGeolocationRelinquished(const std::string& name) {
if (!features::IsCrosPrivacyHubLocationEnabled()) {
return;
}
GeolocationPrivacySwitchController* controller =
GeolocationPrivacySwitchController::Get();
// TODO(b/288854399): Remove this if.
if (controller) {
controller->TrackGeolocationRelinquished(name);
}
}
bool IsCrosLocationOobeNegotiationNeeded() {
// No negotiation needed, if the PH Location feature is not yet enabled.
if (!ash::features::IsCrosPrivacyHubLocationEnabled()) {
return false;
}
const Profile* profile = ProfileManager::GetPrimaryUserProfile();
if (profile->GetPrefs()->IsManagedPreference(
ash::prefs::kUserGeolocationAccessLevel)) {
return false;
}
return true;
}
GeolocationAccessLevel GetSystemGeolocationAccessLevel() {
return GeolocationPrivacySwitchController::Get()->AccessLevel();
}
namespace {
std::optional<bool> camera_led_fallback_for_testing{};
}
// TODO(b/289510726): remove when all cameras fully support the software
// switch.
bool UsingCameraLEDFallback() {
if (!camera_led_fallback_for_testing.has_value()) {
CameraPrivacySwitchController* const controller =
CameraPrivacySwitchController::Get();
CHECK(controller);
return controller->UsingCameraLEDFallback();
}
// Can happen in some testing environments
CHECK(camera_led_fallback_for_testing.has_value());
return camera_led_fallback_for_testing.value();
}
ScopedCameraLedFallbackForTesting::ScopedCameraLedFallbackForTesting(
bool value) {
CHECK(!camera_led_fallback_for_testing.has_value());
camera_led_fallback_for_testing = value;
}
ScopedCameraLedFallbackForTesting::~ScopedCameraLedFallbackForTesting() {
CHECK(camera_led_fallback_for_testing.has_value());
camera_led_fallback_for_testing.reset();
CHECK(!camera_led_fallback_for_testing.has_value());
}
void SetAppAccessNotifier(AppAccessNotifier* app_access_notifier) {
// Wraps the `AppAccessNotifier` to be used from
// `PrivacyHubNotificationController`.
class Wrapper : public SensorDisabledNotificationDelegate {
public:
explicit Wrapper(AppAccessNotifier* notifier)
: notifier_(raw_ref<AppAccessNotifier>::from_ptr(notifier)) {}
std::vector<std::u16string> GetAppsAccessingSensor(Sensor sensor) override {
switch (sensor) {
case Sensor::kCamera:
return notifier_->GetAppsAccessingCamera();
case Sensor::kMicrophone:
return notifier_->GetAppsAccessingMicrophone();
case Sensor::kLocation:
break;
}
NOTREACHED();
}
private:
raw_ref<AppAccessNotifier> notifier_;
};
PrivacyHubNotificationController* controller =
PrivacyHubNotificationController::Get();
CHECK(controller);
controller->SetSensorDisabledNotificationDelegate(
app_access_notifier ? std::make_unique<Wrapper>(app_access_notifier)
: nullptr);
}
std::pair<base::Time, base::Time> SunriseSunsetSchedule() {
const base::Time default_sunrise_time =
base::Time::Now().LocalMidnight() + base::Hours(6);
const base::Time default_sunset_time = default_sunrise_time + base::Hours(12);
const ash::GeolocationController* geolocation_controller =
ash::GeolocationController::Get();
const base::Time sunrise_time =
geolocation_controller
? geolocation_controller->GetSunriseTime().value_or(
default_sunrise_time)
: default_sunrise_time;
const base::Time sunset_time =
geolocation_controller ? geolocation_controller->GetSunsetTime().value_or(
default_sunset_time)
: default_sunrise_time;
return std::make_pair(sunrise_time, sunset_time);
}
bool ContentBlocked(ContentType type) {
switch (type) {
case ContentType::MEDIASTREAM_CAMERA: {
auto* const controller = CameraPrivacySwitchController::Get();
CHECK(controller);
return !controller->IsCameraUsageAllowed();
}
case ContentType::MEDIASTREAM_MIC: {
auto* const controller = MicrophonePrivacySwitchController::Get();
CHECK(controller);
return !controller->IsMicrophoneUsageAllowed();
}
case ContentType::GEOLOCATION: {
if (!features::IsCrosPrivacyHubLocationEnabled()) {
return false; // content not blocked as geo blocking not supported.
}
auto* const controller = GeolocationPrivacySwitchController::Get();
CHECK(controller);
return !controller->IsGeolocationUsageAllowedForApps();
}
default: {
// If the provided content type is not controllable in ChromeOS, then it
// is not blocked.
return false;
}
}
}
std::unique_ptr<ContentBlockObservation> CreateObservationForBlockedContent(
SystemPermissionChangedCallback callback) {
if (!ash::Shell::HasInstance()) {
return nullptr;
}
auto* shell = ash::Shell::Get();
CHECK(shell);
auto* session_controller = shell->session_controller();
if (!session_controller) {
return nullptr;
}
PrefService* pref_service =
session_controller->GetLastActiveUserPrefService();
if (!pref_service) {
return nullptr;
}
auto observation = std::make_unique<ContentBlockObservationImpl>(
session_controller, std::move(callback));
return observation;
}
void OpenSystemSettings(ContentType type) {
const char* settings_path = "";
switch (type) {
case ContentType::MEDIASTREAM_CAMERA: {
settings_path = chromeos::settings::mojom::kPrivacyHubCameraSubpagePath;
break;
}
case ContentType::MEDIASTREAM_MIC: {
settings_path =
chromeos::settings::mojom::kPrivacyHubMicrophoneSubpagePath;
break;
}
case ContentType::GEOLOCATION: {
settings_path =
chromeos::settings::mojom::kPrivacyHubGeolocationSubpagePath;
break;
}
default: {
// This should only be called for camera, microphone, or geolocation.
NOTREACHED();
}
}
chrome::SettingsWindowManager::GetInstance()->ShowOSSettings(
ProfileManager::GetActiveUserProfile(), settings_path);
}
ScopedUserPermissionPrefForTest::ScopedUserPermissionPrefForTest(
ContentType type,
ScopedUserPermissionPrefForTest::AccessLevel access_level)
: content_type_(type), previous_access_level_(GetCurrentAccessLevel(type)) {
// Only Geolocation, camera and mic are supported.
CHECK((ContentType::GEOLOCATION == type) ||
(ContentType::MEDIASTREAM_CAMERA == type) ||
(ContentType::MEDIASTREAM_MIC == type));
if (ContentType::GEOLOCATION != type) {
CHECK(access_level !=
ScopedUserPermissionPrefForTest::AccessLevel::kOnlyAllowedForSystem);
}
SetCurrentAccessLevel(type, access_level);
}
ScopedUserPermissionPrefForTest::~ScopedUserPermissionPrefForTest() {
SetCurrentAccessLevel(content_type_, previous_access_level_);
}
} // namespace ash::privacy_hub_util
|