| 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 "device/bluetooth/bluetooth_uuid.h" | |
| 12 #include "extensions/browser/api/api_resource.h" | |
| 13 #include "extensions/browser/api/api_resource_manager.h" | |
| 14 | |
| 15 namespace net { | |
| 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 device::BluetoothUUID& 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. |count| is maximum amount of bytes received. If an error occurs, | |
| 47 // calls |error_callback| with a reason and a message. In particular, if a | |
| 48 // |Receive| operation is still pending, |error_callback| will be called with | |
| 49 // |kIOPending| error. | |
| 50 virtual void Receive(int count, | |
| 51 const ReceiveCompletionCallback& success_callback, | |
| 52 const ReceiveErrorCompletionCallback& error_callback); | |
| 53 | |
| 54 // Sends |buffer| to the socket and calls |success_callback| when data has | |
| 55 // been successfully sent. |buffer_size| is the numberof bytes contained in | |
| 56 // |buffer|. If an error occurs, calls |error_callback| with a reason and a | |
| 57 // message. Calling |Send| multiple times without waiting for the callbacks to | |
| 58 // be called is a valid usage, as |buffer| instances are buffered until the | |
| 59 // underlying communication channel is available for sending data. | |
| 60 virtual void Send(scoped_refptr<net::IOBuffer> buffer, | |
| 61 int buffer_size, | |
| 62 const SendCompletionCallback& success_callback, | |
| 63 const ErrorCompletionCallback& error_callback); | |
| 64 | |
| 65 const std::string& device_address() const { return device_address_; } | |
| 66 const device::BluetoothUUID& uuid() const { return uuid_; } | |
| 67 | |
| 68 // Overriden from extensions::ApiResource. | |
| 69 virtual bool IsPersistent() const OVERRIDE; | |
| 70 | |
| 71 const std::string& name() const { return name_; } | |
| 72 void set_name(const std::string& name) { name_ = name; } | |
| 73 | |
| 74 bool persistent() const { return persistent_; } | |
| 75 void set_persistent(bool persistent) { persistent_ = persistent; } | |
| 76 | |
| 77 int buffer_size() const { return buffer_size_; } | |
| 78 void set_buffer_size(int buffer_size) { buffer_size_ = buffer_size; } | |
| 79 | |
| 80 bool paused() const { return paused_; } | |
| 81 void set_paused(bool paused) { paused_ = paused; } | |
| 82 | |
| 83 // Platform specific implementations of |BluetoothSocket| require being called | |
| 84 // on the UI thread. | |
| 85 static const content::BrowserThread::ID kThreadId = | |
| 86 content::BrowserThread::UI; | |
| 87 | |
| 88 private: | |
| 89 friend class ApiResourceManager<BluetoothApiSocket>; | |
| 90 static const char* service_name() { return "BluetoothApiSocketManager"; } | |
| 91 | |
| 92 static void OnSocketReceiveError( | |
| 93 const ReceiveErrorCompletionCallback& error_callback, | |
| 94 device::BluetoothSocket::ErrorReason reason, | |
| 95 const std::string& message); | |
| 96 | |
| 97 // The underlying device socket instance. | |
| 98 scoped_refptr<device::BluetoothSocket> socket_; | |
| 99 | |
| 100 // The address of the device this socket is connected to. | |
| 101 std::string device_address_; | |
| 102 | |
| 103 // The uuid of the service this socket is connected to. | |
| 104 device::BluetoothUUID uuid_; | |
| 105 | |
| 106 // Application-defined string - see bluetooth.idl. | |
| 107 std::string name_; | |
| 108 | |
| 109 // Flag indicating whether the socket is left open when the application is | |
| 110 // suspended - see bluetooth.idl. | |
| 111 bool persistent_; | |
| 112 | |
| 113 // The size of the buffer used to receive data - see bluetooth.idl. | |
| 114 int buffer_size_; | |
| 115 | |
| 116 // Flag indicating whether a connected socket blocks its peer from sending | |
| 117 // more data - see bluetooth.idl. | |
| 118 bool paused_; | |
| 119 | |
| 120 DISALLOW_COPY_AND_ASSIGN(BluetoothApiSocket); | |
| 121 }; | |
| 122 | |
| 123 } // namespace extensions | |
| 124 | |
| 125 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_API_SOCKET_H_ | |
| OLD | NEW |