Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / extensions / common / api / bluetooth / bluetooth_manifest_permission.cc
blobd56d2c15c2ac20c419fe4a98675fb543cc04ac2d
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/manifest_constants.h"
16 #include "grit/extensions_strings.h"
17 #include "ipc/ipc_message.h"
18 #include "ui/base/l10n/l10n_util.h"
20 namespace extensions {
22 namespace bluetooth_errors {
23 const char kErrorInvalidUuid[] = "Invalid UUID '*'";
26 namespace errors = bluetooth_errors;
28 namespace {
30 bool ParseUuid(BluetoothManifestPermission* permission,
31 const std::string& uuid,
32 base::string16* error) {
33 device::BluetoothUUID bt_uuid(uuid);
34 if (!bt_uuid.IsValid()) {
35 *error = ErrorUtils::FormatErrorMessageUTF16(
36 errors::kErrorInvalidUuid, uuid);
37 return false;
39 permission->AddPermission(uuid);
40 return true;
43 bool ParseUuidArray(BluetoothManifestPermission* permission,
44 const scoped_ptr<std::vector<std::string> >& uuids,
45 base::string16* error) {
46 for (std::vector<std::string>::const_iterator it = uuids->begin();
47 it != uuids->end();
48 ++it) {
49 if (!ParseUuid(permission, *it, error)) {
50 return false;
53 return true;
56 } // namespace
58 BluetoothManifestPermission::BluetoothManifestPermission()
59 : socket_(false),
60 low_energy_(false) {}
62 BluetoothManifestPermission::~BluetoothManifestPermission() {}
64 // static
65 scoped_ptr<BluetoothManifestPermission> BluetoothManifestPermission::FromValue(
66 const base::Value& value,
67 base::string16* error) {
68 scoped_ptr<core_api::extensions_manifest_types::Bluetooth> bluetooth =
69 core_api::extensions_manifest_types::Bluetooth::FromValue(value, error);
70 if (!bluetooth)
71 return scoped_ptr<BluetoothManifestPermission>();
73 scoped_ptr<BluetoothManifestPermission> result(
74 new BluetoothManifestPermission());
75 if (bluetooth->uuids) {
76 if (!ParseUuidArray(result.get(), bluetooth->uuids, error)) {
77 return scoped_ptr<BluetoothManifestPermission>();
80 if (bluetooth->socket) {
81 result->socket_ = *(bluetooth->socket);
83 if (bluetooth->low_energy) {
84 result->low_energy_ = *(bluetooth->low_energy);
86 return result.Pass();
89 bool BluetoothManifestPermission::CheckRequest(
90 const Extension* extension,
91 const BluetoothPermissionRequest& request) const {
93 device::BluetoothUUID param_uuid(request.uuid);
94 for (BluetoothUuidSet::const_iterator it = uuids_.begin();
95 it != uuids_.end();
96 ++it) {
97 device::BluetoothUUID uuid(*it);
98 if (param_uuid == uuid)
99 return true;
101 return false;
104 bool BluetoothManifestPermission::CheckSocketPermitted(
105 const Extension* extension) const {
106 return socket_;
109 bool BluetoothManifestPermission::CheckLowEnergyPermitted(
110 const Extension* extension) const {
111 return low_energy_;
114 std::string BluetoothManifestPermission::name() const {
115 return manifest_keys::kBluetooth;
118 std::string BluetoothManifestPermission::id() const { return name(); }
120 bool BluetoothManifestPermission::HasMessages() const { return true; }
122 PermissionMessages BluetoothManifestPermission::GetMessages() const {
123 DCHECK(HasMessages());
124 PermissionMessages result;
126 result.push_back(PermissionMessage(
127 PermissionMessage::kBluetooth,
128 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH)));
130 if (!uuids_.empty()) {
131 result.push_back(
132 PermissionMessage(PermissionMessage::kBluetoothDevices,
133 l10n_util::GetStringUTF16(
134 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICES)));
137 return result;
140 bool BluetoothManifestPermission::FromValue(const base::Value* value) {
141 if (!value)
142 return false;
143 base::string16 error;
144 scoped_ptr<BluetoothManifestPermission> manifest_permission(
145 BluetoothManifestPermission::FromValue(*value, &error));
147 if (!manifest_permission)
148 return false;
150 uuids_ = manifest_permission->uuids_;
151 return true;
154 scoped_ptr<base::Value> BluetoothManifestPermission::ToValue() const {
155 core_api::extensions_manifest_types::Bluetooth bluetooth;
156 bluetooth.uuids.reset(new std::vector<std::string>(uuids_.begin(),
157 uuids_.end()));
158 return bluetooth.ToValue().Pass();
161 ManifestPermission* BluetoothManifestPermission::Diff(
162 const ManifestPermission* rhs) const {
163 const BluetoothManifestPermission* other =
164 static_cast<const BluetoothManifestPermission*>(rhs);
166 scoped_ptr<BluetoothManifestPermission> result(
167 new BluetoothManifestPermission());
168 result->uuids_ = base::STLSetDifference<BluetoothUuidSet>(
169 uuids_, other->uuids_);
170 return result.release();
173 ManifestPermission* BluetoothManifestPermission::Union(
174 const ManifestPermission* rhs) const {
175 const BluetoothManifestPermission* other =
176 static_cast<const BluetoothManifestPermission*>(rhs);
178 scoped_ptr<BluetoothManifestPermission> result(
179 new BluetoothManifestPermission());
180 result->uuids_ = base::STLSetUnion<BluetoothUuidSet>(
181 uuids_, other->uuids_);
182 return result.release();
185 ManifestPermission* BluetoothManifestPermission::Intersect(
186 const ManifestPermission* rhs) const {
187 const BluetoothManifestPermission* other =
188 static_cast<const BluetoothManifestPermission*>(rhs);
190 scoped_ptr<BluetoothManifestPermission> result(
191 new BluetoothManifestPermission());
192 result->uuids_ = base::STLSetIntersection<BluetoothUuidSet>(
193 uuids_, other->uuids_);
194 return result.release();
197 void BluetoothManifestPermission::AddPermission(const std::string& uuid) {
198 uuids_.insert(uuid);
201 } // namespace extensions