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.
5 #include "chrome/browser/extensions/api/bluetooth_socket/bluetooth_api_socket.h"
7 #include "device/bluetooth/bluetooth_socket.h"
8 #include "net/base/io_buffer.h"
12 const char kSocketNotConnectedError
[] = "Socket not connected";
13 const char kSocketNotListeningError
[] = "Socket not listening";
17 namespace extensions
{
20 static base::LazyInstance
<
21 BrowserContextKeyedAPIFactory
<ApiResourceManager
<BluetoothApiSocket
> > >
22 g_server_factory
= LAZY_INSTANCE_INITIALIZER
;
26 BrowserContextKeyedAPIFactory
<ApiResourceManager
<BluetoothApiSocket
> >*
27 ApiResourceManager
<BluetoothApiSocket
>::GetFactoryInstance() {
28 return g_server_factory
.Pointer();
31 BluetoothApiSocket::BluetoothApiSocket(const std::string
& owner_extension_id
)
32 : ApiResource(owner_extension_id
),
37 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
40 BluetoothApiSocket::BluetoothApiSocket(
41 const std::string
& owner_extension_id
,
42 scoped_refptr
<device::BluetoothSocket
> socket
,
43 const std::string
& device_address
,
44 const device::BluetoothUUID
& uuid
)
45 : ApiResource(owner_extension_id
),
47 device_address_(device_address
),
53 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
56 BluetoothApiSocket::~BluetoothApiSocket() {
57 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
62 void BluetoothApiSocket::AdoptConnectedSocket(
63 scoped_refptr
<device::BluetoothSocket
> socket
,
64 const std::string
& device_address
,
65 const device::BluetoothUUID
& uuid
) {
66 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
72 device_address_
= device_address
;
77 void BluetoothApiSocket::AdoptListeningSocket(
78 scoped_refptr
<device::BluetoothSocket
> socket
,
79 const device::BluetoothUUID
& uuid
) {
80 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
91 void BluetoothApiSocket::Disconnect(const base::Closure
& callback
) {
92 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
100 socket_
->Disconnect(callback
);
103 bool BluetoothApiSocket::IsPersistent() const {
104 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
108 void BluetoothApiSocket::Receive(
110 const ReceiveCompletionCallback
& success_callback
,
111 const ErrorCompletionCallback
& error_callback
) {
112 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
114 if (!socket_
.get() || !IsConnected()) {
115 error_callback
.Run(BluetoothApiSocket::kNotConnected
,
116 kSocketNotConnectedError
);
120 socket_
->Receive(count
,
122 base::Bind(&OnSocketReceiveError
, error_callback
));
126 void BluetoothApiSocket::OnSocketReceiveError(
127 const ErrorCompletionCallback
& error_callback
,
128 device::BluetoothSocket::ErrorReason reason
,
129 const std::string
& message
) {
130 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
131 BluetoothApiSocket::ErrorReason error_reason
;
133 case device::BluetoothSocket::kIOPending
:
134 error_reason
= BluetoothApiSocket::kIOPending
;
136 case device::BluetoothSocket::kDisconnected
:
137 error_reason
= BluetoothApiSocket::kDisconnected
;
139 case device::BluetoothSocket::kSystemError
:
140 error_reason
= BluetoothApiSocket::kSystemError
;
145 error_callback
.Run(error_reason
, message
);
148 void BluetoothApiSocket::Send(scoped_refptr
<net::IOBuffer
> buffer
,
150 const SendCompletionCallback
& success_callback
,
151 const ErrorCompletionCallback
& error_callback
) {
152 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
154 if (!socket_
.get() || !IsConnected()) {
155 error_callback
.Run(BluetoothApiSocket::kNotConnected
,
156 kSocketNotConnectedError
);
160 socket_
->Send(buffer
,
163 base::Bind(&OnSocketSendError
, error_callback
));
167 void BluetoothApiSocket::OnSocketSendError(
168 const ErrorCompletionCallback
& error_callback
,
169 const std::string
& message
) {
170 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
171 error_callback
.Run(BluetoothApiSocket::kSystemError
, message
);
174 void BluetoothApiSocket::Accept(
175 const AcceptCompletionCallback
& success_callback
,
176 const ErrorCompletionCallback
& error_callback
) {
177 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
179 if (!socket_
.get() || IsConnected()) {
180 error_callback
.Run(BluetoothApiSocket::kNotListening
,
181 kSocketNotListeningError
);
185 socket_
->Accept(success_callback
,
186 base::Bind(&OnSocketAcceptError
, error_callback
));
190 void BluetoothApiSocket::OnSocketAcceptError(
191 const ErrorCompletionCallback
& error_callback
,
192 const std::string
& message
) {
193 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
194 error_callback
.Run(BluetoothApiSocket::kSystemError
, message
);
197 } // namespace extensions