Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(729)

Side by Side Diff: chrome/browser/extensions/api/bluetooth/bluetooth_api.cc

Issue 11743024: Implemented BluetoothExtensionFunction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merge Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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/bluetooth_api.h" 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api.h"
6 6
7 #if defined(OS_CHROMEOS) 7 #if defined(OS_CHROMEOS)
8 #include <errno.h> 8 #include <errno.h>
9 #endif 9 #endif
10 10
(...skipping 25 matching lines...) Expand all
36 using device::BluetoothDevice; 36 using device::BluetoothDevice;
37 using device::BluetoothServiceRecord; 37 using device::BluetoothServiceRecord;
38 using device::BluetoothSocket; 38 using device::BluetoothSocket;
39 39
40 namespace { 40 namespace {
41 41
42 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { 42 extensions::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) {
43 return extensions::BluetoothAPI::Get(profile)->bluetooth_event_router(); 43 return extensions::BluetoothAPI::Get(profile)->bluetooth_event_router();
44 } 44 }
45 45
46 const BluetoothAdapter* GetAdapter(Profile* profile) {
47 return GetEventRouter(profile)->adapter();
48 }
49
50 BluetoothAdapter* GetMutableAdapter(Profile* profile) {
51 return GetEventRouter(profile)->GetMutableAdapter();
52 }
53
54 bool IsBluetoothSupported(Profile* profile) {
55 return GetAdapter(profile) != NULL;
56 }
57
58 } // namespace 46 } // namespace
59 47
60 namespace { 48 namespace {
61 49
62 const char kCouldNotGetLocalOutOfBandPairingData[] = 50 const char kCouldNotGetLocalOutOfBandPairingData[] =
63 "Could not get local Out Of Band Pairing Data"; 51 "Could not get local Out Of Band Pairing Data";
64 const char kCouldNotSetOutOfBandPairingData[] = 52 const char kCouldNotSetOutOfBandPairingData[] =
65 "Could not set Out Of Band Pairing Data"; 53 "Could not set Out Of Band Pairing Data";
66 const char kDevicePermissionDenied[] = "Permission to access device denied"; 54 const char kDevicePermissionDenied[] = "Permission to access device denied";
67 const char kFailedToConnect[] = "Connection failed"; 55 const char kFailedToConnect[] = "Connection failed";
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
114 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) { 102 void BluetoothAPI::OnListenerAdded(const EventListenerInfo& details) {
115 bluetooth_event_router()->OnListenerAdded(); 103 bluetooth_event_router()->OnListenerAdded();
116 } 104 }
117 105
118 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) { 106 void BluetoothAPI::OnListenerRemoved(const EventListenerInfo& details) {
119 bluetooth_event_router()->OnListenerRemoved(); 107 bluetooth_event_router()->OnListenerRemoved();
120 } 108 }
121 109
122 namespace api { 110 namespace api {
123 111
124 bool BluetoothGetAdapterStateFunction::RunImpl() { 112 bool BluetoothGetAdapterStateFunction::DoWork(
125 if (!IsBluetoothSupported(profile())) { 113 scoped_refptr<BluetoothAdapter> adapter) {
126 SetError(kPlatformNotSupported);
127 return false;
128 }
129
130 bluetooth::AdapterState state; 114 bluetooth::AdapterState state;
131 PopulateAdapterState(*GetAdapter(profile()), &state); 115 PopulateAdapterState(*adapter, &state);
132 SetResult(state.ToValue().release()); 116 SetResult(state.ToValue().release());
117 SendResponse(true);
133 return true; 118 return true;
134 } 119 }
135 120
136 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() 121 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction()
137 : callbacks_pending_(0), 122 : callbacks_pending_(0),
138 device_events_sent_(0) {} 123 device_events_sent_(0) {}
139 124
140 void BluetoothGetDevicesFunction::DispatchDeviceSearchResult( 125 void BluetoothGetDevicesFunction::DispatchDeviceSearchResult(
141 const BluetoothDevice& device) { 126 const BluetoothDevice& device) {
142 bluetooth::Device extension_device; 127 bluetooth::Device extension_device;
(...skipping 25 matching lines...) Expand all
168 args->Append(info.release()); 153 args->Append(info.release());
169 154
170 scoped_ptr<extensions::Event> event(new extensions::Event( 155 scoped_ptr<extensions::Event> event(new extensions::Event(
171 extensions::event_names::kBluetoothOnDeviceSearchFinished, args.Pass())); 156 extensions::event_names::kBluetoothOnDeviceSearchFinished, args.Pass()));
172 extensions::ExtensionSystem::Get(profile())->event_router()-> 157 extensions::ExtensionSystem::Get(profile())->event_router()->
173 BroadcastEvent(event.Pass()); 158 BroadcastEvent(event.Pass());
174 159
175 SendResponse(true); 160 SendResponse(true);
176 } 161 }
177 162
178 bool BluetoothGetDevicesFunction::RunImpl() { 163 bool BluetoothGetDevicesFunction::DoWork(
179 if (!IsBluetoothSupported(profile())) { 164 scoped_refptr<BluetoothAdapter> adapter) {
180 SetError(kPlatformNotSupported);
181 return false;
182 }
183
184 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 165 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
185 166
186 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_)); 167 scoped_ptr<GetDevices::Params> params(GetDevices::Params::Create(*args_));
187 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 168 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
188 const bluetooth::GetDevicesOptions& options = params->options; 169 const bluetooth::GetDevicesOptions& options = params->options;
189 170
190 std::string uuid; 171 std::string uuid;
191 if (options.uuid.get() != NULL) { 172 if (options.uuid.get() != NULL) {
192 uuid = device::bluetooth_utils::CanonicalUuid(*options.uuid.get()); 173 uuid = device::bluetooth_utils::CanonicalUuid(*options.uuid.get());
193 if (uuid.empty()) { 174 if (uuid.empty()) {
194 SetError(kInvalidUuid); 175 SetError(kInvalidUuid);
176 SendResponse(false);
195 return false; 177 return false;
196 } 178 }
197 } 179 }
198 180
199 CHECK_EQ(0, callbacks_pending_); 181 CHECK_EQ(0, callbacks_pending_);
200 182
201 BluetoothAdapter::DeviceList devices = 183 BluetoothAdapter::DeviceList devices = adapter->GetDevices();
202 GetMutableAdapter(profile())->GetDevices();
203 for (BluetoothAdapter::DeviceList::iterator i = devices.begin(); 184 for (BluetoothAdapter::DeviceList::iterator i = devices.begin();
204 i != devices.end(); ++i) { 185 i != devices.end(); ++i) {
205 BluetoothDevice* device = *i; 186 BluetoothDevice* device = *i;
206 CHECK(device); 187 CHECK(device);
207 188
208 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid))) 189 if (!uuid.empty() && !(device->ProvidesServiceWithUUID(uuid)))
209 continue; 190 continue;
210 191
211 if (options.name.get() == NULL) { 192 if (options.name.get() == NULL) {
212 DispatchDeviceSearchResult(*device); 193 DispatchDeviceSearchResult(*device);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 } 226 }
246 227
247 SendResponse(true); 228 SendResponse(true);
248 } 229 }
249 230
250 void BluetoothGetServicesFunction::OnErrorCallback() { 231 void BluetoothGetServicesFunction::OnErrorCallback() {
251 SetError(kServiceDiscoveryFailed); 232 SetError(kServiceDiscoveryFailed);
252 SendResponse(false); 233 SendResponse(false);
253 } 234 }
254 235
255 bool BluetoothGetServicesFunction::RunImpl() { 236 bool BluetoothGetServicesFunction::DoWork(
256 if (!IsBluetoothSupported(profile())) { 237 scoped_refptr<BluetoothAdapter> adapter) {
257 SetError(kPlatformNotSupported);
258 return false;
259 }
260
261 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_)); 238 scoped_ptr<GetServices::Params> params(GetServices::Params::Create(*args_));
262 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 239 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
263 const bluetooth::GetServicesOptions& options = params->options; 240 const bluetooth::GetServicesOptions& options = params->options;
264 241
265 BluetoothDevice* device = 242 BluetoothDevice* device = adapter->GetDevice(options.device_address);
266 GetMutableAdapter(profile())->GetDevice(options.device_address);
267 if (!device) { 243 if (!device) {
268 SetError(kInvalidDevice); 244 SetError(kInvalidDevice);
245 SendResponse(false);
269 return false; 246 return false;
270 } 247 }
271 248
272 ListValue* services = new ListValue; 249 ListValue* services = new ListValue;
273 SetResult(services); 250 SetResult(services);
274 251
275 device->GetServiceRecords( 252 device->GetServiceRecords(
276 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback, 253 base::Bind(&BluetoothGetServicesFunction::GetServiceRecordsCallback,
277 this, 254 this,
278 services), 255 services),
(...skipping 15 matching lines...) Expand all
294 result_socket.service_uuid = service_uuid; 271 result_socket.service_uuid = service_uuid;
295 result_socket.id = socket_id; 272 result_socket.id = socket_id;
296 SetResult(result_socket.ToValue().release()); 273 SetResult(result_socket.ToValue().release());
297 SendResponse(true); 274 SendResponse(true);
298 } else { 275 } else {
299 SetError(kFailedToConnect); 276 SetError(kFailedToConnect);
300 SendResponse(false); 277 SendResponse(false);
301 } 278 }
302 } 279 }
303 280
304 bool BluetoothConnectFunction::RunImpl() { 281 bool BluetoothConnectFunction::DoWork(scoped_refptr<BluetoothAdapter> adapter) {
305 if (!IsBluetoothSupported(profile())) {
306 SetError(kPlatformNotSupported);
307 return false;
308 }
309
310 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_)); 282 scoped_ptr<Connect::Params> params(Connect::Params::Create(*args_));
311 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 283 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
312 const bluetooth::ConnectOptions& options = params->options; 284 const bluetooth::ConnectOptions& options = params->options;
313 285
314 BluetoothDevicePermission::CheckParam param(options.device_address); 286 BluetoothDevicePermission::CheckParam param(options.device_address);
315 if (!GetExtension()->CheckAPIPermissionWithParam( 287 if (!GetExtension()->CheckAPIPermissionWithParam(
316 APIPermission::kBluetoothDevice, &param)) { 288 APIPermission::kBluetoothDevice, &param)) {
317 SetError(kDevicePermissionDenied); 289 SetError(kDevicePermissionDenied);
290 SendResponse(false);
318 return false; 291 return false;
319 } 292 }
320 293
321 std::string uuid = device::bluetooth_utils::CanonicalUuid( 294 std::string uuid = device::bluetooth_utils::CanonicalUuid(
322 options.service_uuid); 295 options.service_uuid);
323 if (uuid.empty()) { 296 if (uuid.empty()) {
324 SetError(kInvalidUuid); 297 SetError(kInvalidUuid);
298 SendResponse(false);
325 return false; 299 return false;
326 } 300 }
327 301
328 BluetoothDevice* device = 302 BluetoothDevice* device = adapter->GetDevice(options.device_address);
329 GetMutableAdapter(profile())->GetDevice(options.device_address);
330 if (!device) { 303 if (!device) {
331 SetError(kInvalidDevice); 304 SetError(kInvalidDevice);
305 SendResponse(false);
332 return false; 306 return false;
333 } 307 }
334 308
335 device->ConnectToService(uuid, 309 device->ConnectToService(uuid,
336 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback, 310 base::Bind(&BluetoothConnectFunction::ConnectToServiceCallback,
337 this, 311 this,
338 device, 312 device,
339 uuid)); 313 uuid));
314
340 return true; 315 return true;
341 } 316 }
342 317
343 bool BluetoothDisconnectFunction::RunImpl() { 318 bool BluetoothDisconnectFunction::RunImpl() {
344 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_)); 319 scoped_ptr<Disconnect::Params> params(Disconnect::Params::Create(*args_));
345 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 320 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
346 const bluetooth::DisconnectOptions& options = params->options; 321 const bluetooth::DisconnectOptions& options = params->options;
347 return GetEventRouter(profile())->ReleaseSocket(options.socket_id); 322 return GetEventRouter(profile())->ReleaseSocket(options.socket_id);
348 } 323 }
349 324
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 442
468 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() { 443 void BluetoothSetOutOfBandPairingDataFunction::OnSuccessCallback() {
469 SendResponse(true); 444 SendResponse(true);
470 } 445 }
471 446
472 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() { 447 void BluetoothSetOutOfBandPairingDataFunction::OnErrorCallback() {
473 SetError(kCouldNotSetOutOfBandPairingData); 448 SetError(kCouldNotSetOutOfBandPairingData);
474 SendResponse(false); 449 SendResponse(false);
475 } 450 }
476 451
477 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { 452 bool BluetoothSetOutOfBandPairingDataFunction::DoWork(
478 if (!IsBluetoothSupported(profile())) { 453 scoped_refptr<BluetoothAdapter> adapter) {
479 SetError(kPlatformNotSupported);
480 return false;
481 }
482
483 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer 454 // TODO(bryeung): update to new-style parameter passing when ArrayBuffer
484 // support is added 455 // support is added
485 DictionaryValue* options; 456 DictionaryValue* options;
486 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); 457 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options));
487 std::string address; 458 std::string address;
488 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address)); 459 EXTENSION_FUNCTION_VALIDATE(options->GetString("deviceAddress", &address));
489 460
490 BluetoothDevice* device = GetMutableAdapter(profile())->GetDevice(address); 461 BluetoothDevice* device = adapter->GetDevice(address);
491 if (!device) { 462 if (!device) {
492 SetError(kInvalidDevice); 463 SetError(kInvalidDevice);
464 SendResponse(false);
493 return false; 465 return false;
494 } 466 }
495 467
496 if (options->HasKey("data")) { 468 if (options->HasKey("data")) {
497 DictionaryValue* data_in; 469 DictionaryValue* data_in;
498 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in)); 470 EXTENSION_FUNCTION_VALIDATE(options->GetDictionary("data", &data_in));
499 471
500 device::BluetoothOutOfBandPairingData data_out; 472 device::BluetoothOutOfBandPairingData data_out;
501 473
502 base::BinaryValue* tmp_data; 474 base::BinaryValue* tmp_data;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
549 SetResult(result); 521 SetResult(result);
550 522
551 SendResponse(true); 523 SendResponse(true);
552 } 524 }
553 525
554 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() { 526 void BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback() {
555 SetError(kCouldNotGetLocalOutOfBandPairingData); 527 SetError(kCouldNotGetLocalOutOfBandPairingData);
556 SendResponse(false); 528 SendResponse(false);
557 } 529 }
558 530
559 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { 531 bool BluetoothGetLocalOutOfBandPairingDataFunction::DoWork(
560 if (!IsBluetoothSupported(profile())) { 532 scoped_refptr<BluetoothAdapter> adapter) {
561 SetError(kPlatformNotSupported); 533 adapter->ReadLocalOutOfBandPairingData(
562 return false;
563 }
564
565 GetMutableAdapter(profile())->ReadLocalOutOfBandPairingData(
566 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, 534 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback,
567 this), 535 this),
568 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback, 536 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback,
569 this)); 537 this));
538
570 return true; 539 return true;
571 } 540 }
572 541
573 void BluetoothStartDiscoveryFunction::OnSuccessCallback() { 542 void BluetoothStartDiscoveryFunction::OnSuccessCallback() {
574 GetEventRouter(profile())->SetResponsibleForDiscovery(true); 543 GetEventRouter(profile())->SetResponsibleForDiscovery(true);
575 SendResponse(true); 544 SendResponse(true);
576 } 545 }
577 546
578 void BluetoothStartDiscoveryFunction::OnErrorCallback() { 547 void BluetoothStartDiscoveryFunction::OnErrorCallback() {
579 SetError(kStartDiscoveryFailed); 548 SetError(kStartDiscoveryFailed);
580 SendResponse(false); 549 SendResponse(false);
581 } 550 }
582 551
583 bool BluetoothStartDiscoveryFunction::RunImpl() { 552 bool BluetoothStartDiscoveryFunction::DoWork(
584 if (!IsBluetoothSupported(profile())) { 553 scoped_refptr<BluetoothAdapter> adapter) {
585 SetError(kPlatformNotSupported);
586 return false;
587 }
588
589 GetEventRouter(profile())->SetSendDiscoveryEvents(true); 554 GetEventRouter(profile())->SetSendDiscoveryEvents(true);
590 555
591 // If the adapter is already discovering, there is nothing else to do. 556 // If the adapter is already discovering, there is nothing else to do.
592 if (GetAdapter(profile())->IsDiscovering()) { 557 if (adapter->IsDiscovering()) {
593 SendResponse(true); 558 SendResponse(true);
594 return true; 559 return true;
595 } 560 }
596 561
597 GetMutableAdapter(profile())->SetDiscovering(true, 562 adapter->SetDiscovering(true,
598 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this), 563 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this),
599 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this)); 564 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this));
565
600 return true; 566 return true;
601 } 567 }
602 568
603 void BluetoothStopDiscoveryFunction::OnSuccessCallback() { 569 void BluetoothStopDiscoveryFunction::OnSuccessCallback() {
604 SendResponse(true); 570 SendResponse(true);
605 } 571 }
606 572
607 void BluetoothStopDiscoveryFunction::OnErrorCallback() { 573 void BluetoothStopDiscoveryFunction::OnErrorCallback() {
608 SetError(kStopDiscoveryFailed); 574 SetError(kStopDiscoveryFailed);
609 SendResponse(false); 575 SendResponse(false);
610 } 576 }
611 577
612 bool BluetoothStopDiscoveryFunction::RunImpl() { 578 bool BluetoothStopDiscoveryFunction::DoWork(
613 if (!IsBluetoothSupported(profile())) { 579 scoped_refptr<BluetoothAdapter> adapter) {
614 SetError(kPlatformNotSupported);
615 return false;
616 }
617
618 GetEventRouter(profile())->SetSendDiscoveryEvents(false); 580 GetEventRouter(profile())->SetSendDiscoveryEvents(false);
619 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) { 581 if (GetEventRouter(profile())->IsResponsibleForDiscovery()) {
620 GetMutableAdapter(profile())->SetDiscovering(false, 582 adapter->SetDiscovering(false,
621 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this), 583 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this),
622 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this)); 584 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this));
623 } 585 }
586
624 return true; 587 return true;
625 } 588 }
626 589
627 } // namespace api 590 } // namespace api
628 } // namespace extensions 591 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698