Bug 1869647 - Mark hasStorageAccess.sub.https.window.html as intermittent after wpt...
[gecko.git] / hal / android / AndroidHal.cpp
blob3a7e1bf121e66963bb2fede57ac115f83fdb017f
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "Hal.h"
7 #include "HalImpl.h"
8 #include "WindowIdentifier.h"
9 #include "AndroidBridge.h"
10 #include "mozilla/dom/network/Constants.h"
11 #include "mozilla/java/GeckoAppShellWrappers.h"
12 #include "mozilla/java/GeckoRuntimeWrappers.h"
13 #include "mozilla/widget/ScreenManager.h"
14 #include "nsPIDOMWindow.h"
16 using namespace mozilla::dom;
17 using namespace mozilla::hal;
19 namespace mozilla::hal_impl {
21 void Vibrate(const nsTArray<uint32_t>& pattern, WindowIdentifier&&) {
22 // Ignore the WindowIdentifier parameter; it's here only because hal::Vibrate,
23 // hal_sandbox::Vibrate, and hal_impl::Vibrate all must have the same
24 // signature.
26 // Strangely enough, the Android Java API seems to treat vibrate([0]) as a
27 // nop. But we want to treat vibrate([0]) like CancelVibrate! (Note that we
28 // also need to treat vibrate([]) as a call to CancelVibrate.)
29 bool allZero = true;
30 for (uint32_t i = 0; i < pattern.Length(); i++) {
31 if (pattern[i] != 0) {
32 allZero = false;
33 break;
37 if (allZero) {
38 hal_impl::CancelVibrate(WindowIdentifier());
39 return;
42 AndroidBridge* b = AndroidBridge::Bridge();
43 if (!b) {
44 return;
47 b->Vibrate(pattern);
50 void CancelVibrate(WindowIdentifier&&) {
51 // Ignore WindowIdentifier parameter.
53 java::GeckoAppShell::CancelVibrate();
56 void EnableBatteryNotifications() {
57 java::GeckoAppShell::EnableBatteryNotifications();
60 void DisableBatteryNotifications() {
61 java::GeckoAppShell::DisableBatteryNotifications();
64 void GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo) {
65 AndroidBridge::Bridge()->GetCurrentBatteryInformation(aBatteryInfo);
68 void EnableNetworkNotifications() {
69 java::GeckoAppShell::EnableNetworkNotifications();
72 void DisableNetworkNotifications() {
73 java::GeckoAppShell::DisableNetworkNotifications();
76 void GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo) {
77 AndroidBridge::Bridge()->GetCurrentNetworkInformation(aNetworkInfo);
80 static bool IsSupportedScreenOrientation(hal::ScreenOrientation aOrientation) {
81 // The Android backend only supports these orientations.
82 static constexpr ScreenOrientation kSupportedOrientations[] = {
83 ScreenOrientation::PortraitPrimary,
84 ScreenOrientation::PortraitSecondary,
85 ScreenOrientation::PortraitPrimary | ScreenOrientation::PortraitSecondary,
86 ScreenOrientation::LandscapePrimary,
87 ScreenOrientation::LandscapeSecondary,
88 ScreenOrientation::LandscapePrimary |
89 ScreenOrientation::LandscapeSecondary,
90 ScreenOrientation::PortraitPrimary |
91 ScreenOrientation::PortraitSecondary |
92 ScreenOrientation::LandscapePrimary |
93 ScreenOrientation::LandscapeSecondary,
94 ScreenOrientation::Default,
96 for (auto supportedOrientation : kSupportedOrientations) {
97 if (aOrientation == supportedOrientation) {
98 return true;
101 return false;
104 RefPtr<GenericNonExclusivePromise> LockScreenOrientation(
105 const hal::ScreenOrientation& aOrientation) {
106 if (!IsSupportedScreenOrientation(aOrientation)) {
107 NS_WARNING("Unsupported screen orientation type");
108 return GenericNonExclusivePromise::CreateAndReject(
109 NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__);
112 java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
113 if (!runtime) {
114 return GenericNonExclusivePromise::CreateAndReject(NS_ERROR_DOM_ABORT_ERR,
115 __func__);
118 hal::ScreenOrientation orientation = [&aOrientation]() {
119 if (aOrientation == hal::ScreenOrientation::Default) {
120 // GeckoView only supports single monitor, so get primary screen data for
121 // natural orientation.
122 RefPtr<widget::Screen> screen =
123 widget::ScreenManager::GetSingleton().GetPrimaryScreen();
124 return screen->GetDefaultOrientationType();
126 return aOrientation;
127 }();
129 auto result = runtime->LockScreenOrientation(uint32_t(orientation));
130 auto geckoResult = java::GeckoResult::LocalRef(std::move(result));
131 return GenericNonExclusivePromise::FromGeckoResult(geckoResult)
132 ->Then(
133 GetCurrentSerialEventTarget(), __func__,
134 [](const GenericNonExclusivePromise::ResolveOrRejectValue& aValue) {
135 if (aValue.IsResolve()) {
136 if (aValue.ResolveValue()) {
137 return GenericNonExclusivePromise::CreateAndResolve(true,
138 __func__);
140 // Delegated orientation controller returns failure for
141 // lock.
142 return GenericNonExclusivePromise::CreateAndReject(
143 NS_ERROR_DOM_ABORT_ERR, __func__);
145 // Browser side doesn't implement orientation delegate.
146 return GenericNonExclusivePromise::CreateAndReject(
147 NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__);
151 void UnlockScreenOrientation() {
152 java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
153 if (runtime) {
154 runtime->UnlockScreenOrientation();
158 } // namespace mozilla::hal_impl