Add ICU message format support
[chromium-blink-merge.git] / extensions / browser / api / bluetooth_socket / bluetooth_api_socket.h
blob81ad18c0ba9aa8d2fca404b37cb77ac94c67fc17
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 EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_API_SOCKET_H_
6 #define EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_API_SOCKET_H_
8 #include <string>
10 #include "device/bluetooth/bluetooth_device.h"
11 #include "device/bluetooth/bluetooth_socket.h"
12 #include "device/bluetooth/bluetooth_uuid.h"
13 #include "extensions/browser/api/api_resource.h"
14 #include "extensions/browser/api/api_resource_manager.h"
16 namespace net {
17 class IOBuffer;
18 } // namespace net
20 namespace extensions {
22 // Representation of socket instances from the "bluetooth" namespace,
23 // abstracting away platform differences from the underlying BluetoothSocketXxx
24 // class. All methods must be called on the |kThreadId| thread.
25 class BluetoothApiSocket : public ApiResource {
26 public:
27 enum ErrorReason { kSystemError, kNotConnected, kNotListening, kIOPending,
28 kDisconnected };
30 typedef base::Callback<void(int)> SendCompletionCallback;
31 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)>
32 ReceiveCompletionCallback;
33 typedef base::Callback<void(const device::BluetoothDevice* device,
34 scoped_refptr<device::BluetoothSocket>)>
35 AcceptCompletionCallback;
36 typedef base::Callback<void(ErrorReason, const std::string& error_message)>
37 ErrorCompletionCallback;
39 explicit BluetoothApiSocket(const std::string& owner_extension_id);
40 BluetoothApiSocket(const std::string& owner_extension_id,
41 scoped_refptr<device::BluetoothSocket> socket,
42 const std::string& device_address,
43 const device::BluetoothUUID& uuid);
44 ~BluetoothApiSocket() override;
46 // Adopts a socket |socket| connected to a device with address
47 // |device_address| using the service with UUID |uuid|.
48 virtual void AdoptConnectedSocket(
49 scoped_refptr<device::BluetoothSocket> socket,
50 const std::string& device_address,
51 const device::BluetoothUUID& uuid);
53 // Adopts a socket |socket| listening on a service advertised with UUID
54 // |uuid|.
55 virtual void AdoptListeningSocket(
56 scoped_refptr<device::BluetoothSocket> socket,
57 const device::BluetoothUUID& uuid);
59 // Closes the underlying connection. This is a best effort, and never fails.
60 virtual void Disconnect(const base::Closure& callback);
62 // Receives data from the socket and calls |success_callback| when data is
63 // available. |count| is maximum amount of bytes received. If an error occurs,
64 // calls |error_callback| with a reason and a message. In particular, if a
65 // |Receive| operation is still pending, |error_callback| will be called with
66 // |kIOPending| error.
67 virtual void Receive(int count,
68 const ReceiveCompletionCallback& success_callback,
69 const ErrorCompletionCallback& error_callback);
71 // Sends |buffer| to the socket and calls |success_callback| when data has
72 // been successfully sent. |buffer_size| is the numberof bytes contained in
73 // |buffer|. If an error occurs, calls |error_callback| with a reason and a
74 // message. Calling |Send| multiple times without waiting for the callbacks to
75 // be called is a valid usage, as |buffer| instances are buffered until the
76 // underlying communication channel is available for sending data.
77 virtual void Send(scoped_refptr<net::IOBuffer> buffer,
78 int buffer_size,
79 const SendCompletionCallback& success_callback,
80 const ErrorCompletionCallback& error_callback);
82 // Accepts a client connection from the socket and calls |success_callback|
83 // when one has connected. If an error occurs, calls |error_callback| with a
84 // reason and a message.
85 virtual void Accept(const AcceptCompletionCallback& success_callback,
86 const ErrorCompletionCallback& error_callback);
88 const std::string& device_address() const { return device_address_; }
89 const device::BluetoothUUID& uuid() const { return uuid_; }
91 // Overriden from extensions::ApiResource.
92 bool IsPersistent() const override;
94 const std::string* name() const { return name_.get(); }
95 void set_name(const std::string& name) { name_.reset(new std::string(name)); }
97 bool persistent() const { return persistent_; }
98 void set_persistent(bool persistent) { persistent_ = persistent; }
100 int buffer_size() const { return buffer_size_; }
101 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; }
103 bool paused() const { return paused_; }
104 void set_paused(bool paused) { paused_ = paused; }
106 bool IsConnected() const { return connected_; }
108 // Platform specific implementations of |BluetoothSocket| require being called
109 // on the UI thread.
110 static const content::BrowserThread::ID kThreadId =
111 content::BrowserThread::UI;
113 private:
114 friend class ApiResourceManager<BluetoothApiSocket>;
115 static const char* service_name() { return "BluetoothApiSocketManager"; }
117 static void OnSocketReceiveError(
118 const ErrorCompletionCallback& error_callback,
119 device::BluetoothSocket::ErrorReason reason,
120 const std::string& message);
122 static void OnSocketSendError(
123 const ErrorCompletionCallback& error_callback,
124 const std::string& message);
126 static void OnSocketAcceptError(
127 const ErrorCompletionCallback& error_callback,
128 const std::string& message);
130 // The underlying device socket instance.
131 scoped_refptr<device::BluetoothSocket> socket_;
133 // The address of the device this socket is connected to.
134 std::string device_address_;
136 // The uuid of the service this socket is connected to.
137 device::BluetoothUUID uuid_;
139 // Application-defined string - see bluetooth.idl.
140 scoped_ptr<std::string> name_;
142 // Flag indicating whether the socket is left open when the application is
143 // suspended - see bluetooth.idl.
144 bool persistent_;
146 // The size of the buffer used to receive data - see bluetooth.idl.
147 int buffer_size_;
149 // Flag indicating whether a connected socket blocks its peer from sending
150 // more data - see bluetooth.idl.
151 bool paused_;
153 // Flag indicating whether a socket is connected.
154 bool connected_;
156 DISALLOW_COPY_AND_ASSIGN(BluetoothApiSocket);
159 } // namespace extensions
161 #endif // EXTENSIONS_BROWSER_API_BLUETOOTH_SOCKET_BLUETOOTH_API_SOCKET_H_