OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ y_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ y_api.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/lazy_instance.h" | 8 #include "base/lazy_instance.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ y_event_router.h" | 10 #include "chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energ y_event_router.h" |
(...skipping 20 matching lines...) Expand all Loading... | |
31 BrowserContext* context) { | 31 BrowserContext* context) { |
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
33 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); | 33 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
34 } | 34 } |
35 | 35 |
36 void DoWorkCallback(const base::Callback<bool()>& callback) { | 36 void DoWorkCallback(const base::Callback<bool()>& callback) { |
37 DCHECK(!callback.is_null()); | 37 DCHECK(!callback.is_null()); |
38 callback.Run(); | 38 callback.Run(); |
39 } | 39 } |
40 | 40 |
41 // Converts an apibtle::Characteristic to a base::Value. This function is | |
42 // necessary, as json_schema_compiler::util::AddItemToList has no template | |
43 // specialization for user defined enums, which get treated as integers. This is | |
44 // because Characteristic contains a list of enum CharacteristicProperty. | |
rpaquay
2014/04/29 17:38:30
I would open a bug for this, it sounds like an ove
armansito
2014/04/29 19:31:19
Filed bug and added a TODO here.
rpaquay
2014/04/29 19:33:12
Thanks!
| |
45 scoped_ptr<base::DictionaryValue> CharacteristicToValue( | |
46 apibtle::Characteristic* from) { | |
47 // Copy the properties. Use Characteristic::ToValue to generate the result | |
48 // dictionary without the properties, to prevent json_schema_compiler from | |
49 // failing. | |
50 std::vector<apibtle::CharacteristicProperty> properties = from->properties; | |
51 from->properties.clear(); | |
52 scoped_ptr<base::DictionaryValue> to = from->ToValue(); | |
53 | |
54 // Manually set each property. | |
55 scoped_ptr<base::ListValue> property_list(new base::ListValue()); | |
56 for (std::vector<apibtle::CharacteristicProperty>::iterator iter = | |
57 properties.begin(); | |
58 iter != properties.end(); | |
59 ++iter) | |
60 property_list->Append(new base::StringValue(apibtle::ToString(*iter))); | |
61 | |
62 to->Set("properties", property_list.release()); | |
63 | |
64 return to.Pass(); | |
65 } | |
66 | |
41 } // namespace | 67 } // namespace |
42 | 68 |
43 namespace extensions { | 69 namespace extensions { |
44 | 70 |
45 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > | 71 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > |
46 g_factory = LAZY_INSTANCE_INITIALIZER; | 72 g_factory = LAZY_INSTANCE_INITIALIZER; |
47 | 73 |
48 // static | 74 // static |
49 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* | 75 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* |
50 BluetoothLowEnergyAPI::GetFactoryInstance() { | 76 BluetoothLowEnergyAPI::GetFactoryInstance() { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 } | 194 } |
169 | 195 |
170 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() { | 196 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() { |
171 // TODO(armansito): Implement. | 197 // TODO(armansito): Implement. |
172 SetError("Call not supported."); | 198 SetError("Call not supported."); |
173 SendResponse(false); | 199 SendResponse(false); |
174 return false; | 200 return false; |
175 } | 201 } |
176 | 202 |
177 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { | 203 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { |
178 // TODO(armansito): Implement. | 204 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
179 SetError("Call not supported."); | 205 |
180 SendResponse(false); | 206 BluetoothLowEnergyEventRouter* event_router = |
181 return false; | 207 GetEventRouter(browser_context()); |
208 | |
209 // The adapter must be initialized at this point, but return an error instead | |
210 // of asserting. | |
211 if (!event_router->HasAdapter()) { | |
212 SetError(kErrorAdapterNotInitialized); | |
213 SendResponse(false); | |
214 return false; | |
215 } | |
216 | |
217 scoped_ptr<apibtle::GetCharacteristics::Params> params( | |
218 apibtle::GetCharacteristics::Params::Create(*args_)); | |
219 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | |
220 | |
221 std::string service_id = params->service_id; | |
222 | |
223 BluetoothLowEnergyEventRouter::CharacteristicList characteristic_list; | |
224 if (!event_router->GetCharacteristics(service_id, &characteristic_list)) { | |
225 SetError( | |
226 base::StringPrintf(kErrorServiceNotFoundFormat, service_id.c_str())); | |
227 | |
228 SendResponse(false); | |
229 return false; | |
230 } | |
231 | |
232 // Manually construct the result instead of using | |
233 // apibtle::GetCharacteristics::Result::Create as it doesn't convert lists of | |
234 // enums correctly. | |
235 scoped_ptr<base::ListValue> result(new base::ListValue()); | |
236 for (BluetoothLowEnergyEventRouter::CharacteristicList::iterator iter = | |
237 characteristic_list.begin(); | |
238 iter != characteristic_list.end(); | |
239 ++iter) | |
240 result->Append(CharacteristicToValue(iter->get()).release()); | |
241 | |
242 SetResult(result.release()); | |
243 SendResponse(true); | |
244 | |
245 return true; | |
182 } | 246 } |
183 | 247 |
184 bool BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { | 248 bool BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { |
185 // TODO(armansito): Implement. | 249 // TODO(armansito): Implement. |
186 SetError("Call not supported."); | 250 SetError("Call not supported."); |
187 SendResponse(false); | 251 SendResponse(false); |
188 return false; | 252 return false; |
189 } | 253 } |
190 | 254 |
191 bool BluetoothLowEnergyGetDescriptorFunction::DoWork() { | 255 bool BluetoothLowEnergyGetDescriptorFunction::DoWork() { |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
225 | 289 |
226 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | 290 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
227 // TODO(armansito): Implement. | 291 // TODO(armansito): Implement. |
228 SetError("Call not supported."); | 292 SetError("Call not supported."); |
229 SendResponse(false); | 293 SendResponse(false); |
230 return false; | 294 return false; |
231 } | 295 } |
232 | 296 |
233 } // namespace api | 297 } // namespace api |
234 } // namespace extensions | 298 } // namespace extensions |
OLD | NEW |