OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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_BLUETOOTH_EVENT_ROUTER_H_ | |
6 #define CHROME_BROWSER_EXTENSIONS_BLUETOOTH_EVENT_ROUTER_H_ | |
7 | |
8 #include <map> | |
9 | |
10 #include "base/memory/ref_counted.h" | |
11 #include "base/memory/scoped_vector.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/common/extensions/api/bluetooth.h" | |
14 #include "device/bluetooth/bluetooth_adapter.h" | |
15 #include "device/bluetooth/bluetooth_socket.h" | |
16 | |
17 namespace extensions { | |
18 | |
19 class ExtensionBluetoothEventRouter | |
20 : public device::BluetoothAdapter::Observer { | |
21 public: | |
22 explicit ExtensionBluetoothEventRouter(Profile* profile); | |
23 virtual ~ExtensionBluetoothEventRouter(); | |
24 | |
25 // adapter() will return NULL if the bluetooth adapter is not supported in the | |
26 // current platform. | |
27 const device::BluetoothAdapter* adapter() const { | |
28 return adapter_.get(); | |
29 } | |
30 | |
31 // GetMutableAdapter will return NULL if the bluetooth adapter is not | |
32 // supported in the current platform. | |
33 device::BluetoothAdapter* GetMutableAdapter() { | |
34 return adapter_.get(); | |
35 } | |
36 | |
37 // Register the BluetoothSocket |socket| for use by the extensions system. | |
38 // This class will hold onto the socket for its lifetime, or until | |
39 // ReleaseSocket is called for the socket. Returns an id for the socket. | |
40 int RegisterSocket(scoped_refptr<device::BluetoothSocket> socket); | |
41 | |
42 // Release the BluetoothSocket corresponding to |id|. Returns true if | |
43 // the socket was found and released, false otherwise. | |
44 bool ReleaseSocket(int id); | |
45 | |
46 // Get the BluetoothSocket corresponding to |id|. | |
47 scoped_refptr<device::BluetoothSocket> GetSocket(int id); | |
48 | |
49 // Sets whether this Profile is responsible for the discovering state of the | |
50 // adapter. | |
51 void SetResponsibleForDiscovery(bool responsible); | |
52 bool IsResponsibleForDiscovery() const; | |
53 | |
54 // Sets whether or not DeviceAdded events will be dispatched to extensions. | |
55 void SetSendDiscoveryEvents(bool should_send); | |
56 | |
57 // Dispatch an event that takes a device as a parameter to all renderers. | |
58 void DispatchDeviceEvent( | |
59 const char* event_name, | |
60 const extensions::api::bluetooth::Device& device); | |
61 | |
62 // Override from device::BluetoothAdapter::Observer | |
63 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter, | |
64 bool present) OVERRIDE; | |
65 virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter, | |
66 bool has_power) OVERRIDE; | |
67 virtual void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter, | |
68 bool discovering) OVERRIDE; | |
69 virtual void DeviceAdded(device::BluetoothAdapter* adapter, | |
70 device::BluetoothDevice* device) OVERRIDE; | |
71 | |
72 // Exposed for testing. | |
73 void SetAdapterForTest(device::BluetoothAdapter* adapter) { | |
74 adapter_ = adapter; | |
75 } | |
76 private: | |
77 void DispatchBooleanValueEvent(const char* event_name, bool value); | |
78 | |
79 bool send_discovery_events_; | |
80 bool responsible_for_discovery_; | |
81 | |
82 Profile* profile_; | |
83 scoped_refptr<device::BluetoothAdapter> adapter_; | |
84 | |
85 // The next id to use for referring to a BluetoothSocket. We avoid using | |
86 // the fd of the socket because we don't want to leak that information to | |
87 // the extension javascript. | |
88 int next_socket_id_; | |
89 | |
90 typedef std::map<int, scoped_refptr<device::BluetoothSocket> > SocketMap; | |
91 SocketMap socket_map_; | |
92 | |
93 typedef ScopedVector<extensions::api::bluetooth::Device> | |
94 DeviceList; | |
95 DeviceList discovered_devices_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(ExtensionBluetoothEventRouter); | |
98 }; | |
99 | |
100 } // namespace extensions | |
101 | |
102 #endif // CHROME_BROWSER_EXTENSIONS_BLUETOOTH_EVENT_ROUTER_H_ | |
OLD | NEW |