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 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_
10 #include "device/bluetooth/bluetooth_socket.h"
11 #include "device/bluetooth/bluetooth_uuid.h"
12 #include "extensions/browser/api/api_resource.h"
13 #include "extensions/browser/api/api_resource_manager.h"
19 namespace extensions
{
21 // Representation of socket instances from the "bluetooth" namespace,
22 // abstracting away platform differences from the underlying BluetoothSocketXxx
23 // class. All methods must be called on the |kThreadId| thread.
24 class BluetoothApiSocket
: public ApiResource
{
26 enum ErrorReason
{ kSystemError
, kNotConnected
, kIOPending
, kDisconnected
};
28 typedef base::Callback
<void(int)> SendCompletionCallback
;
29 typedef base::Callback
<void(int, scoped_refptr
<net::IOBuffer
> io_buffer
)>
30 ReceiveCompletionCallback
;
31 typedef base::Callback
<void(ErrorReason
, const std::string
& error_message
)>
32 ErrorCompletionCallback
;
34 explicit BluetoothApiSocket(const std::string
& owner_extension_id
);
35 BluetoothApiSocket(const std::string
& owner_extension_id
,
36 scoped_refptr
<device::BluetoothSocket
> socket
,
37 const std::string
& device_address
,
38 const device::BluetoothUUID
& uuid
);
39 virtual ~BluetoothApiSocket();
41 // Adopts a socket |socket| connected to a device with address
42 // |device_address| using the service with UUID |uuid|.
43 virtual void AdoptConnectedSocket(
44 scoped_refptr
<device::BluetoothSocket
> socket
,
45 const std::string
& device_address
,
46 const device::BluetoothUUID
& uuid
);
48 // Closes the underlying connection. This is a best effort, and never fails.
49 virtual void Disconnect(const base::Closure
& success_callback
);
51 // Receives data from the socket and calls |success_callback| when data is
52 // available. |count| is maximum amount of bytes received. If an error occurs,
53 // calls |error_callback| with a reason and a message. In particular, if a
54 // |Receive| operation is still pending, |error_callback| will be called with
55 // |kIOPending| error.
56 virtual void Receive(int count
,
57 const ReceiveCompletionCallback
& success_callback
,
58 const ErrorCompletionCallback
& error_callback
);
60 // Sends |buffer| to the socket and calls |success_callback| when data has
61 // been successfully sent. |buffer_size| is the numberof bytes contained in
62 // |buffer|. If an error occurs, calls |error_callback| with a reason and a
63 // message. Calling |Send| multiple times without waiting for the callbacks to
64 // be called is a valid usage, as |buffer| instances are buffered until the
65 // underlying communication channel is available for sending data.
66 virtual void Send(scoped_refptr
<net::IOBuffer
> buffer
,
68 const SendCompletionCallback
& success_callback
,
69 const ErrorCompletionCallback
& error_callback
);
71 const std::string
& device_address() const { return device_address_
; }
72 const device::BluetoothUUID
& uuid() const { return uuid_
; }
74 // Overriden from extensions::ApiResource.
75 virtual bool IsPersistent() const OVERRIDE
;
77 const std::string
& name() const { return name_
; }
78 void set_name(const std::string
& name
) { name_
= name
; }
80 bool persistent() const { return persistent_
; }
81 void set_persistent(bool persistent
) { persistent_
= persistent
; }
83 int buffer_size() const { return buffer_size_
; }
84 void set_buffer_size(int buffer_size
) { buffer_size_
= buffer_size
; }
86 bool paused() const { return paused_
; }
87 void set_paused(bool paused
) { paused_
= paused
; }
89 bool IsConnected() const { return connected_
; }
91 // Platform specific implementations of |BluetoothSocket| require being called
93 static const content::BrowserThread::ID kThreadId
=
94 content::BrowserThread::UI
;
97 friend class ApiResourceManager
<BluetoothApiSocket
>;
98 static const char* service_name() { return "BluetoothApiSocketManager"; }
100 static void OnSocketReceiveError(
101 const ErrorCompletionCallback
& error_callback
,
102 device::BluetoothSocket::ErrorReason reason
,
103 const std::string
& message
);
105 static void OnSocketSendError(
106 const ErrorCompletionCallback
& error_callback
,
107 const std::string
& message
);
109 // The underlying device socket instance.
110 scoped_refptr
<device::BluetoothSocket
> socket_
;
112 // The address of the device this socket is connected to.
113 std::string device_address_
;
115 // The uuid of the service this socket is connected to.
116 device::BluetoothUUID uuid_
;
118 // Application-defined string - see bluetooth.idl.
121 // Flag indicating whether the socket is left open when the application is
122 // suspended - see bluetooth.idl.
125 // The size of the buffer used to receive data - see bluetooth.idl.
128 // Flag indicating whether a connected socket blocks its peer from sending
129 // more data - see bluetooth.idl.
132 // Flag indicating whether a socket is connected.
135 DISALLOW_COPY_AND_ASSIGN(BluetoothApiSocket
);
138 } // namespace extensions
140 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_