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 // Closes the underlying connection. This is a best effort, and never fails.
42 virtual void Disconnect(const base::Closure
& success_callback
);
44 // Receives data from the socket and calls |success_callback| when data is
45 // available. |count| is maximum amount of bytes received. If an error occurs,
46 // calls |error_callback| with a reason and a message. In particular, if a
47 // |Receive| operation is still pending, |error_callback| will be called with
48 // |kIOPending| error.
49 virtual void Receive(int count
,
50 const ReceiveCompletionCallback
& success_callback
,
51 const ErrorCompletionCallback
& error_callback
);
53 // Sends |buffer| to the socket and calls |success_callback| when data has
54 // been successfully sent. |buffer_size| is the numberof bytes contained in
55 // |buffer|. If an error occurs, calls |error_callback| with a reason and a
56 // message. Calling |Send| multiple times without waiting for the callbacks to
57 // be called is a valid usage, as |buffer| instances are buffered until the
58 // underlying communication channel is available for sending data.
59 virtual void Send(scoped_refptr
<net::IOBuffer
> buffer
,
61 const SendCompletionCallback
& success_callback
,
62 const ErrorCompletionCallback
& error_callback
);
64 const std::string
& device_address() const { return device_address_
; }
65 const device::BluetoothUUID
& uuid() const { return uuid_
; }
67 // Overriden from extensions::ApiResource.
68 virtual bool IsPersistent() const OVERRIDE
;
70 const std::string
& name() const { return name_
; }
71 void set_name(const std::string
& name
) { name_
= name
; }
73 bool persistent() const { return persistent_
; }
74 void set_persistent(bool persistent
) { persistent_
= persistent
; }
76 int buffer_size() const { return buffer_size_
; }
77 void set_buffer_size(int buffer_size
) { buffer_size_
= buffer_size
; }
79 bool paused() const { return paused_
; }
80 void set_paused(bool paused
) { paused_
= paused
; }
82 bool IsConnected() const { return connected_
; }
84 // Platform specific implementations of |BluetoothSocket| require being called
86 static const content::BrowserThread::ID kThreadId
=
87 content::BrowserThread::UI
;
90 friend class ApiResourceManager
<BluetoothApiSocket
>;
91 static const char* service_name() { return "BluetoothApiSocketManager"; }
93 static void OnSocketReceiveError(
94 const ErrorCompletionCallback
& error_callback
,
95 device::BluetoothSocket::ErrorReason reason
,
96 const std::string
& message
);
98 static void OnSocketSendError(
99 const ErrorCompletionCallback
& error_callback
,
100 const std::string
& message
);
102 // The underlying device socket instance.
103 scoped_refptr
<device::BluetoothSocket
> socket_
;
105 // The address of the device this socket is connected to.
106 std::string device_address_
;
108 // The uuid of the service this socket is connected to.
109 device::BluetoothUUID uuid_
;
111 // Application-defined string - see bluetooth.idl.
114 // Flag indicating whether the socket is left open when the application is
115 // suspended - see bluetooth.idl.
118 // The size of the buffer used to receive data - see bluetooth.idl.
121 // Flag indicating whether a connected socket blocks its peer from sending
122 // more data - see bluetooth.idl.
125 // Flag indicating whether a socket is connected.
128 DISALLOW_COPY_AND_ASSIGN(BluetoothApiSocket
);
131 } // namespace extensions
133 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_