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.h" | |
20 #include "device/bluetooth/bluetooth_adapter_factory.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) { | |
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 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 = devices_.begin(); | |
54 i != devices_.end(); ++i) { | |
55 parts.push_back(*i); | |
56 } | |
57 return JoinString(parts, kSeparator); | |
58 } | |
59 | |
60 bool BluetoothDevicePermission::ManifestEntryForbidden() const { | |
61 return true; | |
62 } | |
63 | |
64 bool BluetoothDevicePermission::HasMessages() const { | |
65 return !devices_.empty(); | |
66 } | |
67 | |
68 PermissionMessages BluetoothDevicePermission::GetMessages() const { | |
69 DCHECK(HasMessages()); | |
70 PermissionMessages result; | |
71 | |
72 scoped_refptr<device::BluetoothAdapter> bluetooth_adapter = | |
73 device::BluetoothAdapterFactory::DefaultAdapter(); | |
74 | |
75 for (std::set<std::string>::const_iterator i = devices_.begin(); | |
76 i != devices_.end(); ++i) { | |
77 | |
78 string16 device_identifier; | |
79 if (bluetooth_adapter) { | |
80 device::BluetoothDevice* device = bluetooth_adapter->GetDevice(*i); | |
81 if (device) | |
82 device_identifier = device->GetName(); | |
83 } | |
84 | |
85 if (device_identifier.length() == 0) { | |
86 UTF8ToUTF16(i->c_str(), i->length(), &device_identifier); | |
87 } | |
88 | |
89 result.push_back(PermissionMessage( | |
90 PermissionMessage::kBluetoothDevice, | |
91 l10n_util::GetStringFUTF16( | |
92 IDS_EXTENSION_PROMPT_WARNING_BLUETOOTH_DEVICE, | |
93 device_identifier))); | |
94 } | |
95 | |
96 return result; | |
97 } | |
98 | |
99 bool BluetoothDevicePermission::Check( | |
100 const APIPermission::CheckParam* param) const { | |
101 const CheckParam* bluetooth_device_parameter = | |
102 static_cast<const CheckParam*>(param); | |
103 for (std::set<std::string>::const_iterator i = devices_.begin(); | |
104 i != devices_.end(); ++i) { | |
105 if (*i == bluetooth_device_parameter->device_address) | |
106 return true; | |
107 } | |
108 return false; | |
109 } | |
110 | |
111 bool BluetoothDevicePermission::Contains(const APIPermission* rhs) const { | |
112 CHECK(rhs->info() == info()); | |
113 const BluetoothDevicePermission* perm = | |
114 static_cast<const BluetoothDevicePermission*>(rhs); | |
115 return std::includes( | |
116 devices_.begin(), devices_.end(), | |
117 perm->devices_.begin(), perm->devices_.end()); | |
118 } | |
119 | |
120 bool BluetoothDevicePermission::Equal(const APIPermission* rhs) const { | |
121 CHECK(rhs->info() == info()); | |
122 const BluetoothDevicePermission* perm = | |
123 static_cast<const BluetoothDevicePermission*>(rhs); | |
124 return devices_ == perm->devices_; | |
125 } | |
126 | |
127 bool BluetoothDevicePermission::FromValue(const base::Value* value) { | |
128 devices_.clear(); | |
129 const base::ListValue* list = NULL; | |
130 | |
131 if (!value) | |
132 return false; | |
133 | |
134 if (!value->GetAsList(&list) || list->GetSize() == 0) | |
135 return false; | |
136 | |
137 for (size_t i = 0; i < list->GetSize(); ++i) { | |
138 std::string device_address; | |
139 if (!list->GetString(i, &device_address)) | |
140 return false; | |
141 devices_.insert(device_address); | |
142 } | |
143 | |
144 return true; | |
145 } | |
146 | |
147 void BluetoothDevicePermission::ToValue(base::Value** value) const { | |
148 base::ListValue* list = new ListValue(); | |
149 std::set<std::string>::const_iterator i; | |
150 for (std::set<std::string>::const_iterator i = devices_.begin(); | |
151 i != devices_.end(); ++i) { | |
152 list->Append(base::Value::CreateStringValue(*i)); | |
153 } | |
154 *value = list; | |
155 } | |
156 | |
157 APIPermission* BluetoothDevicePermission::Clone() const { | |
158 BluetoothDevicePermission* result = new BluetoothDevicePermission(info()); | |
159 result->devices_ = devices_; | |
160 return result; | |
161 } | |
162 | |
163 APIPermission* BluetoothDevicePermission::Diff(const APIPermission* rhs) const { | |
164 CHECK(rhs->info() == info()); | |
165 const BluetoothDevicePermission* perm = | |
166 static_cast<const BluetoothDevicePermission*>(rhs); | |
167 scoped_ptr<BluetoothDevicePermission> result( | |
168 new BluetoothDevicePermission(info())); | |
169 std::set_difference( | |
170 devices_.begin(), devices_.end(), | |
171 perm->devices_.begin(), perm->devices_.end(), | |
172 std::inserter<std::set<std::string> >( | |
173 result->devices_, result->devices_.begin())); | |
174 return result->devices_.empty() ? NULL : result.release(); | |
175 } | |
176 | |
177 APIPermission* BluetoothDevicePermission::Union( | |
178 const APIPermission* rhs) const { | |
179 CHECK(rhs->info() == info()); | |
180 const BluetoothDevicePermission* perm = | |
181 static_cast<const BluetoothDevicePermission*>(rhs); | |
182 scoped_ptr<BluetoothDevicePermission> result( | |
183 new BluetoothDevicePermission(info())); | |
184 std::set_union( | |
185 devices_.begin(), devices_.end(), | |
186 perm->devices_.begin(), perm->devices_.end(), | |
187 std::inserter<std::set<std::string> >( | |
188 result->devices_, result->devices_.begin())); | |
189 return result->devices_.empty() ? NULL : result.release(); | |
190 } | |
191 | |
192 APIPermission* BluetoothDevicePermission::Intersect( | |
193 const APIPermission* rhs) const { | |
194 CHECK(rhs->info() == info()); | |
195 const BluetoothDevicePermission* perm = | |
196 static_cast<const BluetoothDevicePermission*>(rhs); | |
197 scoped_ptr<BluetoothDevicePermission> result( | |
198 new BluetoothDevicePermission(info())); | |
199 std::set_intersection( | |
200 devices_.begin(), devices_.end(), | |
201 perm->devices_.begin(), perm->devices_.end(), | |
202 std::inserter<std::set<std::string> >( | |
203 result->devices_, result->devices_.begin())); | |
204 return result->devices_.empty() ? NULL : result.release(); | |
205 } | |
206 | |
207 void BluetoothDevicePermission::Write(IPC::Message* m) const { | |
208 IPC::WriteParam(m, devices_); | |
209 } | |
210 | |
211 bool BluetoothDevicePermission::Read( | |
212 const IPC::Message* m, PickleIterator* iter) { | |
213 return IPC::ReadParam(m, iter, &devices_); | |
214 } | |
215 | |
216 void BluetoothDevicePermission::Log(std::string* log) const { | |
217 IPC::LogParam(devices_, log); | |
218 } | |
219 | |
220 } // namespace extensions | |
OLD | NEW |