Chromium Code Reviews| 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 #include "url/gurl.h" | |
| 16 #include "url/origin.h" | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 const char kDeviceAddressKey[] = "device-address"; | |
| 21 const char kDeviceNameKey[] = "device-name"; | |
| 22 const char kDeviceUUIDsKey[] = "device-uuids"; | |
| 23 | |
| 24 const base::DictionaryValue* FindForDevice( | |
| 25 const std::vector<scoped_ptr<base::DictionaryValue>>& device_list, | |
| 26 const std::string& device_address) { | |
| 27 for (const scoped_ptr<base::DictionaryValue>& device_dict : device_list) { | |
| 28 std::string tmp_device_address; | |
| 29 if (device_dict->GetString(kDeviceAddressKey, &tmp_device_address) && | |
| 30 tmp_device_address == device_address) { | |
| 31 return device_dict.get(); | |
| 32 } | |
| 33 } | |
| 34 return nullptr; | |
| 35 } | |
| 36 | |
| 37 } // namespace | |
| 38 | |
| 39 BluetoothPermissionContext::BluetoothPermissionContext(Profile* profile) | |
| 40 : ChooserContextBase(profile, CONTENT_SETTINGS_TYPE_BLUETOOTH_CHOOSER_DATA), | |
| 41 weak_ptr_factory_(this) { | |
| 42 device::BluetoothAdapterFactory::GetAdapter( | |
| 43 base::Bind(&BluetoothPermissionContext::OnGetAdapter, | |
| 44 weak_ptr_factory_.GetWeakPtr())); | |
| 45 } | |
| 46 | |
| 47 BluetoothPermissionContext::~BluetoothPermissionContext() {} | |
| 48 | |
| 49 void BluetoothPermissionContext::GrantDevicePermission( | |
| 50 const url::Origin& requesting_origin, | |
| 51 const url::Origin& embedding_origin, | |
| 52 const std::string& device_address) { | |
| 53 scoped_ptr<base::DictionaryValue> device_dict(new base::DictionaryValue()); | |
| 54 device_dict->SetString(kDeviceAddressKey, device_address); | |
| 55 device::BluetoothDevice* device = adapter_->GetDevice(device_address); | |
| 56 DCHECK(device); | |
| 57 device_dict->SetString(kDeviceNameKey, device->GetName()); | |
| 58 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.
| |
| 59 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.
| |
| 60 for (const auto& uuid : uuid_list) | |
| 61 device_uuids_list->AppendString(uuid.value()); | |
| 62 device_dict->Set(kDeviceUUIDsKey, std::move(device_uuids_list)); | |
| 63 | |
| 64 GrantObjectPermission(GURL(requesting_origin.Serialize()), | |
| 65 GURL(embedding_origin.Serialize()), | |
| 66 std::move(device_dict)); | |
| 67 } | |
| 68 | |
| 69 void BluetoothPermissionContext::RevokeDevicePermission( | |
| 70 const url::Origin& requesting_origin, | |
| 71 const url::Origin& embedding_origin, | |
| 72 const std::string& device_address) { | |
| 73 std::vector<scoped_ptr<base::DictionaryValue>> device_list = | |
| 74 GetGrantedObjects(GURL(requesting_origin.Serialize()), | |
| 75 GURL(embedding_origin.Serialize())); | |
| 76 const base::DictionaryValue* entry = | |
| 77 FindForDevice(device_list, device_address); | |
| 78 if (entry) | |
| 79 RevokeObjectPermission(GURL(requesting_origin.Serialize()), | |
| 80 GURL(embedding_origin.Serialize()), *entry); | |
| 81 } | |
| 82 | |
| 83 std::vector<std::string> BluetoothPermissionContext::HasDevicePermission( | |
| 84 const url::Origin& requesting_origin, | |
| 85 const url::Origin& embedding_origin, | |
| 86 const std::string& device_address) { | |
| 87 std::vector<scoped_ptr<base::DictionaryValue>> device_list = | |
| 88 GetGrantedObjects(GURL(requesting_origin.Serialize()), | |
| 89 GURL(embedding_origin.Serialize())); | |
| 90 std::vector<std::string> uuids; | |
| 91 const base::DictionaryValue* device_dict = | |
| 92 FindForDevice(device_list, device_address); | |
| 93 if (device_dict) { | |
| 94 const base::ListValue* uuid_list = nullptr; | |
| 95 device_dict->GetList(kDeviceUUIDsKey, &uuid_list); | |
| 96 size_t length = uuid_list->GetSize(); | |
| 97 for (size_t i = 0; i < length; ++i) { | |
| 98 std::string uuid; | |
| 99 uuid_list->GetString(i, &uuid); | |
| 100 uuids.push_back(uuid); | |
| 101 } | |
| 102 } | |
| 103 return uuids; | |
| 104 } | |
| 105 | |
| 106 void BluetoothPermissionContext::OnGetAdapter( | |
| 107 scoped_refptr<device::BluetoothAdapter> adapter) { | |
| 108 DCHECK(!adapter_.get()); | |
| 109 adapter_ = adapter; | |
| 110 DCHECK(adapter_.get()); | |
| 111 } | |
| 112 | |
| 113 bool BluetoothPermissionContext::IsValidObject( | |
| 114 const base::DictionaryValue& object) { | |
| 115 std::string device_address, device_name; | |
| 116 const base::ListValue* device_uuids = nullptr; | |
| 117 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.
| |
| 118 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.
| |
| 119 object.HasKey(kDeviceNameKey) && | |
| 120 object.GetString(kDeviceNameKey, &device_name) && | |
| 121 object.HasKey(kDeviceUUIDsKey) && | |
| 122 object.GetList(kDeviceUUIDsKey, &device_uuids); | |
| 123 } | |
| OLD | NEW |