Bumping manifests a=b2g-bump
[gecko.git] / dom / bluetooth / BluetoothService.h
blob8a3f006feb9bc369a18d6299331ed0108e7ace3c
1 /* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
2 /* vim: set ts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_dom_bluetooth_bluetootheventservice_h__
8 #define mozilla_dom_bluetooth_bluetootheventservice_h__
10 #include "BluetoothCommon.h"
11 #include "BluetoothProfileManagerBase.h"
12 #include "mozilla/dom/ipc/Blob.h"
13 #include "nsAutoPtr.h"
14 #include "nsClassHashtable.h"
15 #include "nsIDOMFile.h"
16 #include "nsIObserver.h"
17 #include "nsTObserverArray.h"
18 #include "nsThreadUtils.h"
20 namespace mozilla {
21 namespace ipc {
22 class UnixSocketConsumer;
26 BEGIN_BLUETOOTH_NAMESPACE
28 class BluetoothManager;
29 class BluetoothNamedValue;
30 class BluetoothReplyRunnable;
31 class BluetoothSignal;
33 typedef mozilla::ObserverList<BluetoothSignal> BluetoothSignalObserverList;
35 class BluetoothService : public nsIObserver
36 , public BluetoothSignalObserver
38 class ToggleBtTask;
39 friend class ToggleBtTask;
41 class StartupTask;
42 friend class StartupTask;
44 public:
45 class ToggleBtAck : public nsRunnable
47 public:
48 ToggleBtAck(bool aEnabled);
49 NS_IMETHOD Run();
51 private:
52 bool mEnabled;
54 friend class ToggleBtAck;
56 NS_DECL_ISUPPORTS
57 NS_DECL_NSIOBSERVER
59 /**
60 * Add a message handler object from message distribution observer.
61 * Must be called from the main thread.
63 * @param aNodeName Node name of the object
64 * @param aMsgHandler Weak pointer to the object
66 virtual void
67 RegisterBluetoothSignalHandler(const nsAString& aNodeName,
68 BluetoothSignalObserver* aMsgHandler);
70 /**
71 * Remove a message handler object from message distribution observer.
72 * Must be called from the main thread.
74 * @param aNodeName Node name of the object
75 * @param aMsgHandler Weak pointer to the object
77 virtual void
78 UnregisterBluetoothSignalHandler(const nsAString& aNodeName,
79 BluetoothSignalObserver* aMsgHandler);
81 /**
82 * Remove a message handlers for the given observer.
83 * Must be called from the main thread.
85 * @param aMsgHandler Weak pointer to the object
87 void
88 UnregisterAllSignalHandlers(BluetoothSignalObserver* aMsgHandler);
90 /**
91 * Distribute a signal to the observer list
93 * @param aSignal Signal object to distribute
95 * @return NS_OK if signal distributed, NS_ERROR_FAILURE on error
97 void
98 DistributeSignal(const BluetoothSignal& aEvent);
101 * Called when get a Bluetooth Signal from BluetoothDBusService
104 void
105 Notify(const BluetoothSignal& aParam);
108 * Returns the BluetoothService singleton. Only to be called from main thread.
110 * @param aService Pointer to return singleton into.
112 * @return NS_OK on proper assignment, NS_ERROR_FAILURE otherwise (if service
113 * has not yet been started, for instance)
115 static BluetoothService*
116 Get();
118 static already_AddRefed<BluetoothService>
119 FactoryCreate()
121 nsRefPtr<BluetoothService> service = Get();
122 return service.forget();
126 * Returns the path of the default adapter, implemented via a platform
127 * specific method.
129 * @return NS_OK on success, NS_ERROR_FAILURE otherwise
131 virtual nsresult
132 GetDefaultAdapterPathInternal(BluetoothReplyRunnable* aRunnable) = 0;
135 * Returns the properties of paired devices, implemented via a platform
136 * specific method.
138 * @return NS_OK on success, NS_ERROR_FAILURE otherwise
140 virtual nsresult
141 GetPairedDevicePropertiesInternal(const nsTArray<nsString>& aDeviceAddresses,
142 BluetoothReplyRunnable* aRunnable) = 0;
145 * Returns the properties of connected devices regarding to specific profile,
146 * implemented via a platform specific methood.
148 * @return NS_OK on success, NS_ERROR_FAILURE otherwise
150 virtual nsresult
151 GetConnectedDevicePropertiesInternal(uint16_t aServiceUuid,
152 BluetoothReplyRunnable* aRunnable) = 0;
155 * Stop device discovery (platform specific implementation)
157 * @return NS_OK if discovery stopped correctly, false otherwise
159 virtual nsresult
160 StopDiscoveryInternal(BluetoothReplyRunnable* aRunnable) = 0;
163 * Start device discovery (platform specific implementation)
165 * @return NS_OK if discovery stopped correctly, false otherwise
167 virtual nsresult
168 StartDiscoveryInternal(BluetoothReplyRunnable* aRunnable) = 0;
171 * Set a property for the specified object
173 * @param aPropName Name of the property
174 * @param aValue Boolean value
175 * @param aRunnable Runnable to run on async reply
177 * @return NS_OK if property is set correctly, NS_ERROR_FAILURE otherwise
179 virtual nsresult
180 SetProperty(BluetoothObjectType aType,
181 const BluetoothNamedValue& aValue,
182 BluetoothReplyRunnable* aRunnable) = 0;
184 virtual nsresult
185 CreatePairedDeviceInternal(const nsAString& aAddress,
186 int aTimeout,
187 BluetoothReplyRunnable* aRunnable) = 0;
189 virtual nsresult
190 RemoveDeviceInternal(const nsAString& aObjectPath,
191 BluetoothReplyRunnable* aRunnable) = 0;
194 * Get corresponding service channel of specific service on remote device.
195 * It's usually the very first step of establishing an outbound connection.
197 * @param aObjectPath Object path of remote device
198 * @param aServiceUuid UUID of the target service
199 * @param aManager Instance which has callback function OnGetServiceChannel()
201 * @return NS_OK if the task begins, NS_ERROR_FAILURE otherwise
203 virtual nsresult
204 GetServiceChannel(const nsAString& aDeviceAddress,
205 const nsAString& aServiceUuid,
206 BluetoothProfileManagerBase* aManager) = 0;
208 virtual bool
209 UpdateSdpRecords(const nsAString& aDeviceAddress,
210 BluetoothProfileManagerBase* aManager) = 0;
212 virtual bool
213 SetPinCodeInternal(const nsAString& aDeviceAddress, const nsAString& aPinCode,
214 BluetoothReplyRunnable* aRunnable) = 0;
216 virtual bool
217 SetPasskeyInternal(const nsAString& aDeviceAddress, uint32_t aPasskey,
218 BluetoothReplyRunnable* aRunnable) = 0;
220 virtual bool
221 SetPairingConfirmationInternal(const nsAString& aDeviceAddress, bool aConfirm,
222 BluetoothReplyRunnable* aRunnable) = 0;
224 virtual void
225 Connect(const nsAString& aDeviceAddress, uint32_t aCod, uint16_t aServiceUuid,
226 BluetoothReplyRunnable* aRunnable) = 0;
228 virtual void
229 Disconnect(const nsAString& aDeviceAddress, uint16_t aServiceUuid,
230 BluetoothReplyRunnable* aRunnable) = 0;
232 virtual void
233 IsConnected(const uint16_t aServiceUuid, BluetoothReplyRunnable* aRunnable) = 0;
235 virtual void
236 SendFile(const nsAString& aDeviceAddress,
237 BlobParent* aBlobParent,
238 BlobChild* aBlobChild,
239 BluetoothReplyRunnable* aRunnable) = 0;
241 virtual void
242 SendFile(const nsAString& aDeviceAddress,
243 nsIDOMBlob* aBlob,
244 BluetoothReplyRunnable* aRunnable) = 0;
246 virtual void
247 StopSendingFile(const nsAString& aDeviceAddress,
248 BluetoothReplyRunnable* aRunnable) = 0;
250 virtual void
251 ConfirmReceivingFile(const nsAString& aDeviceAddress, bool aConfirm,
252 BluetoothReplyRunnable* aRunnable) = 0;
254 virtual void
255 ConnectSco(BluetoothReplyRunnable* aRunnable) = 0;
257 virtual void
258 DisconnectSco(BluetoothReplyRunnable* aRunnable) = 0;
260 virtual void
261 IsScoConnected(BluetoothReplyRunnable* aRunnable) = 0;
263 #ifdef MOZ_B2G_RIL
264 virtual void
265 AnswerWaitingCall(BluetoothReplyRunnable* aRunnable) = 0;
267 virtual void
268 IgnoreWaitingCall(BluetoothReplyRunnable* aRunnable) = 0;
270 virtual void
271 ToggleCalls(BluetoothReplyRunnable* aRunnable) = 0;
272 #endif
274 virtual void
275 SendMetaData(const nsAString& aTitle,
276 const nsAString& aArtist,
277 const nsAString& aAlbum,
278 int64_t aMediaNumber,
279 int64_t aTotalMediaCount,
280 int64_t aDuration,
281 BluetoothReplyRunnable* aRunnable) = 0;
283 virtual void
284 SendPlayStatus(int64_t aDuration,
285 int64_t aPosition,
286 const nsAString& aPlayStatus,
287 BluetoothReplyRunnable* aRunnable) = 0;
289 virtual void
290 UpdatePlayStatus(uint32_t aDuration,
291 uint32_t aPosition,
292 ControlPlayStatus aPlayStatus) = 0;
294 virtual nsresult
295 SendSinkMessage(const nsAString& aDeviceAddresses,
296 const nsAString& aMessage) = 0;
298 virtual nsresult
299 SendInputMessage(const nsAString& aDeviceAddresses,
300 const nsAString& aMessage) = 0;
302 bool
303 IsEnabled() const
305 return mEnabled;
308 bool
309 IsToggling() const;
312 * Below 2 function/variable are used for ensuring event 'AdapterAdded' will
313 * be fired after event 'Enabled'.
315 void TryFiringAdapterAdded();
316 void AdapterAddedReceived();
318 protected:
319 BluetoothService() : mEnabled(false)
320 , mAdapterAddedReceived(false)
324 virtual ~BluetoothService();
326 bool
327 Init();
329 void
330 Cleanup();
332 nsresult
333 StartBluetooth(bool aIsStartup);
335 nsresult
336 StopBluetooth(bool aIsStartup);
338 nsresult
339 StartStopBluetooth(bool aStart, bool aIsStartup);
342 * Platform specific startup functions go here. Usually deals with member
343 * variables, so not static. Guaranteed to be called outside of main thread.
345 * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise
347 virtual nsresult
348 StartInternal() = 0;
351 * Platform specific startup functions go here. Usually deals with member
352 * variables, so not static. Guaranteed to be called outside of main thread.
354 * @return NS_OK on correct startup, NS_ERROR_FAILURE otherwise
356 virtual nsresult
357 StopInternal() = 0;
360 * Called when XPCOM first creates this service.
362 virtual nsresult
363 HandleStartup();
366 * Called when the startup settings check has completed.
368 nsresult
369 HandleStartupSettingsCheck(bool aEnable);
372 * Called when "mozsettings-changed" observer topic fires.
374 nsresult
375 HandleSettingsChanged(const nsAString& aData);
378 * Called when XPCOM is shutting down.
380 virtual nsresult
381 HandleShutdown();
383 // Called by ToggleBtAck.
384 void
385 SetEnabled(bool aEnabled);
387 // Called by Get().
388 static BluetoothService*
389 Create();
391 typedef nsClassHashtable<nsStringHashKey, BluetoothSignalObserverList >
392 BluetoothSignalObserverTable;
394 BluetoothSignalObserverTable mBluetoothSignalObserverTable;
396 bool mEnabled;
398 private:
399 bool mAdapterAddedReceived;
402 END_BLUETOOTH_NAMESPACE
404 #endif