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 "chrome/grit/generated_resources.h"
14 #include "device/bluetooth/bluetooth_uuid.h"
15 #include "extensions/common/error_utils.h"
16 #include "extensions/common/extension_messages.h"
17 #include "extensions/common/manifest_constants.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()
63 BluetoothManifestPermission::~BluetoothManifestPermission() {}
66 scoped_ptr<BluetoothManifestPermission> BluetoothManifestPermission::FromValue(
67 const base::Value& value,
68 base::string16* error) {
69 scoped_ptr<api::manifest_types::Bluetooth> bluetooth =
70 api::manifest_types::Bluetooth::FromValue(value, error);
72 return scoped_ptr<BluetoothManifestPermission>();
74 scoped_ptr<BluetoothManifestPermission> result(
75 new BluetoothManifestPermission());
76 if (bluetooth->uuids) {
77 if (!ParseUuidArray(result.get(), bluetooth->uuids, error)) {
78 return scoped_ptr<BluetoothManifestPermission>();
81 if (bluetooth->socket) {
82 result->socket_ = *(bluetooth->socket);
84 if (bluetooth->low_energy) {
85 result->low_energy_ = *(bluetooth->low_energy);
90 bool BluetoothManifestPermission::CheckRequest(
91 const Extension* extension,
92 const BluetoothPermissionRequest& request) const {
94 device::BluetoothUUID param_uuid(request.uuid);
95 for (BluetoothUuidSet::const_iterator it = uuids_.begin();
98 device::BluetoothUUID uuid(*it);
99 if (param_uuid == uuid)
105 bool BluetoothManifestPermission::CheckSocketPermitted(
106 const Extension* extension) const {
110 bool BluetoothManifestPermission::CheckLowEnergyPermitted(
111 const Extension* extension) const {
115 std::string BluetoothManifestPermission::name() const {
116 return manifest_keys::kBluetooth;
119 std::string BluetoothManifestPermission::id() const { return name(); }
121 bool BluetoothManifestPermission::HasMessages() const { return true; }
123 PermissionMessages BluetoothManifestPermission::GetMessages() const {
124 DCHECK(HasMessages());
125 PermissionMessages result;
127 result.push_back(PermissionMessage(
128 PermissionMessage::kBluetooth,
129 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH)));
131 if (!uuids_.empty()) {
133 PermissionMessage(PermissionMessage::kBluetoothDevices,
134 l10n_util::GetStringUTF16(
135 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICES)));
141 bool BluetoothManifestPermission::FromValue(const base::Value* value) {
144 base::string16 error;
145 scoped_ptr<BluetoothManifestPermission> manifest_permission(
146 BluetoothManifestPermission::FromValue(*value, &error));
148 if (!manifest_permission)
151 uuids_ = manifest_permission->uuids_;
155 scoped_ptr<base::Value> BluetoothManifestPermission::ToValue() const {
156 api::manifest_types::Bluetooth bluetooth;
157 bluetooth.uuids.reset(new std::vector<std::string>(uuids_.begin(),
159 return bluetooth.ToValue().PassAs<base::Value>();
162 ManifestPermission* BluetoothManifestPermission::Diff(
163 const ManifestPermission* rhs) const {
164 const BluetoothManifestPermission* other =
165 static_cast<const BluetoothManifestPermission*>(rhs);
167 scoped_ptr<BluetoothManifestPermission> result(
168 new BluetoothManifestPermission());
169 result->uuids_ = base::STLSetDifference<BluetoothUuidSet>(
170 uuids_, other->uuids_);
171 return result.release();
174 ManifestPermission* BluetoothManifestPermission::Union(
175 const ManifestPermission* rhs) const {
176 const BluetoothManifestPermission* other =
177 static_cast<const BluetoothManifestPermission*>(rhs);
179 scoped_ptr<BluetoothManifestPermission> result(
180 new BluetoothManifestPermission());
181 result->uuids_ = base::STLSetUnion<BluetoothUuidSet>(
182 uuids_, other->uuids_);
183 return result.release();
186 ManifestPermission* BluetoothManifestPermission::Intersect(
187 const ManifestPermission* rhs) const {
188 const BluetoothManifestPermission* other =
189 static_cast<const BluetoothManifestPermission*>(rhs);
191 scoped_ptr<BluetoothManifestPermission> result(
192 new BluetoothManifestPermission());
193 result->uuids_ = base::STLSetIntersection<BluetoothUuidSet>(
194 uuids_, other->uuids_);
195 return result.release();
198 void BluetoothManifestPermission::AddPermission(const std::string& uuid) {
202 } // namespace extensions