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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/memory/weak_ptr.h"
13 #include "chrome/common/extensions/api/bluetooth.h"
14 #include "device/bluetooth/bluetooth_adapter.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluetooth_socket.h"
22 class BluetoothDevice;
23 class BluetoothProfile;
27 namespace extensions {
29 class ExtensionBluetoothEventRouter
30 : public device::BluetoothAdapter::Observer {
32 explicit ExtensionBluetoothEventRouter(Profile* profile);
33 virtual ~ExtensionBluetoothEventRouter();
35 // Returns true if adapter_ has been initialized for testing or bluetooth
36 // adapter is available for the current platform.
37 bool IsBluetoothSupported() const;
40 const device::BluetoothAdapterFactory::AdapterCallback& callback);
42 // Called when a bluetooth event listener is added.
43 void OnListenerAdded();
45 // Called when a bluetooth event listener is removed.
46 void OnListenerRemoved();
48 // Register the BluetoothSocket |socket| for use by the extensions system.
49 // This class will hold onto the socket for its lifetime, or until
50 // ReleaseSocket is called for the socket. Returns an id for the socket.
51 int RegisterSocket(scoped_refptr<device::BluetoothSocket> socket);
53 // Release the BluetoothSocket corresponding to |id|. Returns true if
54 // the socket was found and released, false otherwise.
55 bool ReleaseSocket(int id);
57 // Add the BluetoothProfile |bluetooth_profile| for use by the extension
58 // system. This class will hold onto the profile for its lifetime, or until
59 // RemoveProfile is called for the profile.
60 void AddProfile(const std::string& uuid,
61 device::BluetoothProfile* bluetooth_profile);
63 // Unregister the BluetoothProfile corersponding to |uuid| and release the
64 // object from this class.
65 void RemoveProfile(const std::string& uuid);
67 // Returns true if the BluetoothProfile corresponding to |uuid| is already
69 bool HasProfile(const std::string& uuid) const;
71 // Returns the BluetoothProfile that corresponds to |uuid|. It returns NULL
72 // if the BluetoothProfile with |uuid| does not exist.
73 device::BluetoothProfile* GetProfile(const std::string& uuid) const;
75 // Get the BluetoothSocket corresponding to |id|.
76 scoped_refptr<device::BluetoothSocket> GetSocket(int id);
78 // Sets whether this Profile is responsible for the discovering state of the
80 void SetResponsibleForDiscovery(bool responsible);
81 bool IsResponsibleForDiscovery() const;
83 // Sets whether or not DeviceAdded events will be dispatched to extensions.
84 void SetSendDiscoveryEvents(bool should_send);
86 // Dispatch an event that takes a device as a parameter to all renderers.
87 void DispatchDeviceEvent(
88 const std::string& event_name,
89 const extensions::api::bluetooth::Device& device);
91 // Dispatch an event that takes a connection socket as a parameter to the
92 // extension that registered the profile that the socket has connected to.
93 void DispatchConnectionEvent(const std::string& extension_id,
94 const std::string& uuid,
95 const device::BluetoothDevice* device,
96 scoped_refptr<device::BluetoothSocket> socket);
98 // Override from device::BluetoothAdapter::Observer
99 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
100 bool present) OVERRIDE;
101 virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
102 bool has_power) OVERRIDE;
103 virtual void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
104 bool discovering) OVERRIDE;
105 virtual void DeviceAdded(device::BluetoothAdapter* adapter,
106 device::BluetoothDevice* device) OVERRIDE;
108 // Exposed for testing.
109 void SetAdapterForTest(device::BluetoothAdapter* adapter) {
113 void InitializeAdapterIfNeeded();
114 void InitializeAdapter(scoped_refptr<device::BluetoothAdapter> adapter);
115 void MaybeReleaseAdapter();
116 void DispatchAdapterStateEvent();
118 bool send_discovery_events_;
119 bool responsible_for_discovery_;
122 scoped_refptr<device::BluetoothAdapter> adapter_;
124 int num_event_listeners_;
126 // The next id to use for referring to a BluetoothSocket. We avoid using
127 // the fd of the socket because we don't want to leak that information to
128 // the extension javascript.
131 typedef std::map<int, scoped_refptr<device::BluetoothSocket> > SocketMap;
132 SocketMap socket_map_;
134 typedef ScopedVector<extensions::api::bluetooth::Device>
136 DeviceList discovered_devices_;
138 // A map that maps uuids to the BluetoothProfile objects.
139 typedef std::map<std::string, device::BluetoothProfile*> BluetoothProfileMap;
140 BluetoothProfileMap bluetooth_profile_map_;
142 base::WeakPtrFactory<ExtensionBluetoothEventRouter> weak_ptr_factory_;
144 DISALLOW_COPY_AND_ASSIGN(ExtensionBluetoothEventRouter);
147 } // namespace extensions
149 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_