1 // Copyright 2014 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.
5 #include "extensions/common/api/bluetooth/bluetooth_manifest_permission.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/stl_util.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "device/bluetooth/bluetooth_uuid.h"
12 #include "extensions/common/api/bluetooth/bluetooth_manifest_data.h"
13 #include "extensions/common/api/extensions_manifest_types.h"
14 #include "extensions/common/error_utils.h"
15 #include "extensions/common/features/behavior_feature.h"
16 #include "extensions/common/features/feature_provider.h"
17 #include "extensions/common/manifest_constants.h"
18 #include "grit/extensions_strings.h"
19 #include "ipc/ipc_message.h"
20 #include "ui/base/l10n/l10n_util.h"
22 namespace extensions
{
24 namespace bluetooth_errors
{
25 const char kErrorInvalidUuid
[] = "Invalid UUID '*'";
28 namespace errors
= bluetooth_errors
;
32 bool ParseUuid(BluetoothManifestPermission
* permission
,
33 const std::string
& uuid
,
34 base::string16
* error
) {
35 device::BluetoothUUID
bt_uuid(uuid
);
36 if (!bt_uuid
.IsValid()) {
37 *error
= ErrorUtils::FormatErrorMessageUTF16(
38 errors::kErrorInvalidUuid
, uuid
);
41 permission
->AddPermission(uuid
);
45 bool ParseUuidArray(BluetoothManifestPermission
* permission
,
46 const scoped_ptr
<std::vector
<std::string
> >& uuids
,
47 base::string16
* error
) {
48 for (std::vector
<std::string
>::const_iterator it
= uuids
->begin();
51 if (!ParseUuid(permission
, *it
, error
)) {
60 BluetoothManifestPermission::BluetoothManifestPermission()
61 : socket_(false), low_energy_(false), peripheral_(false) {
64 BluetoothManifestPermission::~BluetoothManifestPermission() {}
67 scoped_ptr
<BluetoothManifestPermission
> BluetoothManifestPermission::FromValue(
68 const base::Value
& value
,
69 base::string16
* error
) {
70 scoped_ptr
<api::extensions_manifest_types::Bluetooth
> bluetooth
=
71 api::extensions_manifest_types::Bluetooth::FromValue(value
, error
);
73 return scoped_ptr
<BluetoothManifestPermission
>();
75 scoped_ptr
<BluetoothManifestPermission
> result(
76 new BluetoothManifestPermission());
77 if (bluetooth
->uuids
) {
78 if (!ParseUuidArray(result
.get(), bluetooth
->uuids
, error
)) {
79 return scoped_ptr
<BluetoothManifestPermission
>();
82 if (bluetooth
->socket
) {
83 result
->socket_
= *(bluetooth
->socket
);
85 if (bluetooth
->low_energy
) {
86 result
->low_energy_
= *(bluetooth
->low_energy
);
88 if (bluetooth
->peripheral
) {
89 result
->peripheral_
= *(bluetooth
->peripheral
);
94 bool BluetoothManifestPermission::CheckRequest(
95 const Extension
* extension
,
96 const BluetoothPermissionRequest
& request
) const {
98 device::BluetoothUUID
param_uuid(request
.uuid
);
99 for (BluetoothUuidSet::const_iterator it
= uuids_
.begin();
102 device::BluetoothUUID
uuid(*it
);
103 if (param_uuid
== uuid
)
109 bool BluetoothManifestPermission::CheckSocketPermitted(
110 const Extension
* extension
) const {
114 bool BluetoothManifestPermission::CheckLowEnergyPermitted(
115 const Extension
* extension
) const {
119 bool BluetoothManifestPermission::CheckPeripheralPermitted(
120 const Extension
* extension
) const {
124 std::string
BluetoothManifestPermission::name() const {
125 return manifest_keys::kBluetooth
;
128 std::string
BluetoothManifestPermission::id() const { return name(); }
130 PermissionIDSet
BluetoothManifestPermission::GetPermissions() const {
131 PermissionIDSet permissions
;
132 permissions
.insert(APIPermission::kBluetooth
);
133 if (!uuids_
.empty()) {
134 permissions
.insert(APIPermission::kBluetoothDevices
);
139 bool BluetoothManifestPermission::FromValue(const base::Value
* value
) {
142 base::string16 error
;
143 scoped_ptr
<BluetoothManifestPermission
> manifest_permission(
144 BluetoothManifestPermission::FromValue(*value
, &error
));
146 if (!manifest_permission
)
149 uuids_
= manifest_permission
->uuids_
;
153 scoped_ptr
<base::Value
> BluetoothManifestPermission::ToValue() const {
154 api::extensions_manifest_types::Bluetooth bluetooth
;
155 bluetooth
.uuids
.reset(new std::vector
<std::string
>(uuids_
.begin(),
157 return bluetooth
.ToValue().Pass();
160 ManifestPermission
* BluetoothManifestPermission::Diff(
161 const ManifestPermission
* rhs
) const {
162 const BluetoothManifestPermission
* other
=
163 static_cast<const BluetoothManifestPermission
*>(rhs
);
165 scoped_ptr
<BluetoothManifestPermission
> result(
166 new BluetoothManifestPermission());
167 result
->uuids_
= base::STLSetDifference
<BluetoothUuidSet
>(
168 uuids_
, other
->uuids_
);
169 return result
.release();
172 ManifestPermission
* BluetoothManifestPermission::Union(
173 const ManifestPermission
* rhs
) const {
174 const BluetoothManifestPermission
* other
=
175 static_cast<const BluetoothManifestPermission
*>(rhs
);
177 scoped_ptr
<BluetoothManifestPermission
> result(
178 new BluetoothManifestPermission());
179 result
->uuids_
= base::STLSetUnion
<BluetoothUuidSet
>(
180 uuids_
, other
->uuids_
);
181 return result
.release();
184 ManifestPermission
* BluetoothManifestPermission::Intersect(
185 const ManifestPermission
* rhs
) const {
186 const BluetoothManifestPermission
* other
=
187 static_cast<const BluetoothManifestPermission
*>(rhs
);
189 scoped_ptr
<BluetoothManifestPermission
> result(
190 new BluetoothManifestPermission());
191 result
->uuids_
= base::STLSetIntersection
<BluetoothUuidSet
>(
192 uuids_
, other
->uuids_
);
193 return result
.release();
196 void BluetoothManifestPermission::AddPermission(const std::string
& uuid
) {
200 } // namespace extensions