| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/callback_forward.h" | 10 #include "base/callback_forward.h" |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_vector.h" | 12 #include "base/memory/scoped_vector.h" |
| 13 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/common/extensions/api/bluetooth.h" | 14 #include "chrome/common/extensions/api/bluetooth.h" |
| 15 #include "chrome/common/extensions/api/bluetooth_private.h" | 15 #include "chrome/common/extensions/api/bluetooth_private.h" |
| 16 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
| 17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
| 18 #include "device/bluetooth/bluetooth_adapter.h" | 18 #include "device/bluetooth/bluetooth_adapter.h" |
| 19 #include "device/bluetooth/bluetooth_adapter_factory.h" | 19 #include "device/bluetooth/bluetooth_adapter_factory.h" |
| 20 #include "device/bluetooth/bluetooth_socket.h" | |
| 21 #include "device/bluetooth/bluetooth_uuid.h" | |
| 22 | 20 |
| 23 namespace content { | 21 namespace content { |
| 24 class BrowserContext; | 22 class BrowserContext; |
| 25 } | 23 } |
| 26 | 24 |
| 27 namespace device { | 25 namespace device { |
| 28 | 26 |
| 29 class BluetoothDevice; | 27 class BluetoothDevice; |
| 30 class BluetoothDiscoverySession; | 28 class BluetoothDiscoverySession; |
| 31 class BluetoothProfile; | |
| 32 | 29 |
| 33 } // namespace device | 30 } // namespace device |
| 34 | 31 |
| 35 namespace extensions { | 32 namespace extensions { |
| 36 | 33 |
| 37 class BluetoothApiPairingDelegate; | 34 class BluetoothApiPairingDelegate; |
| 38 | 35 |
| 39 class BluetoothEventRouter : public device::BluetoothAdapter::Observer, | 36 class BluetoothEventRouter : public device::BluetoothAdapter::Observer, |
| 40 public content::NotificationObserver { | 37 public content::NotificationObserver { |
| 41 public: | 38 public: |
| 42 explicit BluetoothEventRouter(content::BrowserContext* context); | 39 explicit BluetoothEventRouter(content::BrowserContext* context); |
| 43 virtual ~BluetoothEventRouter(); | 40 virtual ~BluetoothEventRouter(); |
| 44 | 41 |
| 45 // Returns true if adapter_ has been initialized for testing or bluetooth | 42 // Returns true if adapter_ has been initialized for testing or bluetooth |
| 46 // adapter is available for the current platform. | 43 // adapter is available for the current platform. |
| 47 bool IsBluetoothSupported() const; | 44 bool IsBluetoothSupported() const; |
| 48 | 45 |
| 49 void GetAdapter( | 46 void GetAdapter( |
| 50 const device::BluetoothAdapterFactory::AdapterCallback& callback); | 47 const device::BluetoothAdapterFactory::AdapterCallback& callback); |
| 51 | 48 |
| 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); | |
| 59 | |
| 60 // Unregister the BluetoothProfile corersponding to |uuid| and release the | |
| 61 // object from this class. | |
| 62 void RemoveProfile(const device::BluetoothUUID& uuid); | |
| 63 | |
| 64 // Returns true if the BluetoothProfile corresponding to |uuid| is already | |
| 65 // registered. | |
| 66 bool HasProfile(const device::BluetoothUUID& uuid) const; | |
| 67 | |
| 68 // Requests that a new device discovery session be initiated for extension | 49 // Requests that a new device discovery session be initiated for extension |
| 69 // with id |extension_id|. |callback| is called, if a session has been | 50 // with id |extension_id|. |callback| is called, if a session has been |
| 70 // initiated. |error_callback| is called, if the adapter failed to initiate | 51 // initiated. |error_callback| is called, if the adapter failed to initiate |
| 71 // the session or if an active session already exists for the extension. | 52 // the session or if an active session already exists for the extension. |
| 72 void StartDiscoverySession(device::BluetoothAdapter* adapter, | 53 void StartDiscoverySession(device::BluetoothAdapter* adapter, |
| 73 const std::string& extension_id, | 54 const std::string& extension_id, |
| 74 const base::Closure& callback, | 55 const base::Closure& callback, |
| 75 const base::Closure& error_callback); | 56 const base::Closure& error_callback); |
| 76 | 57 |
| 77 // Requests that the active discovery session that belongs to the extension | 58 // Requests that the active discovery session that belongs to the extension |
| 78 // with id |extension_id| be terminated. |callback| is called, if the session | 59 // with id |extension_id| be terminated. |callback| is called, if the session |
| 79 // successfully ended. |error_callback| is called, if the adapter failed to | 60 // successfully ended. |error_callback| is called, if the adapter failed to |
| 80 // terminate the session or if no active discovery session exists for the | 61 // terminate the session or if no active discovery session exists for the |
| 81 // extension. | 62 // extension. |
| 82 void StopDiscoverySession(device::BluetoothAdapter* adapter, | 63 void StopDiscoverySession(device::BluetoothAdapter* adapter, |
| 83 const std::string& extension_id, | 64 const std::string& extension_id, |
| 84 const base::Closure& callback, | 65 const base::Closure& callback, |
| 85 const base::Closure& error_callback); | 66 const base::Closure& error_callback); |
| 86 | 67 |
| 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; | |
| 90 | |
| 91 // Called when a bluetooth event listener is added. | 68 // Called when a bluetooth event listener is added. |
| 92 void OnListenerAdded(); | 69 void OnListenerAdded(); |
| 93 | 70 |
| 94 // Called when a bluetooth event listener is removed. | 71 // Called when a bluetooth event listener is removed. |
| 95 void OnListenerRemoved(); | 72 void OnListenerRemoved(); |
| 96 | 73 |
| 97 // Adds a pairing delegate for an extension. | 74 // Adds a pairing delegate for an extension. |
| 98 void AddPairingDelegate(const std::string& extension_id); | 75 void AddPairingDelegate(const std::string& extension_id); |
| 99 | 76 |
| 100 // Removes the pairing delegate for an extension. | 77 // Removes the pairing delegate for an extension. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 128 virtual void Observe(int type, | 105 virtual void Observe(int type, |
| 129 const content::NotificationSource& source, | 106 const content::NotificationSource& source, |
| 130 const content::NotificationDetails& details) OVERRIDE; | 107 const content::NotificationDetails& details) OVERRIDE; |
| 131 | 108 |
| 132 // BrowserContextKeyedAPI implementation. | 109 // BrowserContextKeyedAPI implementation. |
| 133 static const char* service_name() { return "BluetoothEventRouter"; } | 110 static const char* service_name() { return "BluetoothEventRouter"; } |
| 134 static const bool kServiceRedirectedInIncognito = true; | 111 static const bool kServiceRedirectedInIncognito = true; |
| 135 static const bool kServiceIsNULLWhileTesting = true; | 112 static const bool kServiceIsNULLWhileTesting = true; |
| 136 | 113 |
| 137 private: | 114 private: |
| 138 // Forward declarations of internal structs. | |
| 139 struct ExtensionBluetoothSocketRecord; | |
| 140 struct ExtensionBluetoothProfileRecord; | |
| 141 | |
| 142 void OnAdapterInitialized(const base::Closure& callback, | 115 void OnAdapterInitialized(const base::Closure& callback, |
| 143 scoped_refptr<device::BluetoothAdapter> adapter); | 116 scoped_refptr<device::BluetoothAdapter> adapter); |
| 144 void MaybeReleaseAdapter(); | 117 void MaybeReleaseAdapter(); |
| 145 void DispatchAdapterStateEvent(); | 118 void DispatchAdapterStateEvent(); |
| 146 void DispatchDeviceEvent(const std::string& event_name, | 119 void DispatchDeviceEvent(const std::string& event_name, |
| 147 device::BluetoothDevice* device); | 120 device::BluetoothDevice* device); |
| 148 void CleanUpForExtension(const std::string& extension_id); | 121 void CleanUpForExtension(const std::string& extension_id); |
| 149 void CleanUpAllExtensions(); | 122 void CleanUpAllExtensions(); |
| 150 void OnStartDiscoverySession( | 123 void OnStartDiscoverySession( |
| 151 const std::string& extension_id, | 124 const std::string& extension_id, |
| 152 const base::Closure& callback, | 125 const base::Closure& callback, |
| 153 scoped_ptr<device::BluetoothDiscoverySession> discovery_session); | 126 scoped_ptr<device::BluetoothDiscoverySession> discovery_session); |
| 154 | 127 |
| 155 content::BrowserContext* browser_context_; | 128 content::BrowserContext* browser_context_; |
| 156 scoped_refptr<device::BluetoothAdapter> adapter_; | 129 scoped_refptr<device::BluetoothAdapter> adapter_; |
| 157 | 130 |
| 158 int num_event_listeners_; | 131 int num_event_listeners_; |
| 159 | 132 |
| 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_; | |
| 165 | |
| 166 // A map that maps extension ids to BluetoothDiscoverySession pointers. | 133 // A map that maps extension ids to BluetoothDiscoverySession pointers. |
| 167 typedef std::map<std::string, device::BluetoothDiscoverySession*> | 134 typedef std::map<std::string, device::BluetoothDiscoverySession*> |
| 168 DiscoverySessionMap; | 135 DiscoverySessionMap; |
| 169 DiscoverySessionMap discovery_session_map_; | 136 DiscoverySessionMap discovery_session_map_; |
| 170 | 137 |
| 171 // Maps an extension id to its pairing delegate. | 138 // Maps an extension id to its pairing delegate. |
| 172 typedef std::map<std::string, BluetoothApiPairingDelegate*> | 139 typedef std::map<std::string, BluetoothApiPairingDelegate*> |
| 173 PairingDelegateMap; | 140 PairingDelegateMap; |
| 174 PairingDelegateMap pairing_delegate_map_; | 141 PairingDelegateMap pairing_delegate_map_; |
| 175 | 142 |
| 176 content::NotificationRegistrar registrar_; | 143 content::NotificationRegistrar registrar_; |
| 177 | 144 |
| 178 base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_; | 145 base::WeakPtrFactory<BluetoothEventRouter> weak_ptr_factory_; |
| 179 | 146 |
| 180 DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter); | 147 DISALLOW_COPY_AND_ASSIGN(BluetoothEventRouter); |
| 181 }; | 148 }; |
| 182 | 149 |
| 183 } // namespace extensions | 150 } // namespace extensions |
| 184 | 151 |
| 185 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ | 152 #endif // CHROME_BROWSER_EXTENSIONS_API_BLUETOOTH_BLUETOOTH_EVENT_ROUTER_H_ |
| OLD | NEW |