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/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";
16 namespace extensions
{
19 static base::LazyInstance
<
20 BrowserContextKeyedAPIFactory
<ApiResourceManager
<BluetoothApiSocket
> > >
21 g_server_factory
= LAZY_INSTANCE_INITIALIZER
;
25 BrowserContextKeyedAPIFactory
<ApiResourceManager
<BluetoothApiSocket
> >*
26 ApiResourceManager
<BluetoothApiSocket
>::GetFactoryInstance() {
27 return g_server_factory
.Pointer();
30 BluetoothApiSocket::BluetoothApiSocket(const std::string
& owner_extension_id
)
31 : ApiResource(owner_extension_id
),
36 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
39 BluetoothApiSocket::BluetoothApiSocket(
40 const std::string
& owner_extension_id
,
41 scoped_refptr
<device::BluetoothSocket
> socket
,
42 const std::string
& device_address
,
43 const device::BluetoothUUID
& uuid
)
44 : ApiResource(owner_extension_id
),
46 device_address_(device_address
),
52 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
55 BluetoothApiSocket::~BluetoothApiSocket() {
56 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
61 void BluetoothApiSocket::Disconnect(const base::Closure
& success_callback
) {
62 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
64 if (!socket_
.get() || !IsConnected()) {
65 success_callback
.Run();
69 socket_
->Disconnect(success_callback
);
72 bool BluetoothApiSocket::IsPersistent() const {
73 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
77 void BluetoothApiSocket::Receive(
79 const ReceiveCompletionCallback
& success_callback
,
80 const ErrorCompletionCallback
& error_callback
) {
81 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
83 if (!socket_
.get() || !IsConnected()) {
84 error_callback
.Run(BluetoothApiSocket::kNotConnected
,
85 kSocketNotConnectedError
);
89 socket_
->Receive(count
,
91 base::Bind(&OnSocketReceiveError
, error_callback
));
95 void BluetoothApiSocket::OnSocketReceiveError(
96 const ErrorCompletionCallback
& error_callback
,
97 device::BluetoothSocket::ErrorReason reason
,
98 const std::string
& message
) {
99 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
100 BluetoothApiSocket::ErrorReason error_reason
;
102 case device::BluetoothSocket::kIOPending
:
103 error_reason
= BluetoothApiSocket::kIOPending
;
105 case device::BluetoothSocket::kDisconnected
:
106 error_reason
= BluetoothApiSocket::kDisconnected
;
108 case device::BluetoothSocket::kSystemError
:
109 error_reason
= BluetoothApiSocket::kSystemError
;
114 error_callback
.Run(error_reason
, message
);
117 void BluetoothApiSocket::Send(scoped_refptr
<net::IOBuffer
> buffer
,
119 const SendCompletionCallback
& success_callback
,
120 const ErrorCompletionCallback
& error_callback
) {
121 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
123 if (!socket_
.get() || !IsConnected()) {
124 error_callback
.Run(BluetoothApiSocket::kNotConnected
,
125 kSocketNotConnectedError
);
129 socket_
->Send(buffer
,
132 base::Bind(&OnSocketSendError
, error_callback
));
136 void BluetoothApiSocket::OnSocketSendError(
137 const ErrorCompletionCallback
& error_callback
,
138 const std::string
& message
) {
139 DCHECK(content::BrowserThread::CurrentlyOn(kThreadId
));
140 error_callback
.Run(BluetoothApiSocket::kSystemError
, message
);
144 } // namespace extensions