Bug 1750871 - run mochitest-remote on fission everywhere. r=releng-reviewers,aki
[gecko.git] / dom / base / WindowOrientationObserver.cpp
blob04c858956274c0a134048a3087f910434eb63fe5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=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
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "WindowOrientationObserver.h"
9 #include "nsGlobalWindow.h"
10 #include "mozilla/Hal.h"
12 using namespace mozilla::dom;
14 /**
15 * This class is used by nsGlobalWindowInner to implement window.orientation
16 * and window.onorientationchange. This class is defined in its own file
17 * because Hal.h pulls in windows.h and can't be included from
18 * nsGlobalWindow.cpp
20 WindowOrientationObserver::WindowOrientationObserver(
21 nsGlobalWindowInner* aGlobalWindow)
22 : mWindow(aGlobalWindow) {
23 MOZ_ASSERT(aGlobalWindow);
24 hal::RegisterScreenConfigurationObserver(this);
26 hal::ScreenConfiguration config;
27 hal::GetCurrentScreenConfiguration(&config);
28 mAngle = config.angle();
31 WindowOrientationObserver::~WindowOrientationObserver() {
32 hal::UnregisterScreenConfigurationObserver(this);
35 void WindowOrientationObserver::Notify(
36 const mozilla::hal::ScreenConfiguration& aConfiguration) {
37 uint16_t currentAngle = aConfiguration.angle();
38 if (mAngle != currentAngle && mWindow->IsCurrentInnerWindow()) {
39 mAngle = currentAngle;
40 mWindow->GetOuterWindow()->DispatchCustomEvent(u"orientationchange"_ns);
44 /* static */
45 int16_t WindowOrientationObserver::OrientationAngle() {
46 hal::ScreenConfiguration config;
47 hal::GetCurrentScreenConfiguration(&config);
48 int16_t angle = static_cast<int16_t>(config.angle());
49 // config.angle() returns 0, 90, 180 or 270.
50 // window.orientation returns -90, 0, 90 or 180.
51 return angle <= 180 ? angle : angle - 360;