Index: chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h |
diff --git a/chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h b/chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7fce0cd17ed442f0b50dab034fd4742939a22c45 |
--- /dev/null |
+++ b/chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h |
@@ -0,0 +1,121 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_ |
+#define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_ |
+ |
+#include <string> |
+ |
+#include "device/bluetooth/bluetooth_socket.h" |
+#include "extensions/browser/api/api_resource.h" |
+#include "extensions/browser/api/api_resource_manager.h" |
+ |
+namespace net { |
+class IOBuffer; |
+} // namespace net |
+ |
+namespace extensions { |
+ |
+// Representation of socket instances from the "bluetooth" namespace, |
+// abstracting away platform differences from the underlying BluetoothSocketXxx |
+// class. All methods must be called on the ||kThreadId| thread. |
+class BluetoothApiSocket : public ApiResource { |
+ public: |
+ enum ErrorReason { kSystemError, kIOPending, kDisconnected }; |
+ |
+ typedef base::Callback<void(int)> SendCompletionCallback; |
+ typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> |
+ ReceiveCompletionCallback; |
+ typedef base::Callback<void(const std::string& error_message)> |
+ ErrorCompletionCallback; |
+ typedef base::Callback<void(ErrorReason, const std::string& error_message)> |
+ ReceiveErrorCompletionCallback; |
+ |
+ BluetoothApiSocket(const std::string& owner_extension_id, |
+ scoped_refptr<device::BluetoothSocket> socket, |
+ const std::string& device_address, |
+ const std::string& profile_uuid); |
+ virtual ~BluetoothApiSocket(); |
+ |
+ // Closes the underlying connection. This is a best effort, and never fails. |
+ virtual void Disconnect(const base::Closure& success_callback); |
+ |
+ // Receives data from the socket and calls |success_callback| when data is |
+ // available. If an error occurs, calls |error_callback| with a reason and a |
+ // message. In particular, if a |Receive| operation is still pending, |
+ // |error_callback| will be called with |kIOPending| error. |
keybuk
2014/03/20 01:21:56
count isn't documented
|
+ virtual void Receive(int count, |
+ const ReceiveCompletionCallback& success_callback, |
+ const ReceiveErrorCompletionCallback& error_callback); |
+ |
+ // Sends |buffer| to the socket and calls |success_callback| when data has |
+ // been successfully sent. If an error occurs, calls |error_callback| with a |
+ // reason and a message. Calling |Send| multiple times without waiting for the |
+ // callbacks to be called is a valid usage, as the |buffer| instances are |
+ // buffered until the underlying communication channel is available for |
+ // sending data. |
keybuk
2014/03/20 01:21:56
buffer_size isn't documented
rpaquay
2014/03/20 18:21:11
Done.
|
+ virtual void Send(scoped_refptr<net::IOBuffer> buffer, |
+ int buffer_size, |
+ const SendCompletionCallback& success_callback, |
+ const ErrorCompletionCallback& error_callback); |
+ |
+ const std::string device_address() const { return device_address_; } |
+ const std::string profile_uuid() const { return profile_uuid_; } |
keybuk
2014/03/20 01:21:56
rename to uuid()
rpaquay
2014/03/20 18:21:11
Done.
|
+ |
+ // Overriden from extensions::ApiResource. |
+ virtual bool IsPersistent() const OVERRIDE; |
+ |
+ const std::string& name() const { return name_; } |
+ void set_name(const std::string& name) { name_ = name; } |
+ |
+ bool persistent() const { return persistent_; } |
+ void set_persistent(bool persistent) { persistent_ = persistent; } |
+ |
+ int buffer_size() const { return buffer_size_; } |
+ void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } |
+ |
+ bool paused() const { return paused_; } |
+ void set_paused(bool paused) { paused_ = paused; } |
+ |
+ static const content::BrowserThread::ID kThreadId = |
+ content::BrowserThread::FILE; |
+ |
+ private: |
+ friend class ApiResourceManager<BluetoothApiSocket>; |
+ static const char* service_name() { return "BluetoothApiSocketManager"; } |
+ |
+ static void OnSocketReceiveError( |
+ const ReceiveErrorCompletionCallback& error_callback, |
+ device::BluetoothSocket::ErrorReason reason, |
+ const std::string& message); |
+ |
+ // The underlying device socket instance. |
+ scoped_refptr<device::BluetoothSocket> socket_; |
+ |
+ // The address of the device this socket is connected to. |
+ std::string device_address_; |
+ |
+ // The uuid of the profile this socket is connected to. |
+ std::string profile_uuid_; |
keybuk
2014/03/20 01:21:56
Rename to uuid_, replace mention of profile in the
rpaquay
2014/03/20 18:21:11
Done.
|
+ |
+ // Application-defined string - see bluetooth.idl. |
+ std::string name_; |
+ |
+ // Flag indicating whether the socket is left open when the application is |
+ // suspended - see bluetooth.idl. |
+ bool persistent_; |
+ |
+ // The size of the buffer used to receive data - see bluetooth.idl. |
+ int buffer_size_; |
keybuk
2014/03/20 01:21:56
How is this used for L2CAP sockets which are datag
rpaquay
2014/03/20 18:21:11
Unless I am missing something, this is similar to
keybuk
2014/03/20 18:38:40
Ack
|
+ |
+ // Flag indicating whether a connected socket blocks its peer from sending |
+ // more data - see bluetooth.idl. |
+ bool paused_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(BluetoothApiSocket); |
+}; |
+ |
+} // namespace extensions |
+ |
+#endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_ |