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_SOCKET_EVENT_DISPATCHE
R_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_SOCKET_EVENT_DISPATCHE
R_H_ |
| 7 |
| 8 #include "chrome/browser/extensions/api/bluetooth/bluetooth_api_socket.h" |
| 9 #include "extensions/browser/api/api_resource_manager.h" |
| 10 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 11 |
| 12 namespace content { |
| 13 class BrowserContext; |
| 14 } |
| 15 |
| 16 namespace extensions { |
| 17 struct Event; |
| 18 class BluetoothApiSocket; |
| 19 } |
| 20 |
| 21 namespace extensions { |
| 22 namespace api { |
| 23 |
| 24 // Dispatch events related to "bluetooth" sockets from callback on native socket |
| 25 // instances. There is one instance per browser context. |
| 26 class BluetoothSocketEventDispatcher |
| 27 : public base::RefCountedThreadSafe<BluetoothSocketEventDispatcher> { |
| 28 public: |
| 29 typedef ApiResourceManager<BluetoothApiSocket>::ApiResourceData SocketData; |
| 30 explicit BluetoothSocketEventDispatcher( |
| 31 content::BrowserContext* context, |
| 32 scoped_refptr<SocketData> socket_data); |
| 33 |
| 34 // Socket is active again, start receiving data from it. |
| 35 void OnSocketResume(const std::string& extension_id, int socket_id); |
| 36 |
| 37 private: |
| 38 friend class base::RefCountedThreadSafe<BluetoothSocketEventDispatcher>; |
| 39 virtual ~BluetoothSocketEventDispatcher(); |
| 40 |
| 41 // base::Bind supports methods with up to 6 parameters. ReceiveParams is used |
| 42 // as a workaround that limitation for invoking StartReceive. |
| 43 struct ReceiveParams { |
| 44 ReceiveParams(); |
| 45 ~ReceiveParams(); |
| 46 |
| 47 std::string extension_id; |
| 48 int socket_id; |
| 49 }; |
| 50 |
| 51 // Start a receive and register a callback. |
| 52 void StartReceive(const ReceiveParams& params); |
| 53 |
| 54 // Called when socket receive data. |
| 55 void ReceiveCallback(const ReceiveParams& params, |
| 56 int bytes_read, |
| 57 scoped_refptr<net::IOBuffer> io_buffer); |
| 58 |
| 59 // Called when socket receive data. |
| 60 void ReceiveErrorCallback(const ReceiveParams& params, |
| 61 BluetoothApiSocket::ErrorReason error_reason, |
| 62 const std::string& error); |
| 63 |
| 64 // Post an extension event from IO to UI thread |
| 65 void PostEvent(const ReceiveParams& params, scoped_ptr<Event> event); |
| 66 |
| 67 // Dispatch an extension event on to EventRouter instance on UI thread. |
| 68 void DispatchEvent(const std::string& extension_id, scoped_ptr<Event> event); |
| 69 |
| 70 // Usually FILE thread (except for unit testing). |
| 71 content::BrowserThread::ID thread_id_; |
| 72 void* browser_context_id_; |
| 73 scoped_refptr<SocketData> socket_data_; |
| 74 |
| 75 DISALLOW_COPY_AND_ASSIGN(BluetoothSocketEventDispatcher); |
| 76 }; |
| 77 |
| 78 } // namespace api |
| 79 } // namespace extensions |
| 80 |
| 81 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_SOCKET_EVENT_DISPAT
CHER_H_ |
OLD | NEW |