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

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

Issue 10536159: Bluetooth Extension API: Add a discovery API (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: reland Created 8 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 (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
11 #include <string> 11 #include <string>
12 12
13 #include "base/utf_string_conversions.h" 13 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h"
14 #include "chrome/browser/extensions/extension_service.h" 14 #include "chrome/browser/extensions/extension_service.h"
15 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/common/extensions/api/experimental_bluetooth.h" 16 #include "chrome/common/extensions/api/experimental_bluetooth.h"
17 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 18
19 #if defined(OS_CHROMEOS) 19 #if defined(OS_CHROMEOS)
20 #include "base/memory/ref_counted.h" 20 #include "base/memory/ref_counted.h"
21 #include "base/safe_strerror_posix.h" 21 #include "base/safe_strerror_posix.h"
22 #include "base/synchronization/lock.h"
23 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h" 22 #include "chrome/browser/chromeos/bluetooth/bluetooth_adapter.h"
24 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h" 23 #include "chrome/browser/chromeos/bluetooth/bluetooth_device.h"
25 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h" 24 #include "chrome/browser/chromeos/bluetooth/bluetooth_socket.h"
26 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h" 25 #include "chrome/browser/chromeos/extensions/bluetooth_event_router.h"
27 #include "chromeos/dbus/bluetooth_out_of_band_client.h" 26 #include "chromeos/dbus/bluetooth_out_of_band_client.h"
28 27
29 namespace { 28 namespace {
30 29
31 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) { 30 chromeos::ExtensionBluetoothEventRouter* GetEventRouter(Profile* profile) {
32 return profile->GetExtensionService()->bluetooth_event_router(); 31 return profile->GetExtensionService()->bluetooth_event_router();
33 } 32 }
34 33
35 const chromeos::BluetoothAdapter* GetAdapter(Profile* profile) { 34 const chromeos::BluetoothAdapter* GetAdapter(Profile* profile) {
36 return GetEventRouter(profile)->adapter(); 35 return GetEventRouter(profile)->adapter();
37 } 36 }
38 37
39 chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) { 38 chromeos::BluetoothAdapter* GetMutableAdapter(Profile* profile) {
40 return GetEventRouter(profile)->GetMutableAdapter(); 39 return GetEventRouter(profile)->GetMutableAdapter();
41 } 40 }
42 41
43 // Fill in a Device object from a chromeos::BluetoothDevice.
44 void PopulateApiDevice(const chromeos::BluetoothDevice& device,
45 extensions::api::experimental_bluetooth::Device* out) {
46 out->name = UTF16ToUTF8(device.GetName());
47 out->address = device.address();
48 out->paired = device.IsPaired();
49 out->bonded = device.IsBonded();
50 out->connected = device.IsConnected();
51 }
52
53 // The caller takes ownership of the returned pointer.
54 base::Value* BluetoothDeviceToValue(const chromeos::BluetoothDevice& device) {
55 extensions::api::experimental_bluetooth::Device api_device;
56 PopulateApiDevice(device, &api_device);
57 return api_device.ToValue().release();
58 }
59
60 } // namespace 42 } // namespace
61 #endif 43 #endif
62 44
63 namespace { 45 namespace {
64 46
65 const char kCouldNotGetLocalOutOfBandPairingData[] = 47 const char kCouldNotGetLocalOutOfBandPairingData[] =
66 "Could not get local Out Of Band Pairing Data"; 48 "Could not get local Out Of Band Pairing Data";
67 const char kCouldNotSetOutOfBandPairingData[] = 49 const char kCouldNotSetOutOfBandPairingData[] =
68 "Could not set Out Of Band Pairing Data"; 50 "Could not set Out Of Band Pairing Data";
69 const char kFailedToConnect[] = "Connection failed"; 51 const char kFailedToConnect[] = "Connection failed";
70 const char kInvalidDevice[] = "Invalid device"; 52 const char kInvalidDevice[] = "Invalid device";
71 const char kSocketNotFoundError[] = "Socket not found: invalid socket id"; 53 const char kSocketNotFoundError[] = "Socket not found: invalid socket id";
54 const char kStartDiscoveryFailed[] =
55 "Starting discovery failed, or already discovering";
56 const char kStopDiscoveryFailed[] = "Failed to stop discovery";
72 57
73 } // namespace 58 } // namespace
74 59
75 namespace Connect = extensions::api::experimental_bluetooth::Connect; 60 namespace Connect = extensions::api::experimental_bluetooth::Connect;
76 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect; 61 namespace Disconnect = extensions::api::experimental_bluetooth::Disconnect;
77 namespace GetDevices = extensions::api::experimental_bluetooth::GetDevices; 62 namespace GetDevices = extensions::api::experimental_bluetooth::GetDevices;
78 namespace Read = extensions::api::experimental_bluetooth::Read; 63 namespace Read = extensions::api::experimental_bluetooth::Read;
79 namespace SetOutOfBandPairingData = 64 namespace SetOutOfBandPairingData =
80 extensions::api::experimental_bluetooth::SetOutOfBandPairingData; 65 extensions::api::experimental_bluetooth::SetOutOfBandPairingData;
81 namespace Write = extensions::api::experimental_bluetooth::Write; 66 namespace Write = extensions::api::experimental_bluetooth::Write;
(...skipping 21 matching lines...) Expand all
103 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction() 88 BluetoothGetDevicesFunction::BluetoothGetDevicesFunction()
104 : callbacks_pending_(0) {} 89 : callbacks_pending_(0) {}
105 90
106 void BluetoothGetDevicesFunction::AddDeviceIfTrueCallback( 91 void BluetoothGetDevicesFunction::AddDeviceIfTrueCallback(
107 ListValue* list, 92 ListValue* list,
108 const chromeos::BluetoothDevice* device, 93 const chromeos::BluetoothDevice* device,
109 bool shouldAdd) { 94 bool shouldAdd) {
110 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 95 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
111 96
112 if (shouldAdd) 97 if (shouldAdd)
113 list->Append(BluetoothDeviceToValue(*device)); 98 list->Append(experimental_bluetooth::BluetoothDeviceToValue(*device));
114 99
115 callbacks_pending_--; 100 callbacks_pending_--;
116 if (callbacks_pending_ == -1) { 101 if (callbacks_pending_ == -1) {
117 SendResponse(true); 102 SendResponse(true);
118 Release(); // Added in RunImpl 103 Release(); // Added in RunImpl
119 } 104 }
120 } 105 }
121 106
122 bool BluetoothGetDevicesFunction::RunImpl() { 107 bool BluetoothGetDevicesFunction::RunImpl() {
123 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)); 108 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
(...skipping 16 matching lines...) Expand all
140 GetMutableAdapter(profile())->GetDevices(); 125 GetMutableAdapter(profile())->GetDevices();
141 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin(); 126 for (chromeos::BluetoothAdapter::DeviceList::iterator i = devices.begin();
142 i != devices.end(); ++i) { 127 i != devices.end(); ++i) {
143 chromeos::BluetoothDevice* device = *i; 128 chromeos::BluetoothDevice* device = *i;
144 129
145 if (options.uuid.get() != NULL && 130 if (options.uuid.get() != NULL &&
146 !(device->ProvidesServiceWithUUID(*(options.uuid)))) 131 !(device->ProvidesServiceWithUUID(*(options.uuid))))
147 continue; 132 continue;
148 133
149 if (options.name.get() == NULL) { 134 if (options.name.get() == NULL) {
150 matches->Append(BluetoothDeviceToValue(*device)); 135 matches->Append(experimental_bluetooth::BluetoothDeviceToValue(*device));
151 continue; 136 continue;
152 } 137 }
153 138
154 callbacks_pending_++; 139 callbacks_pending_++;
155 device->ProvidesServiceWithName( 140 device->ProvidesServiceWithName(
156 *(options.name), 141 *(options.name),
157 base::Bind(&BluetoothGetDevicesFunction::AddDeviceIfTrueCallback, 142 base::Bind(&BluetoothGetDevicesFunction::AddDeviceIfTrueCallback,
158 this, 143 this,
159 matches, 144 matches,
160 device)); 145 device));
161 } 146 }
162 callbacks_pending_--; 147 callbacks_pending_--;
163 148
164 if (callbacks_pending_ == -1) { 149 if (callbacks_pending_ == -1) {
165 SendResponse(true); 150 SendResponse(true);
166 Release(); 151 Release();
167 } 152 }
168 153
169 return true; 154 return true;
170 } 155 }
171 156
172 void BluetoothConnectFunction::ConnectToServiceCallback( 157 void BluetoothConnectFunction::ConnectToServiceCallback(
173 const chromeos::BluetoothDevice* device, 158 const chromeos::BluetoothDevice* device,
174 const std::string& service_uuid, 159 const std::string& service_uuid,
175 scoped_refptr<chromeos::BluetoothSocket> socket) { 160 scoped_refptr<chromeos::BluetoothSocket> socket) {
176 if (socket.get()) { 161 if (socket.get()) {
177 int socket_id = GetEventRouter(profile())->RegisterSocket(socket); 162 int socket_id = GetEventRouter(profile())->RegisterSocket(socket);
178 163
179 experimental_bluetooth::Socket result_socket; 164 experimental_bluetooth::Socket result_socket;
180 PopulateApiDevice(*device, &result_socket.device); 165 experimental_bluetooth::BluetoothDeviceToApiDevice(
166 *device, &result_socket.device);
181 result_socket.service_uuid = service_uuid; 167 result_socket.service_uuid = service_uuid;
182 result_socket.id = socket_id; 168 result_socket.id = socket_id;
183 result_.reset(result_socket.ToValue().release()); 169 result_.reset(result_socket.ToValue().release());
184 SendResponse(true); 170 SendResponse(true);
185 } else { 171 } else {
186 SetError(kFailedToConnect); 172 SetError(kFailedToConnect);
187 SendResponse(false); 173 SendResponse(false);
188 } 174 }
189 175
190 Release(); // Added in RunImpl 176 Release(); // Added in RunImpl
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
417 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { 403 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() {
418 AddRef(); // Released in one of the callbacks below 404 AddRef(); // Released in one of the callbacks below
419 GetMutableAdapter(profile())->ReadLocalOutOfBandPairingData( 405 GetMutableAdapter(profile())->ReadLocalOutOfBandPairingData(
420 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback, 406 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ReadCallback,
421 this), 407 this),
422 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback, 408 base::Bind(&BluetoothGetLocalOutOfBandPairingDataFunction::ErrorCallback,
423 this)); 409 this));
424 return true; 410 return true;
425 } 411 }
426 412
413 void BluetoothStartDiscoveryFunction::OnSuccessCallback() {
414 SendResponse(true);
415 Release(); // Added in RunImpl
416 }
417
418 void BluetoothStartDiscoveryFunction::OnErrorCallback() {
419 SetError(kStartDiscoveryFailed);
420 SendResponse(false);
421 Release(); // Added in RunImpl
422 }
423
424 bool BluetoothStartDiscoveryFunction::RunImpl() {
425 GetEventRouter(profile())->SetSendDiscoveryEvents(true);
426
427 // BluetoothAdapter will throw an error if we SetDiscovering(true) when
428 // discovery is already in progress
429 if (GetMutableAdapter(profile())->IsDiscovering()) {
430 SendResponse(true);
431 return true;
432 }
433
434 AddRef(); // Removed in whichever callback is called.
435 GetMutableAdapter(profile())->SetDiscovering(true,
436 base::Bind(&BluetoothStartDiscoveryFunction::OnSuccessCallback, this),
437 base::Bind(&BluetoothStartDiscoveryFunction::OnErrorCallback, this));
438 return true;
439 }
440
441 void BluetoothStopDiscoveryFunction::OnSuccessCallback() {
442 SendResponse(true);
443 Release(); // Added in RunImpl
444 }
445
446 void BluetoothStopDiscoveryFunction::OnErrorCallback() {
447 SetError(kStopDiscoveryFailed);
448 SendResponse(false);
449 Release(); // Added in RunImpl
450 }
451
452 bool BluetoothStopDiscoveryFunction::RunImpl() {
453 GetEventRouter(profile())->SetSendDiscoveryEvents(false);
454 AddRef(); // Removed in whichever callback is called.
455 GetMutableAdapter(profile())->SetDiscovering(false,
456 base::Bind(&BluetoothStopDiscoveryFunction::OnSuccessCallback, this),
457 base::Bind(&BluetoothStopDiscoveryFunction::OnErrorCallback, this));
458 return true;
459 }
460
427 #else 461 #else
428 462
429 // ----------------------------------------------------------------------------- 463 // -----------------------------------------------------------------------------
430 // NIY stubs 464 // NIY stubs
431 // ----------------------------------------------------------------------------- 465 // -----------------------------------------------------------------------------
432 bool BluetoothIsAvailableFunction::RunImpl() { 466 bool BluetoothIsAvailableFunction::RunImpl() {
433 NOTREACHED() << "Not implemented yet"; 467 NOTREACHED() << "Not implemented yet";
434 return false; 468 return false;
435 } 469 }
436 470
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
476 } 510 }
477 511
478 void BluetoothWriteFunction::Work() { 512 void BluetoothWriteFunction::Work() {
479 } 513 }
480 514
481 bool BluetoothWriteFunction::Respond() { 515 bool BluetoothWriteFunction::Respond() {
482 NOTREACHED() << "Not implemented yet"; 516 NOTREACHED() << "Not implemented yet";
483 return false; 517 return false;
484 } 518 }
485 519
520 bool BluetoothStartDiscoveryFunction::RunImpl() {
521 NOTREACHED() << "Not implemented yet";
522 return false;
523 }
524
525 bool BluetoothStopDiscoveryFunction::RunImpl() {
526 NOTREACHED() << "Not implemented yet";
527 return false;
528 }
529
486 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() { 530 bool BluetoothSetOutOfBandPairingDataFunction::RunImpl() {
487 NOTREACHED() << "Not implemented yet"; 531 NOTREACHED() << "Not implemented yet";
488 return false; 532 return false;
489 } 533 }
490 534
491 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() { 535 bool BluetoothGetLocalOutOfBandPairingDataFunction::RunImpl() {
492 NOTREACHED() << "Not implemented yet"; 536 NOTREACHED() << "Not implemented yet";
493 return false; 537 return false;
494 } 538 }
495 539
496 #endif 540 #endif
497 541
498 BluetoothReadFunction::BluetoothReadFunction() {} 542 BluetoothReadFunction::BluetoothReadFunction() {}
499 BluetoothReadFunction::~BluetoothReadFunction() {} 543 BluetoothReadFunction::~BluetoothReadFunction() {}
500 544
501 BluetoothWriteFunction::BluetoothWriteFunction() {} 545 BluetoothWriteFunction::BluetoothWriteFunction() {}
502 BluetoothWriteFunction::~BluetoothWriteFunction() {} 546 BluetoothWriteFunction::~BluetoothWriteFunction() {}
503 547
504 } // namespace api 548 } // namespace api
505 } // namespace extensions 549 } // namespace extensions
OLDNEW
« no previous file with comments | « chrome/browser/extensions/api/bluetooth/bluetooth_api.h ('k') | chrome/browser/extensions/api/bluetooth/bluetooth_api_utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698