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& profile_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. If an error occurs, calls |error_callback| with a reason and a | |
46 // message. In particular, if a |Receive| operation is still pending, | |
47 // |error_callback| will be called with |kIOPending| error. | |
keybuk
2014/03/20 01:21:56
count isn't documented
| |
48 virtual void Receive(int count, | |
49 const ReceiveCompletionCallback& success_callback, | |
50 const ReceiveErrorCompletionCallback& error_callback); | |
51 | |
52 // Sends |buffer| to the socket and calls |success_callback| when data has | |
53 // been successfully sent. If an error occurs, calls |error_callback| with a | |
54 // reason and a message. Calling |Send| multiple times without waiting for the | |
55 // callbacks to be called is a valid usage, as the |buffer| instances are | |
56 // buffered until the underlying communication channel is available for | |
57 // sending data. | |
keybuk
2014/03/20 01:21:56
buffer_size isn't documented
rpaquay
2014/03/20 18:21:11
Done.
| |
58 virtual void Send(scoped_refptr<net::IOBuffer> buffer, | |
59 int buffer_size, | |
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_; } | |
keybuk
2014/03/20 01:21:56
rename to uuid()
rpaquay
2014/03/20 18:21:11
Done.
| |
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_; | |
keybuk
2014/03/20 01:21:56
Rename to uuid_, replace mention of profile in the
rpaquay
2014/03/20 18:21:11
Done.
| |
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_; | |
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
| |
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 |