OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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/permissions/bluetooth_device_permission.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/logging.h" | |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/string16.h" | |
14 #include "base/string_util.h" | |
15 #include "base/utf_string_conversions.h" | |
16 #include "base/values.h" | |
17 #include "chrome/common/extensions/extension_messages.h" | |
18 #include "chrome/common/extensions/permissions/permissions_info.h" | |
19 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
20 #include "device/bluetooth/bluetooth_adapter.h" | |
21 #include "device/bluetooth/bluetooth_device.h" | |
22 #include "grit/generated_resources.h" | |
23 #include "ui/base/l10n/l10n_util.h" | |
24 | |
25 namespace { | |
26 | |
27 const char* kSeparator = "|"; | |
28 | |
29 } // namespace | |
30 | |
31 namespace extensions { | |
32 | |
33 BluetoothDevicePermission::BluetoothDevicePermission( | |
34 const APIPermissionInfo* info) : APIPermission(info) { | |
35 } | |
36 | |
37 BluetoothDevicePermission::~BluetoothDevicePermission() { | |
38 } | |
39 | |
40 void BluetoothDevicePermission::AddDevicesFromString( | |
41 const std::string &devices_string) { | |
Matt Perry
2012/11/27 21:35:32
& before space
bryeung
2012/11/28 21:55:05
Done.
| |
42 std::vector<std::string> devices; | |
43 Tokenize(devices_string, kSeparator, &devices); | |
44 for (std::vector<std::string>::const_iterator i = devices.begin(); | |
45 i != devices.end(); ++i) { | |
46 allowed_devices_.insert(*i); | |
47 } | |
48 } | |
49 | |
50 std::string BluetoothDevicePermission::ToString() const { | |
51 std::vector<std::string> parts; | |
52 parts.push_back(name()); | |
53 for (std::set<std::string>::const_iterator i = allowed_devices_.begin(); | |
54 i != allowed_devices_.end(); ++i) { | |
55 parts.push_back(*i); | |
56 } | |
57 return JoinString(parts, kSeparator); | |
58 } | |
59 | |
60 bool BluetoothDevicePermission::ManifestEntryRequired() const { | |
61 return false; | |
62 } | |
63 | |
64 bool BluetoothDevicePermission::ManifestEntryForbidden() const { | |
65 return true; | |
66 } | |
67 | |
68 bool BluetoothDevicePermission::HasMessages() const { | |
69 return !allowed_devices_.empty(); | |
70 } | |
71 | |
72 PermissionMessages BluetoothDevicePermission::GetMessages() const { | |
73 DCHECK(HasMessages()); | |
74 PermissionMessages result; | |
75 | |
76 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter = | |
77 device::BluetoothAdapterFactory::DefaultAdapter(); | |
78 | |
79 for (std::set<std::string>::const_iterator i = allowed_devices_.begin(); | |
80 i != allowed_devices_.end(); ++i) { | |
81 | |
82 string16 device_identifier; | |
83 if (bluetooth_adapter) { | |
84 device::BluetoothDevice* device = bluetooth_adapter->GetDevice(*i); | |
85 if (device) | |
86 device_identifier = device->GetName(); | |
87 } | |
88 | |
89 if (device_identifier.length() == 0) { | |
Matt Perry
2012/11/27 21:35:32
nit: device_identifier.empty()
bryeung
2012/11/28 21:55:05
done. (Also fixed braces.)
| |
90 UTF8ToUTF16(i->c_str(), i->length(), &device_identifier); | |
91 } | |
92 | |
93 result.push_back(PermissionMessage( | |
94 PermissionMessage::kBluetoothDevice, | |
95 l10n_util::GetStringFUTF16( | |
96 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICE, | |
97 device_identifier))); | |
98 } | |
99 | |
100 return result; | |
101 } | |
102 | |
103 bool BluetoothDevicePermission::Check( | |
104 const APIPermission::CheckParam* param) const { | |
105 const CheckParam* bluetooth_device_parameter = | |
106 static_cast<const CheckParam*>(param); | |
107 for (std::set<std::string>::const_iterator i = allowed_devices_.begin(); | |
108 i != allowed_devices_.end(); ++i) { | |
109 if (*i == bluetooth_device_parameter->device_address) | |
110 return true; | |
111 } | |
112 return false; | |
113 } | |
114 | |
115 bool BluetoothDevicePermission::Contains(const APIPermission* rhs) const { | |
116 CHECK(rhs->info() == info()); | |
117 const BluetoothDevicePermission* perm = | |
118 static_cast<const BluetoothDevicePermission*>(rhs); | |
119 return std::includes( | |
120 allowed_devices_.begin(), allowed_devices_.end(), | |
121 perm->allowed_devices_.begin(), perm->allowed_devices_.end()); | |
122 } | |
123 | |
124 bool BluetoothDevicePermission::Equal(const APIPermission* rhs) const { | |
125 CHECK(rhs->info() == info()); | |
126 const BluetoothDevicePermission* perm = | |
127 static_cast<const BluetoothDevicePermission*>(rhs); | |
128 return allowed_devices_ == perm->allowed_devices_; | |
129 } | |
130 | |
131 bool BluetoothDevicePermission::FromValue(const base::Value* value) { | |
132 allowed_devices_.clear(); | |
133 const base::ListValue* list = NULL; | |
134 | |
135 if (!value) | |
136 return false; | |
137 | |
138 if (!value->GetAsList(&list) || list->GetSize() == 0) | |
139 return false; | |
140 | |
141 for (size_t i = 0; i < list->GetSize(); ++i) { | |
142 std::string device_address; | |
143 if (!list->GetString(i, &device_address)) | |
144 return false; | |
145 allowed_devices_.insert(device_address); | |
146 } | |
147 | |
148 return true; | |
149 } | |
150 | |
151 void BluetoothDevicePermission::ToValue(base::Value** value) const { | |
152 base::ListValue* list = new ListValue(); | |
153 std::set<std::string>::const_iterator i; | |
154 for (std::set<std::string>::const_iterator i = allowed_devices_.begin(); | |
155 i != allowed_devices_.end(); ++i) { | |
156 list->Append(base::Value::CreateStringValue(*i)); | |
157 } | |
158 *value = list; | |
159 } | |
160 | |
161 APIPermission* BluetoothDevicePermission::Clone() const { | |
162 BluetoothDevicePermission* result = new BluetoothDevicePermission(info()); | |
163 result->allowed_devices_ = allowed_devices_; | |
164 return result; | |
165 } | |
166 | |
167 APIPermission* BluetoothDevicePermission::Diff(const APIPermission* rhs) const { | |
168 CHECK(rhs->info() == info()); | |
169 const BluetoothDevicePermission* perm = | |
170 static_cast<const BluetoothDevicePermission*>(rhs); | |
171 scoped_ptr<BluetoothDevicePermission> result( | |
172 new BluetoothDevicePermission(info())); | |
173 std::set_difference( | |
174 allowed_devices_.begin(), allowed_devices_.end(), | |
175 perm->allowed_devices_.begin(), perm->allowed_devices_.end(), | |
176 std::inserter<std::set<std::string> >( | |
177 result->allowed_devices_, result->allowed_devices_.begin())); | |
178 return result->allowed_devices_.empty() ? NULL : result.release(); | |
179 } | |
180 | |
181 APIPermission* BluetoothDevicePermission::Union( | |
182 const APIPermission* rhs) const { | |
183 CHECK(rhs->info() == info()); | |
184 const BluetoothDevicePermission* perm = | |
185 static_cast<const BluetoothDevicePermission*>(rhs); | |
186 scoped_ptr<BluetoothDevicePermission> result( | |
187 new BluetoothDevicePermission(info())); | |
188 std::set_union( | |
189 allowed_devices_.begin(), allowed_devices_.end(), | |
190 perm->allowed_devices_.begin(), perm->allowed_devices_.end(), | |
191 std::inserter<std::set<std::string> >( | |
192 result->allowed_devices_, result->allowed_devices_.begin())); | |
193 return result->allowed_devices_.empty() ? NULL : result.release(); | |
194 } | |
195 | |
196 APIPermission* BluetoothDevicePermission::Intersect( | |
197 const APIPermission* rhs) const { | |
198 CHECK(rhs->info() == info()); | |
199 const BluetoothDevicePermission* perm = | |
200 static_cast<const BluetoothDevicePermission*>(rhs); | |
201 scoped_ptr<BluetoothDevicePermission> result( | |
202 new BluetoothDevicePermission(info())); | |
203 std::set_intersection( | |
204 allowed_devices_.begin(), allowed_devices_.end(), | |
205 perm->allowed_devices_.begin(), perm->allowed_devices_.end(), | |
206 std::inserter<std::set<std::string> >( | |
207 result->allowed_devices_, result->allowed_devices_.begin())); | |
208 return result->allowed_devices_.empty() ? NULL : result.release(); | |
209 } | |
210 | |
211 void BluetoothDevicePermission::Write(IPC::Message* m) const { | |
212 IPC::WriteParam(m, allowed_devices_); | |
213 } | |
214 | |
215 bool BluetoothDevicePermission::Read( | |
216 const IPC::Message* m, PickleIterator* iter) { | |
217 return IPC::ReadParam(m, iter, &allowed_devices_); | |
218 } | |
219 | |
220 void BluetoothDevicePermission::Log(std::string* log) const { | |
221 IPC::LogParam(allowed_devices_, log); | |
222 } | |
223 | |
224 } // namespace extensions | |
OLD | NEW |