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

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: Fix connect response + feedback 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 kDeviceConnectedError[] = "Device already connected";
75 "Given address is not a valid Bluetooth device."; 85 const char kDeviceNotConnectedError[] = "Device not connected";
76 86 const char kPairingNotEnabled[] = "Pairing not enabled";
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[] = 87 const char kInvalidPairingResponseOptions[] =
83 "Invalid pairing response options"; 88 "Invalid pairing response options";
84 89 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"; 90 const char kDisconnectError[] = "Failed to disconnect device";
89
90 const char kSetDiscoveryFilterFailed[] = "Failed to set discovery filter"; 91 const char kSetDiscoveryFilterFailed[] = "Failed to set discovery filter";
91
92 const char kPairingFailed[] = "Pairing failed"; 92 const char kPairingFailed[] = "Pairing failed";
93 93
94 // Returns true if the pairing response options passed into the 94 // Returns true if the pairing response options passed into the
95 // setPairingResponse function are valid. 95 // setPairingResponse function are valid.
96 bool ValidatePairingResponseOptions( 96 bool ValidatePairingResponseOptions(
97 const device::BluetoothDevice* device, 97 const device::BluetoothDevice* device,
98 const bt_private::SetPairingResponseOptions& options) { 98 const bt_private::SetPairingResponseOptions& options) {
99 bool response = options.response != bt_private::PAIRING_RESPONSE_NONE; 99 bool response = options.response != bt_private::PAIRING_RESPONSE_NONE;
100 bool pincode = options.pincode.get() != NULL; 100 bool pincode = options.pincode.get() != nullptr;
101 bool passkey = options.passkey.get() != NULL; 101 bool passkey = options.passkey.get() != nullptr;
102 102
103 if (!response && !pincode && !passkey) 103 if (!response && !pincode && !passkey)
104 return false; 104 return false;
105 if (pincode && passkey) 105 if (pincode && passkey)
106 return false; 106 return false;
107 if (options.response != bt_private::PAIRING_RESPONSE_CONFIRM && 107 if (options.response != bt_private::PAIRING_RESPONSE_CONFIRM &&
108 (pincode || passkey)) 108 (pincode || passkey))
109 return false; 109 return false;
110 110
111 // Check the BluetoothDevice is in expecting the correct response. 111 // Check the BluetoothDevice is in expecting the correct response.
112 if (!device->ExpectingConfirmation() && !device->ExpectingPinCode() && 112 if (!device->ExpectingConfirmation() && !device->ExpectingPinCode() &&
113 !device->ExpectingPasskey()) 113 !device->ExpectingPasskey())
114 return false; 114 return false;
115 if (pincode && !device->ExpectingPinCode()) 115 if (pincode && !device->ExpectingPinCode())
116 return false; 116 return false;
117 if (passkey && !device->ExpectingPasskey()) 117 if (passkey && !device->ExpectingPasskey())
118 return false; 118 return false;
119 if (options.response == bt_private::PAIRING_RESPONSE_CONFIRM && !pincode && 119 if (options.response == bt_private::PAIRING_RESPONSE_CONFIRM && !pincode &&
120 !passkey && !device->ExpectingConfirmation()) 120 !passkey && !device->ExpectingConfirmation())
121 return false; 121 return false;
122 122
123 return true; 123 return true;
124 } 124 }
125 125
126 } // namespace 126 } // namespace
127 127
128 ////////////////////////////////////////////////////////////////////////////////
129
128 BluetoothPrivateSetAdapterStateFunction:: 130 BluetoothPrivateSetAdapterStateFunction::
129 BluetoothPrivateSetAdapterStateFunction() {} 131 BluetoothPrivateSetAdapterStateFunction() {}
130 132
131 BluetoothPrivateSetAdapterStateFunction:: 133 BluetoothPrivateSetAdapterStateFunction::
132 ~BluetoothPrivateSetAdapterStateFunction() {} 134 ~BluetoothPrivateSetAdapterStateFunction() {}
133 135
134 bool BluetoothPrivateSetAdapterStateFunction::DoWork( 136 bool BluetoothPrivateSetAdapterStateFunction::DoWork(
135 scoped_refptr<device::BluetoothAdapter> adapter) { 137 scoped_refptr<device::BluetoothAdapter> adapter) {
136 scoped_ptr<bt_private::SetAdapterState::Params> params( 138 scoped_ptr<bt_private::SetAdapterState::Params> params(
137 bt_private::SetAdapterState::Params::Create(*args_)); 139 bt_private::SetAdapterState::Params::Create(*args_));
138 EXTENSION_FUNCTION_VALIDATE(params.get()); 140 EXTENSION_FUNCTION_VALIDATE(params.get());
139 141
140 if (!adapter->IsPresent()) { 142 if (!adapter->IsPresent()) {
141 SetError(kAdapterNotPresent); 143 SetError(kAdapterNotPresent);
142 SendResponse(false); 144 SendResponse(false);
143 return true; 145 return true;
144 } 146 }
145 147
146 const bt_private::NewAdapterState& new_state = params->adapter_state; 148 const bt_private::NewAdapterState& new_state = params->adapter_state;
147 149
148 // These properties are not owned. 150 // These properties are not owned.
149 std::string* name = new_state.name.get(); 151 std::string* name = new_state.name.get();
150 bool* powered = new_state.powered.get(); 152 bool* powered = new_state.powered.get();
151 bool* discoverable = new_state.discoverable.get(); 153 bool* discoverable = new_state.discoverable.get();
152 154
153 if (name && adapter->GetName() != *name) { 155 if (name && adapter->GetName() != *name) {
154 pending_properties_.insert(kNameProperty); 156 pending_properties_.insert(kNameProperty);
155 adapter->SetName(*name, 157 adapter->SetName(*name, CreatePropertySetCallback(kNameProperty),
156 CreatePropertySetCallback(kNameProperty),
157 CreatePropertyErrorCallback(kNameProperty)); 158 CreatePropertyErrorCallback(kNameProperty));
158 } 159 }
159 160
160 if (powered && adapter->IsPowered() != *powered) { 161 if (powered && adapter->IsPowered() != *powered) {
161 pending_properties_.insert(kPoweredProperty); 162 pending_properties_.insert(kPoweredProperty);
162 adapter->SetPowered(*powered, 163 adapter->SetPowered(*powered, CreatePropertySetCallback(kPoweredProperty),
163 CreatePropertySetCallback(kPoweredProperty),
164 CreatePropertyErrorCallback(kPoweredProperty)); 164 CreatePropertyErrorCallback(kPoweredProperty));
165 } 165 }
166 166
167 if (discoverable && adapter->IsDiscoverable() != *discoverable) { 167 if (discoverable && adapter->IsDiscoverable() != *discoverable) {
168 pending_properties_.insert(kDiscoverableProperty); 168 pending_properties_.insert(kDiscoverableProperty);
169 adapter->SetDiscoverable( 169 adapter->SetDiscoverable(
170 *discoverable, 170 *discoverable, CreatePropertySetCallback(kDiscoverableProperty),
171 CreatePropertySetCallback(kDiscoverableProperty),
172 CreatePropertyErrorCallback(kDiscoverableProperty)); 171 CreatePropertyErrorCallback(kDiscoverableProperty));
173 } 172 }
174 173
175 if (pending_properties_.empty()) 174 if (pending_properties_.empty())
176 SendResponse(true); 175 SendResponse(true);
177 return true; 176 return true;
178 } 177 }
179 178
180 base::Closure 179 base::Closure
181 BluetoothPrivateSetAdapterStateFunction::CreatePropertySetCallback( 180 BluetoothPrivateSetAdapterStateFunction::CreatePropertySetCallback(
182 const std::string& property_name) { 181 const std::string& property_name) {
183 return base::Bind( 182 return base::Bind(
184 &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet, 183 &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet, this,
185 this,
186 property_name); 184 property_name);
187 } 185 }
188 186
189 base::Closure 187 base::Closure
190 BluetoothPrivateSetAdapterStateFunction::CreatePropertyErrorCallback( 188 BluetoothPrivateSetAdapterStateFunction::CreatePropertyErrorCallback(
191 const std::string& property_name) { 189 const std::string& property_name) {
192 return base::Bind( 190 return base::Bind(
193 &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertyError, 191 &BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertyError, this,
194 this,
195 property_name); 192 property_name);
196 } 193 }
197 194
198 void BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet( 195 void BluetoothPrivateSetAdapterStateFunction::OnAdapterPropertySet(
199 const std::string& property) { 196 const std::string& property) {
200 DCHECK(pending_properties_.find(property) != pending_properties_.end()); 197 DCHECK(pending_properties_.find(property) != pending_properties_.end());
201 DCHECK(failed_properties_.find(property) == failed_properties_.end()); 198 DCHECK(failed_properties_.find(property) == failed_properties_.end());
202 199
203 pending_properties_.erase(property); 200 pending_properties_.erase(property);
204 if (pending_properties_.empty()) { 201 if (pending_properties_.empty()) {
(...skipping 13 matching lines...) Expand all
218 failed_properties_.insert(property); 215 failed_properties_.insert(property);
219 if (pending_properties_.empty()) 216 if (pending_properties_.empty())
220 SendError(); 217 SendError();
221 } 218 }
222 219
223 void BluetoothPrivateSetAdapterStateFunction::SendError() { 220 void BluetoothPrivateSetAdapterStateFunction::SendError() {
224 DCHECK(pending_properties_.empty()); 221 DCHECK(pending_properties_.empty());
225 DCHECK(!failed_properties_.empty()); 222 DCHECK(!failed_properties_.empty());
226 223
227 std::vector<std::string> failed_vector; 224 std::vector<std::string> failed_vector;
228 std::copy(failed_properties_.begin(), 225 std::copy(failed_properties_.begin(), failed_properties_.end(),
229 failed_properties_.end(),
230 std::back_inserter(failed_vector)); 226 std::back_inserter(failed_vector));
231 227
232 std::vector<std::string> replacements(1); 228 std::vector<std::string> replacements(1);
233 replacements[0] = base::JoinString(failed_vector, ", "); 229 replacements[0] = base::JoinString(failed_vector, ", ");
234 std::string error = base::ReplaceStringPlaceholders(kSetAdapterPropertyError, 230 std::string error = base::ReplaceStringPlaceholders(kSetAdapterPropertyError,
235 replacements, NULL); 231 replacements, nullptr);
236 SetError(error); 232 SetError(error);
237 SendResponse(false); 233 SendResponse(false);
238 } 234 }
239 235
236 ////////////////////////////////////////////////////////////////////////////////
237
240 BluetoothPrivateSetPairingResponseFunction:: 238 BluetoothPrivateSetPairingResponseFunction::
241 BluetoothPrivateSetPairingResponseFunction() {} 239 BluetoothPrivateSetPairingResponseFunction() {}
242 240
243 BluetoothPrivateSetPairingResponseFunction:: 241 BluetoothPrivateSetPairingResponseFunction::
244 ~BluetoothPrivateSetPairingResponseFunction() {} 242 ~BluetoothPrivateSetPairingResponseFunction() {}
245 243
246 bool BluetoothPrivateSetPairingResponseFunction::DoWork( 244 bool BluetoothPrivateSetPairingResponseFunction::DoWork(
247 scoped_refptr<device::BluetoothAdapter> adapter) { 245 scoped_refptr<device::BluetoothAdapter> adapter) {
248 scoped_ptr<bt_private::SetPairingResponse::Params> params( 246 scoped_ptr<bt_private::SetPairingResponse::Params> params(
249 bt_private::SetPairingResponse::Params::Create(*args_)); 247 bt_private::SetPairingResponse::Params::Create(*args_));
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
289 break; 287 break;
290 default: 288 default:
291 NOTREACHED(); 289 NOTREACHED();
292 } 290 }
293 } 291 }
294 292
295 SendResponse(true); 293 SendResponse(true);
296 return true; 294 return true;
297 } 295 }
298 296
297 ////////////////////////////////////////////////////////////////////////////////
298
299 BluetoothPrivateDisconnectAllFunction::BluetoothPrivateDisconnectAllFunction() { 299 BluetoothPrivateDisconnectAllFunction::BluetoothPrivateDisconnectAllFunction() {
300 } 300 }
301 301
302 BluetoothPrivateDisconnectAllFunction:: 302 BluetoothPrivateDisconnectAllFunction::
303 ~BluetoothPrivateDisconnectAllFunction() { 303 ~BluetoothPrivateDisconnectAllFunction() {}
304 }
305 304
306 bool BluetoothPrivateDisconnectAllFunction::DoWork( 305 bool BluetoothPrivateDisconnectAllFunction::DoWork(
307 scoped_refptr<device::BluetoothAdapter> adapter) { 306 scoped_refptr<device::BluetoothAdapter> adapter) {
308 scoped_ptr<bt_private::DisconnectAll::Params> params( 307 scoped_ptr<bt_private::DisconnectAll::Params> params(
309 bt_private::DisconnectAll::Params::Create(*args_)); 308 bt_private::DisconnectAll::Params::Create(*args_));
310 EXTENSION_FUNCTION_VALIDATE(params.get()); 309 EXTENSION_FUNCTION_VALIDATE(params.get());
311 310
312 device::BluetoothDevice* device = adapter->GetDevice(params->device_address); 311 device::BluetoothDevice* device = adapter->GetDevice(params->device_address);
313 if (!device) { 312 if (!device) {
314 SetError(kDeviceNotFoundError); 313 SetError(kDeviceNotFoundError);
(...skipping 28 matching lines...) Expand all
343 // error. 342 // error.
344 device::BluetoothDevice* device = adapter->GetDevice(device_address); 343 device::BluetoothDevice* device = adapter->GetDevice(device_address);
345 if (device && !device->IsConnected()) 344 if (device && !device->IsConnected())
346 SetError(kDeviceNotConnectedError); 345 SetError(kDeviceNotConnectedError);
347 else 346 else
348 SetError(kDisconnectError); 347 SetError(kDisconnectError);
349 348
350 SendResponse(false); 349 SendResponse(false);
351 } 350 }
352 351
353 void BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback() { 352 ////////////////////////////////////////////////////////////////////////////////
354 SendResponse(true);
355 }
356
357 void BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback() {
358 SetError(kSetDiscoveryFilterFailed);
359 SendResponse(false);
360 }
361 353
362 bool BluetoothPrivateSetDiscoveryFilterFunction::DoWork( 354 bool BluetoothPrivateSetDiscoveryFilterFunction::DoWork(
363 scoped_refptr<device::BluetoothAdapter> adapter) { 355 scoped_refptr<device::BluetoothAdapter> adapter) {
364 scoped_ptr<SetDiscoveryFilter::Params> params( 356 scoped_ptr<SetDiscoveryFilter::Params> params(
365 SetDiscoveryFilter::Params::Create(*args_)); 357 SetDiscoveryFilter::Params::Create(*args_));
366 auto& df_param = params->discovery_filter; 358 auto& df_param = params->discovery_filter;
367 359
368 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter; 360 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter;
369 361
370 // If all filter fields are empty, we are clearing filter. If any field is 362 // 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(), 405 discovery_filter.Pass(), adapter.get(), GetExtensionId(),
414 base::Bind( 406 base::Bind(
415 &BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback, 407 &BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback,
416 this), 408 this),
417 base::Bind( 409 base::Bind(
418 &BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback, 410 &BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback,
419 this)); 411 this));
420 return true; 412 return true;
421 } 413 }
422 414
415 void BluetoothPrivateSetDiscoveryFilterFunction::OnSuccessCallback() {
416 SendResponse(true);
417 }
418
419 void BluetoothPrivateSetDiscoveryFilterFunction::OnErrorCallback() {
420 SetError(kSetDiscoveryFilterFailed);
421 SendResponse(false);
422 }
423
424 ////////////////////////////////////////////////////////////////////////////////
425
426 BluetoothPrivateConnectFunction::BluetoothPrivateConnectFunction() {}
427
428 BluetoothPrivateConnectFunction::~BluetoothPrivateConnectFunction() {}
429
430 bool BluetoothPrivateConnectFunction::DoWork(
431 scoped_refptr<device::BluetoothAdapter> adapter) {
432 scoped_ptr<bt_private::Connect::Params> params(
433 bt_private::Connect::Params::Create(*args_));
434 EXTENSION_FUNCTION_VALIDATE(params.get());
435
436 device::BluetoothDevice* device = adapter->GetDevice(params->device_address);
437 if (!device) {
438 SetError(kDeviceNotFoundError);
439 SendResponse(false);
440 return true;
441 }
442
443 if (device->IsConnected()) {
444 SetError(kDeviceConnectedError);
445 SendResponse(false);
446 return true;
447 }
448
449 device::BluetoothDevice::PairingDelegate* pairing_delegate =
450 BluetoothAPI::Get(browser_context())
451 ->event_router()
452 ->GetPairingDelegate(GetExtensionId());
453 if (!pairing_delegate) {
454 LOG(ERROR) << "No pairing delegate for: " << GetExtensionId();
455 SetError(kPairingNotEnabled);
456 SendResponse(false);
457 return true;
458 }
459 device->Connect(
460 pairing_delegate,
461 base::Bind(&BluetoothPrivateConnectFunction::OnSuccessCallback, this),
462 base::Bind(&BluetoothPrivateConnectFunction::OnErrorCallback, this));
463 return true;
464 }
465
466 void BluetoothPrivateConnectFunction::OnSuccessCallback() {
467 results_ = bt_private::Connect::Results::Create(
468 bt_private::CONNECT_RESULT_TYPE_SUCCESS);
469 SendResponse(true);
470 }
471
472 void BluetoothPrivateConnectFunction::OnErrorCallback(
473 device::BluetoothDevice::ConnectErrorCode error) {
474 bt_private::ConnectResultType result = bt_private::CONNECT_RESULT_TYPE_NONE;
475 switch (error) {
476 case device::BluetoothDevice::ERROR_UNKNOWN:
477 result = bt_private::CONNECT_RESULT_TYPE_UNKNOWNERROR;
478 break;
479 case device::BluetoothDevice::ERROR_INPROGRESS:
480 result = bt_private::CONNECT_RESULT_TYPE_INPROGRESS;
481 break;
482 case device::BluetoothDevice::ERROR_FAILED:
483 result = bt_private::CONNECT_RESULT_TYPE_FAILED;
484 break;
485 case device::BluetoothDevice::ERROR_AUTH_FAILED:
486 result = bt_private::CONNECT_RESULT_TYPE_AUTHFAILED;
487 break;
488 case device::BluetoothDevice::ERROR_AUTH_CANCELED:
489 result = bt_private::CONNECT_RESULT_TYPE_AUTHCANCELED;
490 break;
491 case device::BluetoothDevice::ERROR_AUTH_REJECTED:
492 result = bt_private::CONNECT_RESULT_TYPE_AUTHREJECTED;
493 break;
494 case device::BluetoothDevice::ERROR_AUTH_TIMEOUT:
495 result = bt_private::CONNECT_RESULT_TYPE_AUTHTIMEOUT;
496 break;
497 case device::BluetoothDevice::ERROR_UNSUPPORTED_DEVICE:
498 result = bt_private::CONNECT_RESULT_TYPE_UNSUPPORTEDDEVICE;
499 break;
500 }
501 // Set the result type and respond with true (success).
502 results_ = bt_private::Connect::Results::Create(result);
503 SendResponse(true);
504 }
505
506 ////////////////////////////////////////////////////////////////////////////////
507
423 BluetoothPrivatePairFunction::BluetoothPrivatePairFunction() {} 508 BluetoothPrivatePairFunction::BluetoothPrivatePairFunction() {}
424 509
425 BluetoothPrivatePairFunction::~BluetoothPrivatePairFunction() {} 510 BluetoothPrivatePairFunction::~BluetoothPrivatePairFunction() {}
426 511
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( 512 bool BluetoothPrivatePairFunction::DoWork(
438 scoped_refptr<device::BluetoothAdapter> adapter) { 513 scoped_refptr<device::BluetoothAdapter> adapter) {
439 scoped_ptr<bt_private::Pair::Params> params( 514 scoped_ptr<bt_private::Pair::Params> params(
440 bt_private::Pair::Params::Create(*args_)); 515 bt_private::Pair::Params::Create(*args_));
516 EXTENSION_FUNCTION_VALIDATE(params.get());
441 517
442 device::BluetoothDevice* device = adapter->GetDevice(params->device_address); 518 device::BluetoothDevice* device = adapter->GetDevice(params->device_address);
443 if (!device) { 519 if (!device) {
444 SetError(kDeviceNotFoundError); 520 SetError(kDeviceNotFoundError);
445 SendResponse(false); 521 SendResponse(false);
446 return true; 522 return true;
447 } 523 }
448 524
449 BluetoothEventRouter* router = 525 BluetoothEventRouter* router =
450 BluetoothAPI::Get(browser_context())->event_router(); 526 BluetoothAPI::Get(browser_context())->event_router();
451 if (!router->GetPairingDelegate(GetExtensionId())) { 527 if (!router->GetPairingDelegate(GetExtensionId())) {
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 router->GetPairingDelegate(GetExtensionId()),
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