2011-10-05 22:15:45 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2011-09-30 07:00:48 +00:00
|
|
|
/* vim: set sw=2 ts=8 et ft=cpp : */
|
2012-02-10 10:04:44 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
2012-02-02 06:09:00 +00:00
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
2012-02-10 10:04:44 +00:00
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2011-10-05 22:15:45 +00:00
|
|
|
|
|
|
|
#ifndef mozilla_Hal_h
|
2012-02-10 10:04:44 +00:00
|
|
|
#define mozilla_Hal_h
|
2011-10-05 22:15:45 +00:00
|
|
|
|
|
|
|
#include "nsTArray.h"
|
2016-02-15 06:33:00 +00:00
|
|
|
#include "mozilla/hal_sandbox/PHal.h"
|
2022-02-15 20:22:54 +00:00
|
|
|
#include "mozilla/HalScreenConfiguration.h"
|
2018-08-13 01:57:50 +00:00
|
|
|
#include "mozilla/HalBatteryInformation.h"
|
2018-08-13 01:57:50 +00:00
|
|
|
#include "mozilla/HalNetworkInformation.h"
|
2018-08-13 01:57:50 +00:00
|
|
|
#include "mozilla/HalWakeLockInformation.h"
|
2016-02-15 06:33:00 +00:00
|
|
|
#include "mozilla/HalTypes.h"
|
Bug 1697647 - Add screen orientation lock api r=ipc-reviewers,mccr8,agi,smaug,jonalmeida
Previously, the screenOrientation.lock API was for Fennec and not supported for Fenix and multi-process use. The overall idea is to now allow apps to use the API through a delegate and make asynchronous calls to LockDeviceOrientation. This required replacing the existing code that returned a default false bool to calls that perform the requested orientation change and instead return a promise that contained either an allow or deny value.
Returning a promise instead of a bool involved changing the API calls from the C++ side to Java. The new general control flow of screenOrientation lock follows: an app calls C++ ScreenOrientation.lock() which eventually dispatches LockOrientationTask to resolve the pending orientation promise. Hal.cpp sends an IPC call to the content process and RecvLockScreenOrientation retrieves the current instance of geckoRuntime and calls the java side LockScreenOrientation. Apps must create a delegate and override onOrientationLock to set the requested orientation. In geckoview's testing, this is done with the android API setRequestedOrientation. Once a device orientation change has been triggered, native OnOrientationChange calls to NotifyScreenConfigurationChange, which notifies all observers and dispatches a change event to resolve the pending orientation promise.
Testing:
I used a demo on the GeckoView Example (https://usefulangle.com/demos/105/screen.html) to test locking to landscape orientation. This required a change to the GVE to show the app from recreating the whole thing on orientation change. In the example AndroidManifest xml file, `orientation` prevents restart when orientation changes.
The Junit/Kotlin tests were to verify that the expected orientation delegate was called with the expected new orientation value, in an orientation change, if the new orientation was the same as the current, and if the pre-lock conditions such as being fullscreen were not met.
A static preference `dom.screenorientation.allow-lock` was added to the dom group, since it affects the ui dom) and is currently turned off. C++ can access it through its mirrored variable dom_screenorientation_allow_lock (same name but with underscores). The junit tests turn the preference on and test the lock feature.
Reference:
Orientation constant values:
C++
1 ScreenOrientation_PortraitPrimary); - vertical with button at bottom
2 ScreenOrientation_PortraitSecondary); - vertical with button at top
4 ScreenOrientation_LandscapePrimary); - horizational w button right
8 ScreenOrientation_LandscapeSecondary); - horization button left
16 ScreenOrientation_Default);
Java
1 GeckoScreenOrientation.ScreenOrientation.PORTRAIT_PRIMARY.value
2 GeckoScreenOrientation.ScreenOrientation.PORTRAIT_SECONDARY.value
4 GeckoScreenOrientation.ScreenOrientation.LANDSCAPE_PRIMARY.value
8 GeckoScreenOrientation.ScreenOrientation.LANDSCAPE_SECONDARY.value
Java public API
0 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
1 Activitynfo.SCREEN_ORIENTATION_PORTRAIT
Android
1 ORIENTATION_PORTRAIT
2 ORIENTATION_LANDSCAPE
Differential Revision: https://phabricator.services.mozilla.com/D129427
2021-12-06 13:58:37 +00:00
|
|
|
#include "mozilla/MozPromise.h"
|
2011-10-05 22:15:45 +00:00
|
|
|
|
2022-06-22 13:31:25 +00:00
|
|
|
#include <cstdint>
|
|
|
|
|
2011-12-17 21:04:51 +00:00
|
|
|
/*
|
|
|
|
* Hal.h contains the public Hal API.
|
|
|
|
*
|
|
|
|
* By default, this file defines its functions in the hal namespace, but if
|
|
|
|
* MOZ_HAL_NAMESPACE is defined, we'll define our functions in that namespace.
|
|
|
|
*
|
|
|
|
* This is used by HalImpl.h and HalSandbox.h, which define copies of all the
|
|
|
|
* functions here in the hal_impl and hal_sandbox namespaces.
|
|
|
|
*/
|
2011-09-30 07:00:48 +00:00
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
class nsPIDOMWindowInner;
|
2011-12-17 21:04:26 +00:00
|
|
|
|
2011-12-17 21:04:51 +00:00
|
|
|
#ifndef MOZ_HAL_NAMESPACE
|
2011-10-05 22:15:45 +00:00
|
|
|
# define MOZ_HAL_NAMESPACE hal
|
|
|
|
# define MOZ_DEFINED_HAL_NAMESPACE 1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace mozilla {
|
2011-11-02 15:14:01 +00:00
|
|
|
|
|
|
|
namespace hal {
|
2011-12-17 21:04:51 +00:00
|
|
|
|
|
|
|
class WindowIdentifier;
|
|
|
|
|
2011-11-02 15:14:01 +00:00
|
|
|
} // namespace hal
|
|
|
|
|
2011-12-17 21:04:51 +00:00
|
|
|
namespace MOZ_HAL_NAMESPACE {
|
2011-10-05 22:15:45 +00:00
|
|
|
|
2018-08-31 20:29:30 +00:00
|
|
|
/**
|
|
|
|
* Initializes the HAL. This must be called before any other HAL function.
|
|
|
|
*/
|
|
|
|
void Init();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Shuts down the HAL. Besides freeing all the used resources this will check
|
|
|
|
* that all observers have been properly deregistered and assert if not.
|
|
|
|
*/
|
|
|
|
void Shutdown();
|
|
|
|
|
2011-10-05 22:15:45 +00:00
|
|
|
/**
|
|
|
|
* Turn the default vibrator device on/off per the pattern specified
|
|
|
|
* by |pattern|. Each element in the pattern is the number of
|
|
|
|
* milliseconds to turn the vibrator on or off. The first element in
|
|
|
|
* |pattern| is an "on" element, the next is "off", and so on.
|
|
|
|
*
|
|
|
|
* If |pattern| is empty, any in-progress vibration is canceled.
|
2011-09-30 07:00:48 +00:00
|
|
|
*
|
|
|
|
* Only an active window within an active tab may call Vibrate; calls
|
|
|
|
* from inactive windows and windows on inactive tabs do nothing.
|
|
|
|
*
|
|
|
|
* If you're calling hal::Vibrate from the outside world, pass an
|
2011-12-17 21:04:26 +00:00
|
|
|
* nsIDOMWindow* in place of the WindowIdentifier parameter.
|
|
|
|
* The method with WindowIdentifier will be called automatically.
|
2011-10-05 22:15:45 +00:00
|
|
|
*/
|
2012-05-25 07:18:30 +00:00
|
|
|
void Vibrate(const nsTArray<uint32_t>& pattern, nsPIDOMWindowInner* aWindow);
|
2020-05-05 10:14:24 +00:00
|
|
|
void Vibrate(const nsTArray<uint32_t>& pattern, hal::WindowIdentifier&& id);
|
2011-09-30 07:00:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancel a vibration started by the content window identified by
|
|
|
|
* WindowIdentifier.
|
|
|
|
*
|
|
|
|
* If the window was the last window to start a vibration, the
|
|
|
|
* cancellation request will go through even if the window is not
|
|
|
|
* active.
|
|
|
|
*
|
2011-12-17 21:04:26 +00:00
|
|
|
* As with hal::Vibrate(), if you're calling hal::CancelVibrate from the outside
|
|
|
|
* world, pass an nsIDOMWindow*. The method with WindowIdentifier will be called
|
|
|
|
* automatically.
|
2011-09-30 07:00:48 +00:00
|
|
|
*/
|
2016-01-30 17:05:36 +00:00
|
|
|
void CancelVibrate(nsPIDOMWindowInner* aWindow);
|
2020-05-05 10:14:24 +00:00
|
|
|
void CancelVibrate(hal::WindowIdentifier&& id);
|
2018-11-30 10:46:48 +00:00
|
|
|
|
2018-08-13 01:57:50 +00:00
|
|
|
#define MOZ_DEFINE_HAL_OBSERVER(name_) \
|
|
|
|
/** \
|
|
|
|
* Inform the backend there is a new |name_| observer. \
|
|
|
|
* @param aObserver The observer that should be added. \
|
|
|
|
*/ \
|
|
|
|
void Register##name_##Observer(hal::name_##Observer* aObserver); \
|
|
|
|
/** \
|
|
|
|
* Inform the backend a |name_| observer unregistered. \
|
|
|
|
* @param aObserver The observer that should be removed. \
|
|
|
|
*/ \
|
|
|
|
void Unregister##name_##Observer(hal::name_##Observer* aObserver);
|
2011-11-02 15:14:01 +00:00
|
|
|
|
2018-08-13 01:57:50 +00:00
|
|
|
MOZ_DEFINE_HAL_OBSERVER(Battery);
|
2011-11-02 15:14:01 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current battery information.
|
|
|
|
*/
|
|
|
|
void GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify of a change in the battery state.
|
|
|
|
* @param aBatteryInfo The new battery information.
|
|
|
|
*/
|
|
|
|
void NotifyBatteryChange(const hal::BatteryInformation& aBatteryInfo);
|
|
|
|
|
2012-02-05 19:51:06 +00:00
|
|
|
/**
|
|
|
|
* Register an observer for the sensor of given type.
|
|
|
|
*
|
|
|
|
* The observer will receive data whenever the data generated by the
|
|
|
|
* sensor is avaiable.
|
|
|
|
*/
|
|
|
|
void RegisterSensorObserver(hal::SensorType aSensor,
|
|
|
|
hal::ISensorObserver* aObserver);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unregister an observer for the sensor of given type.
|
|
|
|
*/
|
|
|
|
void UnregisterSensorObserver(hal::SensorType aSensor,
|
|
|
|
hal::ISensorObserver* aObserver);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Post a value generated by a sensor.
|
|
|
|
*
|
|
|
|
* This API is internal to hal; clients shouldn't call it directly.
|
|
|
|
*/
|
|
|
|
void NotifySensorChange(const hal::SensorData& aSensorData);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable sensor notifications from the backend
|
|
|
|
*
|
|
|
|
* This method is only visible from implementation of sensor manager.
|
|
|
|
* Rest of the system should not try this.
|
|
|
|
*/
|
|
|
|
void EnableSensorNotifications(hal::SensorType aSensor);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable sensor notifications from the backend
|
|
|
|
*
|
|
|
|
* This method is only visible from implementation of sensor manager.
|
|
|
|
* Rest of the system should not try this.
|
|
|
|
*/
|
|
|
|
void DisableSensorNotifications(hal::SensorType aSensor);
|
|
|
|
|
2018-08-13 01:57:50 +00:00
|
|
|
MOZ_DEFINE_HAL_OBSERVER(Network);
|
2012-01-16 13:39:57 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current network information.
|
|
|
|
*/
|
|
|
|
void GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify of a change in the network state.
|
|
|
|
* @param aNetworkInfo The new network information.
|
|
|
|
*/
|
|
|
|
void NotifyNetworkChange(const hal::NetworkInformation& aNetworkInfo);
|
|
|
|
|
2012-03-07 11:03:25 +00:00
|
|
|
/**
|
|
|
|
* Enable wake lock notifications from the backend.
|
|
|
|
*
|
|
|
|
* This method is only used by WakeLockObserversManager.
|
|
|
|
*/
|
|
|
|
void EnableWakeLockNotifications();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable wake lock notifications from the backend.
|
|
|
|
*
|
|
|
|
* This method is only used by WakeLockObserversManager.
|
|
|
|
*/
|
|
|
|
void DisableWakeLockNotifications();
|
|
|
|
|
2018-08-13 01:57:50 +00:00
|
|
|
MOZ_DEFINE_HAL_OBSERVER(WakeLock);
|
2012-03-07 11:03:25 +00:00
|
|
|
|
|
|
|
/**
|
2013-02-14 20:41:30 +00:00
|
|
|
* Adjust a wake lock's counts on behalf of a given process.
|
|
|
|
*
|
|
|
|
* In most cases, you shouldn't need to pass the aProcessID argument; the
|
|
|
|
* default of CONTENT_PROCESS_ID_UNKNOWN is probably what you want.
|
|
|
|
*
|
2012-03-07 11:03:25 +00:00
|
|
|
* @param aTopic lock topic
|
|
|
|
* @param aLockAdjust to increase or decrease active locks
|
|
|
|
* @param aHiddenAdjust to increase or decrease hidden locks
|
2013-02-14 20:41:30 +00:00
|
|
|
* @param aProcessID indicates which process we're modifying the wake lock
|
|
|
|
* on behalf of. It is interpreted as
|
|
|
|
*
|
|
|
|
* CONTENT_PROCESS_ID_UNKNOWN: The current process
|
|
|
|
* CONTENT_PROCESS_ID_MAIN: The root process
|
|
|
|
* X: The process with ContentChild::GetID() == X
|
2012-03-07 11:03:25 +00:00
|
|
|
*/
|
|
|
|
void ModifyWakeLock(const nsAString& aTopic, hal::WakeLockControl aLockAdjust,
|
2013-02-14 20:41:30 +00:00
|
|
|
hal::WakeLockControl aHiddenAdjust,
|
2013-02-14 20:41:30 +00:00
|
|
|
uint64_t aProcessID = hal::CONTENT_PROCESS_ID_UNKNOWN);
|
2012-11-17 15:05:18 +00:00
|
|
|
|
2012-03-07 11:03:25 +00:00
|
|
|
/**
|
|
|
|
* Query the wake lock numbers of aTopic.
|
|
|
|
* @param aTopic lock topic
|
|
|
|
* @param aWakeLockInfo wake lock numbers
|
|
|
|
*/
|
|
|
|
void GetWakeLockInfo(const nsAString& aTopic,
|
|
|
|
hal::WakeLockInformation* aWakeLockInfo);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notify of a change in the wake lock state.
|
|
|
|
* @param aWakeLockInfo The new wake lock information.
|
|
|
|
*/
|
|
|
|
void NotifyWakeLockChange(const hal::WakeLockInformation& aWakeLockInfo);
|
|
|
|
|
2012-03-29 19:43:16 +00:00
|
|
|
/**
|
|
|
|
* Lock the screen orientation to the specific orientation.
|
Bug 1697647 - Add screen orientation lock api r=ipc-reviewers,mccr8,agi,smaug,jonalmeida
Previously, the screenOrientation.lock API was for Fennec and not supported for Fenix and multi-process use. The overall idea is to now allow apps to use the API through a delegate and make asynchronous calls to LockDeviceOrientation. This required replacing the existing code that returned a default false bool to calls that perform the requested orientation change and instead return a promise that contained either an allow or deny value.
Returning a promise instead of a bool involved changing the API calls from the C++ side to Java. The new general control flow of screenOrientation lock follows: an app calls C++ ScreenOrientation.lock() which eventually dispatches LockOrientationTask to resolve the pending orientation promise. Hal.cpp sends an IPC call to the content process and RecvLockScreenOrientation retrieves the current instance of geckoRuntime and calls the java side LockScreenOrientation. Apps must create a delegate and override onOrientationLock to set the requested orientation. In geckoview's testing, this is done with the android API setRequestedOrientation. Once a device orientation change has been triggered, native OnOrientationChange calls to NotifyScreenConfigurationChange, which notifies all observers and dispatches a change event to resolve the pending orientation promise.
Testing:
I used a demo on the GeckoView Example (https://usefulangle.com/demos/105/screen.html) to test locking to landscape orientation. This required a change to the GVE to show the app from recreating the whole thing on orientation change. In the example AndroidManifest xml file, `orientation` prevents restart when orientation changes.
The Junit/Kotlin tests were to verify that the expected orientation delegate was called with the expected new orientation value, in an orientation change, if the new orientation was the same as the current, and if the pre-lock conditions such as being fullscreen were not met.
A static preference `dom.screenorientation.allow-lock` was added to the dom group, since it affects the ui dom) and is currently turned off. C++ can access it through its mirrored variable dom_screenorientation_allow_lock (same name but with underscores). The junit tests turn the preference on and test the lock feature.
Reference:
Orientation constant values:
C++
1 ScreenOrientation_PortraitPrimary); - vertical with button at bottom
2 ScreenOrientation_PortraitSecondary); - vertical with button at top
4 ScreenOrientation_LandscapePrimary); - horizational w button right
8 ScreenOrientation_LandscapeSecondary); - horization button left
16 ScreenOrientation_Default);
Java
1 GeckoScreenOrientation.ScreenOrientation.PORTRAIT_PRIMARY.value
2 GeckoScreenOrientation.ScreenOrientation.PORTRAIT_SECONDARY.value
4 GeckoScreenOrientation.ScreenOrientation.LANDSCAPE_PRIMARY.value
8 GeckoScreenOrientation.ScreenOrientation.LANDSCAPE_SECONDARY.value
Java public API
0 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
1 Activitynfo.SCREEN_ORIENTATION_PORTRAIT
Android
1 ORIENTATION_PORTRAIT
2 ORIENTATION_LANDSCAPE
Differential Revision: https://phabricator.services.mozilla.com/D129427
2021-12-06 13:58:37 +00:00
|
|
|
* @return A promise indicating that the screen orientation has been locked.
|
2012-03-29 19:43:16 +00:00
|
|
|
*/
|
2022-03-02 03:48:14 +00:00
|
|
|
[[nodiscard]] RefPtr<GenericNonExclusivePromise> LockScreenOrientation(
|
|
|
|
const hal::ScreenOrientation& aOrientation);
|
2012-03-29 19:43:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Unlock the screen orientation.
|
|
|
|
*/
|
|
|
|
void UnlockScreenOrientation();
|
|
|
|
|
2012-08-05 05:09:39 +00:00
|
|
|
/**
|
2015-02-26 11:43:22 +00:00
|
|
|
* Set the priority of the given process.
|
2012-08-05 05:09:39 +00:00
|
|
|
*
|
|
|
|
* Exactly what this does will vary between platforms. On *nix we might give
|
|
|
|
* background processes higher nice values. On other platforms, we might
|
|
|
|
* ignore this call entirely.
|
|
|
|
*/
|
2017-05-26 15:50:17 +00:00
|
|
|
void SetProcessPriority(int aPid, hal::ProcessPriority aPriority);
|
2012-08-05 05:09:39 +00:00
|
|
|
|
2011-09-30 07:00:48 +00:00
|
|
|
} // namespace MOZ_HAL_NAMESPACE
|
|
|
|
} // namespace mozilla
|
2011-10-05 22:15:45 +00:00
|
|
|
|
|
|
|
#ifdef MOZ_DEFINED_HAL_NAMESPACE
|
|
|
|
# undef MOZ_DEFINED_HAL_NAMESPACE
|
|
|
|
# undef MOZ_HAL_NAMESPACE
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif // mozilla_Hal_h
|