Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / api / bluetooth / bluetooth_event_router.h
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 EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
6 #define EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_
7
8 #include <map>
9
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 "base/scoped_observer.h"
15 #include "content/public/browser/notification_observer.h"
16 #include "content/public/browser/notification_registrar.h"
17 #include "device/bluetooth/bluetooth_adapter.h"
18 #include "device/bluetooth/bluetooth_adapter_factory.h"
19 #include "extensions/browser/extension_registry_observer.h"
20 #include "extensions/common/api/bluetooth.h"
21 #include "extensions/common/api/bluetooth_private.h"
22
23 namespace content {
24 class BrowserContext;
25 }
26
27 namespace device {
28
29 class BluetoothDevice;
30 class BluetoothDiscoverySession;
31
32 }  // namespace device
33
34 namespace extensions {
35 class BluetoothApiPairingDelegate;
36 class ExtensionRegistry;
37
38 class BluetoothEventRouter : public device::BluetoothAdapter::Observer,
39                              public content::NotificationObserver,
40                              public ExtensionRegistryObserver {
41  public:
42   explicit BluetoothEventRouter(content::BrowserContext* context);
43   ~BluetoothEventRouter() override;
44
45   // Returns true if adapter_ has been initialized for testing or bluetooth
46   // adapter is available for the current platform.
47   bool IsBluetoothSupported() const;
48
49   void GetAdapter(
50       const device::BluetoothAdapterFactory::AdapterCallback& callback);
51
52   // Requests that a new device discovery session be initiated for extension
53   // with id |extension_id|. |callback| is called, if a session has been
54   // initiated. |error_callback| is called, if the adapter failed to initiate
55   // the session or if an active session already exists for the extension.
56   void StartDiscoverySession(device::BluetoothAdapter* adapter,
57                              const std::string& extension_id,
58                              const base::Closure& callback,
59                              const base::Closure& error_callback);
60
61   // Requests that the active discovery session that belongs to the extension
62   // with id |extension_id| be terminated. |callback| is called, if the session
63   // successfully ended. |error_callback| is called, if the adapter failed to
64   // terminate the session or if no active discovery session exists for the
65   // extension.
66   void StopDiscoverySession(device::BluetoothAdapter* adapter,
67                             const std::string& extension_id,
68                             const base::Closure& callback,
69                             const base::Closure& error_callback);
70
71   // Called when a bluetooth event listener is added.
72   void OnListenerAdded();
73
74   // Called when a bluetooth event listener is removed.
75   void OnListenerRemoved();
76
77   // Adds a pairing delegate for an extension.
78   void AddPairingDelegate(const std::string& extension_id);
79
80   // Removes the pairing delegate for an extension.
81   void RemovePairingDelegate(const std::string& extension_id);
82
83   // Returns the pairing delegate for an extension or NULL if it doesn't have a
84   // pairing delegate.
85   BluetoothApiPairingDelegate* GetPairingDelegate(
86       const std::string& extension_id);
87
88   // Exposed for testing.
89   void SetAdapterForTest(device::BluetoothAdapter* adapter) {
90     adapter_ = adapter;
91   }
92
93   // Override from device::BluetoothAdapter::Observer.
94   void AdapterPresentChanged(device::BluetoothAdapter* adapter,
95                              bool present) override;
96   void AdapterPoweredChanged(device::BluetoothAdapter* adapter,
97                              bool has_power) override;
98   void AdapterDiscoveringChanged(device::BluetoothAdapter* adapter,
99                                  bool discovering) override;
100   void DeviceAdded(device::BluetoothAdapter* adapter,
101                    device::BluetoothDevice* device) override;
102   void DeviceChanged(device::BluetoothAdapter* adapter,
103                      device::BluetoothDevice* device) override;
104   void DeviceRemoved(device::BluetoothAdapter* adapter,
105                      device::BluetoothDevice* device) override;
106
107   // Overridden from content::NotificationObserver.
108   void Observe(int type,
109                const content::NotificationSource& source,
110                const content::NotificationDetails& details) override;
111
112   // Overridden from ExtensionRegistryObserver.
113   void OnExtensionUnloaded(content::BrowserContext* browser_context,
114                            const Extension* extension,
115                            UnloadedExtensionInfo::Reason reason) override;
116
117   // BrowserContextKeyedAPI implementation.
118   static const char* service_name() { return "BluetoothEventRouter"; }
119   static const bool kServiceRedirectedInIncognito = true;
120   static const bool kServiceIsNULLWhileTesting = true;
121
122  private:
123   void OnAdapterInitialized(const base::Closure& callback,
124                             scoped_refptr<device::BluetoothAdapter> adapter);
125   void MaybeReleaseAdapter();
126   void DispatchAdapterStateEvent();
127   void DispatchDeviceEvent(const std::string& event_name,
128                            device::BluetoothDevice* device);
129   void CleanUpForExtension(const std::string& extension_id);
130   void CleanUpAllExtensions();
131   void OnStartDiscoverySession(
132       const std::string& extension_id,
133       const base::Closure& callback,
134       scoped_ptr<device::BluetoothDiscoverySession> discovery_session);
135
136   content::BrowserContext* browser_context_;
137   scoped_refptr<device::BluetoothAdapter> adapter_;
138
139   int num_event_listeners_;
140
141   // A map that maps extension ids to BluetoothDiscoverySession pointers.
142   typedef std::map<std::string, device::BluetoothDiscoverySession*>
143       DiscoverySessionMap;
144   DiscoverySessionMap discovery_session_map_;
145
146   // Maps an extension id to its pairing delegate.
147   typedef std::map<std::string, BluetoothApiPairingDelegate*>
148       PairingDelegateMap;
149   PairingDelegateMap pairing_delegate_map_;
150
151   content::NotificationRegistrar registrar_;
152
153   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
154       extension_registry_observer_;
155
156   base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_;
157
158   DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter);
159 };
160
161 }  // namespace extensions
162
163 #endif  // EXTENSIONS_BROWSER_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_