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 "extensions/browser/api/bluetooth_socket/bluetooth_api_socket.h"
7 #include "base/lazy_instance.h"
8 #include "device/bluetooth/bluetooth_socket.h"
9 #include "net/base/io_buffer.h"
13 const char kSocketNotConnectedError
[] = "Socket not connected";
14 const char kSocketNotListeningError
[] = "Socket not listening";
18 namespace extensions
{
21 static base::LazyInstance
<
22 BrowserContextKeyedAPIFactory
<ApiResourceManager
<BluetoothApiSocket
> > >
23 g_server_factory
= LAZY_INSTANCE_INITIALIZER
;
27 BrowserContextKeyedAPIFactory
<ApiResourceManager
<BluetoothApiSocket
> >*
28 ApiResourceManager
<BluetoothApiSocket
>::GetFactoryInstance() {
29 return g_server_factory
.Pointer();
32 BluetoothApiSocket::BluetoothApiSocket(const std::string
& owner_extension_id
)
33 : ApiResource(owner_extension_id
),
38 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
41 BluetoothApiSocket::BluetoothApiSocket(
42 const std::string
& owner_extension_id
,
43 scoped_refptr
<device::BluetoothSocket
> socket
,
44 const std::string
& device_address
,
45 const device::BluetoothUUID
& uuid
)
46 : ApiResource(owner_extension_id
),
48 device_address_(device_address
),
54 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
57 BluetoothApiSocket::~BluetoothApiSocket() {
58 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
63 void BluetoothApiSocket::AdoptConnectedSocket(
64 scoped_refptr
<device::BluetoothSocket
> socket
,
65 const std::string
& device_address
,
66 const device::BluetoothUUID
& uuid
) {
67 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
73 device_address_
= device_address
;
78 void BluetoothApiSocket::AdoptListeningSocket(
79 scoped_refptr
<device::BluetoothSocket
> socket
,
80 const device::BluetoothUUID
& uuid
) {
81 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
92 void BluetoothApiSocket::Disconnect(const base::Closure
& callback
) {
93 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
101 socket_
->Disconnect(callback
);
104 bool BluetoothApiSocket::IsPersistent() const {
105 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
109 void BluetoothApiSocket::Receive(
111 const ReceiveCompletionCallback
& success_callback
,
112 const ErrorCompletionCallback
& error_callback
) {
113 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
115 if (!socket_
.get() || !IsConnected()) {
116 error_callback
.Run(BluetoothApiSocket::kNotConnected
,
117 kSocketNotConnectedError
);
121 socket_
->Receive(count
,
123 base::Bind(&OnSocketReceiveError
, error_callback
));
127 void BluetoothApiSocket::OnSocketReceiveError(
128 const ErrorCompletionCallback
& error_callback
,
129 device::BluetoothSocket::ErrorReason reason
,
130 const std::string
& message
) {
131 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
132 BluetoothApiSocket::ErrorReason error_reason
;
134 case device::BluetoothSocket::kIOPending
:
135 error_reason
= BluetoothApiSocket::kIOPending
;
137 case device::BluetoothSocket::kDisconnected
:
138 error_reason
= BluetoothApiSocket::kDisconnected
;
140 case device::BluetoothSocket::kSystemError
:
141 error_reason
= BluetoothApiSocket::kSystemError
;
144 error_callback
.Run(error_reason
, message
);
147 void BluetoothApiSocket::Send(scoped_refptr
<net::IOBuffer
> buffer
,
149 const SendCompletionCallback
& success_callback
,
150 const ErrorCompletionCallback
& error_callback
) {
151 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
153 if (!socket_
.get() || !IsConnected()) {
154 error_callback
.Run(BluetoothApiSocket::kNotConnected
,
155 kSocketNotConnectedError
);
159 socket_
->Send(buffer
,
162 base::Bind(&OnSocketSendError
, error_callback
));
166 void BluetoothApiSocket::OnSocketSendError(
167 const ErrorCompletionCallback
& error_callback
,
168 const std::string
& message
) {
169 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
170 error_callback
.Run(BluetoothApiSocket::kSystemError
, message
);
173 void BluetoothApiSocket::Accept(
174 const AcceptCompletionCallback
& success_callback
,
175 const ErrorCompletionCallback
& error_callback
) {
176 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
178 if (!socket_
.get() || IsConnected()) {
179 error_callback
.Run(BluetoothApiSocket::kNotListening
,
180 kSocketNotListeningError
);
184 socket_
->Accept(success_callback
,
185 base::Bind(&OnSocketAcceptError
, error_callback
));
189 void BluetoothApiSocket::OnSocketAcceptError(
190 const ErrorCompletionCallback
& error_callback
,
191 const std::string
& message
) {
192 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
193 error_callback
.Run(BluetoothApiSocket::kSystemError
, message
);
196 } // namespace extensions