| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" | |
| 6 | |
| 7 #include "device/bluetooth/bluetooth_socket.h" | |
| 8 #include "net/base/io_buffer.h" | |
| 9 | |
| 10 namespace extensions { | |
| 11 | |
| 12 // static | |
| 13 static base::LazyInstance< | |
| 14 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> > > | |
| 15 g_server_factory = LAZY_INSTANCE_INITIALIZER; | |
| 16 | |
| 17 // static | |
| 18 template <> | |
| 19 BrowserContextKeyedAPIFactory<ApiResourceManager<BluetoothApiSocket> >* | |
| 20 ApiResourceManager<BluetoothApiSocket>::GetFactoryInstance() { | |
| 21 return g_server_factory.Pointer(); | |
| 22 } | |
| 23 | |
| 24 BluetoothApiSocket::BluetoothApiSocket( | |
| 25 const std::string& owner_extension_id, | |
| 26 scoped_refptr<device::BluetoothSocket> socket, | |
| 27 const std::string& device_address, | |
| 28 const device::BluetoothUUID& uuid) | |
| 29 : ApiResource(owner_extension_id), | |
| 30 socket_(socket), | |
| 31 device_address_(device_address), | |
| 32 uuid_(uuid), | |
| 33 persistent_(false), | |
| 34 buffer_size_(0), | |
| 35 paused_(true) { | |
| 36 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | |
| 37 } | |
| 38 | |
| 39 BluetoothApiSocket::~BluetoothApiSocket() { | |
| 40 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | |
| 41 socket_->Close(); | |
| 42 } | |
| 43 | |
| 44 void BluetoothApiSocket::Disconnect(const base::Closure& success_callback) { | |
| 45 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | |
| 46 socket_->Disconnect(success_callback); | |
| 47 } | |
| 48 | |
| 49 bool BluetoothApiSocket::IsPersistent() const { | |
| 50 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | |
| 51 return persistent_; | |
| 52 } | |
| 53 | |
| 54 void BluetoothApiSocket::Receive( | |
| 55 int count, | |
| 56 const ReceiveCompletionCallback& success_callback, | |
| 57 const ReceiveErrorCompletionCallback& error_callback) { | |
| 58 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | |
| 59 socket_->Receive(count, | |
| 60 success_callback, | |
| 61 base::Bind(&OnSocketReceiveError, error_callback)); | |
| 62 } | |
| 63 | |
| 64 // static | |
| 65 void BluetoothApiSocket::OnSocketReceiveError( | |
| 66 const ReceiveErrorCompletionCallback& error_callback, | |
| 67 device::BluetoothSocket::ErrorReason reason, | |
| 68 const std::string& message) { | |
| 69 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | |
| 70 BluetoothApiSocket::ErrorReason error_reason; | |
| 71 switch (reason) { | |
| 72 case device::BluetoothSocket::kIOPending: | |
| 73 error_reason = BluetoothApiSocket::kIOPending; | |
| 74 break; | |
| 75 case device::BluetoothSocket::kDisconnected: | |
| 76 error_reason = BluetoothApiSocket::kDisconnected; | |
| 77 break; | |
| 78 case device::BluetoothSocket::kSystemError: | |
| 79 error_reason = BluetoothApiSocket::kSystemError; | |
| 80 break; | |
| 81 default: | |
| 82 NOTREACHED(); | |
| 83 } | |
| 84 error_callback.Run(error_reason, message); | |
| 85 } | |
| 86 | |
| 87 void BluetoothApiSocket::Send(scoped_refptr<net::IOBuffer> buffer, | |
| 88 int buffer_size, | |
| 89 const SendCompletionCallback& success_callback, | |
| 90 const ErrorCompletionCallback& error_callback) { | |
| 91 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId)); | |
| 92 socket_->Send(buffer, buffer_size, success_callback, error_callback); | |
| 93 } | |
| 94 | |
| 95 } // namespace extensions | |
| OLD | NEW |