OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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. | |
4 | |
5 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_permission.h " | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "base/values.h" | |
10 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" | |
11 #include "chrome/common/extensions/api/manifest_types.h" | |
12 #include "chrome/common/extensions/extension_messages.h" | |
13 #include "device/bluetooth/bluetooth_utils.h" | |
14 #include "extensions/common/error_utils.h" | |
15 #include "extensions/common/manifest_constants.h" | |
16 #include "grit/generated_resources.h" | |
17 #include "ipc/ipc_message.h" | |
18 #include "ui/base/l10n/l10n_util.h" | |
19 | |
20 namespace extensions { | |
21 | |
22 namespace bluetooth_errors { | |
23 const char kErrorInvalidProfileUuid[] = "Invalid uuid '*'"; | |
not at google - send to devlin
2014/01/23 20:29:12
UUID
rpaquay
2014/01/24 16:58:23
Done.
| |
24 } | |
25 | |
26 namespace errors = bluetooth_errors; | |
27 | |
28 namespace { | |
29 | |
30 bool ParseUuid(BluetoothManifestPermission* permission, | |
31 const std::string& profile_uuid, | |
32 base::string16* error) { | |
33 std::string canonical_uuid = | |
34 device::bluetooth_utils::CanonicalUuid(profile_uuid); | |
35 if (canonical_uuid.empty()) { | |
36 *error = ErrorUtils::FormatErrorMessageUTF16( | |
37 errors::kErrorInvalidProfileUuid, profile_uuid); | |
38 return false; | |
39 } | |
40 permission->AddPermission(profile_uuid); | |
41 return true; | |
42 } | |
43 | |
44 bool ParseUuidArray(BluetoothManifestPermission* permission, | |
45 const scoped_ptr<std::vector<std::string> >& profiles, | |
46 base::string16* error) { | |
47 for (std::vector<std::string>::const_iterator it = | |
48 profiles->begin(); it != profiles->end(); ++it) { | |
49 if (!ParseUuid(permission, *it, error)) { | |
50 return false; | |
51 } | |
52 } | |
53 return true; | |
54 } | |
55 | |
56 void SetProfileUuids(std::vector<std::string>& uuids, | |
57 BluetoothManifestPermission* permission) { | |
58 for (BluetoothManifestPermission::BluetoothProfileUuidSet::const_iterator it = | |
59 permission->profile_uuids().begin(); | |
60 it != permission->profile_uuids().end() ; ++it) { | |
61 uuids.push_back(*it); | |
62 } | |
63 } | |
64 | |
65 } // namespace | |
66 | |
67 BluetoothManifestPermission::BluetoothManifestPermission() {} | |
68 | |
69 BluetoothManifestPermission::~BluetoothManifestPermission() {} | |
70 | |
71 // static | |
72 scoped_ptr<BluetoothManifestPermission> BluetoothManifestPermission::FromValue( | |
73 const base::Value& value, | |
74 base::string16* error) { | |
75 scoped_ptr<api::manifest_types::Bluetooth> bluetooth = | |
76 api::manifest_types::Bluetooth::FromValue(value, error); | |
77 if (!bluetooth) | |
78 return scoped_ptr<BluetoothManifestPermission>(); | |
79 | |
80 scoped_ptr<BluetoothManifestPermission> result( | |
81 new BluetoothManifestPermission()); | |
82 if (bluetooth->profiles) { | |
83 if (!ParseUuidArray(result.get(), | |
84 bluetooth->profiles, | |
85 error)) { | |
86 return scoped_ptr<BluetoothManifestPermission>(); | |
87 } | |
88 } | |
89 return result.Pass(); | |
90 } | |
91 | |
92 bool BluetoothManifestPermission::CheckRequest( | |
93 const Extension* extension, | |
94 const BluetoothPermissionRequest& request) const { | |
95 | |
96 std::string canonical_param_uuid = device::bluetooth_utils::CanonicalUuid( | |
97 request.profile_uuid); | |
98 for (BluetoothProfileUuidSet::const_iterator it = profile_uuids_.begin(); | |
99 it != profile_uuids_.end(); ++it) { | |
100 | |
not at google - send to devlin
2014/01/23 20:29:12
no blank line
rpaquay
2014/01/24 16:58:23
Done.
| |
101 std::string canonical_uuid = device::bluetooth_utils::CanonicalUuid(*it); | |
102 if (canonical_uuid == canonical_param_uuid) | |
103 return true; | |
104 } | |
105 return false; | |
106 } | |
107 | |
108 std::string BluetoothManifestPermission::name() const { | |
109 return manifest_keys::kBluetooth; | |
110 } | |
111 | |
112 std::string BluetoothManifestPermission::id() const { | |
113 return name(); | |
114 } | |
115 | |
116 bool BluetoothManifestPermission::HasMessages() const { | |
117 return true; | |
118 } | |
119 | |
120 PermissionMessages BluetoothManifestPermission::GetMessages() const { | |
121 DCHECK(HasMessages()); | |
122 PermissionMessages result; | |
123 | |
124 result.push_back(PermissionMessage( | |
125 PermissionMessage::kBluetooth, | |
126 l10n_util::GetStringUTF16( | |
127 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH))); | |
not at google - send to devlin
2014/01/24 21:59:38
Ok if not for scanning what is this even for..? If
| |
128 | |
129 if (!profile_uuids_.empty()) { | |
130 result.push_back(PermissionMessage( | |
131 PermissionMessage::kBluetoothDevices, | |
meacer
2014/01/24 22:22:20
Corresponding permission in USB API is singular: k
| |
132 l10n_util::GetStringUTF16( | |
133 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICES))); | |
134 } | |
not at google - send to devlin
2014/01/23 20:29:12
We should run this by security. Showing two warnin
meacer
2014/01/23 22:25:14
Let's be consistent with the USB API. It has a gen
rpaquay
2014/01/24 00:05:39
+keybuk: Scott, a couple of questions:
1) Do you t
not at google - send to devlin
2014/01/24 21:59:38
Mustafa, what you say makes sense (install vs runt
meacer
2014/01/24 22:22:20
The app can still choose to request bluetoothDevic
| |
135 | |
136 return result; | |
137 } | |
138 | |
139 bool BluetoothManifestPermission::FromValue(const base::Value* value) { | |
140 if (!value) | |
141 return false; | |
142 base::string16 error; | |
143 scoped_ptr<BluetoothManifestPermission> manifest_permission( | |
144 BluetoothManifestPermission::FromValue(*value, &error)); | |
not at google - send to devlin
2014/01/23 20:29:12
what about the error here? Not sure what is suppos
rpaquay
2014/01/24 16:58:23
AFAIK, ultimately this method is called only when
| |
145 | |
146 if (!manifest_permission) | |
147 return false; | |
148 | |
149 profile_uuids_ = manifest_permission->profile_uuids_; | |
150 return true; | |
151 } | |
152 | |
153 scoped_ptr<base::Value> BluetoothManifestPermission::ToValue() const { | |
154 api::manifest_types::Bluetooth bluetooth; | |
155 bluetooth.profiles.reset(new std::vector<std::string>(profile_uuids_)); | |
156 return scoped_ptr<base::Value>(bluetooth.ToValue().release()).Pass(); | |
not at google - send to devlin
2014/01/23 20:29:12
use bluetooth.ToValue().PassAs<base::Value>() here
rpaquay
2014/01/24 16:58:23
Done.
not at google - send to devlin
2014/01/24 21:59:38
(Not confident in my C++ here but...) could you re
rpaquay
2014/01/25 00:48:50
Yep, done.
| |
157 } | |
158 | |
159 ManifestPermission* BluetoothManifestPermission::Clone() const { | |
160 scoped_ptr<BluetoothManifestPermission> result( | |
161 new BluetoothManifestPermission()); | |
162 result->profile_uuids_ = profile_uuids_; | |
163 return result.release(); | |
164 } | |
165 | |
166 ManifestPermission* BluetoothManifestPermission::Diff( | |
167 const ManifestPermission* rhs) const { | |
168 const BluetoothManifestPermission* other = | |
169 static_cast<const BluetoothManifestPermission*>(rhs); | |
170 | |
171 scoped_ptr<BluetoothManifestPermission> result( | |
172 new BluetoothManifestPermission()); | |
173 std::set_difference( | |
not at google - send to devlin
2014/01/23 20:29:12
Can you use https://code.google.com/p/chromium/cod
rpaquay
2014/01/24 16:58:23
Done.
| |
174 profile_uuids_.begin(), profile_uuids_.end(), | |
175 other->profile_uuids_.begin(), other->profile_uuids_.end(), | |
176 std::inserter<BluetoothProfileUuidSet>( | |
177 result->profile_uuids_, result->profile_uuids_.begin())); | |
178 return result.release(); | |
179 } | |
180 | |
181 ManifestPermission* BluetoothManifestPermission::Union( | |
182 const ManifestPermission* rhs) const { | |
183 const BluetoothManifestPermission* other = | |
184 static_cast<const BluetoothManifestPermission*>(rhs); | |
185 | |
186 scoped_ptr<BluetoothManifestPermission> result( | |
187 new BluetoothManifestPermission()); | |
188 std::set_union( | |
not at google - send to devlin
2014/01/23 20:29:12
(no equivalent for union but maybe there should be
rpaquay
2014/01/24 16:58:23
Done.
| |
189 profile_uuids_.begin(), profile_uuids_.end(), | |
190 other->profile_uuids_.begin(), other->profile_uuids_.end(), | |
191 std::inserter<BluetoothProfileUuidSet>( | |
192 result->profile_uuids_, result->profile_uuids_.begin())); | |
193 return result.release(); | |
194 } | |
195 | |
196 ManifestPermission* BluetoothManifestPermission::Intersect( | |
197 const ManifestPermission* rhs) const { | |
198 const BluetoothManifestPermission* other = | |
199 static_cast<const BluetoothManifestPermission*>(rhs); | |
200 | |
201 scoped_ptr<BluetoothManifestPermission> result( | |
202 new BluetoothManifestPermission()); | |
203 std::set_intersection( | |
204 profile_uuids_.begin(), profile_uuids_.end(), | |
205 other->profile_uuids_.begin(), other->profile_uuids_.end(), | |
206 std::inserter<BluetoothProfileUuidSet>( | |
207 result->profile_uuids_, result->profile_uuids_.begin())); | |
208 return result.release(); | |
209 } | |
210 | |
211 bool BluetoothManifestPermission::Contains(const ManifestPermission* rhs) | |
212 const { | |
213 const BluetoothManifestPermission* other = | |
214 static_cast<const BluetoothManifestPermission*>(rhs); | |
215 | |
216 return std::includes( | |
217 profile_uuids_.begin(), profile_uuids_.end(), | |
218 other->profile_uuids_.begin(), other->profile_uuids_.end()); | |
219 } | |
220 | |
221 bool BluetoothManifestPermission::Equal(const ManifestPermission* rhs) const { | |
222 const BluetoothManifestPermission* other = | |
223 static_cast<const BluetoothManifestPermission*>(rhs); | |
224 | |
225 return (profile_uuids_ == other->profile_uuids_); | |
226 } | |
227 | |
228 void BluetoothManifestPermission::Write(IPC::Message* m) const { | |
229 IPC::WriteParam(m, profile_uuids_); | |
230 } | |
231 | |
232 bool BluetoothManifestPermission::Read(const IPC::Message* m, | |
233 PickleIterator* iter) { | |
234 return IPC::ReadParam(m, iter, &profile_uuids_); | |
235 } | |
236 | |
237 void BluetoothManifestPermission::Log(std::string* log) const { | |
238 IPC::LogParam(profile_uuids_, log); | |
239 } | |
240 | |
241 void BluetoothManifestPermission::AddPermission( | |
242 const std::string& profile_uuid) { | |
243 profile_uuids_.push_back(profile_uuid); | |
244 } | |
245 | |
246 } // namespace extensions | |
OLD | NEW |