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 // TODO(armansito): Remove this function once the described bug is fixed. |
| 42 // (See crbug.com/368368). |
| 43 // |
| 44 // Converts an apibtle::Characteristic to a base::Value. This function is |
| 45 // necessary, as json_schema_compiler::util::AddItemToList has no template |
| 46 // specialization for user defined enums, which get treated as integers. This is |
| 47 // because Characteristic contains a list of enum CharacteristicProperty. |
| 48 scoped_ptr<base::DictionaryValue> CharacteristicToValue( |
| 49 apibtle::Characteristic* from) { |
| 50 // Copy the properties. Use Characteristic::ToValue to generate the result |
| 51 // dictionary without the properties, to prevent json_schema_compiler from |
| 52 // failing. |
| 53 std::vector<apibtle::CharacteristicProperty> properties = from->properties; |
| 54 from->properties.clear(); |
| 55 scoped_ptr<base::DictionaryValue> to = from->ToValue(); |
| 56 |
| 57 // Manually set each property. |
| 58 scoped_ptr<base::ListValue> property_list(new base::ListValue()); |
| 59 for (std::vector<apibtle::CharacteristicProperty>::iterator iter = |
| 60 properties.begin(); |
| 61 iter != properties.end(); |
| 62 ++iter) |
| 63 property_list->Append(new base::StringValue(apibtle::ToString(*iter))); |
| 64 |
| 65 to->Set("properties", property_list.release()); |
| 66 |
| 67 return to.Pass(); |
| 68 } |
| 69 |
41 } // namespace | 70 } // namespace |
42 | 71 |
43 namespace extensions { | 72 namespace extensions { |
44 | 73 |
45 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > | 74 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > |
46 g_factory = LAZY_INSTANCE_INITIALIZER; | 75 g_factory = LAZY_INSTANCE_INITIALIZER; |
47 | 76 |
48 // static | 77 // static |
49 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* | 78 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* |
50 BluetoothLowEnergyAPI::GetFactoryInstance() { | 79 BluetoothLowEnergyAPI::GetFactoryInstance() { |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 } | 197 } |
169 | 198 |
170 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() { | 199 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() { |
171 // TODO(armansito): Implement. | 200 // TODO(armansito): Implement. |
172 SetError("Call not supported."); | 201 SetError("Call not supported."); |
173 SendResponse(false); | 202 SendResponse(false); |
174 return false; | 203 return false; |
175 } | 204 } |
176 | 205 |
177 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { | 206 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { |
178 // TODO(armansito): Implement. | 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
179 SetError("Call not supported."); | 208 |
180 SendResponse(false); | 209 BluetoothLowEnergyEventRouter* event_router = |
181 return false; | 210 GetEventRouter(browser_context()); |
| 211 |
| 212 // The adapter must be initialized at this point, but return an error instead |
| 213 // of asserting. |
| 214 if (!event_router->HasAdapter()) { |
| 215 SetError(kErrorAdapterNotInitialized); |
| 216 SendResponse(false); |
| 217 return false; |
| 218 } |
| 219 |
| 220 scoped_ptr<apibtle::GetCharacteristics::Params> params( |
| 221 apibtle::GetCharacteristics::Params::Create(*args_)); |
| 222 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
| 223 |
| 224 std::string service_id = params->service_id; |
| 225 |
| 226 BluetoothLowEnergyEventRouter::CharacteristicList characteristic_list; |
| 227 if (!event_router->GetCharacteristics(service_id, &characteristic_list)) { |
| 228 SetError( |
| 229 base::StringPrintf(kErrorServiceNotFoundFormat, service_id.c_str())); |
| 230 |
| 231 SendResponse(false); |
| 232 return false; |
| 233 } |
| 234 |
| 235 // Manually construct the result instead of using |
| 236 // apibtle::GetCharacteristics::Result::Create as it doesn't convert lists of |
| 237 // enums correctly. |
| 238 scoped_ptr<base::ListValue> result(new base::ListValue()); |
| 239 for (BluetoothLowEnergyEventRouter::CharacteristicList::iterator iter = |
| 240 characteristic_list.begin(); |
| 241 iter != characteristic_list.end(); |
| 242 ++iter) |
| 243 result->Append(CharacteristicToValue(iter->get()).release()); |
| 244 |
| 245 SetResult(result.release()); |
| 246 SendResponse(true); |
| 247 |
| 248 return true; |
182 } | 249 } |
183 | 250 |
184 bool BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { | 251 bool BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { |
185 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 252 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
186 | 253 |
187 BluetoothLowEnergyEventRouter* event_router = | 254 BluetoothLowEnergyEventRouter* event_router = |
188 GetEventRouter(browser_context()); | 255 GetEventRouter(browser_context()); |
189 | 256 |
190 // The adapter must be initialized at this point, but return an error instead | 257 // The adapter must be initialized at this point, but return an error instead |
191 // of asserting. | 258 // of asserting. |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 319 |
253 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | 320 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
254 // TODO(armansito): Implement. | 321 // TODO(armansito): Implement. |
255 SetError("Call not supported."); | 322 SetError("Call not supported."); |
256 SendResponse(false); | 323 SendResponse(false); |
257 return false; | 324 return false; |
258 } | 325 } |
259 | 326 |
260 } // namespace api | 327 } // namespace api |
261 } // namespace extensions | 328 } // namespace extensions |
OLD | NEW |