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_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_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 "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" 17 18 class Profile; 19 20 namespace device { 21 22 class BluetoothDevice; 23 class BluetoothProfile; 24 25 } // namespace device 26 27 namespace extensions { 28 29 class ExtensionBluetoothEventRouter 30 : public device::BluetoothAdapter::Observer { 31 public: 32 explicit ExtensionBluetoothEventRouter(Profile* profile); 33 virtual ~ExtensionBluetoothEventRouter(); 34 35 // Returns true if adapter_ has been initialized for testing or bluetooth 36 // adapter is available for the current platform. 37 bool IsBluetoothSupported() const; 38 39 void GetAdapter( 40 const device::BluetoothAdapterFactory::AdapterCallback& callback); 41 42 // Called when a bluetooth event listener is added. 43 void OnListenerAdded(); 44 45 // Called when a bluetooth event listener is removed. 46 void OnListenerRemoved(); 47 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); 52 53 // Release the BluetoothSocket corresponding to |id|. Returns true if 54 // the socket was found and released, false otherwise. 55 bool ReleaseSocket(int id); 56 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); 62 63 // Unregister the BluetoothProfile corersponding to |uuid| and release the 64 // object from this class. 65 void RemoveProfile(const std::string& uuid); 66 67 // Returns true if the BluetoothProfile corresponding to |uuid| is already 68 // registered. 69 bool HasProfile(const std::string& uuid) const; 70 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; 74 75 // Get the BluetoothSocket corresponding to |id|. 76 scoped_refptr<device::BluetoothSocket> GetSocket(int id); 77 78 // Sets whether this Profile is responsible for the discovering state of the 79 // adapter. 80 void SetResponsibleForDiscovery(bool responsible); 81 bool IsResponsibleForDiscovery() const; 82 83 // Sets whether or not DeviceAdded events will be dispatched to extensions. 84 void SetSendDiscoveryEvents(bool should_send); 85 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); 90 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); 97 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; 107 108 // Exposed for testing. 109 void SetAdapterForTest(device::BluetoothAdapter* adapter) { 110 adapter_ = adapter; 111 } 112 private: 113 void InitializeAdapterIfNeeded(); 114 void InitializeAdapter(scoped_refptr<device::BluetoothAdapter> adapter); 115 void MaybeReleaseAdapter(); 116 void DispatchAdapterStateEvent(); 117 118 bool send_discovery_events_; 119 bool responsible_for_discovery_; 120 121 Profile* profile_; 122 scoped_refptr<device::BluetoothAdapter> adapter_; 123 124 int num_event_listeners_; 125 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. 129 int next_socket_id_; 130 131 typedef std::map<int, scoped_refptr<device::BluetoothSocket> > SocketMap; 132 SocketMap socket_map_; 133 134 typedef ScopedVector<extensions::api::bluetooth::Device> 135 DeviceList; 136 DeviceList discovered_devices_; 137 138 // A map that maps uuids to the BluetoothProfile objects. 139 typedef std::map<std::string, device::BluetoothProfile*> BluetoothProfileMap; 140 BluetoothProfileMap bluetooth_profile_map_; 141 142 base::WeakPtrFactory<ExtensionBluetoothEventRouter> weak_ptr_factory_; 143 144 DISALLOW_COPY_AND_ASSIGN(ExtensionBluetoothEventRouter); 145 }; 146 147 } // namespace extensions 148 149 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ 150