1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et ft=cpp : */
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/. */
10 #include "mozilla/hal_sandbox/PHal.h"
11 #include "mozilla/HalTypes.h"
12 #include "base/basictypes.h"
13 #include "base/platform_thread.h"
14 #include "mozilla/Observer.h"
15 #include "mozilla/Types.h"
17 #include "mozilla/dom/MozPowerManagerBinding.h"
18 #include "mozilla/dom/battery/Types.h"
19 #include "mozilla/dom/network/Types.h"
20 #include "mozilla/dom/power/Types.h"
21 #include "mozilla/dom/ScreenOrientation.h"
22 #include "mozilla/HalScreenConfiguration.h"
25 * Hal.h contains the public Hal API.
27 * By default, this file defines its functions in the hal namespace, but if
28 * MOZ_HAL_NAMESPACE is defined, we'll define our functions in that namespace.
30 * This is used by HalImpl.h and HalSandbox.h, which define copies of all the
31 * functions here in the hal_impl and hal_sandbox namespaces.
36 #ifndef MOZ_HAL_NAMESPACE
37 # define MOZ_HAL_NAMESPACE hal
38 # define MOZ_DEFINED_HAL_NAMESPACE 1
45 typedef Observer
<void_t
> AlarmObserver
;
47 class WindowIdentifier
;
49 typedef Observer
<int64_t> SystemClockChangeObserver
;
50 typedef Observer
<SystemTimezoneChangeInformation
> SystemTimezoneChangeObserver
;
54 namespace MOZ_HAL_NAMESPACE
{
57 * Turn the default vibrator device on/off per the pattern specified
58 * by |pattern|. Each element in the pattern is the number of
59 * milliseconds to turn the vibrator on or off. The first element in
60 * |pattern| is an "on" element, the next is "off", and so on.
62 * If |pattern| is empty, any in-progress vibration is canceled.
64 * Only an active window within an active tab may call Vibrate; calls
65 * from inactive windows and windows on inactive tabs do nothing.
67 * If you're calling hal::Vibrate from the outside world, pass an
68 * nsIDOMWindow* in place of the WindowIdentifier parameter.
69 * The method with WindowIdentifier will be called automatically.
71 void Vibrate(const nsTArray
<uint32_t>& pattern
,
72 nsIDOMWindow
* aWindow
);
73 void Vibrate(const nsTArray
<uint32_t>& pattern
,
74 const hal::WindowIdentifier
&id
);
77 * Cancel a vibration started by the content window identified by
80 * If the window was the last window to start a vibration, the
81 * cancellation request will go through even if the window is not
84 * As with hal::Vibrate(), if you're calling hal::CancelVibrate from the outside
85 * world, pass an nsIDOMWindow*. The method with WindowIdentifier will be called
88 void CancelVibrate(nsIDOMWindow
* aWindow
);
89 void CancelVibrate(const hal::WindowIdentifier
&id
);
92 * Inform the battery backend there is a new battery observer.
93 * @param aBatteryObserver The observer that should be added.
95 void RegisterBatteryObserver(BatteryObserver
* aBatteryObserver
);
98 * Inform the battery backend a battery observer unregistered.
99 * @param aBatteryObserver The observer that should be removed.
101 void UnregisterBatteryObserver(BatteryObserver
* aBatteryObserver
);
104 * Returns the current battery information.
106 void GetCurrentBatteryInformation(hal::BatteryInformation
* aBatteryInfo
);
109 * Notify of a change in the battery state.
110 * @param aBatteryInfo The new battery information.
112 void NotifyBatteryChange(const hal::BatteryInformation
& aBatteryInfo
);
115 * Determine whether the device's screen is currently enabled.
117 bool GetScreenEnabled();
120 * Enable or disable the device's screen.
122 * Note that it may take a few seconds for the screen to turn on or off.
124 void SetScreenEnabled(bool aEnabled
);
127 * Determine whether the device's keypad/button backlight is currently enabled.
129 bool GetKeyLightEnabled();
132 * Enable or disable the device's keypad/button backlight.
134 void SetKeyLightEnabled(bool aEnabled
);
137 * Get the brightness of the device's screen's backlight, on a scale from 0
138 * (very dim) to 1 (full blast).
140 * If the display is currently disabled, this returns the brightness the
141 * backlight will have when the display is re-enabled.
143 double GetScreenBrightness();
146 * Set the brightness of the device's screen's backlight, on a scale from 0
147 * (very dimm) to 1 (full blast). Values larger than 1 are treated like 1, and
148 * values smaller than 0 are treated like 0.
150 * Note that we may reduce the resolution of the given brightness value before
151 * sending it to the screen. Therefore if you call SetScreenBrightness(x)
152 * followed by GetScreenBrightness(), the value returned by
153 * GetScreenBrightness() may not be exactly x.
155 void SetScreenBrightness(double aBrightness
);
158 * Determine whether the device is allowed to sleep.
160 bool GetCpuSleepAllowed();
163 * Set whether the device is allowed to suspend automatically after
164 * the screen is disabled.
166 void SetCpuSleepAllowed(bool aAllowed
);
169 * Register an observer for the sensor of given type.
171 * The observer will receive data whenever the data generated by the
172 * sensor is avaiable.
174 void RegisterSensorObserver(hal::SensorType aSensor
,
175 hal::ISensorObserver
*aObserver
);
178 * Unregister an observer for the sensor of given type.
180 void UnregisterSensorObserver(hal::SensorType aSensor
,
181 hal::ISensorObserver
*aObserver
);
184 * Post a value generated by a sensor.
186 * This API is internal to hal; clients shouldn't call it directly.
188 void NotifySensorChange(const hal::SensorData
&aSensorData
);
191 * Enable sensor notifications from the backend
193 * This method is only visible from implementation of sensor manager.
194 * Rest of the system should not try this.
196 void EnableSensorNotifications(hal::SensorType aSensor
);
199 * Disable sensor notifications from the backend
201 * This method is only visible from implementation of sensor manager.
202 * Rest of the system should not try this.
204 void DisableSensorNotifications(hal::SensorType aSensor
);
208 * Inform the network backend there is a new network observer.
209 * @param aNetworkObserver The observer that should be added.
211 void RegisterNetworkObserver(NetworkObserver
* aNetworkObserver
);
214 * Inform the network backend a network observer unregistered.
215 * @param aNetworkObserver The observer that should be removed.
217 void UnregisterNetworkObserver(NetworkObserver
* aNetworkObserver
);
220 * Returns the current network information.
222 void GetCurrentNetworkInformation(hal::NetworkInformation
* aNetworkInfo
);
225 * Notify of a change in the network state.
226 * @param aNetworkInfo The new network information.
228 void NotifyNetworkChange(const hal::NetworkInformation
& aNetworkInfo
);
231 * Adjusting system clock.
232 * @param aDeltaMilliseconds The difference compared with current system clock.
234 void AdjustSystemClock(int64_t aDeltaMilliseconds
);
238 * @param aTimezoneSpec The definition can be found in
239 * http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
241 void SetTimezone(const nsCString
& aTimezoneSpec
);
245 * http://en.wikipedia.org/wiki/List_of_tz_database_time_zones
247 nsCString
GetTimezone();
250 * Get timezone offset
251 * returns the timezone offset relative to UTC in minutes (DST effect included)
253 int32_t GetTimezoneOffset();
256 * Register observer for system clock changed notification.
257 * @param aObserver The observer that should be added.
259 void RegisterSystemClockChangeObserver(
260 hal::SystemClockChangeObserver
* aObserver
);
263 * Unregister the observer for system clock changed.
264 * @param aObserver The observer that should be removed.
266 void UnregisterSystemClockChangeObserver(
267 hal::SystemClockChangeObserver
* aObserver
);
270 * Notify of a change in the system clock.
271 * @param aClockDeltaMS
273 void NotifySystemClockChange(const int64_t& aClockDeltaMS
);
276 * Register observer for system timezone changed notification.
277 * @param aObserver The observer that should be added.
279 void RegisterSystemTimezoneChangeObserver(
280 hal::SystemTimezoneChangeObserver
* aObserver
);
283 * Unregister the observer for system timezone changed.
284 * @param aObserver The observer that should be removed.
286 void UnregisterSystemTimezoneChangeObserver(
287 hal::SystemTimezoneChangeObserver
* aObserver
);
290 * Notify of a change in the system timezone.
291 * @param aSystemTimezoneChangeInfo
293 void NotifySystemTimezoneChange(
294 const hal::SystemTimezoneChangeInformation
& aSystemTimezoneChangeInfo
);
299 * This API is currently only allowed to be used from the main process.
304 * Power off the device.
306 * This API is currently only allowed to be used from the main process.
311 * Enable wake lock notifications from the backend.
313 * This method is only used by WakeLockObserversManager.
315 void EnableWakeLockNotifications();
318 * Disable wake lock notifications from the backend.
320 * This method is only used by WakeLockObserversManager.
322 void DisableWakeLockNotifications();
325 * Inform the wake lock backend there is a new wake lock observer.
326 * @param aWakeLockObserver The observer that should be added.
328 void RegisterWakeLockObserver(WakeLockObserver
* aObserver
);
331 * Inform the wake lock backend a wake lock observer unregistered.
332 * @param aWakeLockObserver The observer that should be removed.
334 void UnregisterWakeLockObserver(WakeLockObserver
* aObserver
);
337 * Adjust a wake lock's counts on behalf of a given process.
339 * In most cases, you shouldn't need to pass the aProcessID argument; the
340 * default of CONTENT_PROCESS_ID_UNKNOWN is probably what you want.
342 * @param aTopic lock topic
343 * @param aLockAdjust to increase or decrease active locks
344 * @param aHiddenAdjust to increase or decrease hidden locks
345 * @param aProcessID indicates which process we're modifying the wake lock
346 * on behalf of. It is interpreted as
348 * CONTENT_PROCESS_ID_UNKNOWN: The current process
349 * CONTENT_PROCESS_ID_MAIN: The root process
350 * X: The process with ContentChild::GetID() == X
352 void ModifyWakeLock(const nsAString
&aTopic
,
353 hal::WakeLockControl aLockAdjust
,
354 hal::WakeLockControl aHiddenAdjust
,
355 uint64_t aProcessID
= hal::CONTENT_PROCESS_ID_UNKNOWN
);
358 * Query the wake lock numbers of aTopic.
359 * @param aTopic lock topic
360 * @param aWakeLockInfo wake lock numbers
362 void GetWakeLockInfo(const nsAString
&aTopic
, hal::WakeLockInformation
*aWakeLockInfo
);
365 * Notify of a change in the wake lock state.
366 * @param aWakeLockInfo The new wake lock information.
368 void NotifyWakeLockChange(const hal::WakeLockInformation
& aWakeLockInfo
);
371 * Inform the backend there is a new screen configuration observer.
372 * @param aScreenConfigurationObserver The observer that should be added.
374 void RegisterScreenConfigurationObserver(hal::ScreenConfigurationObserver
* aScreenConfigurationObserver
);
377 * Inform the backend a screen configuration observer unregistered.
378 * @param aScreenConfigurationObserver The observer that should be removed.
380 void UnregisterScreenConfigurationObserver(hal::ScreenConfigurationObserver
* aScreenConfigurationObserver
);
383 * Returns the current screen configuration.
385 void GetCurrentScreenConfiguration(hal::ScreenConfiguration
* aScreenConfiguration
);
388 * Notify of a change in the screen configuration.
389 * @param aScreenConfiguration The new screen orientation.
391 void NotifyScreenConfigurationChange(const hal::ScreenConfiguration
& aScreenConfiguration
);
394 * Lock the screen orientation to the specific orientation.
395 * @return Whether the lock has been accepted.
397 bool LockScreenOrientation(const dom::ScreenOrientationInternal
& aOrientation
);
400 * Unlock the screen orientation.
402 void UnlockScreenOrientation();
405 * Register an observer for the switch of given SwitchDevice.
407 * The observer will receive data whenever the data generated by the
410 void RegisterSwitchObserver(hal::SwitchDevice aDevice
, hal::SwitchObserver
*aSwitchObserver
);
413 * Unregister an observer for the switch of given SwitchDevice.
415 void UnregisterSwitchObserver(hal::SwitchDevice aDevice
, hal::SwitchObserver
*aSwitchObserver
);
418 * Notify the state of the switch.
420 * This API is internal to hal; clients shouldn't call it directly.
422 void NotifySwitchChange(const hal::SwitchEvent
& aEvent
);
425 * Get current switch information.
427 hal::SwitchState
GetCurrentSwitchState(hal::SwitchDevice aDevice
);
430 * Notify switch status change from input device.
432 void NotifySwitchStateFromInputDevice(hal::SwitchDevice aDevice
,
433 hal::SwitchState aState
);
436 * Register an observer that is notified when a programmed alarm
439 * Currently, there can only be 0 or 1 alarm observers.
441 bool RegisterTheOneAlarmObserver(hal::AlarmObserver
* aObserver
);
444 * Unregister the alarm observer. Doing so will implicitly cancel any
447 void UnregisterTheOneAlarmObserver();
450 * Notify that the programmed alarm has expired.
452 * This API is internal to hal; clients shouldn't call it directly.
454 void NotifyAlarmFired();
457 * Program the real-time clock to expire at the time |aSeconds|,
458 * |aNanoseconds|. These specify a point in real time relative to the
459 * UNIX epoch. The alarm will fire at this time point even if the
460 * real-time clock is changed; that is, this alarm respects changes to
461 * the real-time clock. Return true iff the alarm was programmed.
463 * The alarm can be reprogrammed at any time.
465 * This API is currently only allowed to be used from non-sandboxed
468 bool SetAlarm(int32_t aSeconds
, int32_t aNanoseconds
);
471 * Set the priority of the given process.
473 * Exactly what this does will vary between platforms. On *nix we might give
474 * background processes higher nice values. On other platforms, we might
475 * ignore this call entirely.
477 void SetProcessPriority(int aPid
,
478 hal::ProcessPriority aPriority
,
483 * Set the current thread's priority to appropriate platform-specific value for
484 * given functionality. Instead of providing arbitrary priority numbers you
485 * must specify a type of function like THREAD_PRIORITY_COMPOSITOR.
487 void SetCurrentThreadPriority(hal::ThreadPriority aThreadPriority
);
490 * Set a thread priority to appropriate platform-specific value for
491 * given functionality. Instead of providing arbitrary priority numbers you
492 * must specify a type of function like THREAD_PRIORITY_COMPOSITOR.
494 void SetThreadPriority(PlatformThreadId aThreadId
,
495 hal::ThreadPriority aThreadPriority
);
498 * Register an observer for the FM radio.
500 void RegisterFMRadioObserver(hal::FMRadioObserver
* aRadioObserver
);
503 * Unregister the observer for the FM radio.
505 void UnregisterFMRadioObserver(hal::FMRadioObserver
* aRadioObserver
);
508 * Register an observer for the FM radio.
510 void RegisterFMRadioRDSObserver(hal::FMRadioRDSObserver
* aRDSObserver
);
513 * Unregister the observer for the FM radio.
515 void UnregisterFMRadioRDSObserver(hal::FMRadioRDSObserver
* aRDSObserver
);
518 * Notify observers that a call to EnableFMRadio, DisableFMRadio, or FMRadioSeek
519 * has completed, and indicate what the call returned.
521 void NotifyFMRadioStatus(const hal::FMRadioOperationInformation
& aRadioState
);
524 * Notify observers of new RDS data
525 * This can be called on any thread.
527 void NotifyFMRadioRDSGroup(const hal::FMRadioRDSGroup
& aRDSGroup
);
530 * Enable the FM radio and configure it according to the settings in aInfo.
532 void EnableFMRadio(const hal::FMRadioSettings
& aInfo
);
535 * Disable the FM radio.
537 void DisableFMRadio();
540 * Seek to an available FM radio station.
542 * This can be called off main thread, but all calls must be completed
543 * before calling DisableFMRadio.
545 void FMRadioSeek(const hal::FMRadioSeekDirection
& aDirection
);
548 * Get the current FM radio settings.
550 void GetFMRadioSettings(hal::FMRadioSettings
* aInfo
);
553 * Set the FM radio's frequency.
555 * This can be called off main thread, but all calls must be completed
556 * before calling DisableFMRadio.
558 void SetFMRadioFrequency(const uint32_t frequency
);
561 * Get the FM radio's frequency.
563 uint32_t GetFMRadioFrequency();
566 * Get FM radio power state
571 * Get FM radio signal strength
573 uint32_t GetFMRadioSignalStrength();
576 * Cancel FM radio seeking
578 void CancelFMRadioSeek();
581 * Get FM radio band settings by country.
583 hal::FMRadioSettings
GetFMBandSettings(hal::FMRadioCountry aCountry
);
586 * Enable RDS data reception
588 bool EnableRDS(uint32_t aMask
);
591 * Disable RDS data reception
596 * Start a watchdog to compulsively shutdown the system if it hangs.
597 * @param aMode Specify how to shutdown the system.
598 * @param aTimeoutSecs Specify the delayed seconds to shutdown the system.
600 * This API is currently only allowed to be used from the main process.
602 void StartForceQuitWatchdog(hal::ShutdownMode aMode
, int32_t aTimeoutSecs
);
605 * Perform Factory Reset to wipe out all user data.
607 void FactoryReset(mozilla::dom::FactoryResetReason
& aReason
);
610 * Start monitoring disk space for low space situations.
612 * This API is currently only allowed to be used from the main process.
614 void StartDiskSpaceWatcher();
617 * Stop monitoring disk space for low space situations.
619 * This API is currently only allowed to be used from the main process.
621 void StopDiskSpaceWatcher();
624 * Get total system memory of device being run on in bytes.
626 * Returns 0 if we are unable to determine this information from /proc/meminfo.
628 uint32_t GetTotalSystemMemory();
631 * Get the level of total system memory on device in MiB.
632 * (round the value up to the next power of two)
634 * Returns 0 if we are unable to determine this information from /proc/meminfo.
636 uint32_t GetTotalSystemMemoryLevel();
639 * Determine whether the headphone switch event is from input device
641 bool IsHeadphoneEventFromInputDev();
644 * Start the system service with the specified name and arguments.
646 nsresult
StartSystemService(const char* aSvcName
, const char* aArgs
);
649 * Stop the system service with the specified name.
651 void StopSystemService(const char* aSvcName
);
654 * Determine whether the system service with the specified name is running.
656 bool SystemServiceIsRunning(const char* aSvcName
);
658 } // namespace MOZ_HAL_NAMESPACE
659 } // namespace mozilla
661 #ifdef MOZ_DEFINED_HAL_NAMESPACE
662 # undef MOZ_DEFINED_HAL_NAMESPACE
663 # undef MOZ_HAL_NAMESPACE
666 #endif // mozilla_Hal_h