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

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

Powered by Google App Engine
This is Rietveld 408576698