Skip to content

Commit 51f5539

Browse files
committed
Bug 1754813 - Make Hal::ScreenOrientation an enum class. r=gsvelto,m_kato,geckoview-reviewers
This adds proper IPC validation too. Differential Revision: https://phabricator.services.mozilla.com/D138461
1 parent c722e22 commit 51f5539

File tree

14 files changed

+130
-99
lines changed

14 files changed

+130
-99
lines changed

docshell/base/BrowsingContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ already_AddRefed<BrowsingContext> BrowsingContext::CreateDetached(
400400
fields.mDefaultLoadFlags =
401401
inherit ? inherit->GetDefaultLoadFlags() : nsIRequest::LOAD_NORMAL;
402402

403-
fields.mOrientationLock = mozilla::hal::eScreenOrientation_None;
403+
fields.mOrientationLock = mozilla::hal::ScreenOrientation::None;
404404

405405
fields.mUseGlobalHistory = inherit ? inherit->GetUseGlobalHistory() : false;
406406

docshell/base/SyncedContextInlines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "mozilla/dom/ContentParent.h"
1313
#include "mozilla/dom/ContentChild.h"
1414
#include "nsReadableUtils.h"
15+
#include "mozilla/HalIPCUtils.h"
1516

1617
namespace mozilla {
1718
namespace dom {

docshell/base/nsDocShell.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4886,7 +4886,7 @@ void nsDocShell::ActivenessMaybeChanged() {
48864886
// Update orientation when the top-level browsing context becomes active.
48874887
if (isActive && mBrowsingContext->IsTop()) {
48884888
// We only care about the top-level browsing context.
4889-
uint16_t orientation = mBrowsingContext->GetOrientationLock();
4889+
auto orientation = mBrowsingContext->GetOrientationLock();
48904890
ScreenOrientation::UpdateActiveOrientationLock(orientation);
48914891
}
48924892

@@ -9459,13 +9459,13 @@ nsresult nsDocShell::InternalLoad(nsDocShellLoadState* aLoadState,
94599459
// lock the orientation of the document to the document's default
94609460
// orientation. We don't explicitly check for a top-level browsing context
94619461
// here because orientation is only set on top-level browsing contexts.
9462-
if (mBrowsingContext->GetOrientationLock() != hal::eScreenOrientation_None) {
9462+
if (mBrowsingContext->GetOrientationLock() != hal::ScreenOrientation::None) {
94639463
MOZ_ASSERT(mBrowsingContext->IsTop());
94649464
MOZ_ALWAYS_SUCCEEDS(
9465-
mBrowsingContext->SetOrientationLock(hal::eScreenOrientation_None));
9465+
mBrowsingContext->SetOrientationLock(hal::ScreenOrientation::None));
94669466
if (mBrowsingContext->IsActive()) {
94679467
ScreenOrientation::UpdateActiveOrientationLock(
9468-
hal::eScreenOrientation_None);
9468+
hal::ScreenOrientation::None);
94699469
}
94709470
}
94719471

dom/base/ScreenOrientation.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ NS_IMPL_RELEASE_INHERITED(ScreenOrientation, DOMEventTargetHelper)
3636
static OrientationType InternalOrientationToType(
3737
hal::ScreenOrientation aOrientation) {
3838
switch (aOrientation) {
39-
case hal::eScreenOrientation_PortraitPrimary:
39+
case hal::ScreenOrientation::PortraitPrimary:
4040
return OrientationType::Portrait_primary;
41-
case hal::eScreenOrientation_PortraitSecondary:
41+
case hal::ScreenOrientation::PortraitSecondary:
4242
return OrientationType::Portrait_secondary;
43-
case hal::eScreenOrientation_LandscapePrimary:
43+
case hal::ScreenOrientation::LandscapePrimary:
4444
return OrientationType::Landscape_primary;
45-
case hal::eScreenOrientation_LandscapeSecondary:
45+
case hal::ScreenOrientation::LandscapeSecondary:
4646
return OrientationType::Landscape_secondary;
4747
default:
4848
MOZ_CRASH("Bad aOrientation value");
@@ -53,13 +53,13 @@ static hal::ScreenOrientation OrientationTypeToInternal(
5353
OrientationType aOrientation) {
5454
switch (aOrientation) {
5555
case OrientationType::Portrait_primary:
56-
return hal::eScreenOrientation_PortraitPrimary;
56+
return hal::ScreenOrientation::PortraitPrimary;
5757
case OrientationType::Portrait_secondary:
58-
return hal::eScreenOrientation_PortraitSecondary;
58+
return hal::ScreenOrientation::PortraitSecondary;
5959
case OrientationType::Landscape_primary:
60-
return hal::eScreenOrientation_LandscapePrimary;
60+
return hal::ScreenOrientation::LandscapePrimary;
6161
case OrientationType::Landscape_secondary:
62-
return hal::eScreenOrientation_LandscapeSecondary;
62+
return hal::ScreenOrientation::LandscapeSecondary;
6363
default:
6464
MOZ_CRASH("Bad aOrientation value");
6565
}
@@ -154,7 +154,7 @@ ScreenOrientation::LockOrientationTask::~LockOrientationTask() = default;
154154

155155
bool ScreenOrientation::LockOrientationTask::OrientationLockContains(
156156
OrientationType aOrientationType) {
157-
return mOrientationLock & OrientationTypeToInternal(aOrientationType);
157+
return bool(mOrientationLock & OrientationTypeToInternal(aOrientationType));
158158
}
159159

160160
NS_IMETHODIMP
@@ -179,7 +179,7 @@ ScreenOrientation::LockOrientationTask::Run() {
179179
return NS_OK;
180180
}
181181

182-
if (mOrientationLock == hal::eScreenOrientation_None) {
182+
if (mOrientationLock == hal::ScreenOrientation::None) {
183183
mScreenOrientation->UnlockDeviceOrientation();
184184
mPromise->MaybeResolveWithUndefined();
185185
mDocument->ClearOrientationPendingPromise();
@@ -211,7 +211,7 @@ ScreenOrientation::LockOrientationTask::Run() {
211211

212212
BrowsingContext* bc = mDocument->GetBrowsingContext();
213213
if (OrientationLockContains(bc->GetCurrentOrientationType()) ||
214-
(mOrientationLock == hal::eScreenOrientation_Default &&
214+
(mOrientationLock == hal::ScreenOrientation::Default &&
215215
bc->GetCurrentOrientationAngle() == 0)) {
216216
// Orientation lock will not cause an orientation change.
217217
mPromise->MaybeResolveWithUndefined();
@@ -223,37 +223,37 @@ ScreenOrientation::LockOrientationTask::Run() {
223223

224224
already_AddRefed<Promise> ScreenOrientation::Lock(
225225
OrientationLockType aOrientation, ErrorResult& aRv) {
226-
hal::ScreenOrientation orientation = hal::eScreenOrientation_None;
226+
hal::ScreenOrientation orientation = hal::ScreenOrientation::None;
227227

228228
switch (aOrientation) {
229229
case OrientationLockType::Any:
230-
orientation = hal::eScreenOrientation_PortraitPrimary |
231-
hal::eScreenOrientation_PortraitSecondary |
232-
hal::eScreenOrientation_LandscapePrimary |
233-
hal::eScreenOrientation_LandscapeSecondary;
230+
orientation = hal::ScreenOrientation::PortraitPrimary |
231+
hal::ScreenOrientation::PortraitSecondary |
232+
hal::ScreenOrientation::LandscapePrimary |
233+
hal::ScreenOrientation::LandscapeSecondary;
234234
break;
235235
case OrientationLockType::Natural:
236-
orientation |= hal::eScreenOrientation_Default;
236+
orientation |= hal::ScreenOrientation::Default;
237237
break;
238238
case OrientationLockType::Landscape:
239-
orientation = hal::eScreenOrientation_LandscapePrimary |
240-
hal::eScreenOrientation_LandscapeSecondary;
239+
orientation = hal::ScreenOrientation::LandscapePrimary |
240+
hal::ScreenOrientation::LandscapeSecondary;
241241
break;
242242
case OrientationLockType::Portrait:
243-
orientation = hal::eScreenOrientation_PortraitPrimary |
244-
hal::eScreenOrientation_PortraitSecondary;
243+
orientation = hal::ScreenOrientation::PortraitPrimary |
244+
hal::ScreenOrientation::PortraitSecondary;
245245
break;
246246
case OrientationLockType::Portrait_primary:
247-
orientation = hal::eScreenOrientation_PortraitPrimary;
247+
orientation = hal::ScreenOrientation::PortraitPrimary;
248248
break;
249249
case OrientationLockType::Portrait_secondary:
250-
orientation = hal::eScreenOrientation_PortraitSecondary;
250+
orientation = hal::ScreenOrientation::PortraitSecondary;
251251
break;
252252
case OrientationLockType::Landscape_primary:
253-
orientation = hal::eScreenOrientation_LandscapePrimary;
253+
orientation = hal::ScreenOrientation::LandscapePrimary;
254254
break;
255255
case OrientationLockType::Landscape_secondary:
256-
orientation = hal::eScreenOrientation_LandscapeSecondary;
256+
orientation = hal::ScreenOrientation::LandscapeSecondary;
257257
break;
258258
default:
259259
NS_WARNING("Unexpected orientation type");
@@ -401,7 +401,7 @@ RefPtr<MozPromise<bool, bool, false>> ScreenOrientation::LockDeviceOrientation(
401401
}
402402

403403
void ScreenOrientation::Unlock(ErrorResult& aRv) {
404-
RefPtr<Promise> p = LockInternal(hal::eScreenOrientation_None, aRv);
404+
RefPtr<Promise> p = LockInternal(hal::ScreenOrientation::None, aRv);
405405
}
406406

407407
void ScreenOrientation::UnlockDeviceOrientation() {
@@ -517,10 +517,10 @@ void ScreenOrientation::Notify(const hal::ScreenConfiguration& aConfiguration) {
517517
}
518518

519519
hal::ScreenOrientation orientation = aConfiguration.orientation();
520-
if (orientation != hal::eScreenOrientation_PortraitPrimary &&
521-
orientation != hal::eScreenOrientation_PortraitSecondary &&
522-
orientation != hal::eScreenOrientation_LandscapePrimary &&
523-
orientation != hal::eScreenOrientation_LandscapeSecondary) {
520+
if (orientation != hal::ScreenOrientation::PortraitPrimary &&
521+
orientation != hal::ScreenOrientation::PortraitSecondary &&
522+
orientation != hal::ScreenOrientation::LandscapePrimary &&
523+
orientation != hal::ScreenOrientation::LandscapeSecondary) {
524524
// The platform may notify of some other values from
525525
// an orientation lock, but we only care about real
526526
// changes to screen orientation which result in one of
@@ -559,7 +559,7 @@ void ScreenOrientation::Notify(const hal::ScreenConfiguration& aConfiguration) {
559559

560560
void ScreenOrientation::UpdateActiveOrientationLock(
561561
hal::ScreenOrientation aOrientation) {
562-
if (aOrientation == hal::eScreenOrientation_None) {
562+
if (aOrientation == hal::ScreenOrientation::None) {
563563
hal::UnlockScreenOrientation();
564564
} else {
565565
hal::LockScreenOrientation(aOrientation)

gfx/layers/ipc/LayersMessages.ipdlh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ using mozilla::TimeDuration from "mozilla/TimeStamp.h";
2828
using class mozilla::TimeStamp from "mozilla/TimeStamp.h";
2929
using mozilla::ScreenRotation from "mozilla/WidgetUtils.h";
3030
using nsCSSPropertyID from "nsCSSPropertyID.h";
31-
using hal::ScreenOrientation from "mozilla/HalScreenConfiguration.h";
31+
using hal::ScreenOrientation from "mozilla/HalIPCUtils.h";
3232
using struct mozilla::layers::TextureInfo from "mozilla/layers/CompositorTypes.h";
3333
using mozilla::CSSPoint from "Units.h";
3434
using mozilla::CSSRect from "Units.h";

hal/HalIPCUtils.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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/. */
6+
7+
#ifndef mozilla_HalIPCUtils_h
8+
#define mozilla_HalIPCUtils_h
9+
10+
#include "HalScreenConfiguration.h"
11+
12+
#include "ipc/EnumSerializer.h"
13+
14+
namespace IPC {
15+
16+
template <>
17+
struct ParamTraits<mozilla::hal::ScreenOrientation>
18+
: public BitFlagsEnumSerializer<mozilla::hal::ScreenOrientation,
19+
mozilla::hal::kAllScreenOrientationBits> {};
20+
21+
} // namespace IPC
22+
23+
#endif

hal/HalScreenConfiguration.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,30 @@
88
#define mozilla_HalScreenConfiguration_h
99

1010
#include "mozilla/Observer.h"
11+
#include "mozilla/TypedEnumBits.h"
1112

12-
namespace mozilla {
13-
namespace hal {
13+
namespace mozilla::hal {
1414

1515
// Make sure that any change to ScreenOrientation values are also made in
1616
// mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoScreenOrientation.java
17-
typedef uint32_t ScreenOrientation;
17+
enum class ScreenOrientation : uint32_t {
18+
None = 0,
19+
PortraitPrimary = 1u << 0,
20+
PortraitSecondary = 1u << 1,
21+
LandscapePrimary = 1u << 2,
22+
LandscapeSecondary = 1u << 3,
23+
// Default will use the natural orientation for the device, it could be
24+
// PortraitPrimary or LandscapePrimary depends on display resolution
25+
Default = 1u << 4,
26+
};
1827

19-
static const ScreenOrientation eScreenOrientation_None = 0;
20-
static const ScreenOrientation eScreenOrientation_PortraitPrimary = 1u << 0;
21-
static const ScreenOrientation eScreenOrientation_PortraitSecondary = 1u << 1;
22-
static const ScreenOrientation eScreenOrientation_LandscapePrimary = 1u << 2;
23-
static const ScreenOrientation eScreenOrientation_LandscapeSecondary = 1u << 3;
24-
// eScreenOrientation_Default will use the natural orientation for the deivce,
25-
// it could be PortraitPrimary or LandscapePrimary depends on display resolution
26-
static const ScreenOrientation eScreenOrientation_Default = 1u << 4;
28+
constexpr auto kAllScreenOrientationBits = ScreenOrientation((1 << 5) - 1);
29+
30+
MOZ_MAKE_ENUM_CLASS_BITWISE_OPERATORS(ScreenOrientation);
2731

2832
class ScreenConfiguration;
29-
typedef Observer<ScreenConfiguration> ScreenConfigurationObserver;
33+
using ScreenConfigurationObserver = Observer<ScreenConfiguration>;
3034

31-
} // namespace hal
32-
} // namespace mozilla
35+
} // namespace mozilla::hal
3336

3437
#endif // mozilla_HalScreenConfiguration_h

hal/android/AndroidHal.cpp

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@
1616
using namespace mozilla::dom;
1717
using namespace mozilla::hal;
1818

19-
namespace java = mozilla::java;
20-
21-
namespace mozilla {
22-
namespace hal_impl {
19+
namespace mozilla::hal_impl {
2320

2421
void Vibrate(const nsTArray<uint32_t>& pattern, WindowIdentifier&&) {
2522
// Ignore the WindowIdentifier parameter; it's here only because hal::Vibrate,
@@ -102,47 +99,56 @@ void GetCurrentScreenConfiguration(ScreenConfiguration* aScreenConfiguration) {
10299
aScreenConfiguration->angle() = bridge->GetScreenAngle();
103100
}
104101

102+
static bool IsSupportedScreenOrientation(hal::ScreenOrientation aOrientation) {
103+
// The Android backend only supports these orientations.
104+
static constexpr ScreenOrientation kSupportedOrientations[] = {
105+
ScreenOrientation::PortraitPrimary,
106+
ScreenOrientation::PortraitSecondary,
107+
ScreenOrientation::PortraitPrimary | ScreenOrientation::PortraitSecondary,
108+
ScreenOrientation::LandscapePrimary,
109+
ScreenOrientation::LandscapeSecondary,
110+
ScreenOrientation::LandscapePrimary |
111+
ScreenOrientation::LandscapeSecondary,
112+
ScreenOrientation::PortraitPrimary |
113+
ScreenOrientation::PortraitSecondary |
114+
ScreenOrientation::LandscapePrimary |
115+
ScreenOrientation::LandscapeSecondary,
116+
ScreenOrientation::Default,
117+
};
118+
for (auto supportedOrientation : kSupportedOrientations) {
119+
if (aOrientation == supportedOrientation) {
120+
return true;
121+
}
122+
}
123+
return false;
124+
}
125+
105126
RefPtr<MozPromise<bool, bool, false>> LockScreenOrientation(
106127
const hal::ScreenOrientation& aOrientation) {
107-
switch (aOrientation) {
108-
// The Android backend only supports these orientations.
109-
case eScreenOrientation_PortraitPrimary:
110-
case eScreenOrientation_PortraitSecondary:
111-
case eScreenOrientation_PortraitPrimary |
112-
eScreenOrientation_PortraitSecondary:
113-
case eScreenOrientation_LandscapePrimary:
114-
case eScreenOrientation_LandscapeSecondary:
115-
case eScreenOrientation_LandscapePrimary |
116-
eScreenOrientation_LandscapeSecondary:
117-
case eScreenOrientation_PortraitPrimary |
118-
eScreenOrientation_PortraitSecondary |
119-
eScreenOrientation_LandscapePrimary |
120-
eScreenOrientation_LandscapeSecondary:
121-
case eScreenOrientation_Default: {
122-
java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
123-
if (runtime != NULL) {
124-
auto result = runtime->LockScreenOrientation(aOrientation);
125-
auto geckoResult = java::GeckoResult::LocalRef(std::move(result));
126-
return geckoResult
127-
? MozPromise<bool, bool, false>::FromGeckoResult(geckoResult)
128-
: MozPromise<bool, bool, false>::CreateAndReject(false,
129-
__func__);
130-
} else {
131-
return MozPromise<bool, bool, false>::CreateAndReject(false, __func__);
132-
}
133-
}
134-
default:
135-
NS_WARNING("Unsupported screen orientation type");
136-
return MozPromise<bool, bool, false>::CreateAndReject(false, __func__);
128+
using LockPromise = MozPromise<bool, bool, false>;
129+
130+
if (!IsSupportedScreenOrientation(aOrientation)) {
131+
NS_WARNING("Unsupported screen orientation type");
132+
return LockPromise::CreateAndReject(false, __func__);
133+
}
134+
135+
java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
136+
if (!runtime) {
137+
return LockPromise::CreateAndReject(false, __func__);
138+
}
139+
auto result = runtime->LockScreenOrientation(uint32_t(aOrientation));
140+
auto geckoResult = java::GeckoResult::LocalRef(std::move(result));
141+
if (!geckoResult) {
142+
return LockPromise::CreateAndReject(false, __func__);
137143
}
144+
return LockPromise::FromGeckoResult(geckoResult);
138145
}
139146

140147
void UnlockScreenOrientation() {
141148
java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
142-
if (runtime != NULL) {
149+
if (runtime) {
143150
runtime->UnlockScreenOrientation();
144151
}
145152
}
146153

147-
} // namespace hal_impl
148-
} // namespace mozilla
154+
} // namespace mozilla::hal_impl

hal/fallback/FallbackScreenConfiguration.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ inline void GetCurrentScreenConfiguration(
2020
aScreenConfiguration->orientation() =
2121
aScreenConfiguration->rect().Width() >=
2222
aScreenConfiguration->rect().Height()
23-
? hal::eScreenOrientation_LandscapePrimary
24-
: hal::eScreenOrientation_PortraitPrimary;
23+
? hal::ScreenOrientation::LandscapePrimary
24+
: hal::ScreenOrientation::PortraitPrimary;
2525
}
2626

2727
} // namespace fallback

hal/moz.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ EXPORTS.mozilla += [
1111
"Hal.h",
1212
"HalBatteryInformation.h",
1313
"HalImpl.h",
14+
"HalIPCUtils.h",
1415
"HalNetworkInformation.h",
1516
"HalSandbox.h",
1617
"HalScreenConfiguration.h",

0 commit comments

Comments
 (0)