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_utils.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 kErrorInvalidProfileUuid
[] = "Invalid UUID '*'";
27 namespace errors
= bluetooth_errors
;
31 bool ParseUuid(BluetoothManifestPermission
* permission
,
32 const std::string
& profile_uuid
,
33 base::string16
* error
) {
34 std::string canonical_uuid
=
35 device::bluetooth_utils::CanonicalUuid(profile_uuid
);
36 if (canonical_uuid
.empty()) {
37 *error
= ErrorUtils::FormatErrorMessageUTF16(
38 errors::kErrorInvalidProfileUuid
, profile_uuid
);
41 permission
->AddPermission(profile_uuid
);
45 bool ParseUuidArray(BluetoothManifestPermission
* permission
,
46 const scoped_ptr
<std::vector
<std::string
> >& profiles
,
47 base::string16
* error
) {
48 for (std::vector
<std::string
>::const_iterator it
= profiles
->begin();
49 it
!= profiles
->end();
51 if (!ParseUuid(permission
, *it
, error
)) {
60 BluetoothManifestPermission::BluetoothManifestPermission() {}
62 BluetoothManifestPermission::~BluetoothManifestPermission() {}
65 scoped_ptr
<BluetoothManifestPermission
> BluetoothManifestPermission::FromValue(
66 const base::Value
& value
,
67 base::string16
* error
) {
68 scoped_ptr
<api::manifest_types::Bluetooth
> bluetooth
=
69 api::manifest_types::Bluetooth::FromValue(value
, error
);
71 return scoped_ptr
<BluetoothManifestPermission
>();
73 scoped_ptr
<BluetoothManifestPermission
> result(
74 new BluetoothManifestPermission());
75 if (bluetooth
->profiles
) {
76 if (!ParseUuidArray(result
.get(), bluetooth
->profiles
, error
)) {
77 return scoped_ptr
<BluetoothManifestPermission
>();
83 bool BluetoothManifestPermission::CheckRequest(
84 const Extension
* extension
,
85 const BluetoothPermissionRequest
& request
) const {
87 std::string canonical_param_uuid
=
88 device::bluetooth_utils::CanonicalUuid(request
.profile_uuid
);
89 for (BluetoothProfileUuidSet::const_iterator it
= profile_uuids_
.begin();
90 it
!= profile_uuids_
.end();
92 std::string canonical_uuid
= device::bluetooth_utils::CanonicalUuid(*it
);
93 if (canonical_uuid
== canonical_param_uuid
)
99 std::string
BluetoothManifestPermission::name() const {
100 return manifest_keys::kBluetooth
;
103 std::string
BluetoothManifestPermission::id() const { return name(); }
105 bool BluetoothManifestPermission::HasMessages() const { return true; }
107 PermissionMessages
BluetoothManifestPermission::GetMessages() const {
108 DCHECK(HasMessages());
109 PermissionMessages result
;
111 result
.push_back(PermissionMessage(
112 PermissionMessage::kBluetooth
,
113 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH
)));
115 if (!profile_uuids_
.empty()) {
117 PermissionMessage(PermissionMessage::kBluetoothDevices
,
118 l10n_util::GetStringUTF16(
119 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICES
)));
125 bool BluetoothManifestPermission::FromValue(const base::Value
* value
) {
128 base::string16 error
;
129 scoped_ptr
<BluetoothManifestPermission
> manifest_permission(
130 BluetoothManifestPermission::FromValue(*value
, &error
));
132 if (!manifest_permission
)
135 profile_uuids_
= manifest_permission
->profile_uuids_
;
139 scoped_ptr
<base::Value
> BluetoothManifestPermission::ToValue() const {
140 api::manifest_types::Bluetooth bluetooth
;
141 bluetooth
.profiles
.reset(new std::vector
<std::string
>(profile_uuids_
.begin(),
142 profile_uuids_
.end()));
143 return bluetooth
.ToValue().PassAs
<base::Value
>();
146 ManifestPermission
* BluetoothManifestPermission::Clone() const {
147 scoped_ptr
<BluetoothManifestPermission
> result(
148 new BluetoothManifestPermission());
149 result
->profile_uuids_
= profile_uuids_
;
150 return result
.release();
153 ManifestPermission
* BluetoothManifestPermission::Diff(
154 const ManifestPermission
* rhs
) const {
155 const BluetoothManifestPermission
* other
=
156 static_cast<const BluetoothManifestPermission
*>(rhs
);
158 scoped_ptr
<BluetoothManifestPermission
> result(
159 new BluetoothManifestPermission());
160 result
->profile_uuids_
= base::STLSetDifference
<BluetoothProfileUuidSet
>(
161 profile_uuids_
, other
->profile_uuids_
);
162 return result
.release();
165 ManifestPermission
* BluetoothManifestPermission::Union(
166 const ManifestPermission
* rhs
) const {
167 const BluetoothManifestPermission
* other
=
168 static_cast<const BluetoothManifestPermission
*>(rhs
);
170 scoped_ptr
<BluetoothManifestPermission
> result(
171 new BluetoothManifestPermission());
172 result
->profile_uuids_
= base::STLSetUnion
<BluetoothProfileUuidSet
>(
173 profile_uuids_
, other
->profile_uuids_
);
174 return result
.release();
177 ManifestPermission
* BluetoothManifestPermission::Intersect(
178 const ManifestPermission
* rhs
) const {
179 const BluetoothManifestPermission
* other
=
180 static_cast<const BluetoothManifestPermission
*>(rhs
);
182 scoped_ptr
<BluetoothManifestPermission
> result(
183 new BluetoothManifestPermission());
184 result
->profile_uuids_
= base::STLSetIntersection
<BluetoothProfileUuidSet
>(
185 profile_uuids_
, other
->profile_uuids_
);
186 return result
.release();
189 bool BluetoothManifestPermission::Contains(const ManifestPermission
* rhs
)
191 const BluetoothManifestPermission
* other
=
192 static_cast<const BluetoothManifestPermission
*>(rhs
);
194 return base::STLIncludes(profile_uuids_
, other
->profile_uuids_
);
197 bool BluetoothManifestPermission::Equal(const ManifestPermission
* rhs
) const {
198 const BluetoothManifestPermission
* other
=
199 static_cast<const BluetoothManifestPermission
*>(rhs
);
201 return (profile_uuids_
== other
->profile_uuids_
);
204 void BluetoothManifestPermission::Write(IPC::Message
* m
) const {
205 IPC::WriteParam(m
, profile_uuids_
);
208 bool BluetoothManifestPermission::Read(const IPC::Message
* m
,
209 PickleIterator
* iter
) {
210 return IPC::ReadParam(m
, iter
, &profile_uuids_
);
213 void BluetoothManifestPermission::Log(std::string
* log
) const {
214 IPC::LogParam(profile_uuids_
, log
);
217 void BluetoothManifestPermission::AddPermission(
218 const std::string
& profile_uuid
) {
219 profile_uuids_
.insert(profile_uuid
);
222 } // namespace extensions