| Index: chrome/browser/ui/bluetooth/bluetooth_permission_context.cc
|
| diff --git a/chrome/browser/ui/bluetooth/bluetooth_permission_context.cc b/chrome/browser/ui/bluetooth/bluetooth_permission_context.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6ee8a59432c10fec219faa2ef1b0022f8bdf791f
|
| --- /dev/null
|
| +++ b/chrome/browser/ui/bluetooth/bluetooth_permission_context.cc
|
| @@ -0,0 +1,117 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "chrome/browser/ui/bluetooth/bluetooth_permission_context.h"
|
| +
|
| +#include <utility>
|
| +
|
| +#include "base/bind.h"
|
| +#include "base/values.h"
|
| +#include "device/bluetooth/bluetooth_adapter.h"
|
| +#include "device/bluetooth/bluetooth_adapter_factory.h"
|
| +#include "device/bluetooth/bluetooth_device.h"
|
| +#include "device/bluetooth/bluetooth_uuid.h"
|
| +
|
| +namespace {
|
| +
|
| +const char kDeviceAddressKey[] = "device-address";
|
| +const char kDeviceNameKey[] = "device-name";
|
| +const char kDeviceUUIDsKey[] = "device-uuids";
|
| +
|
| +const base::DictionaryValue* FindForDevice(
|
| + const std::vector<scoped_ptr<base::DictionaryValue>>& device_list,
|
| + const std::string& device_address) {
|
| + for (const scoped_ptr<base::DictionaryValue>& device_dict : device_list) {
|
| + std::string tmp_device_address;
|
| + if (device_dict->GetString(kDeviceAddressKey, &tmp_device_address) &&
|
| + tmp_device_address == device_address) {
|
| + return device_dict.get();
|
| + }
|
| + }
|
| + return nullptr;
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +BluetoothPermissionContext::BluetoothPermissionContext(Profile* profile)
|
| + : ChooserContextBase(profile, CONTENT_SETTINGS_TYPE_BLUETOOTH_CHOOSER_DATA),
|
| + weak_ptr_factory_(this) {
|
| + device::BluetoothAdapterFactory::GetAdapter(
|
| + base::Bind(&BluetoothPermissionContext::OnGetAdapter,
|
| + weak_ptr_factory_.GetWeakPtr()));
|
| +}
|
| +
|
| +BluetoothPermissionContext::~BluetoothPermissionContext() {}
|
| +
|
| +void BluetoothPermissionContext::GrantDevicePermission(
|
| + const GURL& requesting_origin,
|
| + const GURL& embedding_origin,
|
| + const std::string& device_address) {
|
| + scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue());
|
| + device_dict->SetString(kDeviceAddressKey, device_address);
|
| + device::BluetoothDevice* device = adapter_->GetDevice(device_address);
|
| + DCHECK(device);
|
| + device_dict->SetString(kDeviceNameKey, device->GetName());
|
| + scoped_ptr<base::ListValue> device_uuids_list(new base::ListValue());
|
| + device::BluetoothDevice::UUIDList uuid_list = device->GetUUIDs();
|
| + for (const auto& uuid : uuid_list)
|
| + device_uuids_list->AppendString(uuid.value());
|
| + device_dict->Set(kDeviceUUIDsKey, std::move(device_uuids_list));
|
| +
|
| + GrantObjectPermission(requesting_origin, embedding_origin,
|
| + std::move(device_dict));
|
| +}
|
| +
|
| +void BluetoothPermissionContext::RevokeDevicePermission(
|
| + const GURL& requesting_origin,
|
| + const GURL& embedding_origin,
|
| + const std::string& device_address) {
|
| + std::vector<scoped_ptr<base::DictionaryValue>> device_list =
|
| + GetGrantedObjects(requesting_origin, embedding_origin);
|
| + const base::DictionaryValue* entry =
|
| + FindForDevice(device_list, device_address);
|
| + if (entry)
|
| + RevokeObjectPermission(requesting_origin, embedding_origin, *entry);
|
| +}
|
| +
|
| +std::vector<std::string> BluetoothPermissionContext::HasDevicePermission(
|
| + const GURL& requesting_origin,
|
| + const GURL& embedding_origin,
|
| + const std::string& device_address) {
|
| + std::vector<scoped_ptr<base::DictionaryValue>> device_list =
|
| + GetGrantedObjects(requesting_origin, embedding_origin);
|
| + std::vector<std::string> uuids;
|
| + const base::DictionaryValue* device_dict =
|
| + FindForDevice(device_list, device_address);
|
| + if (device_dict) {
|
| + const base::ListValue* uuid_list = nullptr;
|
| + device_dict->GetList(kDeviceUUIDsKey, &uuid_list);
|
| + size_t length = uuid_list->GetSize();
|
| + for (size_t i = 0; i < length; ++i) {
|
| + std::string uuid;
|
| + uuid_list->GetString(i, &uuid);
|
| + uuids.push_back(uuid);
|
| + }
|
| + }
|
| + return uuids;
|
| +}
|
| +
|
| +void BluetoothPermissionContext::OnGetAdapter(
|
| + scoped_refptr<device::BluetoothAdapter> adapter) {
|
| + DCHECK(!adapter_.get());
|
| + adapter_ = adapter;
|
| + DCHECK(adapter_.get());
|
| +}
|
| +
|
| +bool BluetoothPermissionContext::IsValidObject(
|
| + const base::DictionaryValue& object) {
|
| + std::string device_address, device_name;
|
| + const base::ListValue* device_uuids = nullptr;
|
| + return object.size() == 3 && object.HasKey(kDeviceAddressKey) &&
|
| + object.GetString(kDeviceAddressKey, &device_address) &&
|
| + object.HasKey(kDeviceNameKey) &&
|
| + object.GetString(kDeviceNameKey, &device_name) &&
|
| + object.HasKey(kDeviceUUIDsKey) &&
|
| + object.GetList(kDeviceUUIDsKey, &device_uuids);
|
| +}
|
|
|