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
|
// Copyright 2016 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/policy/handlers/bluetooth_policy_handler.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/ref_counted.h"
#include "base/syslog_logging.h"
#include "base/values.h"
#include "chromeos/ash/components/settings/cros_settings_names.h"
#include "chromeos/ash/components/settings/cros_settings_provider.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/public/cpp/bluetooth_uuid.h"
namespace policy {
BluetoothPolicyHandler::BluetoothPolicyHandler(ash::CrosSettings* cros_settings)
: cros_settings_(cros_settings) {
allow_bluetooth_subscription_ = cros_settings_->AddSettingsObserver(
ash::kAllowBluetooth,
base::BindRepeating(&BluetoothPolicyHandler::OnBluetoothPolicyChanged,
weak_factory_.GetWeakPtr()));
allowed_services_subscription_ = cros_settings_->AddSettingsObserver(
ash::kDeviceAllowedBluetoothServices,
base::BindRepeating(&BluetoothPolicyHandler::OnBluetoothPolicyChanged,
weak_factory_.GetWeakPtr()));
device::BluetoothAdapterFactory::Get()->GetAdapter(
base::BindOnce(&BluetoothPolicyHandler::InitializeOnAdapterReady,
weak_factory_.GetWeakPtr()));
// Fire it once so we're sure we get an invocation on startup.
OnBluetoothPolicyChanged();
}
BluetoothPolicyHandler::~BluetoothPolicyHandler() {
if (adapter_) {
adapter_->RemoveObserver(this);
}
}
void BluetoothPolicyHandler::AdapterPresentChanged(
device::BluetoothAdapter* adapter,
bool present) {
SetBluetoothPolicy();
}
void BluetoothPolicyHandler::AdapterPoweredChanged(
device::BluetoothAdapter* adapter,
bool powered) {
SetBluetoothPolicy();
}
void BluetoothPolicyHandler::InitializeOnAdapterReady(
scoped_refptr<device::BluetoothAdapter> adapter) {
adapter_ = std::move(adapter);
adapter_->AddObserver(this);
SetBluetoothPolicy();
}
void BluetoothPolicyHandler::OnBluetoothPolicyChanged() {
ash::CrosSettingsProvider::TrustedStatus status =
cros_settings_->PrepareTrustedValues(
base::BindOnce(&BluetoothPolicyHandler::OnBluetoothPolicyChanged,
weak_factory_.GetWeakPtr()));
if (status != ash::CrosSettingsProvider::TRUSTED)
return;
new_policy_update_pending_ = true;
SetBluetoothPolicy();
}
void SetServiceAllowListSuccess() {
SYSLOG(INFO) << "Set ServiceAllowList Success";
}
void SetServiceAllowListFailed() {
SYSLOG(ERROR) << "Set ServiceAllowList Failed";
}
void BluetoothPolicyHandler::SetBluetoothPolicy() {
// Get the updated policy.
bool allow_bluetooth = true;
const base::Value::List* allowed_services_list = nullptr;
std::vector<device::BluetoothUUID> allowed_services;
if (!adapter_ || !adapter_->IsInitialized() || !adapter_->IsPresent() ||
!adapter_->IsPowered() || !new_policy_update_pending_) {
return;
}
cros_settings_->GetBoolean(ash::kAllowBluetooth, &allow_bluetooth);
if (!allow_bluetooth) {
adapter_->SetPowered(false, base::DoNothing(), base::DoNothing());
adapter_->Shutdown();
}
// Pass empty list to SetServiceAllowList means no restriction for users,
// which is the same behavior as leaving DeviceAllowedBluetoothService unset.
// Therefore, we don't need to handle the case when device management server
// returns an empty list even if the policy did not set.
if (cros_settings_->GetList(ash::kDeviceAllowedBluetoothServices,
&allowed_services_list)) {
for (const auto& list_value : *allowed_services_list) {
if (!list_value.is_string())
continue;
const std::string& uuid_str = list_value.GetString();
device::BluetoothUUID uuid(uuid_str);
if (!uuid.IsValid()) {
SYSLOG(WARNING) << "Failed to parse '" << uuid_str
<< "' into UUID struct";
continue;
}
allowed_services.push_back(uuid);
}
std::ostringstream info_stream;
info_stream << "Setting " << allowed_services.size()
<< " UUIDs as allowed services;";
for (size_t i = 0; i < allowed_services.size(); i++) {
info_stream << " " << i << "th allowed service uuid: "
<< allowed_services[i].canonical_value() << ";";
}
SYSLOG(INFO) << info_stream.str();
adapter_->SetServiceAllowList(allowed_services,
base::BindOnce(SetServiceAllowListSuccess),
base::BindOnce(SetServiceAllowListFailed));
}
// Reset the indicator to false to make sure we don't bother setting the same
// policy to the daemon, although it is harmless.
new_policy_update_pending_ = false;
}
} // namespace policy
|