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/utils.h" | 10 #include "chrome/browser/extensions/api/bluetooth_low_energy/utils.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
62 return kErrorOperationFailed; | 62 return kErrorOperationFailed; |
63 } | 63 } |
64 return ""; | 64 return ""; |
65 } | 65 } |
66 | 66 |
67 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( | 67 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( |
68 BrowserContext* context) { | 68 BrowserContext* context) { |
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
70 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); | 70 return extensions::BluetoothLowEnergyAPI::Get(context)->event_router(); |
71 } | 71 } |
72 | 72 |
armansito
2014/07/18 03:26:37
Does this code actually compile? The reason these
Ilya Sherman
2014/07/18 21:37:00
Oh, wow: EXTENSION_FUNCTION_VALIDATE returns a val
| |
73 void DoWorkCallback(const base::Callback<bool()>& callback) { | |
74 DCHECK(!callback.is_null()); | |
75 callback.Run(); | |
76 } | |
77 | |
78 } // namespace | 73 } // namespace |
79 | 74 |
80 | 75 |
81 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > | 76 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI> > |
82 g_factory = LAZY_INSTANCE_INITIALIZER; | 77 g_factory = LAZY_INSTANCE_INITIALIZER; |
83 | 78 |
84 // static | 79 // static |
85 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* | 80 BrowserContextKeyedAPIFactory<BluetoothLowEnergyAPI>* |
86 BluetoothLowEnergyAPI::GetFactoryInstance() { | 81 BluetoothLowEnergyAPI::GetFactoryInstance() { |
87 return g_factory.Pointer(); | 82 return g_factory.Pointer(); |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
122 return false; | 117 return false; |
123 } | 118 } |
124 | 119 |
125 BluetoothLowEnergyEventRouter* event_router = | 120 BluetoothLowEnergyEventRouter* event_router = |
126 GetEventRouter(browser_context()); | 121 GetEventRouter(browser_context()); |
127 if (!event_router->IsBluetoothSupported()) { | 122 if (!event_router->IsBluetoothSupported()) { |
128 SetError(kErrorPlatformNotSupported); | 123 SetError(kErrorPlatformNotSupported); |
129 return false; | 124 return false; |
130 } | 125 } |
131 | 126 |
132 // It is safe to pass |this| here as ExtensionFunction is refcounted. | 127 if (!event_router->InitializeAdapterAndInvokeCallback( |
133 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( | 128 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this))) { |
134 &DoWorkCallback, | |
135 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { | |
136 SetError(kErrorAdapterNotInitialized); | 129 SetError(kErrorAdapterNotInitialized); |
137 return false; | 130 return false; |
138 } | 131 } |
139 | 132 |
140 return true; | 133 return true; |
141 } | 134 } |
142 | 135 |
143 bool BluetoothLowEnergyConnectFunction::DoWork() { | 136 void BluetoothLowEnergyConnectFunction::DoWork() { |
144 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 137 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
145 | 138 |
146 BluetoothLowEnergyEventRouter* event_router = | 139 BluetoothLowEnergyEventRouter* event_router = |
147 GetEventRouter(browser_context()); | 140 GetEventRouter(browser_context()); |
148 | 141 |
149 // The adapter must be initialized at this point, but return an error instead | 142 // The adapter must be initialized at this point, but return an error instead |
150 // of asserting. | 143 // of asserting. |
151 if (!event_router->HasAdapter()) { | 144 if (!event_router->HasAdapter()) { |
152 SetError(kErrorAdapterNotInitialized); | 145 SetError(kErrorAdapterNotInitialized); |
153 SendResponse(false); | 146 SendResponse(false); |
154 return false; | 147 return; |
155 } | 148 } |
156 | 149 |
157 scoped_ptr<apibtle::Connect::Params> params( | 150 scoped_ptr<apibtle::Connect::Params> params( |
158 apibtle::Connect::Params::Create(*args_)); | 151 apibtle::Connect::Params::Create(*args_)); |
159 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 152 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
160 | 153 |
161 bool persistent = false; // Not persistent by default. | 154 bool persistent = false; // Not persistent by default. |
162 apibtle::ConnectProperties* properties = params.get()->properties.get(); | 155 apibtle::ConnectProperties* properties = params.get()->properties.get(); |
163 if (properties) | 156 if (properties) |
164 persistent = properties->persistent; | 157 persistent = properties->persistent; |
165 | 158 |
166 event_router->Connect( | 159 event_router->Connect( |
167 persistent, | 160 persistent, |
168 GetExtension(), | 161 GetExtension(), |
169 params->device_address, | 162 params->device_address, |
170 base::Bind(&BluetoothLowEnergyConnectFunction::SuccessCallback, this), | 163 base::Bind(&BluetoothLowEnergyConnectFunction::SuccessCallback, this), |
171 base::Bind(&BluetoothLowEnergyConnectFunction::ErrorCallback, this)); | 164 base::Bind(&BluetoothLowEnergyConnectFunction::ErrorCallback, this)); |
172 | |
173 return true; | |
174 } | 165 } |
175 | 166 |
176 void BluetoothLowEnergyConnectFunction::SuccessCallback() { | 167 void BluetoothLowEnergyConnectFunction::SuccessCallback() { |
177 SendResponse(true); | 168 SendResponse(true); |
178 } | 169 } |
179 | 170 |
180 void BluetoothLowEnergyConnectFunction::ErrorCallback( | 171 void BluetoothLowEnergyConnectFunction::ErrorCallback( |
181 BluetoothLowEnergyEventRouter::Status status) { | 172 BluetoothLowEnergyEventRouter::Status status) { |
182 SetError(StatusToString(status)); | 173 SetError(StatusToString(status)); |
183 SendResponse(false); | 174 SendResponse(false); |
184 } | 175 } |
185 | 176 |
186 bool BluetoothLowEnergyDisconnectFunction::DoWork() { | 177 void BluetoothLowEnergyDisconnectFunction::DoWork() { |
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
188 | 179 |
189 BluetoothLowEnergyEventRouter* event_router = | 180 BluetoothLowEnergyEventRouter* event_router = |
190 GetEventRouter(browser_context()); | 181 GetEventRouter(browser_context()); |
191 | 182 |
192 // The adapter must be initialized at this point, but return an error instead | 183 // The adapter must be initialized at this point, but return an error instead |
193 // of asserting. | 184 // of asserting. |
194 if (!event_router->HasAdapter()) { | 185 if (!event_router->HasAdapter()) { |
195 SetError(kErrorAdapterNotInitialized); | 186 SetError(kErrorAdapterNotInitialized); |
196 SendResponse(false); | 187 SendResponse(false); |
197 return false; | 188 return; |
198 } | 189 } |
199 | 190 |
200 scoped_ptr<apibtle::Disconnect::Params> params( | 191 scoped_ptr<apibtle::Disconnect::Params> params( |
201 apibtle::Disconnect::Params::Create(*args_)); | 192 apibtle::Disconnect::Params::Create(*args_)); |
202 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 193 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
203 | 194 |
204 event_router->Disconnect( | 195 event_router->Disconnect( |
205 GetExtension(), | 196 GetExtension(), |
206 params->device_address, | 197 params->device_address, |
207 base::Bind(&BluetoothLowEnergyDisconnectFunction::SuccessCallback, this), | 198 base::Bind(&BluetoothLowEnergyDisconnectFunction::SuccessCallback, this), |
208 base::Bind(&BluetoothLowEnergyDisconnectFunction::ErrorCallback, this)); | 199 base::Bind(&BluetoothLowEnergyDisconnectFunction::ErrorCallback, this)); |
209 | |
210 return true; | |
211 } | 200 } |
212 | 201 |
213 void BluetoothLowEnergyDisconnectFunction::SuccessCallback() { | 202 void BluetoothLowEnergyDisconnectFunction::SuccessCallback() { |
214 SendResponse(true); | 203 SendResponse(true); |
215 } | 204 } |
216 | 205 |
217 void BluetoothLowEnergyDisconnectFunction::ErrorCallback( | 206 void BluetoothLowEnergyDisconnectFunction::ErrorCallback( |
218 BluetoothLowEnergyEventRouter::Status status) { | 207 BluetoothLowEnergyEventRouter::Status status) { |
219 SetError(StatusToString(status)); | 208 SetError(StatusToString(status)); |
220 SendResponse(false); | 209 SendResponse(false); |
221 } | 210 } |
222 | 211 |
223 bool BluetoothLowEnergyGetServiceFunction::DoWork() { | 212 void BluetoothLowEnergyGetServiceFunction::DoWork() { |
224 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 213 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
225 | 214 |
226 BluetoothLowEnergyEventRouter* event_router = | 215 BluetoothLowEnergyEventRouter* event_router = |
227 GetEventRouter(browser_context()); | 216 GetEventRouter(browser_context()); |
228 | 217 |
229 // The adapter must be initialized at this point, but return an error instead | 218 // The adapter must be initialized at this point, but return an error instead |
230 // of asserting. | 219 // of asserting. |
231 if (!event_router->HasAdapter()) { | 220 if (!event_router->HasAdapter()) { |
232 SetError(kErrorAdapterNotInitialized); | 221 SetError(kErrorAdapterNotInitialized); |
233 SendResponse(false); | 222 SendResponse(false); |
234 return false; | 223 return; |
235 } | 224 } |
236 | 225 |
237 scoped_ptr<apibtle::GetService::Params> params( | 226 scoped_ptr<apibtle::GetService::Params> params( |
238 apibtle::GetService::Params::Create(*args_)); | 227 apibtle::GetService::Params::Create(*args_)); |
239 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 228 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
240 | 229 |
241 apibtle::Service service; | 230 apibtle::Service service; |
242 BluetoothLowEnergyEventRouter::Status status = | 231 BluetoothLowEnergyEventRouter::Status status = |
243 event_router->GetService(params->service_id, &service); | 232 event_router->GetService(params->service_id, &service); |
244 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 233 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
245 SetError(StatusToString(status)); | 234 SetError(StatusToString(status)); |
246 SendResponse(false); | 235 SendResponse(false); |
247 return false; | 236 return; |
248 } | 237 } |
249 | 238 |
250 results_ = apibtle::GetService::Results::Create(service); | 239 results_ = apibtle::GetService::Results::Create(service); |
251 SendResponse(true); | 240 SendResponse(true); |
252 | |
253 return true; | |
254 } | 241 } |
255 | 242 |
256 bool BluetoothLowEnergyGetServicesFunction::DoWork() { | 243 void BluetoothLowEnergyGetServicesFunction::DoWork() { |
257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
258 | 245 |
259 BluetoothLowEnergyEventRouter* event_router = | 246 BluetoothLowEnergyEventRouter* event_router = |
260 GetEventRouter(browser_context()); | 247 GetEventRouter(browser_context()); |
261 | 248 |
262 // The adapter must be initialized at this point, but return an error instead | 249 // The adapter must be initialized at this point, but return an error instead |
263 // of asserting. | 250 // of asserting. |
264 if (!event_router->HasAdapter()) { | 251 if (!event_router->HasAdapter()) { |
265 SetError(kErrorAdapterNotInitialized); | 252 SetError(kErrorAdapterNotInitialized); |
266 SendResponse(false); | 253 SendResponse(false); |
267 return false; | 254 return; |
268 } | 255 } |
269 | 256 |
270 scoped_ptr<apibtle::GetServices::Params> params( | 257 scoped_ptr<apibtle::GetServices::Params> params( |
271 apibtle::GetServices::Params::Create(*args_)); | 258 apibtle::GetServices::Params::Create(*args_)); |
272 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 259 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
273 | 260 |
274 BluetoothLowEnergyEventRouter::ServiceList service_list; | 261 BluetoothLowEnergyEventRouter::ServiceList service_list; |
275 if (!event_router->GetServices(params->device_address, &service_list)) { | 262 if (!event_router->GetServices(params->device_address, &service_list)) { |
276 SetError(kErrorNotFound); | 263 SetError(kErrorNotFound); |
277 SendResponse(false); | 264 SendResponse(false); |
278 return false; | 265 return; |
279 } | 266 } |
280 | 267 |
281 results_ = apibtle::GetServices::Results::Create(service_list); | 268 results_ = apibtle::GetServices::Results::Create(service_list); |
282 SendResponse(true); | 269 SendResponse(true); |
283 | |
284 return true; | |
285 } | 270 } |
286 | 271 |
287 bool BluetoothLowEnergyGetCharacteristicFunction::DoWork() { | 272 void BluetoothLowEnergyGetCharacteristicFunction::DoWork() { |
288 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 273 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
289 | 274 |
290 BluetoothLowEnergyEventRouter* event_router = | 275 BluetoothLowEnergyEventRouter* event_router = |
291 GetEventRouter(browser_context()); | 276 GetEventRouter(browser_context()); |
292 | 277 |
293 // The adapter must be initialized at this point, but return an error instead | 278 // The adapter must be initialized at this point, but return an error instead |
294 // of asserting. | 279 // of asserting. |
295 if (!event_router->HasAdapter()) { | 280 if (!event_router->HasAdapter()) { |
296 SetError(kErrorAdapterNotInitialized); | 281 SetError(kErrorAdapterNotInitialized); |
297 SendResponse(false); | 282 SendResponse(false); |
298 return false; | 283 return; |
299 } | 284 } |
300 | 285 |
301 scoped_ptr<apibtle::GetCharacteristic::Params> params( | 286 scoped_ptr<apibtle::GetCharacteristic::Params> params( |
302 apibtle::GetCharacteristic::Params::Create(*args_)); | 287 apibtle::GetCharacteristic::Params::Create(*args_)); |
303 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 288 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
304 | 289 |
305 apibtle::Characteristic characteristic; | 290 apibtle::Characteristic characteristic; |
306 BluetoothLowEnergyEventRouter::Status status = | 291 BluetoothLowEnergyEventRouter::Status status = |
307 event_router->GetCharacteristic( | 292 event_router->GetCharacteristic( |
308 GetExtension(), params->characteristic_id, &characteristic); | 293 GetExtension(), params->characteristic_id, &characteristic); |
309 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 294 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
310 SetError(StatusToString(status)); | 295 SetError(StatusToString(status)); |
311 SendResponse(false); | 296 SendResponse(false); |
312 return false; | 297 return; |
313 } | 298 } |
314 | 299 |
315 // Manually construct the result instead of using | 300 // Manually construct the result instead of using |
316 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of | 301 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of |
317 // enums correctly. | 302 // enums correctly. |
318 SetResult(apibtle::CharacteristicToValue(&characteristic).release()); | 303 SetResult(apibtle::CharacteristicToValue(&characteristic).release()); |
319 SendResponse(true); | 304 SendResponse(true); |
320 | |
321 return true; | |
322 } | 305 } |
323 | 306 |
324 bool BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { | 307 void BluetoothLowEnergyGetCharacteristicsFunction::DoWork() { |
325 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
326 | 309 |
327 BluetoothLowEnergyEventRouter* event_router = | 310 BluetoothLowEnergyEventRouter* event_router = |
328 GetEventRouter(browser_context()); | 311 GetEventRouter(browser_context()); |
329 | 312 |
330 // The adapter must be initialized at this point, but return an error instead | 313 // The adapter must be initialized at this point, but return an error instead |
331 // of asserting. | 314 // of asserting. |
332 if (!event_router->HasAdapter()) { | 315 if (!event_router->HasAdapter()) { |
333 SetError(kErrorAdapterNotInitialized); | 316 SetError(kErrorAdapterNotInitialized); |
334 SendResponse(false); | 317 SendResponse(false); |
335 return false; | 318 return; |
336 } | 319 } |
337 | 320 |
338 scoped_ptr<apibtle::GetCharacteristics::Params> params( | 321 scoped_ptr<apibtle::GetCharacteristics::Params> params( |
339 apibtle::GetCharacteristics::Params::Create(*args_)); | 322 apibtle::GetCharacteristics::Params::Create(*args_)); |
340 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 323 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
341 | 324 |
342 BluetoothLowEnergyEventRouter::CharacteristicList characteristic_list; | 325 BluetoothLowEnergyEventRouter::CharacteristicList characteristic_list; |
343 BluetoothLowEnergyEventRouter::Status status = | 326 BluetoothLowEnergyEventRouter::Status status = |
344 event_router->GetCharacteristics( | 327 event_router->GetCharacteristics( |
345 GetExtension(), params->service_id, &characteristic_list); | 328 GetExtension(), params->service_id, &characteristic_list); |
346 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 329 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
347 SetError(StatusToString(status)); | 330 SetError(StatusToString(status)); |
348 SendResponse(false); | 331 SendResponse(false); |
349 return false; | 332 return; |
350 } | 333 } |
351 | 334 |
352 // Manually construct the result instead of using | 335 // Manually construct the result instead of using |
353 // apibtle::GetCharacteristics::Result::Create as it doesn't convert lists of | 336 // apibtle::GetCharacteristics::Result::Create as it doesn't convert lists of |
354 // enums correctly. | 337 // enums correctly. |
355 scoped_ptr<base::ListValue> result(new base::ListValue()); | 338 scoped_ptr<base::ListValue> result(new base::ListValue()); |
356 for (BluetoothLowEnergyEventRouter::CharacteristicList::iterator iter = | 339 for (BluetoothLowEnergyEventRouter::CharacteristicList::iterator iter = |
357 characteristic_list.begin(); | 340 characteristic_list.begin(); |
358 iter != characteristic_list.end(); | 341 iter != characteristic_list.end(); |
359 ++iter) | 342 ++iter) |
360 result->Append(apibtle::CharacteristicToValue(iter->get()).release()); | 343 result->Append(apibtle::CharacteristicToValue(iter->get()).release()); |
361 | 344 |
362 SetResult(result.release()); | 345 SetResult(result.release()); |
363 SendResponse(true); | 346 SendResponse(true); |
364 | |
365 return true; | |
366 } | 347 } |
367 | 348 |
368 bool BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { | 349 void BluetoothLowEnergyGetIncludedServicesFunction::DoWork() { |
369 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
370 | 351 |
371 BluetoothLowEnergyEventRouter* event_router = | 352 BluetoothLowEnergyEventRouter* event_router = |
372 GetEventRouter(browser_context()); | 353 GetEventRouter(browser_context()); |
373 | 354 |
374 // The adapter must be initialized at this point, but return an error instead | 355 // The adapter must be initialized at this point, but return an error instead |
375 // of asserting. | 356 // of asserting. |
376 if (!event_router->HasAdapter()) { | 357 if (!event_router->HasAdapter()) { |
377 SetError(kErrorAdapterNotInitialized); | 358 SetError(kErrorAdapterNotInitialized); |
378 SendResponse(false); | 359 SendResponse(false); |
379 return false; | 360 return; |
380 } | 361 } |
381 | 362 |
382 scoped_ptr<apibtle::GetIncludedServices::Params> params( | 363 scoped_ptr<apibtle::GetIncludedServices::Params> params( |
383 apibtle::GetIncludedServices::Params::Create(*args_)); | 364 apibtle::GetIncludedServices::Params::Create(*args_)); |
384 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 365 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
385 | 366 |
386 BluetoothLowEnergyEventRouter::ServiceList service_list; | 367 BluetoothLowEnergyEventRouter::ServiceList service_list; |
387 BluetoothLowEnergyEventRouter::Status status = | 368 BluetoothLowEnergyEventRouter::Status status = |
388 event_router->GetIncludedServices(params->service_id, &service_list); | 369 event_router->GetIncludedServices(params->service_id, &service_list); |
389 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 370 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
390 SetError(StatusToString(status)); | 371 SetError(StatusToString(status)); |
391 SendResponse(false); | 372 SendResponse(false); |
392 return false; | 373 return; |
393 } | 374 } |
394 | 375 |
395 results_ = apibtle::GetIncludedServices::Results::Create(service_list); | 376 results_ = apibtle::GetIncludedServices::Results::Create(service_list); |
396 SendResponse(true); | 377 SendResponse(true); |
397 | |
398 return true; | |
399 } | 378 } |
400 | 379 |
401 bool BluetoothLowEnergyGetDescriptorFunction::DoWork() { | 380 void BluetoothLowEnergyGetDescriptorFunction::DoWork() { |
402 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 381 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
403 | 382 |
404 BluetoothLowEnergyEventRouter* event_router = | 383 BluetoothLowEnergyEventRouter* event_router = |
405 GetEventRouter(browser_context()); | 384 GetEventRouter(browser_context()); |
406 | 385 |
407 // The adapter must be initialized at this point, but return an error instead | 386 // The adapter must be initialized at this point, but return an error instead |
408 // of asserting. | 387 // of asserting. |
409 if (!event_router->HasAdapter()) { | 388 if (!event_router->HasAdapter()) { |
410 SetError(kErrorAdapterNotInitialized); | 389 SetError(kErrorAdapterNotInitialized); |
411 SendResponse(false); | 390 SendResponse(false); |
412 return false; | 391 return; |
413 } | 392 } |
414 | 393 |
415 scoped_ptr<apibtle::GetDescriptor::Params> params( | 394 scoped_ptr<apibtle::GetDescriptor::Params> params( |
416 apibtle::GetDescriptor::Params::Create(*args_)); | 395 apibtle::GetDescriptor::Params::Create(*args_)); |
417 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 396 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
418 | 397 |
419 apibtle::Descriptor descriptor; | 398 apibtle::Descriptor descriptor; |
420 BluetoothLowEnergyEventRouter::Status status = event_router->GetDescriptor( | 399 BluetoothLowEnergyEventRouter::Status status = event_router->GetDescriptor( |
421 GetExtension(), params->descriptor_id, &descriptor); | 400 GetExtension(), params->descriptor_id, &descriptor); |
422 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 401 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
423 SetError(StatusToString(status)); | 402 SetError(StatusToString(status)); |
424 SendResponse(false); | 403 SendResponse(false); |
425 return false; | 404 return; |
426 } | 405 } |
427 | 406 |
428 // Manually construct the result instead of using | 407 // Manually construct the result instead of using |
429 // apibtle::GetDescriptor::Result::Create as it doesn't convert lists of enums | 408 // apibtle::GetDescriptor::Result::Create as it doesn't convert lists of enums |
430 // correctly. | 409 // correctly. |
431 SetResult(apibtle::DescriptorToValue(&descriptor).release()); | 410 SetResult(apibtle::DescriptorToValue(&descriptor).release()); |
432 SendResponse(true); | 411 SendResponse(true); |
433 | |
434 return true; | |
435 } | 412 } |
436 | 413 |
437 bool BluetoothLowEnergyGetDescriptorsFunction::DoWork() { | 414 void BluetoothLowEnergyGetDescriptorsFunction::DoWork() { |
438 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 415 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
439 | 416 |
440 BluetoothLowEnergyEventRouter* event_router = | 417 BluetoothLowEnergyEventRouter* event_router = |
441 GetEventRouter(browser_context()); | 418 GetEventRouter(browser_context()); |
442 | 419 |
443 // The adapter must be initialized at this point, but return an error instead | 420 // The adapter must be initialized at this point, but return an error instead |
444 // of asserting. | 421 // of asserting. |
445 if (!event_router->HasAdapter()) { | 422 if (!event_router->HasAdapter()) { |
446 SetError(kErrorAdapterNotInitialized); | 423 SetError(kErrorAdapterNotInitialized); |
447 SendResponse(false); | 424 SendResponse(false); |
448 return false; | 425 return; |
449 } | 426 } |
450 | 427 |
451 scoped_ptr<apibtle::GetDescriptors::Params> params( | 428 scoped_ptr<apibtle::GetDescriptors::Params> params( |
452 apibtle::GetDescriptors::Params::Create(*args_)); | 429 apibtle::GetDescriptors::Params::Create(*args_)); |
453 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 430 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
454 | 431 |
455 BluetoothLowEnergyEventRouter::DescriptorList descriptor_list; | 432 BluetoothLowEnergyEventRouter::DescriptorList descriptor_list; |
456 BluetoothLowEnergyEventRouter::Status status = event_router->GetDescriptors( | 433 BluetoothLowEnergyEventRouter::Status status = event_router->GetDescriptors( |
457 GetExtension(), params->characteristic_id, &descriptor_list); | 434 GetExtension(), params->characteristic_id, &descriptor_list); |
458 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 435 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
459 SetError(StatusToString(status)); | 436 SetError(StatusToString(status)); |
460 SendResponse(false); | 437 SendResponse(false); |
461 return false; | 438 return; |
462 } | 439 } |
463 | 440 |
464 // Manually construct the result instead of using | 441 // Manually construct the result instead of using |
465 // apibtle::GetDescriptors::Result::Create as it doesn't convert lists of | 442 // apibtle::GetDescriptors::Result::Create as it doesn't convert lists of |
466 // enums correctly. | 443 // enums correctly. |
467 scoped_ptr<base::ListValue> result(new base::ListValue()); | 444 scoped_ptr<base::ListValue> result(new base::ListValue()); |
468 for (BluetoothLowEnergyEventRouter::DescriptorList::iterator iter = | 445 for (BluetoothLowEnergyEventRouter::DescriptorList::iterator iter = |
469 descriptor_list.begin(); | 446 descriptor_list.begin(); |
470 iter != descriptor_list.end(); | 447 iter != descriptor_list.end(); |
471 ++iter) | 448 ++iter) |
472 result->Append(apibtle::DescriptorToValue(iter->get()).release()); | 449 result->Append(apibtle::DescriptorToValue(iter->get()).release()); |
473 | 450 |
474 SetResult(result.release()); | 451 SetResult(result.release()); |
475 SendResponse(true); | 452 SendResponse(true); |
476 | |
477 return true; | |
478 } | 453 } |
479 | 454 |
480 bool BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { | 455 void BluetoothLowEnergyReadCharacteristicValueFunction::DoWork() { |
481 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 456 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
482 | 457 |
483 BluetoothLowEnergyEventRouter* event_router = | 458 BluetoothLowEnergyEventRouter* event_router = |
484 GetEventRouter(browser_context()); | 459 GetEventRouter(browser_context()); |
485 | 460 |
486 // The adapter must be initialized at this point, but return an error instead | 461 // The adapter must be initialized at this point, but return an error instead |
487 // of asserting. | 462 // of asserting. |
488 if (!event_router->HasAdapter()) { | 463 if (!event_router->HasAdapter()) { |
489 SetError(kErrorAdapterNotInitialized); | 464 SetError(kErrorAdapterNotInitialized); |
490 SendResponse(false); | 465 SendResponse(false); |
491 return false; | 466 return; |
492 } | 467 } |
493 | 468 |
494 scoped_ptr<apibtle::ReadCharacteristicValue::Params> params( | 469 scoped_ptr<apibtle::ReadCharacteristicValue::Params> params( |
495 apibtle::ReadCharacteristicValue::Params::Create(*args_)); | 470 apibtle::ReadCharacteristicValue::Params::Create(*args_)); |
496 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 471 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
497 | 472 |
498 instance_id_ = params->characteristic_id; | 473 instance_id_ = params->characteristic_id; |
499 event_router->ReadCharacteristicValue( | 474 event_router->ReadCharacteristicValue( |
500 GetExtension(), | 475 GetExtension(), |
501 instance_id_, | 476 instance_id_, |
502 base::Bind( | 477 base::Bind( |
503 &BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback, | 478 &BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback, |
504 this), | 479 this), |
505 base::Bind( | 480 base::Bind( |
506 &BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback, | 481 &BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback, |
507 this)); | 482 this)); |
508 | |
509 return true; | |
510 } | 483 } |
511 | 484 |
512 void BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback() { | 485 void BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback() { |
513 // Obtain info on the characteristic and see whether or not the characteristic | 486 // Obtain info on the characteristic and see whether or not the characteristic |
514 // is still around. | 487 // is still around. |
515 apibtle::Characteristic characteristic; | 488 apibtle::Characteristic characteristic; |
516 BluetoothLowEnergyEventRouter::Status status = | 489 BluetoothLowEnergyEventRouter::Status status = |
517 GetEventRouter(browser_context()) | 490 GetEventRouter(browser_context()) |
518 ->GetCharacteristic(GetExtension(), instance_id_, &characteristic); | 491 ->GetCharacteristic(GetExtension(), instance_id_, &characteristic); |
519 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 492 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
520 SetError(StatusToString(status)); | 493 SetError(StatusToString(status)); |
521 SendResponse(false); | 494 SendResponse(false); |
522 return; | 495 return; |
523 } | 496 } |
524 | 497 |
525 // Manually construct the result instead of using | 498 // Manually construct the result instead of using |
526 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of | 499 // apibtle::GetCharacteristic::Result::Create as it doesn't convert lists of |
527 // enums correctly. | 500 // enums correctly. |
528 SetResult(apibtle::CharacteristicToValue(&characteristic).release()); | 501 SetResult(apibtle::CharacteristicToValue(&characteristic).release()); |
529 SendResponse(true); | 502 SendResponse(true); |
530 } | 503 } |
531 | 504 |
532 void BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback( | 505 void BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback( |
533 BluetoothLowEnergyEventRouter::Status status) { | 506 BluetoothLowEnergyEventRouter::Status status) { |
534 SetError(StatusToString(status)); | 507 SetError(StatusToString(status)); |
535 SendResponse(false); | 508 SendResponse(false); |
536 } | 509 } |
537 | 510 |
538 bool BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { | 511 void BluetoothLowEnergyWriteCharacteristicValueFunction::DoWork() { |
539 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 512 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
540 | 513 |
541 BluetoothLowEnergyEventRouter* event_router = | 514 BluetoothLowEnergyEventRouter* event_router = |
542 GetEventRouter(browser_context()); | 515 GetEventRouter(browser_context()); |
543 | 516 |
544 // The adapter must be initialized at this point, but return an error instead | 517 // The adapter must be initialized at this point, but return an error instead |
545 // of asserting. | 518 // of asserting. |
546 if (!event_router->HasAdapter()) { | 519 if (!event_router->HasAdapter()) { |
547 SetError(kErrorAdapterNotInitialized); | 520 SetError(kErrorAdapterNotInitialized); |
548 SendResponse(false); | 521 SendResponse(false); |
549 return false; | 522 return; |
550 } | 523 } |
551 | 524 |
552 scoped_ptr<apibtle::WriteCharacteristicValue::Params> params( | 525 scoped_ptr<apibtle::WriteCharacteristicValue::Params> params( |
553 apibtle::WriteCharacteristicValue::Params::Create(*args_)); | 526 apibtle::WriteCharacteristicValue::Params::Create(*args_)); |
554 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 527 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
555 | 528 |
556 std::vector<uint8> value(params->value.begin(), params->value.end()); | 529 std::vector<uint8> value(params->value.begin(), params->value.end()); |
557 event_router->WriteCharacteristicValue( | 530 event_router->WriteCharacteristicValue( |
558 GetExtension(), | 531 GetExtension(), |
559 params->characteristic_id, | 532 params->characteristic_id, |
560 value, | 533 value, |
561 base::Bind( | 534 base::Bind( |
562 &BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback, | 535 &BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback, |
563 this), | 536 this), |
564 base::Bind( | 537 base::Bind( |
565 &BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback, | 538 &BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback, |
566 this)); | 539 this)); |
567 | |
568 return true; | |
569 } | 540 } |
570 | 541 |
571 void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() { | 542 void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() { |
572 results_ = apibtle::WriteCharacteristicValue::Results::Create(); | 543 results_ = apibtle::WriteCharacteristicValue::Results::Create(); |
573 SendResponse(true); | 544 SendResponse(true); |
574 } | 545 } |
575 | 546 |
576 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback( | 547 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback( |
577 BluetoothLowEnergyEventRouter::Status status) { | 548 BluetoothLowEnergyEventRouter::Status status) { |
578 SetError(StatusToString(status)); | 549 SetError(StatusToString(status)); |
579 SendResponse(false); | 550 SendResponse(false); |
580 } | 551 } |
581 | 552 |
582 bool BluetoothLowEnergyStartCharacteristicNotificationsFunction::DoWork() { | 553 void BluetoothLowEnergyStartCharacteristicNotificationsFunction::DoWork() { |
583 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 554 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
584 | 555 |
585 BluetoothLowEnergyEventRouter* event_router = | 556 BluetoothLowEnergyEventRouter* event_router = |
586 GetEventRouter(browser_context()); | 557 GetEventRouter(browser_context()); |
587 | 558 |
588 // The adapter must be initialized at this point, but return an error instead | 559 // The adapter must be initialized at this point, but return an error instead |
589 // of asserting. | 560 // of asserting. |
590 if (!event_router->HasAdapter()) { | 561 if (!event_router->HasAdapter()) { |
591 SetError(kErrorAdapterNotInitialized); | 562 SetError(kErrorAdapterNotInitialized); |
592 SendResponse(false); | 563 SendResponse(false); |
593 return false; | 564 return; |
594 } | 565 } |
595 | 566 |
596 scoped_ptr<apibtle::StartCharacteristicNotifications::Params> params( | 567 scoped_ptr<apibtle::StartCharacteristicNotifications::Params> params( |
597 apibtle::StartCharacteristicNotifications::Params::Create(*args_)); | 568 apibtle::StartCharacteristicNotifications::Params::Create(*args_)); |
598 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 569 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
599 | 570 |
600 bool persistent = false; // Not persistent by default. | 571 bool persistent = false; // Not persistent by default. |
601 apibtle::NotificationProperties* properties = params.get()->properties.get(); | 572 apibtle::NotificationProperties* properties = params.get()->properties.get(); |
602 if (properties) | 573 if (properties) |
603 persistent = properties->persistent; | 574 persistent = properties->persistent; |
604 | 575 |
605 event_router->StartCharacteristicNotifications( | 576 event_router->StartCharacteristicNotifications( |
606 persistent, | 577 persistent, |
607 GetExtension(), | 578 GetExtension(), |
608 params->characteristic_id, | 579 params->characteristic_id, |
609 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction:: | 580 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction:: |
610 SuccessCallback, | 581 SuccessCallback, |
611 this), | 582 this), |
612 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction:: | 583 base::Bind(&BluetoothLowEnergyStartCharacteristicNotificationsFunction:: |
613 ErrorCallback, | 584 ErrorCallback, |
614 this)); | 585 this)); |
615 | |
616 return true; | |
617 } | 586 } |
618 | 587 |
619 void | 588 void |
620 BluetoothLowEnergyStartCharacteristicNotificationsFunction::SuccessCallback() { | 589 BluetoothLowEnergyStartCharacteristicNotificationsFunction::SuccessCallback() { |
621 SendResponse(true); | 590 SendResponse(true); |
622 } | 591 } |
623 | 592 |
624 void BluetoothLowEnergyStartCharacteristicNotificationsFunction::ErrorCallback( | 593 void BluetoothLowEnergyStartCharacteristicNotificationsFunction::ErrorCallback( |
625 BluetoothLowEnergyEventRouter::Status status) { | 594 BluetoothLowEnergyEventRouter::Status status) { |
626 SetError(StatusToString(status)); | 595 SetError(StatusToString(status)); |
627 SendResponse(false); | 596 SendResponse(false); |
628 } | 597 } |
629 | 598 |
630 bool BluetoothLowEnergyStopCharacteristicNotificationsFunction::DoWork() { | 599 void BluetoothLowEnergyStopCharacteristicNotificationsFunction::DoWork() { |
631 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 600 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
632 | 601 |
633 BluetoothLowEnergyEventRouter* event_router = | 602 BluetoothLowEnergyEventRouter* event_router = |
634 GetEventRouter(browser_context()); | 603 GetEventRouter(browser_context()); |
635 | 604 |
636 // The adapter must be initialized at this point, but return an error instead | 605 // The adapter must be initialized at this point, but return an error instead |
637 // of asserting. | 606 // of asserting. |
638 if (!event_router->HasAdapter()) { | 607 if (!event_router->HasAdapter()) { |
639 SetError(kErrorAdapterNotInitialized); | 608 SetError(kErrorAdapterNotInitialized); |
640 SendResponse(false); | 609 SendResponse(false); |
641 return false; | 610 return; |
642 } | 611 } |
643 | 612 |
644 scoped_ptr<apibtle::StopCharacteristicNotifications::Params> params( | 613 scoped_ptr<apibtle::StopCharacteristicNotifications::Params> params( |
645 apibtle::StopCharacteristicNotifications::Params::Create(*args_)); | 614 apibtle::StopCharacteristicNotifications::Params::Create(*args_)); |
646 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 615 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
647 | 616 |
648 event_router->StopCharacteristicNotifications( | 617 event_router->StopCharacteristicNotifications( |
649 GetExtension(), | 618 GetExtension(), |
650 params->characteristic_id, | 619 params->characteristic_id, |
651 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction:: | 620 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction:: |
652 SuccessCallback, | 621 SuccessCallback, |
653 this), | 622 this), |
654 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction:: | 623 base::Bind(&BluetoothLowEnergyStopCharacteristicNotificationsFunction:: |
655 ErrorCallback, | 624 ErrorCallback, |
656 this)); | 625 this)); |
657 | |
658 return true; | |
659 } | 626 } |
660 | 627 |
661 void | 628 void |
662 BluetoothLowEnergyStopCharacteristicNotificationsFunction::SuccessCallback() { | 629 BluetoothLowEnergyStopCharacteristicNotificationsFunction::SuccessCallback() { |
663 SendResponse(true); | 630 SendResponse(true); |
664 } | 631 } |
665 | 632 |
666 void BluetoothLowEnergyStopCharacteristicNotificationsFunction::ErrorCallback( | 633 void BluetoothLowEnergyStopCharacteristicNotificationsFunction::ErrorCallback( |
667 BluetoothLowEnergyEventRouter::Status status) { | 634 BluetoothLowEnergyEventRouter::Status status) { |
668 SetError(StatusToString(status)); | 635 SetError(StatusToString(status)); |
669 SendResponse(false); | 636 SendResponse(false); |
670 } | 637 } |
671 | 638 |
672 bool BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { | 639 void BluetoothLowEnergyReadDescriptorValueFunction::DoWork() { |
673 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 640 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
674 | 641 |
675 BluetoothLowEnergyEventRouter* event_router = | 642 BluetoothLowEnergyEventRouter* event_router = |
676 GetEventRouter(browser_context()); | 643 GetEventRouter(browser_context()); |
677 | 644 |
678 // The adapter must be initialized at this point, but return an error instead | 645 // The adapter must be initialized at this point, but return an error instead |
679 // of asserting. | 646 // of asserting. |
680 if (!event_router->HasAdapter()) { | 647 if (!event_router->HasAdapter()) { |
681 SetError(kErrorAdapterNotInitialized); | 648 SetError(kErrorAdapterNotInitialized); |
682 SendResponse(false); | 649 SendResponse(false); |
683 return false; | 650 return; |
684 } | 651 } |
685 | 652 |
686 scoped_ptr<apibtle::ReadDescriptorValue::Params> params( | 653 scoped_ptr<apibtle::ReadDescriptorValue::Params> params( |
687 apibtle::ReadDescriptorValue::Params::Create(*args_)); | 654 apibtle::ReadDescriptorValue::Params::Create(*args_)); |
688 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 655 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
689 | 656 |
690 instance_id_ = params->descriptor_id; | 657 instance_id_ = params->descriptor_id; |
691 event_router->ReadDescriptorValue( | 658 event_router->ReadDescriptorValue( |
692 GetExtension(), | 659 GetExtension(), |
693 instance_id_, | 660 instance_id_, |
694 base::Bind( | 661 base::Bind( |
695 &BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback, | 662 &BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback, |
696 this), | 663 this), |
697 base::Bind(&BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback, | 664 base::Bind(&BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback, |
698 this)); | 665 this)); |
699 | |
700 return true; | |
701 } | 666 } |
702 | 667 |
703 void BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback() { | 668 void BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback() { |
704 // Obtain info on the descriptor and see whether or not the descriptor is | 669 // Obtain info on the descriptor and see whether or not the descriptor is |
705 // still around. | 670 // still around. |
706 apibtle::Descriptor descriptor; | 671 apibtle::Descriptor descriptor; |
707 BluetoothLowEnergyEventRouter::Status status = | 672 BluetoothLowEnergyEventRouter::Status status = |
708 GetEventRouter(browser_context()) | 673 GetEventRouter(browser_context()) |
709 ->GetDescriptor(GetExtension(), instance_id_, &descriptor); | 674 ->GetDescriptor(GetExtension(), instance_id_, &descriptor); |
710 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { | 675 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) { |
711 SetError(StatusToString(status)); | 676 SetError(StatusToString(status)); |
712 SendResponse(false); | 677 SendResponse(false); |
713 return; | 678 return; |
714 } | 679 } |
715 | 680 |
716 // Manually construct the result instead of using | 681 // Manually construct the result instead of using |
717 // apibtle::GetDescriptor::Results::Create as it doesn't convert lists of | 682 // apibtle::GetDescriptor::Results::Create as it doesn't convert lists of |
718 // enums correctly. | 683 // enums correctly. |
719 SetResult(apibtle::DescriptorToValue(&descriptor).release()); | 684 SetResult(apibtle::DescriptorToValue(&descriptor).release()); |
720 SendResponse(true); | 685 SendResponse(true); |
721 } | 686 } |
722 | 687 |
723 void BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback( | 688 void BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback( |
724 BluetoothLowEnergyEventRouter::Status status) { | 689 BluetoothLowEnergyEventRouter::Status status) { |
725 SetError(StatusToString(status)); | 690 SetError(StatusToString(status)); |
726 SendResponse(false); | 691 SendResponse(false); |
727 } | 692 } |
728 | 693 |
729 bool BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { | 694 void BluetoothLowEnergyWriteDescriptorValueFunction::DoWork() { |
730 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 695 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
731 | 696 |
732 BluetoothLowEnergyEventRouter* event_router = | 697 BluetoothLowEnergyEventRouter* event_router = |
733 GetEventRouter(browser_context()); | 698 GetEventRouter(browser_context()); |
734 | 699 |
735 // The adapter must be initialized at this point, but return an error instead | 700 // The adapter must be initialized at this point, but return an error instead |
736 // of asserting. | 701 // of asserting. |
737 if (!event_router->HasAdapter()) { | 702 if (!event_router->HasAdapter()) { |
738 SetError(kErrorAdapterNotInitialized); | 703 SetError(kErrorAdapterNotInitialized); |
739 SendResponse(false); | 704 SendResponse(false); |
740 return false; | 705 return; |
741 } | 706 } |
742 | 707 |
743 scoped_ptr<apibtle::WriteDescriptorValue::Params> params( | 708 scoped_ptr<apibtle::WriteDescriptorValue::Params> params( |
744 apibtle::WriteDescriptorValue::Params::Create(*args_)); | 709 apibtle::WriteDescriptorValue::Params::Create(*args_)); |
745 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); | 710 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); |
746 | 711 |
747 std::vector<uint8> value(params->value.begin(), params->value.end()); | 712 std::vector<uint8> value(params->value.begin(), params->value.end()); |
748 event_router->WriteDescriptorValue( | 713 event_router->WriteDescriptorValue( |
749 GetExtension(), | 714 GetExtension(), |
750 params->descriptor_id, | 715 params->descriptor_id, |
751 value, | 716 value, |
752 base::Bind( | 717 base::Bind( |
753 &BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback, | 718 &BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback, |
754 this), | 719 this), |
755 base::Bind(&BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback, | 720 base::Bind(&BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback, |
756 this)); | 721 this)); |
757 | |
758 return true; | |
759 } | 722 } |
760 | 723 |
761 void BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback() { | 724 void BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback() { |
762 results_ = apibtle::WriteDescriptorValue::Results::Create(); | 725 results_ = apibtle::WriteDescriptorValue::Results::Create(); |
763 SendResponse(true); | 726 SendResponse(true); |
764 } | 727 } |
765 | 728 |
766 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback( | 729 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback( |
767 BluetoothLowEnergyEventRouter::Status status) { | 730 BluetoothLowEnergyEventRouter::Status status) { |
768 SetError(StatusToString(status)); | 731 SetError(StatusToString(status)); |
769 SendResponse(false); | 732 SendResponse(false); |
770 } | 733 } |
771 | 734 |
772 } // namespace api | 735 } // namespace api |
773 } // namespace extensions | 736 } // namespace extensions |
OLD | NEW |