Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / extensions / api / bluetooth / bluetooth_event_router.h
blobd3c3a9ad7e919b3b0ac2284e099693f6f4346f9c
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_
8 #include <map>
10 #include "base/callback_forward.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/scoped_vector.h"
13 #include "base/memory/weak_ptr.h"
14 #include "chrome/common/extensions/api/bluetooth.h"
15 #include "chrome/common/extensions/api/bluetooth_private.h"
16 #include "content/public/browser/notification_observer.h"
17 #include "content/public/browser/notification_registrar.h"
18 #include "device/bluetooth/bluetooth_adapter.h"
19 #include "device/bluetooth/bluetooth_adapter_factory.h"
20 #include "device/bluetooth/bluetooth_socket.h"
21 #include "device/bluetooth/bluetooth_uuid.h"
23 namespace content {
24 class BrowserContext;
27 namespace device {
29 class BluetoothDevice;
30 class BluetoothDiscoverySession;
31 class BluetoothProfile;
33 } // namespace device
35 namespace extensions {
37 class BluetoothApiPairingDelegate;
39 class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
40 public content::NotificationObserver {
41 public:
42 explicit BluetoothEventRouter(content::BrowserContext* context);
43 virtual ~BluetoothEventRouter();
45 // Returns true if adapter_ has been initialized for testing or bluetooth
46 // adapter is available for the current platform.
47 bool IsBluetoothSupported() const;
49 void GetAdapter(
50 const device::BluetoothAdapterFactory::AdapterCallback& callback);
52 // Add the BluetoothProfile |bluetooth_profile| for use by the extension
53 // system. This class will hold onto the profile until RemoveProfile is
54 // called for the profile, or until the extension that added the profile
55 // is disabled/reloaded.
56 void AddProfile(const device::BluetoothUUID& uuid,
57 const std::string& extension_id,
58 device::BluetoothProfile* bluetooth_profile);
60 // Unregister the BluetoothProfile corersponding to |uuid| and release the
61 // object from this class.
62 void RemoveProfile(const device::BluetoothUUID& uuid);
64 // Returns true if the BluetoothProfile corresponding to |uuid| is already
65 // registered.
66 bool HasProfile(const device::BluetoothUUID& uuid) const;
68 // Requests that a new device discovery session be initiated for extension
69 // with id |extension_id|. |callback| is called, if a session has been
70 // initiated. |error_callback| is called, if the adapter failed to initiate
71 // the session or if an active session already exists for the extension.
72 void StartDiscoverySession(device::BluetoothAdapter* adapter,
73 const std::string& extension_id,
74 const base::Closure& callback,
75 const base::Closure& error_callback);
77 // Requests that the active discovery session that belongs to the extension
78 // with id |extension_id| be terminated. |callback| is called, if the session
79 // successfully ended. |error_callback| is called, if the adapter failed to
80 // terminate the session or if no active discovery session exists for the
81 // extension.
82 void StopDiscoverySession(device::BluetoothAdapter* adapter,
83 const std::string& extension_id,
84 const base::Closure& callback,
85 const base::Closure& error_callback);
87 // Returns the BluetoothProfile that corresponds to |uuid|. It returns NULL
88 // if the BluetoothProfile with |uuid| does not exist.
89 device::BluetoothProfile* GetProfile(const device::BluetoothUUID& uuid) const;
91 // Called when a bluetooth event listener is added.
92 void OnListenerAdded();
94 // Called when a bluetooth event listener is removed.
95 void OnListenerRemoved();
97 // Adds a pairing delegate for an extension.
98 void AddPairingDelegate(const std::string& extension_id);
100 // Removes the pairing delegate for an extension.
101 void RemovePairingDelegate(const std::string& extension_id);
103 // Returns the pairing delegate for an extension or NULL if it doesn't have a
104 // pairing delegate.
105 BluetoothApiPairingDelegate* GetPairingDelegate(
106 const std::string& extension_id);
108 // Exposed for testing.
109 void SetAdapterForTest(device::BluetoothAdapter* adapter) {
110 adapter_ = adapter;
113 // Override from device::BluetoothAdapter::Observer.
114 virtual void AdapterPresentChanged(device::BluetoothAdapter* adapter,
115 bool present) OVERRIDE;
116 virtual void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
117 bool has_power) OVERRIDE;
118 virtual void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
119 bool discovering) OVERRIDE;
120 virtual void DeviceAdded(device::BluetoothAdapter* adapter,
121 device::BluetoothDevice* device) OVERRIDE;
122 virtual void DeviceChanged(device::BluetoothAdapter* adapter,
123 device::BluetoothDevice* device) OVERRIDE;
124 virtual void DeviceRemoved(device::BluetoothAdapter* adapter,
125 device::BluetoothDevice* device) OVERRIDE;
127 // Overridden from content::NotificationObserver.
128 virtual void Observe(int type,
129 const content::NotificationSource& source,
130 const content::NotificationDetails& details) OVERRIDE;
132 // BrowserContextKeyedAPI implementation.
133 static const char* service_name() { return "BluetoothEventRouter"; }
134 static const bool kServiceRedirectedInIncognito = true;
135 static const bool kServiceIsNULLWhileTesting = true;
137 private:
138 // Forward declarations of internal structs.
139 struct ExtensionBluetoothSocketRecord;
140 struct ExtensionBluetoothProfileRecord;
142 void OnAdapterInitialized(const base::Closure& callback,
143 scoped_refptr<device::BluetoothAdapter> adapter);
144 void MaybeReleaseAdapter();
145 void DispatchAdapterStateEvent();
146 void DispatchDeviceEvent(const std::string& event_name,
147 device::BluetoothDevice* device);
148 void CleanUpForExtension(const std::string& extension_id);
149 void CleanUpAllExtensions();
150 void OnStartDiscoverySession(
151 const std::string& extension_id,
152 const base::Closure& callback,
153 scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
155 content::BrowserContext* browser_context_;
156 scoped_refptr<device::BluetoothAdapter> adapter_;
158 int num_event_listeners_;
160 // Maps uuids to a struct containing a Bluetooth profile and its
161 // associated extension id.
162 typedef std::map<device::BluetoothUUID, ExtensionBluetoothProfileRecord>
163 BluetoothProfileMap;
164 BluetoothProfileMap bluetooth_profile_map_;
166 // A map that maps extension ids to BluetoothDiscoverySession pointers.
167 typedef std::map<std::string, device::BluetoothDiscoverySession*>
168 DiscoverySessionMap;
169 DiscoverySessionMap discovery_session_map_;
171 // Maps an extension id to its pairing delegate.
172 typedef std::map<std::string, BluetoothApiPairingDelegate*>
173 PairingDelegateMap;
174 PairingDelegateMap pairing_delegate_map_;
176 content::NotificationRegistrar registrar_;
178 base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_;
180 DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter);
183 } // namespace extensions
185 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_