OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "device/bluetooth/bluetooth_socket.h" |
| 11 #include "extensions/browser/api/api_resource.h" |
| 12 #include "extensions/browser/api/api_resource_manager.h" |
| 13 |
| 14 namespace net { |
| 15 class IOBuffer; |
| 16 } // namespace net |
| 17 |
| 18 namespace extensions { |
| 19 |
| 20 // Representation of socket instances from the "bluetooth" namespace, |
| 21 // abstracting away platform differences from the underlying BluetoothSocketXxx |
| 22 // class. All methods must be called on the |kThreadId| thread. |
| 23 class BluetoothApiSocket : public ApiResource { |
| 24 public: |
| 25 enum ErrorReason { kSystemError, kIOPending, kDisconnected }; |
| 26 |
| 27 typedef base::Callback<void(int)> SendCompletionCallback; |
| 28 typedef base::Callback<void(int, scoped_refptr<net::IOBuffer> io_buffer)> |
| 29 ReceiveCompletionCallback; |
| 30 typedef base::Callback<void(const std::string& error_message)> |
| 31 ErrorCompletionCallback; |
| 32 typedef base::Callback<void(ErrorReason, const std::string& error_message)> |
| 33 ReceiveErrorCompletionCallback; |
| 34 |
| 35 BluetoothApiSocket(const std::string& owner_extension_id, |
| 36 scoped_refptr<device::BluetoothSocket> socket, |
| 37 const std::string& device_address, |
| 38 const std::string& uuid); |
| 39 virtual ~BluetoothApiSocket(); |
| 40 |
| 41 // Closes the underlying connection. This is a best effort, and never fails. |
| 42 virtual void Disconnect(const base::Closure& success_callback); |
| 43 |
| 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 ReceiveErrorCompletionCallback& error_callback); |
| 52 |
| 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, |
| 60 int buffer_size, |
| 61 const SendCompletionCallback& success_callback, |
| 62 const ErrorCompletionCallback& error_callback); |
| 63 |
| 64 const std::string device_address() const { return device_address_; } |
| 65 const std::string uuid() const { return uuid_; } |
| 66 |
| 67 // Overriden from extensions::ApiResource. |
| 68 virtual bool IsPersistent() const OVERRIDE; |
| 69 |
| 70 const std::string& name() const { return name_; } |
| 71 void set_name(const std::string& name) { name_ = name; } |
| 72 |
| 73 bool persistent() const { return persistent_; } |
| 74 void set_persistent(bool persistent) { persistent_ = persistent; } |
| 75 |
| 76 int buffer_size() const { return buffer_size_; } |
| 77 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } |
| 78 |
| 79 bool paused() const { return paused_; } |
| 80 void set_paused(bool paused) { paused_ = paused; } |
| 81 |
| 82 // Platform specific implementations of |BluetoothSocket| require being called |
| 83 // on the UI thread. |
| 84 static const content::BrowserThread::ID kThreadId = |
| 85 content::BrowserThread::UI; |
| 86 |
| 87 private: |
| 88 friend class ApiResourceManager<BluetoothApiSocket>; |
| 89 static const char* service_name() { return "BluetoothApiSocketManager"; } |
| 90 |
| 91 static void OnSocketReceiveError( |
| 92 const ReceiveErrorCompletionCallback& error_callback, |
| 93 device::BluetoothSocket::ErrorReason reason, |
| 94 const std::string& message); |
| 95 |
| 96 // The underlying device socket instance. |
| 97 scoped_refptr<device::BluetoothSocket> socket_; |
| 98 |
| 99 // The address of the device this socket is connected to. |
| 100 std::string device_address_; |
| 101 |
| 102 // The uuid of the service this socket is connected to. |
| 103 std::string uuid_; |
| 104 |
| 105 // Application-defined string - see bluetooth.idl. |
| 106 std::string name_; |
| 107 |
| 108 // Flag indicating whether the socket is left open when the application is |
| 109 // suspended - see bluetooth.idl. |
| 110 bool persistent_; |
| 111 |
| 112 // The size of the buffer used to receive data - see bluetooth.idl. |
| 113 int buffer_size_; |
| 114 |
| 115 // Flag indicating whether a connected socket blocks its peer from sending |
| 116 // more data - see bluetooth.idl. |
| 117 bool paused_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(BluetoothApiSocket); |
| 120 }; |
| 121 |
| 122 } // namespace extensions |
| 123 |
| 124 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_ |
OLD | NEW |