#ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
#define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
#include <map>
#include "base/callback_forward.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_vector.h"
#include "base/memory/weak_ptr.h"
#include "chrome/common/extensions/api/bluetooth.h"
#include "chrome/common/extensions/api/bluetooth_private.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_socket.h"
#include "device/bluetooth/bluetooth_uuid.h"
namespace content {
class BrowserContext;
}
namespace device {
class BluetoothDevice;
class BluetoothDiscoverySession;
class BluetoothProfile;
}
namespace extensions {
class BluetoothApiPairingDelegate;
class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
public content::NotificationObserver {
public:
explicit BluetoothEventRouter(content::BrowserContext* context);
virtual ~BluetoothEventRouter();
bool IsBluetoothSupported() const;
void GetAdapter(
const device::BluetoothAdapterFactory::AdapterCallback& callback);
int RegisterSocket(const std::string& extension_id,
scoped_refptr<device::BluetoothSocket> socket);
bool ReleaseSocket(int id);
void AddProfile(const device::BluetoothUUID& uuid,
const std::string& extension_id,
device::BluetoothProfile* bluetooth_profile);
void RemoveProfile(const device::BluetoothUUID& uuid);
bool HasProfile(const device::BluetoothUUID& uuid) const;
void StartDiscoverySession(device::BluetoothAdapter* adapter,
const std::string& extension_id,
const base::Closure& callback,
const base::Closure& error_callback);
void StopDiscoverySession(device::BluetoothAdapter* adapter,
const std::string& extension_id,
const base::Closure& callback,
const base::Closure& error_callback);
device::BluetoothProfile* GetProfile(const device::BluetoothUUID& uuid) const;
scoped_refptr<device::BluetoothSocket> GetSocket(int id);
void DispatchConnectionEvent(const std::string& extension_id,
const device::BluetoothUUID& uuid,
const device::BluetoothDevice* device,
scoped_refptr<device::BluetoothSocket> socket);
void OnListenerAdded();
void OnListenerRemoved();
void AddPairingDelegate(const std::string& extension_id);
void RemovePairingDelegate(const std::string& extension_id);
BluetoothApiPairingDelegate* GetPairingDelegate(
const std::string& extension_id);
void SetAdapterForTest(device::BluetoothAdapter* adapter) {
adapter_ = adapter;
}
virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
bool present) OVERRIDE;
virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
bool has_power) OVERRIDE;
virtual void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
bool discovering) OVERRIDE;
virtual void DeviceAdded(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) OVERRIDE;
virtual void DeviceChanged(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) OVERRIDE;
virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
device::BluetoothDevice* device) OVERRIDE;
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
static const char* service_name() { return "BluetoothEventRouter"; }
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsNULLWhileTesting = true;
private:
struct ExtensionBluetoothSocketRecord;
struct ExtensionBluetoothProfileRecord;
void OnAdapterInitialized(const base::Closure& callback,
scoped_refptr<device::BluetoothAdapter> adapter);
void MaybeReleaseAdapter();
void DispatchAdapterStateEvent();
void DispatchDeviceEvent(const std::string& event_name,
device::BluetoothDevice* device);
void CleanUpForExtension(const std::string& extension_id);
void CleanUpAllExtensions();
void OnStartDiscoverySession(
const std::string& extension_id,
const base::Closure& callback,
scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
content::BrowserContext* browser_context_;
scoped_refptr<device::BluetoothAdapter> adapter_;
int num_event_listeners_;
int next_socket_id_;
typedef std::map<int, ExtensionBluetoothSocketRecord> SocketMap;
SocketMap socket_map_;
typedef std::map<device::BluetoothUUID, ExtensionBluetoothProfileRecord>
BluetoothProfileMap;
BluetoothProfileMap bluetooth_profile_map_;
typedef std::map<std::string, device::BluetoothDiscoverySession*>
DiscoverySessionMap;
DiscoverySessionMap discovery_session_map_;
typedef std::map<std::string, BluetoothApiPairingDelegate*>
PairingDelegateMap;
PairingDelegateMap pairing_delegate_map_;
content::NotificationRegistrar registrar_;
base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter);
};
}
#endif