Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / chrome / common / extensions / api / bluetooth / bluetooth_manifest_permission.cc
blobc5ead3c52a083df49a099e71762512a5e0a8b24a
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;
29 namespace {
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);
38 return false;
40 permission->AddPermission(uuid);
41 return true;
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();
48 it != uuids->end();
49 ++it) {
50 if (!ParseUuid(permission, *it, error)) {
51 return false;
54 return true;
57 } // namespace
59 BluetoothManifestPermission::BluetoothManifestPermission() {}
61 BluetoothManifestPermission::~BluetoothManifestPermission() {}
63 // static
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);
69 if (!bluetooth)
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>();
79 return result.Pass();
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();
88 it != uuids_.end();
89 ++it) {
90 device::BluetoothUUID uuid(*it);
91 if (param_uuid == uuid)
92 return true;
94 return false;
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()) {
114 result.push_back(
115 PermissionMessage(PermissionMessage::kBluetoothDevices,
116 l10n_util::GetStringUTF16(
117 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICES)));
120 return result;
123 bool BluetoothManifestPermission::FromValue(const base::Value* value) {
124 if (!value)
125 return false;
126 base::string16 error;
127 scoped_ptr<BluetoothManifestPermission> manifest_permission(
128 BluetoothManifestPermission::FromValue(*value, &error));
130 if (!manifest_permission)
131 return false;
133 uuids_ = manifest_permission->uuids_;
134 return true;
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(),
140 uuids_.end()));
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)
188 const {
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) {
216 uuids_.insert(uuid);
219 } // namespace extensions