Chromium Code Reviews| 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..d98d487ac4366825c7e96a31ac00b429e2ef342b |
| --- /dev/null |
| +++ b/chrome/browser/ui/bluetooth/bluetooth_permission_context.cc |
| @@ -0,0 +1,123 @@ |
| +// 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" |
| +#include "url/gurl.h" |
| +#include "url/origin.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 url::Origin& requesting_origin, |
| + const url::Origin& 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()); |
|
Jeffrey Yasskin
2016/01/21 00:32:30
Please break up functions like this a little more
juncai
2016/01/25 18:53:32
Done.
|
| + device::BluetoothDevice::UUIDList uuid_list = device->GetUUIDs(); |
|
Jeffrey Yasskin
2016/01/21 00:32:30
The domain doesn't automatically get permission to
juncai
2016/01/25 18:53:32
Done.
|
| + for (const auto& uuid : uuid_list) |
| + device_uuids_list->AppendString(uuid.value()); |
| + device_dict->Set(kDeviceUUIDsKey, std::move(device_uuids_list)); |
| + |
| + GrantObjectPermission(GURL(requesting_origin.Serialize()), |
| + GURL(embedding_origin.Serialize()), |
| + std::move(device_dict)); |
| +} |
| + |
| +void BluetoothPermissionContext::RevokeDevicePermission( |
| + const url::Origin& requesting_origin, |
| + const url::Origin& embedding_origin, |
| + const std::string& device_address) { |
| + std::vector<scoped_ptr<base::DictionaryValue>> device_list = |
| + GetGrantedObjects(GURL(requesting_origin.Serialize()), |
| + GURL(embedding_origin.Serialize())); |
| + const base::DictionaryValue* entry = |
| + FindForDevice(device_list, device_address); |
| + if (entry) |
| + RevokeObjectPermission(GURL(requesting_origin.Serialize()), |
| + GURL(embedding_origin.Serialize()), *entry); |
| +} |
| + |
| +std::vector<std::string> BluetoothPermissionContext::HasDevicePermission( |
| + const url::Origin& requesting_origin, |
| + const url::Origin& embedding_origin, |
| + const std::string& device_address) { |
| + std::vector<scoped_ptr<base::DictionaryValue>> device_list = |
| + GetGrantedObjects(GURL(requesting_origin.Serialize()), |
| + GURL(embedding_origin.Serialize())); |
| + 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) && |
|
Jeffrey Yasskin
2016/01/21 00:32:30
Could you comment that this check needs to be back
juncai
2016/01/25 18:53:32
Done.
|
| + object.GetString(kDeviceAddressKey, &device_address) && |
|
Jeffrey Yasskin
2016/01/21 00:32:30
Pass nullptr as the second argument to GetString o
juncai
2016/01/25 18:53:32
Done.
|
| + object.HasKey(kDeviceNameKey) && |
| + object.GetString(kDeviceNameKey, &device_name) && |
| + object.HasKey(kDeviceUUIDsKey) && |
| + object.GetList(kDeviceUUIDsKey, &device_uuids); |
| +} |