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

Side by Side Diff: chrome/browser/extensions/api/bluetooth_low_energy/bluetooth_low_energy_api.cc

Issue 349433002: chrome.bluetoothLowEnergy: Introduce connect and disconnect methods. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebased & marked occurrences isLocal as nodoc Created 6 years, 6 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 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"
11 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" 11 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h"
12 #include "chrome/common/extensions/api/bluetooth_low_energy.h" 12 #include "chrome/common/extensions/api/bluetooth_low_energy.h"
13 #include "content/public/browser/browser_thread.h" 13 #include "content/public/browser/browser_thread.h"
14 #include "extensions/browser/event_router.h" 14 #include "extensions/browser/event_router.h"
15 #include "extensions/common/permissions/permissions_data.h" 15 #include "extensions/common/permissions/permissions_data.h"
16 16
17 using content::BrowserContext; 17 using content::BrowserContext;
18 using content::BrowserThread; 18 using content::BrowserThread;
19 19
20 namespace apibtle = extensions::api::bluetooth_low_energy; 20 namespace apibtle = extensions::api::bluetooth_low_energy;
21 21
22 namespace extensions { 22 namespace extensions {
23 23
24 namespace { 24 namespace {
25 25
26 const char kErrorAdapterNotInitialized[] = 26 const char kErrorAdapterNotInitialized[] =
27 "Could not initialize Bluetooth adapter"; 27 "Could not initialize Bluetooth adapter";
28 const char kErrorAlreadyConnected[] = "Already connected";
29 const char kErrorNotConnected[] = "Not connected";
28 const char kErrorNotFound[] = "Instance not found"; 30 const char kErrorNotFound[] = "Instance not found";
29 const char kErrorOperationFailed[] = "Operation failed"; 31 const char kErrorOperationFailed[] = "Operation failed";
30 const char kErrorPermissionDenied[] = "Permission denied"; 32 const char kErrorPermissionDenied[] = "Permission denied";
31 const char kErrorPlatformNotSupported[] = 33 const char kErrorPlatformNotSupported[] =
32 "This operation is not supported on the current platform"; 34 "This operation is not supported on the current platform";
33 35
34 // Returns the correct error string based on error status |status|. This is used 36 // Returns the correct error string based on error status |status|. This is used
35 // to set the value of |chrome.runtime.lastError.message| and should not be 37 // to set the value of |chrome.runtime.lastError.message| and should not be
36 // passed |BluetoothLowEnergyEventRouter::kStatusSuccess|. 38 // passed |BluetoothLowEnergyEventRouter::kStatusSuccess|.
37 std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) { 39 std::string StatusToString(BluetoothLowEnergyEventRouter::Status status) {
38 switch (status) { 40 switch (status) {
39 case BluetoothLowEnergyEventRouter::kStatusErrorPermissionDenied: 41 case BluetoothLowEnergyEventRouter::kStatusErrorPermissionDenied:
40 return kErrorPermissionDenied; 42 return kErrorPermissionDenied;
41 case BluetoothLowEnergyEventRouter::kStatusErrorNotFound: 43 case BluetoothLowEnergyEventRouter::kStatusErrorNotFound:
42 return kErrorNotFound; 44 return kErrorNotFound;
45 case BluetoothLowEnergyEventRouter::kStatusErrorAlreadyConnected:
46 return kErrorAlreadyConnected;
47 case BluetoothLowEnergyEventRouter::kStatusErrorNotConnected:
48 return kErrorNotConnected;
43 case BluetoothLowEnergyEventRouter::kStatusSuccess: 49 case BluetoothLowEnergyEventRouter::kStatusSuccess:
44 NOTREACHED(); 50 NOTREACHED();
45 break; 51 break;
46 default: 52 default:
47 return kErrorOperationFailed; 53 return kErrorOperationFailed;
48 } 54 }
49 return ""; 55 return "";
50 } 56 }
51 57
52 extensions::BluetoothLowEnergyEventRouter* GetEventRouter( 58 extensions::BluetoothLowEnergyEventRouter* GetEventRouter(
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
118 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind( 124 if (!event_router->InitializeAdapterAndInvokeCallback(base::Bind(
119 &DoWorkCallback, 125 &DoWorkCallback,
120 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) { 126 base::Bind(&BluetoothLowEnergyExtensionFunction::DoWork, this)))) {
121 SetError(kErrorAdapterNotInitialized); 127 SetError(kErrorAdapterNotInitialized);
122 return false; 128 return false;
123 } 129 }
124 130
125 return true; 131 return true;
126 } 132 }
127 133
134 bool BluetoothLowEnergyConnectFunction::DoWork() {
135 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
136
137 BluetoothLowEnergyEventRouter* event_router =
138 GetEventRouter(browser_context());
139
140 // The adapter must be initialized at this point, but return an error instead
141 // of asserting.
142 if (!event_router->HasAdapter()) {
143 SetError(kErrorAdapterNotInitialized);
144 SendResponse(false);
145 return false;
146 }
147
148 scoped_ptr<apibtle::Connect::Params> params(
149 apibtle::Connect::Params::Create(*args_));
150 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
151
152 bool persistent = false; // Not persistent by default.
153 apibtle::ConnectProperties* properties = params.get()->properties.get();
154 if (properties)
155 persistent = properties->persistent;
156
157 event_router->Connect(
158 persistent,
159 GetExtension(),
160 params->device_address,
161 base::Bind(&BluetoothLowEnergyConnectFunction::SuccessCallback, this),
162 base::Bind(&BluetoothLowEnergyConnectFunction::ErrorCallback, this));
163
164 return true;
165 }
166
167 void BluetoothLowEnergyConnectFunction::SuccessCallback() {
168 SendResponse(true);
169 }
170
171 void BluetoothLowEnergyConnectFunction::ErrorCallback(
172 BluetoothLowEnergyEventRouter::Status status) {
173 SetError(StatusToString(status));
174 SendResponse(false);
175 }
176
177 bool BluetoothLowEnergyDisconnectFunction::DoWork() {
178 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
179
180 BluetoothLowEnergyEventRouter* event_router =
181 GetEventRouter(browser_context());
182
183 // The adapter must be initialized at this point, but return an error instead
184 // of asserting.
185 if (!event_router->HasAdapter()) {
186 SetError(kErrorAdapterNotInitialized);
187 SendResponse(false);
188 return false;
189 }
190
191 scoped_ptr<apibtle::Disconnect::Params> params(
192 apibtle::Disconnect::Params::Create(*args_));
193 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
194
195 event_router->Disconnect(
196 GetExtension(),
197 params->device_address,
198 base::Bind(&BluetoothLowEnergyDisconnectFunction::SuccessCallback, this),
199 base::Bind(&BluetoothLowEnergyDisconnectFunction::ErrorCallback, this));
200
201 return true;
202 }
203
204 void BluetoothLowEnergyDisconnectFunction::SuccessCallback() {
205 SendResponse(true);
206 }
207
208 void BluetoothLowEnergyDisconnectFunction::ErrorCallback(
209 BluetoothLowEnergyEventRouter::Status status) {
210 SetError(StatusToString(status));
211 SendResponse(false);
212 }
213
128 bool BluetoothLowEnergyGetServiceFunction::DoWork() { 214 bool BluetoothLowEnergyGetServiceFunction::DoWork() {
129 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 215 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
130 216
131 BluetoothLowEnergyEventRouter* event_router = 217 BluetoothLowEnergyEventRouter* event_router =
132 GetEventRouter(browser_context()); 218 GetEventRouter(browser_context());
133 219
134 // The adapter must be initialized at this point, but return an error instead 220 // The adapter must be initialized at this point, but return an error instead
135 // of asserting. 221 // of asserting.
136 if (!event_router->HasAdapter()) { 222 if (!event_router->HasAdapter()) {
137 SetError(kErrorAdapterNotInitialized); 223 SetError(kErrorAdapterNotInitialized);
(...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after
394 SetError(kErrorAdapterNotInitialized); 480 SetError(kErrorAdapterNotInitialized);
395 SendResponse(false); 481 SendResponse(false);
396 return false; 482 return false;
397 } 483 }
398 484
399 scoped_ptr<apibtle::ReadCharacteristicValue::Params> params( 485 scoped_ptr<apibtle::ReadCharacteristicValue::Params> params(
400 apibtle::ReadCharacteristicValue::Params::Create(*args_)); 486 apibtle::ReadCharacteristicValue::Params::Create(*args_));
401 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 487 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
402 488
403 instance_id_ = params->characteristic_id; 489 instance_id_ = params->characteristic_id;
404 BluetoothLowEnergyEventRouter::Status status = 490 event_router->ReadCharacteristicValue(
405 event_router->ReadCharacteristicValue( 491 GetExtension(),
406 GetExtension(), 492 instance_id_,
407 instance_id_, 493 base::Bind(
408 base::Bind(&BluetoothLowEnergyReadCharacteristicValueFunction:: 494 &BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback,
409 SuccessCallback, 495 this),
410 this), 496 base::Bind(
411 base::Bind( 497 &BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback,
412 &BluetoothLowEnergyReadCharacteristicValueFunction::ErrorCallback, 498 this));
413 this));
414 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) {
415 SetError(StatusToString(status));
416 SendResponse(false);
417 return false;
418 }
419 499
420 return true; 500 return true;
421 } 501 }
422 502
423 void BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback() { 503 void BluetoothLowEnergyReadCharacteristicValueFunction::SuccessCallback() {
424 // Obtain info on the characteristic and see whether or not the characteristic 504 // Obtain info on the characteristic and see whether or not the characteristic
425 // is still around. 505 // is still around.
426 apibtle::Characteristic characteristic; 506 apibtle::Characteristic characteristic;
427 BluetoothLowEnergyEventRouter::Status status = 507 BluetoothLowEnergyEventRouter::Status status =
428 GetEventRouter(browser_context()) 508 GetEventRouter(browser_context())
(...skipping 29 matching lines...) Expand all
458 SetError(kErrorAdapterNotInitialized); 538 SetError(kErrorAdapterNotInitialized);
459 SendResponse(false); 539 SendResponse(false);
460 return false; 540 return false;
461 } 541 }
462 542
463 scoped_ptr<apibtle::WriteCharacteristicValue::Params> params( 543 scoped_ptr<apibtle::WriteCharacteristicValue::Params> params(
464 apibtle::WriteCharacteristicValue::Params::Create(*args_)); 544 apibtle::WriteCharacteristicValue::Params::Create(*args_));
465 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 545 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
466 546
467 std::vector<uint8> value(params->value.begin(), params->value.end()); 547 std::vector<uint8> value(params->value.begin(), params->value.end());
468 548 event_router->WriteCharacteristicValue(
469 BluetoothLowEnergyEventRouter::Status status = 549 GetExtension(),
470 event_router->WriteCharacteristicValue( 550 params->characteristic_id,
471 GetExtension(), 551 value,
472 params->characteristic_id, 552 base::Bind(
473 value, 553 &BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback,
474 base::Bind(&BluetoothLowEnergyWriteCharacteristicValueFunction:: 554 this),
475 SuccessCallback, 555 base::Bind(
476 this), 556 &BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback,
477 base::Bind(&BluetoothLowEnergyWriteCharacteristicValueFunction:: 557 this));
478 ErrorCallback,
479 this));
480 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) {
481 SetError(StatusToString(status));
482 SendResponse(false);
483 return false;
484 }
485 558
486 return true; 559 return true;
487 } 560 }
488 561
489 void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() { 562 void BluetoothLowEnergyWriteCharacteristicValueFunction::SuccessCallback() {
490 results_ = apibtle::WriteCharacteristicValue::Results::Create(); 563 results_ = apibtle::WriteCharacteristicValue::Results::Create();
491 SendResponse(true); 564 SendResponse(true);
492 } 565 }
493 566
494 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback( 567 void BluetoothLowEnergyWriteCharacteristicValueFunction::ErrorCallback(
(...skipping 14 matching lines...) Expand all
509 SetError(kErrorAdapterNotInitialized); 582 SetError(kErrorAdapterNotInitialized);
510 SendResponse(false); 583 SendResponse(false);
511 return false; 584 return false;
512 } 585 }
513 586
514 scoped_ptr<apibtle::ReadDescriptorValue::Params> params( 587 scoped_ptr<apibtle::ReadDescriptorValue::Params> params(
515 apibtle::ReadDescriptorValue::Params::Create(*args_)); 588 apibtle::ReadDescriptorValue::Params::Create(*args_));
516 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 589 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
517 590
518 instance_id_ = params->descriptor_id; 591 instance_id_ = params->descriptor_id;
519 BluetoothLowEnergyEventRouter::Status status = 592 event_router->ReadDescriptorValue(
520 event_router->ReadDescriptorValue( 593 GetExtension(),
521 GetExtension(), 594 instance_id_,
522 instance_id_, 595 base::Bind(
523 base::Bind( 596 &BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback,
524 &BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback, 597 this),
525 this), 598 base::Bind(&BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback,
526 base::Bind( 599 this));
527 &BluetoothLowEnergyReadDescriptorValueFunction::ErrorCallback,
528 this));
529 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) {
530 SetError(StatusToString(status));
531 SendResponse(false);
532 return false;
533 }
534 600
535 return true; 601 return true;
536 } 602 }
537 603
538 void BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback() { 604 void BluetoothLowEnergyReadDescriptorValueFunction::SuccessCallback() {
539 // Obtain info on the descriptor and see whether or not the descriptor is 605 // Obtain info on the descriptor and see whether or not the descriptor is
540 // still around. 606 // still around.
541 apibtle::Descriptor descriptor; 607 apibtle::Descriptor descriptor;
542 BluetoothLowEnergyEventRouter::Status status = 608 BluetoothLowEnergyEventRouter::Status status =
543 GetEventRouter(browser_context()) 609 GetEventRouter(browser_context())
(...skipping 29 matching lines...) Expand all
573 SetError(kErrorAdapterNotInitialized); 639 SetError(kErrorAdapterNotInitialized);
574 SendResponse(false); 640 SendResponse(false);
575 return false; 641 return false;
576 } 642 }
577 643
578 scoped_ptr<apibtle::WriteDescriptorValue::Params> params( 644 scoped_ptr<apibtle::WriteDescriptorValue::Params> params(
579 apibtle::WriteDescriptorValue::Params::Create(*args_)); 645 apibtle::WriteDescriptorValue::Params::Create(*args_));
580 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL); 646 EXTENSION_FUNCTION_VALIDATE(params.get() != NULL);
581 647
582 std::vector<uint8> value(params->value.begin(), params->value.end()); 648 std::vector<uint8> value(params->value.begin(), params->value.end());
583 649 event_router->WriteDescriptorValue(
584 BluetoothLowEnergyEventRouter::Status status = 650 GetExtension(),
585 event_router->WriteDescriptorValue( 651 params->descriptor_id,
586 GetExtension(), 652 value,
587 params->descriptor_id, 653 base::Bind(
588 value, 654 &BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback,
589 base::Bind( 655 this),
590 &BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback, 656 base::Bind(&BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback,
591 this), 657 this));
592 base::Bind(
593 &BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback,
594 this));
595 if (status != BluetoothLowEnergyEventRouter::kStatusSuccess) {
596 SetError(StatusToString(status));
597 SendResponse(false);
598 return false;
599 }
600 658
601 return true; 659 return true;
602 } 660 }
603 661
604 void BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback() { 662 void BluetoothLowEnergyWriteDescriptorValueFunction::SuccessCallback() {
605 results_ = apibtle::WriteDescriptorValue::Results::Create(); 663 results_ = apibtle::WriteDescriptorValue::Results::Create();
606 SendResponse(true); 664 SendResponse(true);
607 } 665 }
608 666
609 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback( 667 void BluetoothLowEnergyWriteDescriptorValueFunction::ErrorCallback(
610 BluetoothLowEnergyEventRouter::Status status) { 668 BluetoothLowEnergyEventRouter::Status status) {
611 SetError(StatusToString(status)); 669 SetError(StatusToString(status));
612 SendResponse(false); 670 SendResponse(false);
613 } 671 }
614 672
615 } // namespace api 673 } // namespace api
616 } // namespace extensions 674 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698