Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / extensions / common / api / bluetooth / bluetooth_manifest_permission.cc
blob9b2e7a34a4bb774efc8ff2f0bfabc0398b4793a1
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;
30 namespace {
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);
39 return false;
41 permission->AddPermission(uuid);
42 return true;
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();
49 it != uuids->end();
50 ++it) {
51 if (!ParseUuid(permission, *it, error)) {
52 return false;
55 return true;
58 } // namespace
60 BluetoothManifestPermission::BluetoothManifestPermission()
61 : socket_(false), low_energy_(false), peripheral_(false) {
64 BluetoothManifestPermission::~BluetoothManifestPermission() {}
66 // static
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);
72 if (!bluetooth)
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);
91 return result.Pass();
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();
100 it != uuids_.end();
101 ++it) {
102 device::BluetoothUUID uuid(*it);
103 if (param_uuid == uuid)
104 return true;
106 return false;
109 bool BluetoothManifestPermission::CheckSocketPermitted(
110 const Extension* extension) const {
111 return socket_;
114 bool BluetoothManifestPermission::CheckLowEnergyPermitted(
115 const Extension* extension) const {
116 return low_energy_;
119 bool BluetoothManifestPermission::CheckPeripheralPermitted(
120 const Extension* extension) const {
121 if (!FeatureProvider::GetBehaviorFeature(
122 BehaviorFeature::kBluetoothPeripheral)
123 ->IsAvailableToExtension(extension)
124 .is_available())
125 return false;
126 return peripheral_;
129 std::string BluetoothManifestPermission::name() const {
130 return manifest_keys::kBluetooth;
133 std::string BluetoothManifestPermission::id() const { return name(); }
135 PermissionIDSet BluetoothManifestPermission::GetPermissions() const {
136 PermissionIDSet permissions;
137 permissions.insert(APIPermission::kBluetooth);
138 if (!uuids_.empty()) {
139 permissions.insert(APIPermission::kBluetoothDevices);
141 return permissions;
144 bool BluetoothManifestPermission::HasMessages() const { return true; }
146 PermissionMessages BluetoothManifestPermission::GetMessages() const {
147 // When modifying this function, be careful to also modify GetPermissions()
148 // above to have the same functionality.
149 DCHECK(HasMessages());
150 PermissionMessages result;
152 result.push_back(PermissionMessage(
153 PermissionMessage::kBluetooth,
154 l10n_util::GetStringUTF16(IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH)));
156 if (!uuids_.empty()) {
157 result.push_back(
158 PermissionMessage(PermissionMessage::kBluetoothDevices,
159 l10n_util::GetStringUTF16(
160 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICES)));
163 return result;
166 bool BluetoothManifestPermission::FromValue(const base::Value* value) {
167 if (!value)
168 return false;
169 base::string16 error;
170 scoped_ptr<BluetoothManifestPermission> manifest_permission(
171 BluetoothManifestPermission::FromValue(*value, &error));
173 if (!manifest_permission)
174 return false;
176 uuids_ = manifest_permission->uuids_;
177 return true;
180 scoped_ptr<base::Value> BluetoothManifestPermission::ToValue() const {
181 api::extensions_manifest_types::Bluetooth bluetooth;
182 bluetooth.uuids.reset(new std::vector<std::string>(uuids_.begin(),
183 uuids_.end()));
184 return bluetooth.ToValue().Pass();
187 ManifestPermission* BluetoothManifestPermission::Diff(
188 const ManifestPermission* rhs) const {
189 const BluetoothManifestPermission* other =
190 static_cast<const BluetoothManifestPermission*>(rhs);
192 scoped_ptr<BluetoothManifestPermission> result(
193 new BluetoothManifestPermission());
194 result->uuids_ = base::STLSetDifference<BluetoothUuidSet>(
195 uuids_, other->uuids_);
196 return result.release();
199 ManifestPermission* BluetoothManifestPermission::Union(
200 const ManifestPermission* rhs) const {
201 const BluetoothManifestPermission* other =
202 static_cast<const BluetoothManifestPermission*>(rhs);
204 scoped_ptr<BluetoothManifestPermission> result(
205 new BluetoothManifestPermission());
206 result->uuids_ = base::STLSetUnion<BluetoothUuidSet>(
207 uuids_, other->uuids_);
208 return result.release();
211 ManifestPermission* BluetoothManifestPermission::Intersect(
212 const ManifestPermission* rhs) const {
213 const BluetoothManifestPermission* other =
214 static_cast<const BluetoothManifestPermission*>(rhs);
216 scoped_ptr<BluetoothManifestPermission> result(
217 new BluetoothManifestPermission());
218 result->uuids_ = base::STLSetIntersection<BluetoothUuidSet>(
219 uuids_, other->uuids_);
220 return result.release();
223 void BluetoothManifestPermission::AddPermission(const std::string& uuid) {
224 uuids_.insert(uuid);
227 } // namespace extensions