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 DrainableIOBuffer; |
| 16 class IOBuffer; |
| 17 } // namespace net |
| 18 |
| 19 namespace extensions { |
| 20 |
| 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 { |
| 25 public: |
| 26 enum ErrorReason { kSystemError, kIOPending, kDisconnected }; |
| 27 |
| 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(const std::string& error_message)> |
| 32 ErrorCompletionCallback; |
| 33 typedef base::Callback<void(ErrorReason, const std::string& error_message)> |
| 34 ReceiveErrorCompletionCallback; |
| 35 |
| 36 BluetoothApiSocket(const std::string& owner_extension_id, |
| 37 scoped_refptr<device::BluetoothSocket> socket, |
| 38 const std::string& device_address, |
| 39 const std::string& profile_uuid); |
| 40 virtual ~BluetoothApiSocket(); |
| 41 |
| 42 // Closes the underlying connection. This is a best effort, and never fails. |
| 43 virtual void Disconnect(const base::Closure& success_callback); |
| 44 |
| 45 // Receives data from the socket and calls |success_callback| when data is |
| 46 // available. If an error occurs, calls |error_callback| with a reason and a |
| 47 // message. In particular, if a |Receive| operation is still pending, |
| 48 // |error_callback| will be called with |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. If an error occurs, calls |error_callback| with a |
| 55 // reason and a message. Calling |Send| multiple times without waiting for the |
| 56 // callbacks to be called is a valid usage, as the |buffer| instances are |
| 57 // buffered until the underlying communication channel is available for |
| 58 // sending data. |
| 59 virtual void Send(scoped_refptr<net::DrainableIOBuffer> buffer, |
| 60 const SendCompletionCallback& success_callback, |
| 61 const ErrorCompletionCallback& error_callback); |
| 62 |
| 63 const std::string device_address() const { return device_address_; } |
| 64 const std::string profile_uuid() const { return profile_uuid_; } |
| 65 |
| 66 // Overriden from extensions::ApiResource. |
| 67 virtual bool IsPersistent() const OVERRIDE; |
| 68 |
| 69 const std::string& name() const { return name_; } |
| 70 void set_name(const std::string& name) { name_ = name; } |
| 71 |
| 72 bool persistent() const { return persistent_; } |
| 73 void set_persistent(bool persistent) { persistent_ = persistent; } |
| 74 |
| 75 int buffer_size() const { return buffer_size_; } |
| 76 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } |
| 77 |
| 78 bool paused() const { return paused_; } |
| 79 void set_paused(bool paused) { paused_ = paused; } |
| 80 |
| 81 static const content::BrowserThread::ID kThreadId = |
| 82 content::BrowserThread::FILE; |
| 83 |
| 84 private: |
| 85 friend class ApiResourceManager<BluetoothApiSocket>; |
| 86 static const char* service_name() { return "BluetoothApiSocketManager"; } |
| 87 |
| 88 static void OnSocketReceiveError( |
| 89 const ReceiveErrorCompletionCallback& error_callback, |
| 90 device::BluetoothSocket::ErrorReason reason, |
| 91 const std::string& message); |
| 92 |
| 93 // The underlying device socket instance. |
| 94 scoped_refptr<device::BluetoothSocket> socket_; |
| 95 |
| 96 // The address of the device this socket is connected to. |
| 97 std::string device_address_; |
| 98 |
| 99 // The uuid of the profile this socket is connected to. |
| 100 std::string profile_uuid_; |
| 101 |
| 102 // Application-defined string - see bluetooth.idl. |
| 103 std::string name_; |
| 104 |
| 105 // Flag indicating whether the socket is left open when the application is |
| 106 // suspended - see bluetooth.idl. |
| 107 bool persistent_; |
| 108 |
| 109 // The size of the buffer used to receive data - see bluetooth.idl. |
| 110 int buffer_size_; |
| 111 |
| 112 // Flag indicating whether a connected socket blocks its peer from sending |
| 113 // more data - see bluetooth.idl. |
| 114 bool paused_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(BluetoothApiSocket); |
| 117 }; |
| 118 |
| 119 } // namespace extensions |
| 120 |
| 121 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_ |
OLD | NEW |