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