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 "chrome/common/extensions/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 "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h"
12 #include "chrome/common/extensions/api/manifest_types.h"
13 #include "device/bluetooth/bluetooth_uuid.h"
14 #include "extensions/common/error_utils.h"
15 #include "extensions/common/extension_messages.h"
16 #include "extensions/common/manifest_constants.h"
17 #include "grit/generated_resources.h"
18 #include "ipc/ipc_message.h"
19 #include "ui/base/l10n/l10n_util.h"
21 namespace extensions
{
23 namespace bluetooth_errors
{
24 const char kErrorInvalidUuid
[] = "Invalid UUID '*'";
27 namespace errors
= bluetooth_errors
;
31 bool ParseUuid(BluetoothManifestPermission
* permission
,
32 const std::string
& uuid
,
33 base::string16
* error
) {
34 device::BluetoothUUID
bt_uuid(uuid
);
35 if (!bt_uuid
.IsValid()) {
36 *error
= ErrorUtils::FormatErrorMessageUTF16(
37 errors::kErrorInvalidUuid
, uuid
);
40 permission
->AddPermission(uuid
);
44 bool ParseUuidArray(BluetoothManifestPermission
* permission
,
45 const scoped_ptr
<std::vector
<std::string
> >& uuids
,
46 base::string16
* error
) {
47 for (std::vector
<std::string
>::const_iterator it
= uuids
->begin();
50 if (!ParseUuid(permission
, *it
, error
)) {
59 BluetoothManifestPermission::BluetoothManifestPermission() {}
61 BluetoothManifestPermission::~BluetoothManifestPermission() {}
64 scoped_ptr
<BluetoothManifestPermission
> BluetoothManifestPermission::FromValue(
65 const base::Value
& value
,
66 base::string16
* error
) {
67 scoped_ptr
<api::manifest_types::Bluetooth
> bluetooth
=
68 api::manifest_types::Bluetooth::FromValue(value
, error
);
70 return scoped_ptr
<BluetoothManifestPermission
>();
72 scoped_ptr
<BluetoothManifestPermission
> result(
73 new BluetoothManifestPermission());
74 if (bluetooth
->uuids
) {
75 if (!ParseUuidArray(result
.get(), bluetooth
->uuids
, error
)) {
76 return scoped_ptr
<BluetoothManifestPermission
>();
82 bool BluetoothManifestPermission::CheckRequest(
83 const Extension
* extension
,
84 const BluetoothPermissionRequest
& request
) const {
86 device::BluetoothUUID
param_uuid(request
.uuid
);
87 for (BluetoothUuidSet::const_iterator it
= uuids_
.begin();
90 device::BluetoothUUID
uuid(*it
);
91 if (param_uuid
== uuid
)
97 std::string
BluetoothManifestPermission::name() const {
98 return manifest_keys::kBluetooth
;
101 std::string
BluetoothManifestPermission::id() const { return name(); }
103 bool BluetoothManifestPermission::HasMessages() const { return true; }
105 PermissionMessages
BluetoothManifestPermission::GetMessages() const {
106 DCHECK(HasMessages());
107 PermissionMessages result
;
109 result
.push_back(PermissionMessage(
110 PermissionMessage::kBluetooth
,
111 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH
)));
113 if (!uuids_
.empty()) {
115 PermissionMessage(PermissionMessage::kBluetoothDevices
,
116 l10n_util::GetStringUTF16(
117 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICES
)));
123 bool BluetoothManifestPermission::FromValue(const base::Value
* value
) {
126 base::string16 error
;
127 scoped_ptr
<BluetoothManifestPermission
> manifest_permission(
128 BluetoothManifestPermission::FromValue(*value
, &error
));
130 if (!manifest_permission
)
133 uuids_
= manifest_permission
->uuids_
;
137 scoped_ptr
<base::Value
> BluetoothManifestPermission::ToValue() const {
138 api::manifest_types::Bluetooth bluetooth
;
139 bluetooth
.uuids
.reset(new std::vector
<std::string
>(uuids_
.begin(),
141 return bluetooth
.ToValue().PassAs
<base::Value
>();
144 ManifestPermission
* BluetoothManifestPermission::Clone() const {
145 scoped_ptr
<BluetoothManifestPermission
> result(
146 new BluetoothManifestPermission());
147 result
->uuids_
= uuids_
;
148 return result
.release();
151 ManifestPermission
* BluetoothManifestPermission::Diff(
152 const ManifestPermission
* rhs
) const {
153 const BluetoothManifestPermission
* other
=
154 static_cast<const BluetoothManifestPermission
*>(rhs
);
156 scoped_ptr
<BluetoothManifestPermission
> result(
157 new BluetoothManifestPermission());
158 result
->uuids_
= base::STLSetDifference
<BluetoothUuidSet
>(
159 uuids_
, other
->uuids_
);
160 return result
.release();
163 ManifestPermission
* BluetoothManifestPermission::Union(
164 const ManifestPermission
* rhs
) const {
165 const BluetoothManifestPermission
* other
=
166 static_cast<const BluetoothManifestPermission
*>(rhs
);
168 scoped_ptr
<BluetoothManifestPermission
> result(
169 new BluetoothManifestPermission());
170 result
->uuids_
= base::STLSetUnion
<BluetoothUuidSet
>(
171 uuids_
, other
->uuids_
);
172 return result
.release();
175 ManifestPermission
* BluetoothManifestPermission::Intersect(
176 const ManifestPermission
* rhs
) const {
177 const BluetoothManifestPermission
* other
=
178 static_cast<const BluetoothManifestPermission
*>(rhs
);
180 scoped_ptr
<BluetoothManifestPermission
> result(
181 new BluetoothManifestPermission());
182 result
->uuids_
= base::STLSetIntersection
<BluetoothUuidSet
>(
183 uuids_
, other
->uuids_
);
184 return result
.release();
187 bool BluetoothManifestPermission::Contains(const ManifestPermission
* rhs
)
189 const BluetoothManifestPermission
* other
=
190 static_cast<const BluetoothManifestPermission
*>(rhs
);
192 return base::STLIncludes(uuids_
, other
->uuids_
);
195 bool BluetoothManifestPermission::Equal(const ManifestPermission
* rhs
) const {
196 const BluetoothManifestPermission
* other
=
197 static_cast<const BluetoothManifestPermission
*>(rhs
);
199 return (uuids_
== other
->uuids_
);
202 void BluetoothManifestPermission::Write(IPC::Message
* m
) const {
203 IPC::WriteParam(m
, uuids_
);
206 bool BluetoothManifestPermission::Read(const IPC::Message
* m
,
207 PickleIterator
* iter
) {
208 return IPC::ReadParam(m
, iter
, &uuids_
);
211 void BluetoothManifestPermission::Log(std::string
* log
) const {
212 IPC::LogParam(uuids_
, log
);
215 void BluetoothManifestPermission::AddPermission(const std::string
& uuid
) {
219 } // namespace extensions