OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_api.h" | 5 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_api.h" |
6 | 6 |
7 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" | 7 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" |
8 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_
dispatcher.h" | 8 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_socket_event_
dispatcher.h" |
9 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" | 9 #include "chrome/common/extensions/api/bluetooth/bluetooth_manifest_data.h" |
10 #include "content/public/browser/browser_context.h" | 10 #include "content/public/browser/browser_context.h" |
(...skipping 27 matching lines...) Expand all Loading... |
38 if (!socket->name().empty()) { | 38 if (!socket->name().empty()) { |
39 socket_info->name.reset(new std::string(socket->name())); | 39 socket_info->name.reset(new std::string(socket->name())); |
40 } | 40 } |
41 socket_info->persistent = socket->persistent(); | 41 socket_info->persistent = socket->persistent(); |
42 if (socket->buffer_size() > 0) { | 42 if (socket->buffer_size() > 0) { |
43 socket_info->buffer_size.reset(new int(socket->buffer_size())); | 43 socket_info->buffer_size.reset(new int(socket->buffer_size())); |
44 } | 44 } |
45 socket_info->paused = socket->paused(); | 45 socket_info->paused = socket->paused(); |
46 socket_info->connected = socket->IsConnected(); | 46 socket_info->connected = socket->IsConnected(); |
47 | 47 |
48 // TODO(keybuk): These should not be present if socket isn't connected or | 48 if (socket->IsConnected()) |
49 // listening. | 49 socket_info->address.reset(new std::string(socket->device_address())); |
50 socket_info->address.reset(new std::string(socket->device_address())); | |
51 socket_info->uuid.reset(new std::string(socket->uuid().canonical_value())); | 50 socket_info->uuid.reset(new std::string(socket->uuid().canonical_value())); |
52 | 51 |
53 return socket_info; | 52 return socket_info; |
54 } | 53 } |
55 | 54 |
56 void SetSocketProperties(BluetoothApiSocket* socket, | 55 void SetSocketProperties(BluetoothApiSocket* socket, |
57 SocketProperties* properties) { | 56 SocketProperties* properties) { |
58 if (properties->name.get()) { | 57 if (properties->name.get()) { |
59 socket->set_name(*properties->name.get()); | 58 socket->set_name(*properties->name.get()); |
60 } | 59 } |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 socket->set_paused(params_->paused); | 215 socket->set_paused(params_->paused); |
217 if (!params_->paused) { | 216 if (!params_->paused) { |
218 socket_event_dispatcher_->OnSocketResume(extension_id(), | 217 socket_event_dispatcher_->OnSocketResume(extension_id(), |
219 params_->socket_id); | 218 params_->socket_id); |
220 } | 219 } |
221 } | 220 } |
222 | 221 |
223 results_ = bluetooth_socket::SetPaused::Results::Create(); | 222 results_ = bluetooth_socket::SetPaused::Results::Create(); |
224 } | 223 } |
225 | 224 |
226 bool BluetoothSocketListenUsingRfcommFunction::RunAsync() { | 225 BluetoothSocketListenFunction::BluetoothSocketListenFunction() {} |
227 // TODO(keybuk): Implement. | 226 |
228 SetError("Not yet implemented."); | 227 BluetoothSocketListenFunction::~BluetoothSocketListenFunction() {} |
229 return false; | 228 |
| 229 bool BluetoothSocketListenFunction::Prepare() { |
| 230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 231 if (!CreateParams()) |
| 232 return false; |
| 233 socket_event_dispatcher_ = GetSocketEventDispatcher(browser_context()); |
| 234 return socket_event_dispatcher_ != NULL; |
230 } | 235 } |
231 | 236 |
232 bool BluetoothSocketListenUsingInsecureRfcommFunction::RunAsync() { | 237 void BluetoothSocketListenFunction::AsyncWorkStart() { |
233 // TODO(keybuk): Implement. | 238 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
234 SetError("Not yet implemented."); | 239 device::BluetoothAdapterFactory::GetAdapter( |
235 return false; | 240 base::Bind(&BluetoothSocketListenFunction::OnGetAdapter, this)); |
236 } | 241 } |
237 | 242 |
238 bool BluetoothSocketListenUsingL2capFunction::RunAsync() { | 243 void BluetoothSocketListenFunction::OnGetAdapter( |
239 // TODO(keybuk): Implement. | 244 scoped_refptr<device::BluetoothAdapter> adapter) { |
240 SetError("Not yet implemented."); | 245 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
241 return false; | 246 BluetoothApiSocket* socket = GetSocket(socket_id()); |
| 247 if (!socket) { |
| 248 error_ = kSocketNotFoundError; |
| 249 AsyncWorkCompleted(); |
| 250 return; |
| 251 } |
| 252 |
| 253 device::BluetoothUUID bluetooth_uuid(uuid()); |
| 254 if (!bluetooth_uuid.IsValid()) { |
| 255 error_ = kInvalidUuidError; |
| 256 AsyncWorkCompleted(); |
| 257 return; |
| 258 } |
| 259 |
| 260 BluetoothPermissionRequest param(uuid()); |
| 261 if (!BluetoothManifestData::CheckRequest(GetExtension(), param)) { |
| 262 error_ = kPermissionDeniedError; |
| 263 AsyncWorkCompleted(); |
| 264 return; |
| 265 } |
| 266 |
| 267 CreateService( |
| 268 adapter, |
| 269 bluetooth_uuid, |
| 270 base::Bind(&BluetoothSocketListenFunction::OnCreateService, this), |
| 271 base::Bind(&BluetoothSocketListenFunction::OnCreateServiceError, this)); |
| 272 } |
| 273 |
| 274 |
| 275 void BluetoothSocketListenFunction::OnCreateService( |
| 276 scoped_refptr<device::BluetoothSocket> socket) { |
| 277 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
| 278 |
| 279 // Fetch the socket again since this is not a reference-counted object, and |
| 280 // it may have gone away in the meantime (we check earlier to avoid making |
| 281 // a connection in the case of an obvious programming error). |
| 282 BluetoothApiSocket* api_socket = GetSocket(socket_id()); |
| 283 if (!api_socket) { |
| 284 error_ = kSocketNotFoundError; |
| 285 AsyncWorkCompleted(); |
| 286 return; |
| 287 } |
| 288 |
| 289 api_socket->AdoptListeningSocket(socket, |
| 290 device::BluetoothUUID(uuid())); |
| 291 socket_event_dispatcher_->OnSocketListen(extension_id(), socket_id()); |
| 292 |
| 293 CreateResults(); |
| 294 AsyncWorkCompleted(); |
| 295 } |
| 296 |
| 297 void BluetoothSocketListenFunction::OnCreateServiceError( |
| 298 const std::string& message) { |
| 299 DCHECK(BrowserThread::CurrentlyOn(work_thread_id())); |
| 300 error_ = message; |
| 301 AsyncWorkCompleted(); |
| 302 } |
| 303 |
| 304 BluetoothSocketListenUsingRfcommFunction:: |
| 305 BluetoothSocketListenUsingRfcommFunction() {} |
| 306 |
| 307 BluetoothSocketListenUsingRfcommFunction:: |
| 308 ~BluetoothSocketListenUsingRfcommFunction() {} |
| 309 |
| 310 int BluetoothSocketListenUsingRfcommFunction::socket_id() const { |
| 311 return params_->socket_id; |
| 312 } |
| 313 |
| 314 const std::string& BluetoothSocketListenUsingRfcommFunction::uuid() const { |
| 315 return params_->uuid; |
| 316 } |
| 317 |
| 318 bool BluetoothSocketListenUsingRfcommFunction::CreateParams() { |
| 319 params_ = bluetooth_socket::ListenUsingRfcomm::Params::Create(*args_); |
| 320 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 321 return true; |
| 322 } |
| 323 |
| 324 void BluetoothSocketListenUsingRfcommFunction::CreateService( |
| 325 scoped_refptr<device::BluetoothAdapter> adapter, |
| 326 const device::BluetoothUUID& uuid, |
| 327 const device::BluetoothAdapter::CreateServiceCallback& callback, |
| 328 const device::BluetoothAdapter::CreateServiceErrorCallback& |
| 329 error_callback) { |
| 330 int channel = params_->channel; |
| 331 if (!channel) |
| 332 channel = device::BluetoothAdapter::kChannelAuto; |
| 333 |
| 334 adapter->CreateRfcommService(uuid, channel, false, callback, error_callback); |
| 335 } |
| 336 |
| 337 void BluetoothSocketListenUsingRfcommFunction::CreateResults() { |
| 338 results_ = bluetooth_socket::ListenUsingRfcomm::Results::Create(); |
| 339 } |
| 340 |
| 341 BluetoothSocketListenUsingInsecureRfcommFunction:: |
| 342 BluetoothSocketListenUsingInsecureRfcommFunction() {} |
| 343 |
| 344 BluetoothSocketListenUsingInsecureRfcommFunction:: |
| 345 ~BluetoothSocketListenUsingInsecureRfcommFunction() {} |
| 346 |
| 347 int BluetoothSocketListenUsingInsecureRfcommFunction::socket_id() const { |
| 348 return params_->socket_id; |
| 349 } |
| 350 |
| 351 const std::string& |
| 352 BluetoothSocketListenUsingInsecureRfcommFunction::uuid() const { |
| 353 return params_->uuid; |
| 354 } |
| 355 |
| 356 bool BluetoothSocketListenUsingInsecureRfcommFunction::CreateParams() { |
| 357 params_ = bluetooth_socket::ListenUsingInsecureRfcomm::Params::Create(*args_); |
| 358 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 359 return true; |
| 360 } |
| 361 |
| 362 void BluetoothSocketListenUsingInsecureRfcommFunction::CreateService( |
| 363 scoped_refptr<device::BluetoothAdapter> adapter, |
| 364 const device::BluetoothUUID& uuid, |
| 365 const device::BluetoothAdapter::CreateServiceCallback& callback, |
| 366 const device::BluetoothAdapter::CreateServiceErrorCallback& |
| 367 error_callback) { |
| 368 int channel = params_->channel; |
| 369 if (!channel) |
| 370 channel = device::BluetoothAdapter::kChannelAuto; |
| 371 |
| 372 adapter->CreateRfcommService(uuid, channel, true, callback, error_callback); |
| 373 } |
| 374 |
| 375 void BluetoothSocketListenUsingInsecureRfcommFunction::CreateResults() { |
| 376 results_ = bluetooth_socket::ListenUsingInsecureRfcomm::Results::Create(); |
| 377 } |
| 378 |
| 379 BluetoothSocketListenUsingL2capFunction:: |
| 380 BluetoothSocketListenUsingL2capFunction() {} |
| 381 |
| 382 BluetoothSocketListenUsingL2capFunction:: |
| 383 ~BluetoothSocketListenUsingL2capFunction() {} |
| 384 |
| 385 int BluetoothSocketListenUsingL2capFunction::socket_id() const { |
| 386 return params_->socket_id; |
| 387 } |
| 388 |
| 389 const std::string& BluetoothSocketListenUsingL2capFunction::uuid() const { |
| 390 return params_->uuid; |
| 391 } |
| 392 |
| 393 bool BluetoothSocketListenUsingL2capFunction::CreateParams() { |
| 394 params_ = bluetooth_socket::ListenUsingL2cap::Params::Create(*args_); |
| 395 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
| 396 return true; |
| 397 } |
| 398 |
| 399 void BluetoothSocketListenUsingL2capFunction::CreateService( |
| 400 scoped_refptr<device::BluetoothAdapter> adapter, |
| 401 const device::BluetoothUUID& uuid, |
| 402 const device::BluetoothAdapter::CreateServiceCallback& callback, |
| 403 const device::BluetoothAdapter::CreateServiceErrorCallback& |
| 404 error_callback) { |
| 405 int psm = params_->psm; |
| 406 if (!psm) |
| 407 psm = device::BluetoothAdapter::kPsmAuto; |
| 408 |
| 409 adapter->CreateL2capService(uuid, psm, callback, error_callback); |
| 410 } |
| 411 |
| 412 void BluetoothSocketListenUsingL2capFunction::CreateResults() { |
| 413 results_ = bluetooth_socket::ListenUsingL2cap::Results::Create(); |
242 } | 414 } |
243 | 415 |
244 BluetoothSocketConnectFunction::BluetoothSocketConnectFunction() {} | 416 BluetoothSocketConnectFunction::BluetoothSocketConnectFunction() {} |
245 | 417 |
246 BluetoothSocketConnectFunction::~BluetoothSocketConnectFunction() {} | 418 BluetoothSocketConnectFunction::~BluetoothSocketConnectFunction() {} |
247 | 419 |
248 bool BluetoothSocketConnectFunction::Prepare() { | 420 bool BluetoothSocketConnectFunction::Prepare() { |
249 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 421 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
250 params_ = bluetooth_socket::Connect::Params::Create(*args_); | 422 params_ = bluetooth_socket::Connect::Params::Create(*args_); |
251 EXTENSION_FUNCTION_VALIDATE(params_.get()); | 423 EXTENSION_FUNCTION_VALIDATE(params_.get()); |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
462 if (socket) { | 634 if (socket) { |
463 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); | 635 socket_infos.push_back(CreateSocketInfo(socket_id, socket)); |
464 } | 636 } |
465 } | 637 } |
466 } | 638 } |
467 results_ = bluetooth_socket::GetSockets::Results::Create(socket_infos); | 639 results_ = bluetooth_socket::GetSockets::Results::Create(socket_infos); |
468 } | 640 } |
469 | 641 |
470 } // namespace api | 642 } // namespace api |
471 } // namespace extensions | 643 } // namespace extensions |
OLD | NEW |