1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/bluetooth/BluetoothService.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,407 @@ 1.4 +/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */ 1.5 +/* vim: set ts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.8 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#ifndef mozilla_dom_bluetooth_bluetootheventservice_h__ 1.11 +#define mozilla_dom_bluetooth_bluetootheventservice_h__ 1.12 + 1.13 +#include "BluetoothCommon.h" 1.14 +#include "BluetoothProfileManagerBase.h" 1.15 +#include "mozilla/dom/ipc/Blob.h" 1.16 +#include "nsAutoPtr.h" 1.17 +#include "nsClassHashtable.h" 1.18 +#include "nsIDOMFile.h" 1.19 +#include "nsIObserver.h" 1.20 +#include "nsTObserverArray.h" 1.21 +#include "nsThreadUtils.h" 1.22 + 1.23 +namespace mozilla { 1.24 +namespace ipc { 1.25 +class UnixSocketConsumer; 1.26 +} 1.27 +} 1.28 + 1.29 +BEGIN_BLUETOOTH_NAMESPACE 1.30 + 1.31 +class BluetoothManager; 1.32 +class BluetoothNamedValue; 1.33 +class BluetoothReplyRunnable; 1.34 +class BluetoothSignal; 1.35 + 1.36 +typedef mozilla::ObserverList<BluetoothSignal> BluetoothSignalObserverList; 1.37 + 1.38 +class BluetoothService : public nsIObserver 1.39 + , public BluetoothSignalObserver 1.40 +{ 1.41 + class ToggleBtTask; 1.42 + friend class ToggleBtTask; 1.43 + 1.44 + class StartupTask; 1.45 + friend class StartupTask; 1.46 + 1.47 +public: 1.48 + class ToggleBtAck : public nsRunnable 1.49 + { 1.50 + public: 1.51 + ToggleBtAck(bool aEnabled); 1.52 + NS_IMETHOD Run(); 1.53 + 1.54 + private: 1.55 + bool mEnabled; 1.56 + }; 1.57 + friend class ToggleBtAck; 1.58 + 1.59 + NS_DECL_ISUPPORTS 1.60 + NS_DECL_NSIOBSERVER 1.61 + 1.62 + /** 1.63 + * Add a message handler object from message distribution observer. 1.64 + * Must be called from the main thread. 1.65 + * 1.66 + * @param aNodeName Node name of the object 1.67 + * @param aMsgHandler Weak pointer to the object 1.68 + */ 1.69 + virtual void 1.70 + RegisterBluetoothSignalHandler(const nsAString& aNodeName, 1.71 + BluetoothSignalObserver* aMsgHandler); 1.72 + 1.73 + /** 1.74 + * Remove a message handler object from message distribution observer. 1.75 + * Must be called from the main thread. 1.76 + * 1.77 + * @param aNodeName Node name of the object 1.78 + * @param aMsgHandler Weak pointer to the object 1.79 + */ 1.80 + virtual void 1.81 + UnregisterBluetoothSignalHandler(const nsAString& aNodeName, 1.82 + BluetoothSignalObserver* aMsgHandler); 1.83 + 1.84 + /** 1.85 + * Remove a message handlers for the given observer. 1.86 + * Must be called from the main thread. 1.87 + * 1.88 + * @param aMsgHandler Weak pointer to the object 1.89 + */ 1.90 + void 1.91 + UnregisterAllSignalHandlers(BluetoothSignalObserver* aMsgHandler); 1.92 + 1.93 + /** 1.94 + * Distribute a signal to the observer list 1.95 + * 1.96 + * @param aSignal Signal object to distribute 1.97 + * 1.98 + * @return NS_OK if signal distributed, NS_ERROR_FAILURE on error 1.99 + */ 1.100 + void 1.101 + DistributeSignal(const BluetoothSignal& aEvent); 1.102 + 1.103 + /** 1.104 + * Called when get a Bluetooth Signal from BluetoothDBusService 1.105 + * 1.106 + */ 1.107 + void 1.108 + Notify(const BluetoothSignal& aParam); 1.109 + 1.110 + /** 1.111 + * Returns the BluetoothService singleton. Only to be called from main thread. 1.112 + * 1.113 + * @param aService Pointer to return singleton into. 1.114 + * 1.115 + * @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise (if service 1.116 + * has not yet been started, for instance) 1.117 + */ 1.118 + static BluetoothService* 1.119 + Get(); 1.120 + 1.121 + static already_AddRefed<BluetoothService> 1.122 + FactoryCreate() 1.123 + { 1.124 + nsRefPtr<BluetoothService> service = Get(); 1.125 + return service.forget(); 1.126 + } 1.127 + 1.128 + /** 1.129 + * Returns the path of the default adapter, implemented via a platform 1.130 + * specific method. 1.131 + * 1.132 + * @return NS_OK on success, NS_ERROR_FAILURE otherwise 1.133 + */ 1.134 + virtual nsresult 1.135 + GetDefaultAdapterPathInternal(BluetoothReplyRunnable* aRunnable) = 0; 1.136 + 1.137 + /** 1.138 + * Returns the properties of paired devices, implemented via a platform 1.139 + * specific method. 1.140 + * 1.141 + * @return NS_OK on success, NS_ERROR_FAILURE otherwise 1.142 + */ 1.143 + virtual nsresult 1.144 + GetPairedDevicePropertiesInternal(const nsTArray<nsString>& aDeviceAddresses, 1.145 + BluetoothReplyRunnable* aRunnable) = 0; 1.146 + 1.147 + /** 1.148 + * Returns the properties of connected devices regarding to specific profile, 1.149 + * implemented via a platform specific methood. 1.150 + * 1.151 + * @return NS_OK on success, NS_ERROR_FAILURE otherwise 1.152 + */ 1.153 + virtual nsresult 1.154 + GetConnectedDevicePropertiesInternal(uint16_t aServiceUuid, 1.155 + BluetoothReplyRunnable* aRunnable) = 0; 1.156 + 1.157 + /** 1.158 + * Stop device discovery (platform specific implementation) 1.159 + * 1.160 + * @return NS_OK if discovery stopped correctly, false otherwise 1.161 + */ 1.162 + virtual nsresult 1.163 + StopDiscoveryInternal(BluetoothReplyRunnable* aRunnable) = 0; 1.164 + 1.165 + /** 1.166 + * Start device discovery (platform specific implementation) 1.167 + * 1.168 + * @return NS_OK if discovery stopped correctly, false otherwise 1.169 + */ 1.170 + virtual nsresult 1.171 + StartDiscoveryInternal(BluetoothReplyRunnable* aRunnable) = 0; 1.172 + 1.173 + /** 1.174 + * Set a property for the specified object 1.175 + * 1.176 + * @param aPropName Name of the property 1.177 + * @param aValue Boolean value 1.178 + * @param aRunnable Runnable to run on async reply 1.179 + * 1.180 + * @return NS_OK if property is set correctly, NS_ERROR_FAILURE otherwise 1.181 + */ 1.182 + virtual nsresult 1.183 + SetProperty(BluetoothObjectType aType, 1.184 + const BluetoothNamedValue& aValue, 1.185 + BluetoothReplyRunnable* aRunnable) = 0; 1.186 + 1.187 + virtual nsresult 1.188 + CreatePairedDeviceInternal(const nsAString& aAddress, 1.189 + int aTimeout, 1.190 + BluetoothReplyRunnable* aRunnable) = 0; 1.191 + 1.192 + virtual nsresult 1.193 + RemoveDeviceInternal(const nsAString& aObjectPath, 1.194 + BluetoothReplyRunnable* aRunnable) = 0; 1.195 + 1.196 + /** 1.197 + * Get corresponding service channel of specific service on remote device. 1.198 + * It's usually the very first step of establishing an outbound connection. 1.199 + * 1.200 + * @param aObjectPath Object path of remote device 1.201 + * @param aServiceUuid UUID of the target service 1.202 + * @param aManager Instance which has callback function OnGetServiceChannel() 1.203 + * 1.204 + * @return NS_OK if the task begins, NS_ERROR_FAILURE otherwise 1.205 + */ 1.206 + virtual nsresult 1.207 + GetServiceChannel(const nsAString& aDeviceAddress, 1.208 + const nsAString& aServiceUuid, 1.209 + BluetoothProfileManagerBase* aManager) = 0; 1.210 + 1.211 + virtual bool 1.212 + UpdateSdpRecords(const nsAString& aDeviceAddress, 1.213 + BluetoothProfileManagerBase* aManager) = 0; 1.214 + 1.215 + virtual bool 1.216 + SetPinCodeInternal(const nsAString& aDeviceAddress, const nsAString& aPinCode, 1.217 + BluetoothReplyRunnable* aRunnable) = 0; 1.218 + 1.219 + virtual bool 1.220 + SetPasskeyInternal(const nsAString& aDeviceAddress, uint32_t aPasskey, 1.221 + BluetoothReplyRunnable* aRunnable) = 0; 1.222 + 1.223 + virtual bool 1.224 + SetPairingConfirmationInternal(const nsAString& aDeviceAddress, bool aConfirm, 1.225 + BluetoothReplyRunnable* aRunnable) = 0; 1.226 + 1.227 + virtual void 1.228 + Connect(const nsAString& aDeviceAddress, uint32_t aCod, uint16_t aServiceUuid, 1.229 + BluetoothReplyRunnable* aRunnable) = 0; 1.230 + 1.231 + virtual void 1.232 + Disconnect(const nsAString& aDeviceAddress, uint16_t aServiceUuid, 1.233 + BluetoothReplyRunnable* aRunnable) = 0; 1.234 + 1.235 + virtual bool 1.236 + IsConnected(uint16_t aServiceUuid) = 0; 1.237 + 1.238 + virtual void 1.239 + SendFile(const nsAString& aDeviceAddress, 1.240 + BlobParent* aBlobParent, 1.241 + BlobChild* aBlobChild, 1.242 + BluetoothReplyRunnable* aRunnable) = 0; 1.243 + 1.244 + virtual void 1.245 + SendFile(const nsAString& aDeviceAddress, 1.246 + nsIDOMBlob* aBlob, 1.247 + BluetoothReplyRunnable* aRunnable) = 0; 1.248 + 1.249 + virtual void 1.250 + StopSendingFile(const nsAString& aDeviceAddress, 1.251 + BluetoothReplyRunnable* aRunnable) = 0; 1.252 + 1.253 + virtual void 1.254 + ConfirmReceivingFile(const nsAString& aDeviceAddress, bool aConfirm, 1.255 + BluetoothReplyRunnable* aRunnable) = 0; 1.256 + 1.257 + virtual void 1.258 + ConnectSco(BluetoothReplyRunnable* aRunnable) = 0; 1.259 + 1.260 + virtual void 1.261 + DisconnectSco(BluetoothReplyRunnable* aRunnable) = 0; 1.262 + 1.263 + virtual void 1.264 + IsScoConnected(BluetoothReplyRunnable* aRunnable) = 0; 1.265 + 1.266 +#ifdef MOZ_B2G_RIL 1.267 + virtual void 1.268 + AnswerWaitingCall(BluetoothReplyRunnable* aRunnable) = 0; 1.269 + 1.270 + virtual void 1.271 + IgnoreWaitingCall(BluetoothReplyRunnable* aRunnable) = 0; 1.272 + 1.273 + virtual void 1.274 + ToggleCalls(BluetoothReplyRunnable* aRunnable) = 0; 1.275 +#endif 1.276 + 1.277 + virtual void 1.278 + SendMetaData(const nsAString& aTitle, 1.279 + const nsAString& aArtist, 1.280 + const nsAString& aAlbum, 1.281 + int64_t aMediaNumber, 1.282 + int64_t aTotalMediaCount, 1.283 + int64_t aDuration, 1.284 + BluetoothReplyRunnable* aRunnable) = 0; 1.285 + 1.286 + virtual void 1.287 + SendPlayStatus(int64_t aDuration, 1.288 + int64_t aPosition, 1.289 + const nsAString& aPlayStatus, 1.290 + BluetoothReplyRunnable* aRunnable) = 0; 1.291 + 1.292 + virtual void 1.293 + UpdatePlayStatus(uint32_t aDuration, 1.294 + uint32_t aPosition, 1.295 + ControlPlayStatus aPlayStatus) = 0; 1.296 + 1.297 + virtual nsresult 1.298 + SendSinkMessage(const nsAString& aDeviceAddresses, 1.299 + const nsAString& aMessage) = 0; 1.300 + 1.301 + virtual nsresult 1.302 + SendInputMessage(const nsAString& aDeviceAddresses, 1.303 + const nsAString& aMessage) = 0; 1.304 + 1.305 + bool 1.306 + IsEnabled() const 1.307 + { 1.308 + return mEnabled; 1.309 + } 1.310 + 1.311 + bool 1.312 + IsToggling() const; 1.313 + 1.314 + void 1.315 + RemoveObserverFromTable(const nsAString& key); 1.316 + 1.317 + /** 1.318 + * Below 2 function/variable are used for ensuring event 'AdapterAdded' will 1.319 + * be fired after event 'Enabled'. 1.320 + */ 1.321 + void TryFiringAdapterAdded(); 1.322 + void AdapterAddedReceived(); 1.323 + 1.324 +protected: 1.325 + BluetoothService() : mEnabled(false) 1.326 + , mAdapterAddedReceived(false) 1.327 + { 1.328 + } 1.329 + 1.330 + virtual ~BluetoothService(); 1.331 + 1.332 + bool 1.333 + Init(); 1.334 + 1.335 + void 1.336 + Cleanup(); 1.337 + 1.338 + nsresult 1.339 + StartBluetooth(bool aIsStartup); 1.340 + 1.341 + nsresult 1.342 + StopBluetooth(bool aIsStartup); 1.343 + 1.344 + nsresult 1.345 + StartStopBluetooth(bool aStart, bool aIsStartup); 1.346 + 1.347 + /** 1.348 + * Platform specific startup functions go here. Usually deals with member 1.349 + * variables, so not static. Guaranteed to be called outside of main thread. 1.350 + * 1.351 + * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise 1.352 + */ 1.353 + virtual nsresult 1.354 + StartInternal() = 0; 1.355 + 1.356 + /** 1.357 + * Platform specific startup functions go here. Usually deals with member 1.358 + * variables, so not static. Guaranteed to be called outside of main thread. 1.359 + * 1.360 + * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise 1.361 + */ 1.362 + virtual nsresult 1.363 + StopInternal() = 0; 1.364 + 1.365 + /** 1.366 + * Called when XPCOM first creates this service. 1.367 + */ 1.368 + virtual nsresult 1.369 + HandleStartup(); 1.370 + 1.371 + /** 1.372 + * Called when the startup settings check has completed. 1.373 + */ 1.374 + nsresult 1.375 + HandleStartupSettingsCheck(bool aEnable); 1.376 + 1.377 + /** 1.378 + * Called when "mozsettings-changed" observer topic fires. 1.379 + */ 1.380 + nsresult 1.381 + HandleSettingsChanged(const nsAString& aData); 1.382 + 1.383 + /** 1.384 + * Called when XPCOM is shutting down. 1.385 + */ 1.386 + virtual nsresult 1.387 + HandleShutdown(); 1.388 + 1.389 + // Called by ToggleBtAck. 1.390 + void 1.391 + SetEnabled(bool aEnabled); 1.392 + 1.393 + // Called by Get(). 1.394 + static BluetoothService* 1.395 + Create(); 1.396 + 1.397 + typedef nsClassHashtable<nsStringHashKey, BluetoothSignalObserverList > 1.398 + BluetoothSignalObserverTable; 1.399 + 1.400 + BluetoothSignalObserverTable mBluetoothSignalObserverTable; 1.401 + 1.402 + bool mEnabled; 1.403 + 1.404 +private: 1.405 + bool mAdapterAddedReceived; 1.406 +}; 1.407 + 1.408 +END_BLUETOOTH_NAMESPACE 1.409 + 1.410 +#endif