1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#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 "base/scoped_observer.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 "extensions/browser/extension_registry_observer.h"
namespace content {
class BrowserContext;
}
namespace device {
class BluetoothDevice;
class BluetoothDiscoverySession;
} // namespace device
namespace extensions {
class BluetoothApiPairingDelegate;
class ExtensionRegistry;
class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
public content::NotificationObserver,
public ExtensionRegistryObserver {
public:
explicit BluetoothEventRouter(content::BrowserContext* context);
virtual ~BluetoothEventRouter();
// Returns true if adapter_ has been initialized for testing or bluetooth
// adapter is available for the current platform.
bool IsBluetoothSupported() const;
void GetAdapter(
const device::BluetoothAdapterFactory::AdapterCallback& callback);
// Requests that a new device discovery session be initiated for extension
// with id |extension_id|. |callback| is called, if a session has been
// initiated. |error_callback| is called, if the adapter failed to initiate
// the session or if an active session already exists for the extension.
void StartDiscoverySession(device::BluetoothAdapter* adapter,
const std::string& extension_id,
const base::Closure& callback,
const base::Closure& error_callback);
// Requests that the active discovery session that belongs to the extension
// with id |extension_id| be terminated. |callback| is called, if the session
// successfully ended. |error_callback| is called, if the adapter failed to
// terminate the session or if no active discovery session exists for the
// extension.
void StopDiscoverySession(device::BluetoothAdapter* adapter,
const std::string& extension_id,
const base::Closure& callback,
const base::Closure& error_callback);
// Called when a bluetooth event listener is added.
void OnListenerAdded();
// Called when a bluetooth event listener is removed.
void OnListenerRemoved();
// Adds a pairing delegate for an extension.
void AddPairingDelegate(const std::string& extension_id);
// Removes the pairing delegate for an extension.
void RemovePairingDelegate(const std::string& extension_id);
// Returns the pairing delegate for an extension or NULL if it doesn't have a
// pairing delegate.
BluetoothApiPairingDelegate* GetPairingDelegate(
const std::string& extension_id);
// Exposed for testing.
void SetAdapterForTest(device::BluetoothAdapter* adapter) {
adapter_ = adapter;
}
// Override from device::BluetoothAdapter::Observer.
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;
// Overridden from content::NotificationObserver.
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
// Overridden from ExtensionRegistryObserver.
virtual void OnExtensionUnloaded(
content::BrowserContext* browser_context,
const Extension* extension,
UnloadedExtensionInfo::Reason reason) OVERRIDE;
// BrowserContextKeyedAPI implementation.
static const char* service_name() { return "BluetoothEventRouter"; }
static const bool kServiceRedirectedInIncognito = true;
static const bool kServiceIsNULLWhileTesting = true;
private:
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_;
// A map that maps extension ids to BluetoothDiscoverySession pointers.
typedef std::map<std::string, device::BluetoothDiscoverySession*>
DiscoverySessionMap;
DiscoverySessionMap discovery_session_map_;
// Maps an extension id to its pairing delegate.
typedef std::map<std::string, BluetoothApiPairingDelegate*>
PairingDelegateMap;
PairingDelegateMap pairing_delegate_map_;
content::NotificationRegistrar registrar_;
ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
extension_registry_observer_;
base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
|