OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/ui/bluetooth/bluetooth_permission_context.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "base/bind.h" |
| 10 #include "base/values.h" |
| 11 #include "device/bluetooth/bluetooth_adapter.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 13 #include "device/bluetooth/bluetooth_device.h" |
| 14 #include "device/bluetooth/bluetooth_uuid.h" |
| 15 |
| 16 namespace { |
| 17 |
| 18 const char kDeviceAddressKey[] = "device-address"; |
| 19 const char kDeviceNameKey[] = "device-name"; |
| 20 const char kDeviceUUIDsKey[] = "device-uuids"; |
| 21 |
| 22 const base::DictionaryValue* FindForDevice( |
| 23 const std::vector<scoped_ptr<base::DictionaryValue>>& device_list, |
| 24 const std::string& device_address) { |
| 25 for (const scoped_ptr<base::DictionaryValue>& device_dict : device_list) { |
| 26 std::string tmp_device_address; |
| 27 if (device_dict->GetString(kDeviceAddressKey, &tmp_device_address) && |
| 28 tmp_device_address == device_address) { |
| 29 return device_dict.get(); |
| 30 } |
| 31 } |
| 32 return nullptr; |
| 33 } |
| 34 |
| 35 } // namespace |
| 36 |
| 37 BluetoothPermissionContext::BluetoothPermissionContext(Profile* profile) |
| 38 : ChooserContextBase(profile, CONTENT_SETTINGS_TYPE_BLUETOOTH_CHOOSER_DATA), |
| 39 weak_ptr_factory_(this) { |
| 40 device::BluetoothAdapterFactory::GetAdapter( |
| 41 base::Bind(&BluetoothPermissionContext::OnGetAdapter, |
| 42 weak_ptr_factory_.GetWeakPtr())); |
| 43 } |
| 44 |
| 45 BluetoothPermissionContext::~BluetoothPermissionContext() {} |
| 46 |
| 47 void BluetoothPermissionContext::GrantDevicePermission( |
| 48 const GURL& requesting_origin, |
| 49 const GURL& embedding_origin, |
| 50 const std::string& device_address) { |
| 51 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue()); |
| 52 device_dict->SetString(kDeviceAddressKey, device_address); |
| 53 device::BluetoothDevice* device = adapter_->GetDevice(device_address); |
| 54 DCHECK(device); |
| 55 device_dict->SetString(kDeviceNameKey, device->GetName()); |
| 56 scoped_ptr<base::ListValue> device_uuids_list(new base::ListValue()); |
| 57 device::BluetoothDevice::UUIDList uuid_list = device->GetUUIDs(); |
| 58 for (const auto& uuid : uuid_list) |
| 59 device_uuids_list->AppendString(uuid.value()); |
| 60 device_dict->Set(kDeviceUUIDsKey, std::move(device_uuids_list)); |
| 61 |
| 62 GrantObjectPermission(requesting_origin, embedding_origin, |
| 63 std::move(device_dict)); |
| 64 } |
| 65 |
| 66 void BluetoothPermissionContext::RevokeDevicePermission( |
| 67 const GURL& requesting_origin, |
| 68 const GURL& embedding_origin, |
| 69 const std::string& device_address) { |
| 70 std::vector<scoped_ptr<base::DictionaryValue>> device_list = |
| 71 GetGrantedObjects(requesting_origin, embedding_origin); |
| 72 const base::DictionaryValue* entry = |
| 73 FindForDevice(device_list, device_address); |
| 74 if (entry) |
| 75 RevokeObjectPermission(requesting_origin, embedding_origin, *entry); |
| 76 } |
| 77 |
| 78 std::vector<std::string> BluetoothPermissionContext::HasDevicePermission( |
| 79 const GURL& requesting_origin, |
| 80 const GURL& embedding_origin, |
| 81 const std::string& device_address) { |
| 82 std::vector<scoped_ptr<base::DictionaryValue>> device_list = |
| 83 GetGrantedObjects(requesting_origin, embedding_origin); |
| 84 std::vector<std::string> uuids; |
| 85 const base::DictionaryValue* device_dict = |
| 86 FindForDevice(device_list, device_address); |
| 87 if (device_dict) { |
| 88 const base::ListValue* uuid_list = nullptr; |
| 89 device_dict->GetList(kDeviceUUIDsKey, &uuid_list); |
| 90 size_t length = uuid_list->GetSize(); |
| 91 for (size_t i = 0; i < length; ++i) { |
| 92 std::string uuid; |
| 93 uuid_list->GetString(i, &uuid); |
| 94 uuids.push_back(uuid); |
| 95 } |
| 96 } |
| 97 return uuids; |
| 98 } |
| 99 |
| 100 void BluetoothPermissionContext::OnGetAdapter( |
| 101 scoped_refptr<device::BluetoothAdapter> adapter) { |
| 102 DCHECK(!adapter_.get()); |
| 103 adapter_ = adapter; |
| 104 DCHECK(adapter_.get()); |
| 105 } |
| 106 |
| 107 bool BluetoothPermissionContext::IsValidObject( |
| 108 const base::DictionaryValue& object) { |
| 109 std::string device_address, device_name; |
| 110 const base::ListValue* device_uuids = nullptr; |
| 111 return object.size() == 3 && object.HasKey(kDeviceAddressKey) && |
| 112 object.GetString(kDeviceAddressKey, &device_address) && |
| 113 object.HasKey(kDeviceNameKey) && |
| 114 object.GetString(kDeviceNameKey, &device_name) && |
| 115 object.HasKey(kDeviceUUIDsKey) && |
| 116 object.GetList(kDeviceUUIDsKey, &device_uuids); |
| 117 } |
OLD | NEW |