Backed out 2 changesets (bug 1900622) for causing Bug 1908553 and ktlint failure...
[gecko.git] / dom / geolocation / GeolocationPosition.cpp
blob19ca68525198ffa953e705c34ef66d02c5547240
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 "mozilla/dom/GeolocationPosition.h"
8 #include "mozilla/dom/GeolocationCoordinates.h"
10 #include "mozilla/FloatingPoint.h"
11 #include "mozilla/dom/GeolocationPositionBinding.h"
13 using mozilla::EqualOrBothNaN;
15 // NaN() is a more convenient function name.
16 inline double NaN() { return mozilla::UnspecifiedNaN<double>(); }
18 ////////////////////////////////////////////////////
19 // nsGeoPositionCoords
20 ////////////////////////////////////////////////////
21 nsGeoPositionCoords::nsGeoPositionCoords(double aLat, double aLong, double aAlt,
22 double aHError, double aVError,
23 double aHeading, double aSpeed)
24 : mLat(aLat),
25 mLong(aLong),
26 mAlt(aAlt),
27 mHError((aHError >= 0) ? aHError : 0)
28 // altitudeAccuracy without an altitude doesn't make any sense.
30 mVError((aVError >= 0 && !std::isnan(aAlt)) ? aVError : NaN())
31 // If the hosting device is stationary (i.e. the value of the speed
32 // attribute is 0), then the value of the heading attribute must be NaN
33 // (or null).
35 mHeading((aHeading >= 0 && aHeading < 360 && aSpeed > 0) ? aHeading
36 : NaN()),
37 mSpeed(aSpeed >= 0 ? aSpeed : NaN()) {
38 // Sanity check the location provider's results in debug builds. If the
39 // location provider is returning bogus results, we'd like to know, but
40 // we prefer to return some position data to JavaScript over a
41 // POSITION_UNAVAILABLE error.
42 MOZ_ASSERT(aLat >= -90 && aLat <= 90);
43 MOZ_ASSERT(aLong >= -180 && aLong <= 180);
44 MOZ_ASSERT(!(aLat == 0 && aLong == 0)); // valid but probably a bug
46 MOZ_ASSERT(EqualOrBothNaN(mAlt, aAlt));
47 MOZ_ASSERT(mHError == aHError);
48 MOZ_ASSERT(EqualOrBothNaN(mVError, aVError));
49 MOZ_ASSERT(EqualOrBothNaN(mHeading, aHeading));
50 MOZ_ASSERT(EqualOrBothNaN(mSpeed, aSpeed));
53 nsGeoPositionCoords::~nsGeoPositionCoords() = default;
55 NS_INTERFACE_MAP_BEGIN(nsGeoPositionCoords)
56 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPositionCoords)
57 NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPositionCoords)
58 NS_INTERFACE_MAP_END
60 NS_IMPL_ADDREF(nsGeoPositionCoords)
61 NS_IMPL_RELEASE(nsGeoPositionCoords)
63 NS_IMETHODIMP
64 nsGeoPositionCoords::GetLatitude(double* aLatitude) {
65 *aLatitude = mLat;
66 return NS_OK;
69 NS_IMETHODIMP
70 nsGeoPositionCoords::GetLongitude(double* aLongitude) {
71 *aLongitude = mLong;
72 return NS_OK;
75 NS_IMETHODIMP
76 nsGeoPositionCoords::GetAltitude(double* aAltitude) {
77 *aAltitude = mAlt;
78 return NS_OK;
81 NS_IMETHODIMP
82 nsGeoPositionCoords::GetAccuracy(double* aAccuracy) {
83 *aAccuracy = mHError;
84 return NS_OK;
87 NS_IMETHODIMP
88 nsGeoPositionCoords::GetAltitudeAccuracy(double* aAltitudeAccuracy) {
89 *aAltitudeAccuracy = mVError;
90 return NS_OK;
93 NS_IMETHODIMP
94 nsGeoPositionCoords::GetHeading(double* aHeading) {
95 *aHeading = mHeading;
96 return NS_OK;
99 NS_IMETHODIMP
100 nsGeoPositionCoords::GetSpeed(double* aSpeed) {
101 *aSpeed = mSpeed;
102 return NS_OK;
105 ////////////////////////////////////////////////////
106 // nsGeoPosition
107 ////////////////////////////////////////////////////
109 nsGeoPosition::nsGeoPosition(double aLat, double aLong, double aAlt,
110 double aHError, double aVError, double aHeading,
111 double aSpeed, EpochTimeStamp aTimestamp)
112 : mTimestamp(aTimestamp) {
113 mCoords = new nsGeoPositionCoords(aLat, aLong, aAlt, aHError, aVError,
114 aHeading, aSpeed);
117 nsGeoPosition::nsGeoPosition(nsIDOMGeoPositionCoords* aCoords,
118 EpochTimeStamp aTimestamp)
119 : mTimestamp(aTimestamp), mCoords(aCoords) {}
121 nsGeoPosition::~nsGeoPosition() = default;
123 NS_INTERFACE_MAP_BEGIN(nsGeoPosition)
124 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPosition)
125 NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPosition)
126 NS_INTERFACE_MAP_END
128 NS_IMPL_ADDREF(nsGeoPosition)
129 NS_IMPL_RELEASE(nsGeoPosition)
131 NS_IMETHODIMP
132 nsGeoPosition::GetTimestamp(EpochTimeStamp* aTimestamp) {
133 *aTimestamp = mTimestamp;
134 return NS_OK;
137 NS_IMETHODIMP
138 nsGeoPosition::GetCoords(nsIDOMGeoPositionCoords** aCoords) {
139 NS_IF_ADDREF(*aCoords = mCoords);
140 return NS_OK;
143 namespace mozilla::dom {
145 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GeolocationPosition, mParent,
146 mCoordinates)
147 NS_IMPL_CYCLE_COLLECTING_ADDREF(GeolocationPosition)
148 NS_IMPL_CYCLE_COLLECTING_RELEASE(GeolocationPosition)
149 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GeolocationPosition)
150 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
151 NS_INTERFACE_MAP_ENTRY(nsISupports)
152 NS_INTERFACE_MAP_END
154 GeolocationPosition::GeolocationPosition(nsISupports* aParent,
155 nsIDOMGeoPosition* aGeoPosition)
156 : mParent(aParent), mGeoPosition(aGeoPosition) {}
158 GeolocationPosition::~GeolocationPosition() = default;
160 nsISupports* GeolocationPosition::GetParentObject() const { return mParent; }
162 JSObject* GeolocationPosition::WrapObject(JSContext* aCx,
163 JS::Handle<JSObject*> aGivenProto) {
164 return GeolocationPosition_Binding::Wrap(aCx, this, aGivenProto);
167 GeolocationCoordinates* GeolocationPosition::Coords() {
168 if (!mCoordinates) {
169 nsCOMPtr<nsIDOMGeoPositionCoords> coords;
170 mGeoPosition->GetCoords(getter_AddRefs(coords));
171 MOZ_ASSERT(coords, "coords should not be null");
173 mCoordinates = new GeolocationCoordinates(this, coords);
176 return mCoordinates;
179 uint64_t GeolocationPosition::Timestamp() const {
180 uint64_t rv;
182 mGeoPosition->GetTimestamp(&rv);
183 return rv;
186 } // namespace mozilla::dom