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" |
11 #include "chrome/common/extensions/api/bluetooth_low_energy.h" | 11 #include "chrome/common/extensions/api/bluetooth_low_energy.h" |
12 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
13 #include "extensions/browser/event_router.h" | 13 #include "extensions/browser/event_router.h" |
14 | 14 |
15 using content::BrowserContext; | 15 using content::BrowserContext; |
16 using content::BrowserThread; | 16 using content::BrowserThread; |
17 | 17 |
18 namespace apibtle = extensions::api::bluetooth_low_energy; | 18 namespace apibtle = extensions::api::bluetooth_low_energy; |
19 | 19 |
20 namespace { | 20 namespace { |
21 | 21 |
22 const char kErrorAdapterNotInitialized[] = | 22 const char kErrorAdapterNotInitialized[] = |
23 "Could not initialize Bluetooth adapter."; | 23 "Could not initialize Bluetooth adapter."; |
24 const char kErrorCharacteristicNotFoundFormat[] = | |
25 "Characteristic with ID \"%s\" not found."; | |
24 const char kErrorDeviceNotFoundFormat[] = | 26 const char kErrorDeviceNotFoundFormat[] = |
25 "Device with address \"%s\" not found."; | 27 "Device with address \"%s\" not found."; |
26 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; | 28 const char kErrorServiceNotFoundFormat[] = "Service with ID \"%s\" not found."; |
27 const char kErrorPlatformNotSupported[] = | 29 const char kErrorPlatformNotSupported[] = |
28 "This operation is not supported on the current platform"; | 30 "This operation is not supported on the current platform"; |
29 | 31 |
30 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( | 32 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
31 BrowserContext* context) { | 33 BrowserContext* context) { |
32 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
33 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); | 35 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
190 return false; | 192 return false; |
191 } | 193 } |
192 | 194 |
193 results_ = apibtle::GetServices::Results::Create(service_list); | 195 results_ = apibtle::GetServices::Results::Create(service_list); |
194 SendResponse(true); | 196 SendResponse(true); |
195 | 197 |
196 return true; | 198 return true; |
197 } | 199 } |
198 | 200 |
199 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() { | 201 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() { |
200 // TODO(armansito): Implement. | 202 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
201 SetError("Call not supported."); | 203 |
202 SendResponse(false); | 204 BluetoothLowEnergyEventRouter* event_router = |
203 return false; | 205 GetEventRouter(browser_context()); |
206 | |
207 // The adapter must be initialized at this point, but return an error instead | |
208 // of asserting. | |
209 if (!event_router->HasAdapter()) { | |
210 SetError(kErrorAdapterNotInitialized); | |
211 SendResponse(false); | |
212 return false; | |
213 } | |
214 | |
215 scoped_ptr<apibtle::GetCharacteristic::Params> params( | |
216 apibtle::GetCharacteristic::Params::Create(*args_)); | |
217 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | |
218 | |
219 std::string characteristic_id = params->characteristic_id; | |
220 | |
221 apibtle::Characteristic characteristic; | |
222 if (!event_router->GetCharacteristic(characteristic_id, &characteristic)) { | |
223 SetError(base::StringPrintf(kErrorCharacteristicNotFoundFormat, | |
224 characteristic_id.c_str())); | |
225 SendResponse(false); | |
226 return false; | |
227 } | |
228 | |
229 // Manually construct the result instead of using | |
230 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of | |
231 // enums correctly. | |
232 SetResult(CharacteristicToValue(&characteristic).release()); | |
rpaquay
2014/05/05 16:07:48
Wasn't this bug was recently fixed? (crbug/ 368368
armansito
2014/05/05 17:39:32
No, it's still a problem. There is a CL that does
| |
233 SendResponse(true); | |
234 | |
235 return true; | |
204 } | 236 } |
205 | 237 |
206 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { | 238 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { |
207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 239 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
208 | 240 |
209 BluetoothLowEnergyEventRouter* event_router = | 241 BluetoothLowEnergyEventRouter* event_router = |
210 GetEventRouter(browser_context()); | 242 GetEventRouter(browser_context()); |
211 | 243 |
212 // The adapter must be initialized at this point, but return an error instead | 244 // The adapter must be initialized at this point, but return an error instead |
213 // of asserting. | 245 // of asserting. |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
319 | 351 |
320 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | 352 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
321 // TODO(armansito): Implement. | 353 // TODO(armansito): Implement. |
322 SetError("Call not supported."); | 354 SetError("Call not supported."); |
323 SendResponse(false); | 355 SendResponse(false); |
324 return false; | 356 return false; |
325 } | 357 } |
326 | 358 |
327 } // namespace api | 359 } // namespace api |
328 } // namespace extensions | 360 } // namespace extensions |
OLD | NEW |