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

Side by Side Diff: extensions/browser/api/bluetooth/bluetooth_private_api.cc

Issue 1419023004: Add bluetoothPrivate.connect (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@issue_543294_bluetooth_options_2b_props
Patch Set: Rebase Created 5 years, 1 month 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
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 "extensions/browser/api/bluetooth/bluetooth_private_api.h" 5 #include "extensions/browser/api/bluetooth/bluetooth_private_api.h"
6 6
7 #include "base/callback.h" 7 #include "base/callback.h"
8 #include "base/lazy_instance.h" 8 #include "base/lazy_instance.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "device/bluetooth/bluetooth_adapter.h" 10 #include "device/bluetooth/bluetooth_adapter.h"
11 #include "device/bluetooth/bluetooth_adapter_factory.h" 11 #include "device/bluetooth/bluetooth_adapter_factory.h"
12 #include "device/bluetooth/bluetooth_discovery_session.h" 12 #include "device/bluetooth/bluetooth_discovery_session.h"
13 #include "extensions/browser/api/bluetooth/bluetooth_api.h" 13 #include "extensions/browser/api/bluetooth/bluetooth_api.h"
14 #include "extensions/browser/api/bluetooth/bluetooth_api_pairing_delegate.h" 14 #include "extensions/browser/api/bluetooth/bluetooth_api_pairing_delegate.h"
15 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h" 15 #include "extensions/browser/api/bluetooth/bluetooth_event_router.h"
16 #include "extensions/common/api/bluetooth_private.h" 16 #include "extensions/common/api/bluetooth_private.h"
17 17
18 namespace bt_private = extensions::api::bluetooth_private; 18 namespace bt_private = extensions::api::bluetooth_private;
19 namespace SetDiscoveryFilter = bt_private::SetDiscoveryFilter; 19 namespace SetDiscoveryFilter = bt_private::SetDiscoveryFilter;
20 20
21 namespace extensions { 21 namespace extensions {
22 22
23 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothPrivateAPI> > 23 static base::LazyInstance<BrowserContextKeyedAPIFactory<BluetoothPrivateAPI>>
24 g_factory = LAZY_INSTANCE_INITIALIZER; 24 g_factory = LAZY_INSTANCE_INITIALIZER;
25 25
26 namespace {
27
28 std::string GetListenerId(const EventListenerInfo& details) {
29 return !details.extension_id.empty() ? details.extension_id
30 : details.listener_url.host();
31 }
32
33 } // namespace
34
26 // static 35 // static
27 BrowserContextKeyedAPIFactory<BluetoothPrivateAPI>* 36 BrowserContextKeyedAPIFactory<BluetoothPrivateAPI>*
28 BluetoothPrivateAPI::GetFactoryInstance() { 37 BluetoothPrivateAPI::GetFactoryInstance() {
29 return g_factory.Pointer(); 38 return g_factory.Pointer();
30 } 39 }
31 40
32 BluetoothPrivateAPI::BluetoothPrivateAPI(content::BrowserContext* context) 41 BluetoothPrivateAPI::BluetoothPrivateAPI(content::BrowserContext* context)
33 : browser_context_(context) { 42 : browser_context_(context) {
34 EventRouter::Get(browser_context_) 43 EventRouter::Get(browser_context_)
35 ->RegisterObserver(this, bt_private::OnPairing::kEventName); 44 ->RegisterObserver(this, bt_private::OnPairing::kEventName);
36 } 45 }
37 46
38 BluetoothPrivateAPI::~BluetoothPrivateAPI() {} 47 BluetoothPrivateAPI::~BluetoothPrivateAPI() {}
39 48
40 void BluetoothPrivateAPI::Shutdown() { 49 void BluetoothPrivateAPI::Shutdown() {
41 EventRouter::Get(browser_context_)->UnregisterObserver(this); 50 EventRouter::Get(browser_context_)->UnregisterObserver(this);
42 } 51 }
43 52
44 void BluetoothPrivateAPI::OnListenerAdded(const EventListenerInfo& details) { 53 void BluetoothPrivateAPI::OnListenerAdded(const EventListenerInfo& details) {
45 // This function can be called multiple times for the same JS listener, for 54 // This function can be called multiple times for the same JS listener, for
46 // example, once for the addListener call and again if it is a lazy listener. 55 // example, once for the addListener call and again if it is a lazy listener.
47 if (!details.browser_context) 56 if (!details.browser_context)
48 return; 57 return;
49 58
50 BluetoothAPI::Get(browser_context_)->event_router()->AddPairingDelegate( 59 BluetoothAPI::Get(browser_context_)
51 details.extension_id); 60 ->event_router()
61 ->AddPairingDelegate(GetListenerId(details));
52 } 62 }
53 63
54 void BluetoothPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) { 64 void BluetoothPrivateAPI::OnListenerRemoved(const EventListenerInfo& details) {
55 // This function can be called multiple times for the same JS listener, for 65 // This function can be called multiple times for the same JS listener, for
56 // example, once for the addListener call and again if it is a lazy listener. 66 // example, once for the addListener call and again if it is a lazy listener.
57 if (!details.browser_context) 67 if (!details.browser_context)
58 return; 68 return;
59 69
60 BluetoothAPI::Get(browser_context_)->event_router()->RemovePairingDelegate( 70 BluetoothAPI::Get(browser_context_)
61 details.extension_id); 71 ->event_router()
72 ->RemovePairingDelegate(GetListenerId(details));
62 } 73 }
63 74
64 namespace api { 75 namespace api {
65 76
66 namespace { 77 namespace {
67 78
68 const char kNameProperty[] = "name"; 79 const char kNameProperty[] = "name";
69 const char kPoweredProperty[] = "powered"; 80 const char kPoweredProperty[] = "powered";
70 const char kDiscoverableProperty[] = "discoverable"; 81 const char kDiscoverableProperty[] = "discoverable";
71
72 const char kSetAdapterPropertyError[] = "Error setting adapter properties: $1"; 82 const char kSetAdapterPropertyError[] = "Error setting adapter properties: $1";
73 83 const char kDeviceNotFoundError[] = "Invalid Bluetooth device";
74 const char kDeviceNotFoundError[] = 84 const char kDeviceNotConnectedError[] = "Device not connected";
75 "Given address is not a valid Bluetooth device."; 85 const char kPairingNotEnabled[] = "Pairing not enabled";
76
77 const char kDeviceNotConnectedError[] = "Device is not connected";
78
79 const char kPairingNotEnabled[] =
80 "Pairing must be enabled to set a pairing response.";
81
82 const char kInvalidPairingResponseOptions[] = 86 const char kInvalidPairingResponseOptions[] =
83 "Invalid pairing response options"; 87 "Invalid pairing response options";
84 88 const char kAdapterNotPresent[] = "Failed to find a Bluetooth adapter";
85 const char kAdapterNotPresent[] =
86 "Could not find a Bluetooth adapter.";
87
88 const char kDisconnectError[] = "Failed to disconnect device"; 89 const char kDisconnectError[] = "Failed to disconnect device";
89
90 const char kSetDiscoveryFilterFailed[] = "Failed to set discovery filter"; 90 const char kSetDiscoveryFilterFailed[] = "Failed to set discovery filter";
91
92 const char kPairingFailed[] = "Pairing failed"; 91 const char kPairingFailed[] = "Pairing failed";
93 92
94 // Returns true if the pairing response options passed into the 93 // Returns true if the pairing response options passed into the
95 // setPairingResponse function are valid. 94 // setPairingResponse function are valid.
96 bool ValidatePairingResponseOptions( 95 bool ValidatePairingResponseOptions(
97 const device::BluetoothDevice* device, 96 const device::BluetoothDevice* device,
98 const bt_private::SetPairingResponseOptions& options) { 97 const bt_private::SetPairingResponseOptions& options) {
99 bool response = options.response != bt_private::PAIRING_RESPONSE_NONE; 98 bool response = options.response != bt_private::PAIRING_RESPONSE_NONE;
100 bool pincode = options.pincode.get() != NULL; 99 bool pincode = options.pincode.get() != nullptr;
101 bool passkey = options.passkey.get() != NULL; 100 bool passkey = options.passkey.get() != nullptr;
102 101
103 if (!response && !pincode && !passkey) 102 if (!response && !pincode && !passkey)
104 return false; 103 return false;
105 if (pincode && passkey) 104 if (pincode && passkey)
106 return false; 105 return false;
107 if (options.response != bt_private::PAIRING_RESPONSE_CONFIRM && 106 if (options.response != bt_private::PAIRING_RESPONSE_CONFIRM &&
108 (pincode || passkey)) 107 (pincode || passkey))
109 return false; 108 return false;
110 109
111 // Check the BluetoothDevice is in expecting the correct response. 110 // Check the BluetoothDevice is in expecting the correct response.
112 if (!device->ExpectingConfirmation() && !device->ExpectingPinCode() && 111 if (!device->ExpectingConfirmation() && !device->ExpectingPinCode() &&
113 !device->ExpectingPasskey()) 112 !device->ExpectingPasskey())
114 return false; 113 return false;
115 if (pincode && !device->ExpectingPinCode()) 114 if (pincode && !device->ExpectingPinCode())
116 return false; 115 return false;
117 if (passkey && !device->ExpectingPasskey()) 116 if (passkey && !device->ExpectingPasskey())
118 return false; 117 return false;
119 if (options.response == bt_private::PAIRING_RESPONSE_CONFIRM && !pincode && 118 if (options.response == bt_private::PAIRING_RESPONSE_CONFIRM && !pincode &&
120 !passkey && !device->ExpectingConfirmation()) 119 !passkey && !device->ExpectingConfirmation())
121 return false; 120 return false;
122 121
123 return true; 122 return true;
124 } 123 }
125 124
126 } // namespace 125 } // namespace
127 126
127 ////////////////////////////////////////////////////////////////////////////////
128
128 BluetoothPrivateSetAdapterStateFunction:: 129 BluetoothPrivateSetAdapterStateFunction::
129 BluetoothPrivateSetAdapterStateFunction() {} 130 BluetoothPrivateSetAdapterStateFunction() {}
130 131
131 BluetoothPrivateSetAdapterStateFunction:: 132 BluetoothPrivateSetAdapterStateFunction::
132 ~BluetoothPrivateSetAdapterStateFunction() {} 133 ~BluetoothPrivateSetAdapterStateFunction() {}
133 134
134 bool BluetoothPrivateSetAdapterStateFunction::DoWork( 135 bool BluetoothPrivateSetAdapterStateFunction::DoWork(
135 scoped_refptr<device::BluetoothAdapter> adapter) { 136 scoped_refptr<device::BluetoothAdapter> adapter) {
136 scoped_ptr<bt_private::SetAdapterState::Params> params( 137 scoped_ptr<bt_private::SetAdapterState::Params> params(
137 bt_private::SetAdapterState::Params::Create(*args_)); 138 bt_private::SetAdapterState::Params::Create(*args_));
138 EXTENSION_FUNCTION_VALIDATE(params.get()); 139 EXTENSION_FUNCTION_VALIDATE(params.get());
139 140
140 if (!adapter->IsPresent()) { 141 if (!adapter->IsPresent()) {
141 SetError(kAdapterNotPresent); 142 SetError(kAdapterNotPresent);
142 SendResponse(false); 143 SendResponse(false);
143 return true; 144 return true;
144 } 145 }
145 146
146 const bt_private::NewAdapterState& new_state = params->adapter_state; 147 const bt_private::NewAdapterState& new_state = params->adapter_state;
147 148
148 // These properties are not owned. 149 // These properties are not owned.
149 std::string* name = new_state.name.get(); 150 std::string* name = new_state.name.get();
150 bool* powered = new_state.powered.get(); 151 bool* powered = new_state.powered.get();
151 bool* discoverable = new_state.discoverable.get(); 152 bool* discoverable = new_state.discoverable.get();
152 153
153 if (name && adapter->GetName() != *name) { 154 if (name && adapter->GetName() != *name) {
154 pending_properties_.insert(kNameProperty); 155 pending_properties_.insert(kNameProperty);
155 adapter->SetName(*name, 156 adapter->SetName(*name, CreatePropertySetCallback(kNameProperty),
156 CreatePropertySetCallback(kNameProperty),
157 CreatePropertyErrorCallback(kNameProperty)); 157 CreatePropertyErrorCallback(kNameProperty));
158 } 158 }
159 159
160 if (powered && adapter->IsPowered() != *powered) { 160 if (powered && adapter->IsPowered() != *powered) {
161 pending_properties_.insert(kPoweredProperty); 161 pending_properties_.insert(kPoweredProperty);
162 adapter->SetPowered(*powered, 162 adapter->SetPowered(*powered, CreatePropertySetCallback(kPoweredProperty),
163 CreatePropertySetCallback(kPoweredProperty),
164 CreatePropertyErrorCallback(kPoweredProperty)); 163 CreatePropertyErrorCallback(kPoweredProperty));
165 } 164 }
166 165
167 if (discoverable && adapter->IsDiscoverable() != *discoverable) { 166 if (discoverable && adapter->IsDiscoverable() != *discoverable) {
168 pending_properties_.insert(kDiscoverableProperty); 167 pending_properties_.insert(kDiscoverableProperty);
169 adapter->SetDiscoverable( 168 adapter->SetDiscoverable(
170 *discoverable, 169 *discoverable, CreatePropertySetCallback(kDiscoverableProperty),
171 CreatePropertySetCallback(kDiscoverableProperty),
172 CreatePropertyErrorCallback(kDiscoverableProperty)); 170 CreatePropertyErrorCallback(kDiscoverableProperty));
173 } 171 }
174 172
175 if (pending_properties_.empty()) 173 if (pending_properties_.empty())
176 SendResponse(true); 174 SendResponse(true);
177 return true; 175 return true;
178 } 176 }
179 177
180 base::Closure 178 base::Closure
181 BluetoothPrivateSetAdapterStateFunction::CreatePropertySetCallback( 179 BluetoothPrivateSetAdapterStateFunction::CreatePropertySetCallback(
182 const std::string& property_name) { 180 const std::string& property_name) {
183 return base::Bind( 181 return base::Bind(
184 &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet, 182 &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet, this,
185 this,
186 property_name); 183 property_name);
187 } 184 }
188 185
189 base::Closure 186 base::Closure
190 BluetoothPrivateSetAdapterStateFunction::CreatePropertyErrorCallback( 187 BluetoothPrivateSetAdapterStateFunction::CreatePropertyErrorCallback(
191 const std::string& property_name) { 188 const std::string& property_name) {
192 return base::Bind( 189 return base::Bind(
193 &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertyError, 190 &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertyError, this,
194 this,
195 property_name); 191 property_name);
196 } 192 }
197 193
198 void BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet( 194 void BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet(
199 const std::string& property) { 195 const std::string& property) {
200 DCHECK(pending_properties_.find(property) != pending_properties_.end()); 196 DCHECK(pending_properties_.find(property) != pending_properties_.end());
201 DCHECK(failed_properties_.find(property) == failed_properties_.end()); 197 DCHECK(failed_properties_.find(property) == failed_properties_.end());
202 198
203 pending_properties_.erase(property); 199 pending_properties_.erase(property);
204 if (pending_properties_.empty()) { 200 if (pending_properties_.empty()) {
(...skipping 13 matching lines...) Expand all
218 failed_properties_.insert(property); 214 failed_properties_.insert(property);
219 if (pending_properties_.empty()) 215 if (pending_properties_.empty())
220 SendError(); 216 SendError();
221 } 217 }
222 218
223 void BluetoothPrivateSetAdapterStateFunction::SendError() { 219 void BluetoothPrivateSetAdapterStateFunction::SendError() {
224 DCHECK(pending_properties_.empty()); 220 DCHECK(pending_properties_.empty());
225 DCHECK(!failed_properties_.empty()); 221 DCHECK(!failed_properties_.empty());
226 222
227 std::vector<std::string> failed_vector; 223 std::vector<std::string> failed_vector;
228 std::copy(failed_properties_.begin(), 224 std::copy(failed_properties_.begin(), failed_properties_.end(),
229 failed_properties_.end(),
230 std::back_inserter(failed_vector)); 225 std::back_inserter(failed_vector));
231 226
232 std::vector<std::string> replacements(1); 227 std::vector<std::string> replacements(1);
233 replacements[0] = base::JoinString(failed_vector, ", "); 228 replacements[0] = base::JoinString(failed_vector, ", ");
234 std::string error = base::ReplaceStringPlaceholders(kSetAdapterPropertyError, 229 std::string error = base::ReplaceStringPlaceholders(kSetAdapterPropertyError,
235 replacements, NULL); 230 replacements, nullptr);
236 SetError(error); 231 SetError(error);
237 SendResponse(false); 232 SendResponse(false);
238 } 233 }
239 234
235 ////////////////////////////////////////////////////////////////////////////////
236
240 BluetoothPrivateSetPairingResponseFunction:: 237 BluetoothPrivateSetPairingResponseFunction::
241 BluetoothPrivateSetPairingResponseFunction() {} 238 BluetoothPrivateSetPairingResponseFunction() {}
242 239
243 BluetoothPrivateSetPairingResponseFunction:: 240 BluetoothPrivateSetPairingResponseFunction::
244 ~BluetoothPrivateSetPairingResponseFunction() {} 241 ~BluetoothPrivateSetPairingResponseFunction() {}
245 242
246 bool BluetoothPrivateSetPairingResponseFunction::DoWork( 243 bool BluetoothPrivateSetPairingResponseFunction::DoWork(
247 scoped_refptr<device::BluetoothAdapter> adapter) { 244 scoped_refptr<device::BluetoothAdapter> adapter) {
248 scoped_ptr<bt_private::SetPairingResponse::Params> params( 245 scoped_ptr<bt_private::SetPairingResponse::Params> params(
249 bt_private::SetPairingResponse::Params::Create(*args_)); 246 bt_private::SetPairingResponse::Params::Create(*args_));
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 break; 286 break;
290 default: 287 default:
291 NOTREACHED(); 288 NOTREACHED();
292 } 289 }
293 } 290 }
294 291
295 SendResponse(true); 292 SendResponse(true);
296 return true; 293 return true;
297 } 294 }
298 295
296 ////////////////////////////////////////////////////////////////////////////////
297
299 BluetoothPrivateDisconnectAllFunction::BluetoothPrivateDisconnectAllFunction() { 298 BluetoothPrivateDisconnectAllFunction::BluetoothPrivateDisconnectAllFunction() {
300 } 299 }
301 300
302 BluetoothPrivateDisconnectAllFunction:: 301 BluetoothPrivateDisconnectAllFunction::
303 ~BluetoothPrivateDisconnectAllFunction() { 302 ~BluetoothPrivateDisconnectAllFunction() {}
304 }
305 303
306 bool BluetoothPrivateDisconnectAllFunction::DoWork( 304 bool BluetoothPrivateDisconnectAllFunction::DoWork(
307 scoped_refptr<device::BluetoothAdapter> adapter) { 305 scoped_refptr<device::BluetoothAdapter> adapter) {
308 scoped_ptr<bt_private::DisconnectAll::Params> params( 306 scoped_ptr<bt_private::DisconnectAll::Params> params(
309 bt_private::DisconnectAll::Params::Create(*args_)); 307 bt_private::DisconnectAll::Params::Create(*args_));
310 EXTENSION_FUNCTION_VALIDATE(params.get()); 308 EXTENSION_FUNCTION_VALIDATE(params.get());
311 309
312 device::BluetoothDevice* device = adapter->GetDevice(params->device_address); 310 device::BluetoothDevice* device = adapter->GetDevice(params->device_address);
313 if (!device) { 311 if (!device) {
314 SetError(kDeviceNotFoundError); 312 SetError(kDeviceNotFoundError);
(...skipping 28 matching lines...) Expand all
343 // error. 341 // error.
344 device::BluetoothDevice* device = adapter->GetDevice(device_address); 342 device::BluetoothDevice* device = adapter->GetDevice(device_address);
345 if (device && !device->IsConnected()) 343 if (device && !device->IsConnected())
346 SetError(kDeviceNotConnectedError); 344 SetError(kDeviceNotConnectedError);
347 else 345 else
348 SetError(kDisconnectError); 346 SetError(kDisconnectError);
349 347
350 SendResponse(false); 348 SendResponse(false);
351 } 349 }
352 350
353 void BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback() { 351 ////////////////////////////////////////////////////////////////////////////////
354 SendResponse(true);
355 }
356
357 void BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback() {
358 SetError(kSetDiscoveryFilterFailed);
359 SendResponse(false);
360 }
361 352
362 bool BluetoothPrivateSetDiscoveryFilterFunction::DoWork( 353 bool BluetoothPrivateSetDiscoveryFilterFunction::DoWork(
363 scoped_refptr<device::BluetoothAdapter> adapter) { 354 scoped_refptr<device::BluetoothAdapter> adapter) {
364 scoped_ptr<SetDiscoveryFilter::Params> params( 355 scoped_ptr<SetDiscoveryFilter::Params> params(
365 SetDiscoveryFilter::Params::Create(*args_)); 356 SetDiscoveryFilter::Params::Create(*args_));
366 auto& df_param = params->discovery_filter; 357 auto& df_param = params->discovery_filter;
367 358
368 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter; 359 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter;
369 360
370 // If all filter fields are empty, we are clearing filter. If any field is 361 // If all filter fields are empty, we are clearing filter. If any field is
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
413 discovery_filter.Pass(), adapter.get(), GetExtensionId(), 404 discovery_filter.Pass(), adapter.get(), GetExtensionId(),
414 base::Bind( 405 base::Bind(
415 &BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback, 406 &BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback,
416 this), 407 this),
417 base::Bind( 408 base::Bind(
418 &BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback, 409 &BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback,
419 this)); 410 this));
420 return true; 411 return true;
421 } 412 }
422 413
414 void BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback() {
415 SendResponse(true);
416 }
417
418 void BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback() {
419 SetError(kSetDiscoveryFilterFailed);
420 SendResponse(false);
421 }
422
423 ////////////////////////////////////////////////////////////////////////////////
424
425 BluetoothPrivateConnectFunction::BluetoothPrivateConnectFunction() {}
426
427 BluetoothPrivateConnectFunction::~BluetoothPrivateConnectFunction() {}
428
429 bool BluetoothPrivateConnectFunction::DoWork(
430 scoped_refptr<device::BluetoothAdapter> adapter) {
431 scoped_ptr<bt_private::Connect::Params> params(
432 bt_private::Connect::Params::Create(*args_));
433 EXTENSION_FUNCTION_VALIDATE(params.get());
434
435 device::BluetoothDevice* device = adapter->GetDevice(params->device_address);
436 if (!device) {
437 SetError(kDeviceNotFoundError);
438 SendResponse(false);
439 return true;
440 }
441
442 if (device->IsConnected()) {
443 results_ = bt_private::Connect::Results::Create(
444 bt_private::CONNECT_RESULT_TYPE_ALREADYCONNECTED);
445 SendResponse(true);
446 return true;
447 }
448
449 // pairing_delegate may be null for connect.
450 device::BluetoothDevice::PairingDelegate* pairing_delegate =
451 BluetoothAPI::Get(browser_context())
452 ->event_router()
453 ->GetPairingDelegate(GetExtensionId());
454 device->Connect(
455 pairing_delegate,
456 base::Bind(&BluetoothPrivateConnectFunction::OnSuccessCallback, this),
457 base::Bind(&BluetoothPrivateConnectFunction::OnErrorCallback, this));
458 return true;
459 }
460
461 void BluetoothPrivateConnectFunction::OnSuccessCallback() {
462 results_ = bt_private::Connect::Results::Create(
463 bt_private::CONNECT_RESULT_TYPE_SUCCESS);
464 SendResponse(true);
465 }
466
467 void BluetoothPrivateConnectFunction::OnErrorCallback(
468 device::BluetoothDevice::ConnectErrorCode error) {
469 bt_private::ConnectResultType result = bt_private::CONNECT_RESULT_TYPE_NONE;
470 switch (error) {
471 case device::BluetoothDevice::ERROR_UNKNOWN:
472 result = bt_private::CONNECT_RESULT_TYPE_UNKNOWNERROR;
473 break;
474 case device::BluetoothDevice::ERROR_INPROGRESS:
475 result = bt_private::CONNECT_RESULT_TYPE_INPROGRESS;
476 break;
477 case device::BluetoothDevice::ERROR_FAILED:
478 result = bt_private::CONNECT_RESULT_TYPE_FAILED;
479 break;
480 case device::BluetoothDevice::ERROR_AUTH_FAILED:
481 result = bt_private::CONNECT_RESULT_TYPE_AUTHFAILED;
482 break;
483 case device::BluetoothDevice::ERROR_AUTH_CANCELED:
484 result = bt_private::CONNECT_RESULT_TYPE_AUTHCANCELED;
485 break;
486 case device::BluetoothDevice::ERROR_AUTH_REJECTED:
487 result = bt_private::CONNECT_RESULT_TYPE_AUTHREJECTED;
488 break;
489 case device::BluetoothDevice::ERROR_AUTH_TIMEOUT:
490 result = bt_private::CONNECT_RESULT_TYPE_AUTHTIMEOUT;
491 break;
492 case device::BluetoothDevice::ERROR_UNSUPPORTED_DEVICE:
493 result = bt_private::CONNECT_RESULT_TYPE_UNSUPPORTEDDEVICE;
494 break;
495 }
496 // Set the result type and respond with true (success).
497 results_ = bt_private::Connect::Results::Create(result);
498 SendResponse(true);
499 }
500
501 ////////////////////////////////////////////////////////////////////////////////
502
423 BluetoothPrivatePairFunction::BluetoothPrivatePairFunction() {} 503 BluetoothPrivatePairFunction::BluetoothPrivatePairFunction() {}
424 504
425 BluetoothPrivatePairFunction::~BluetoothPrivatePairFunction() {} 505 BluetoothPrivatePairFunction::~BluetoothPrivatePairFunction() {}
426 506
427 void BluetoothPrivatePairFunction::OnSuccessCallback() {
428 SendResponse(true);
429 }
430
431 void BluetoothPrivatePairFunction::OnErrorCallback(
432 device::BluetoothDevice::ConnectErrorCode error) {
433 SetError(kPairingFailed);
434 SendResponse(false);
435 }
436
437 bool BluetoothPrivatePairFunction::DoWork( 507 bool BluetoothPrivatePairFunction::DoWork(
438 scoped_refptr<device::BluetoothAdapter> adapter) { 508 scoped_refptr<device::BluetoothAdapter> adapter) {
439 scoped_ptr<bt_private::Pair::Params> params( 509 scoped_ptr<bt_private::Pair::Params> params(
440 bt_private::Pair::Params::Create(*args_)); 510 bt_private::Pair::Params::Create(*args_));
511 EXTENSION_FUNCTION_VALIDATE(params.get());
441 512
442 device::BluetoothDevice* device = adapter->GetDevice(params->device_address); 513 device::BluetoothDevice* device = adapter->GetDevice(params->device_address);
443 if (!device) { 514 if (!device) {
444 SetError(kDeviceNotFoundError); 515 SetError(kDeviceNotFoundError);
445 SendResponse(false); 516 SendResponse(false);
446 return true; 517 return true;
447 } 518 }
448 519
449 BluetoothEventRouter* router = 520 device::BluetoothDevice::PairingDelegate* pairing_delegate =
450 BluetoothAPI::Get(browser_context())->event_router(); 521 BluetoothAPI::Get(browser_context())
451 if (!router->GetPairingDelegate(GetExtensionId())) { 522 ->event_router()
523 ->GetPairingDelegate(GetExtensionId());
524
525 // pairing_delegate must be set (by adding an onPairing listener) before
526 // any calls to pair().
527 if (!pairing_delegate) {
452 SetError(kPairingNotEnabled); 528 SetError(kPairingNotEnabled);
453 SendResponse(false); 529 SendResponse(false);
454 return true; 530 return true;
455 } 531 }
456 532
457 device->Pair( 533 device->Pair(
458 router->GetPairingDelegate(GetExtensionId()), 534 pairing_delegate,
459 base::Bind(&BluetoothPrivatePairFunction::OnSuccessCallback, this), 535 base::Bind(&BluetoothPrivatePairFunction::OnSuccessCallback, this),
460 base::Bind(&BluetoothPrivatePairFunction::OnErrorCallback, this)); 536 base::Bind(&BluetoothPrivatePairFunction::OnErrorCallback, this));
461 return true; 537 return true;
462 } 538 }
463 539
540 void BluetoothPrivatePairFunction::OnSuccessCallback() {
541 SendResponse(true);
542 }
543
544 void BluetoothPrivatePairFunction::OnErrorCallback(
545 device::BluetoothDevice::ConnectErrorCode error) {
546 SetError(kPairingFailed);
547 SendResponse(false);
548 }
549
550 ////////////////////////////////////////////////////////////////////////////////
551
464 } // namespace api 552 } // namespace api
465 553
466 } // namespace extensions 554 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698