Bug 1806483 - Enable TSAN cppunittests by default. r=jmaher
[gecko.git] / dom / geolocation / GeolocationPosition.cpp
blob1a984e96de991fc26a918b067f2cfcbd5f2cb8c0
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;
14 using mozilla::IsNaN;
16 // NaN() is a more convenient function name.
17 inline double NaN() { return mozilla::UnspecifiedNaN<double>(); }
19 ////////////////////////////////////////////////////
20 // nsGeoPositionCoords
21 ////////////////////////////////////////////////////
22 nsGeoPositionCoords::nsGeoPositionCoords(double aLat, double aLong, double aAlt,
23 double aHError, double aVError,
24 double aHeading, double aSpeed)
25 : mLat(aLat),
26 mLong(aLong),
27 mAlt(aAlt),
28 mHError((aHError >= 0) ? aHError : 0)
29 // altitudeAccuracy without an altitude doesn't make any sense.
31 mVError((aVError >= 0 && !IsNaN(aAlt)) ? aVError : NaN())
32 // If the hosting device is stationary (i.e. the value of the speed
33 // attribute is 0), then the value of the heading attribute must be NaN
34 // (or null).
36 mHeading((aHeading >= 0 && aHeading < 360 && aSpeed > 0) ? aHeading
37 : NaN()),
38 mSpeed(aSpeed >= 0 ? aSpeed : NaN()) {
39 // Sanity check the location provider's results in debug builds. If the
40 // location provider is returning bogus results, we'd like to know, but
41 // we prefer to return some position data to JavaScript over a
42 // POSITION_UNAVAILABLE error.
43 MOZ_ASSERT(aLat >= -90 && aLat <= 90);
44 MOZ_ASSERT(aLong >= -180 && aLong <= 180);
45 MOZ_ASSERT(!(aLat == 0 && aLong == 0)); // valid but probably a bug
47 MOZ_ASSERT(EqualOrBothNaN(mAlt, aAlt));
48 MOZ_ASSERT(mHError == aHError);
49 MOZ_ASSERT(EqualOrBothNaN(mVError, aVError));
50 MOZ_ASSERT(EqualOrBothNaN(mHeading, aHeading));
51 MOZ_ASSERT(EqualOrBothNaN(mSpeed, aSpeed));
54 nsGeoPositionCoords::~nsGeoPositionCoords() = default;
56 NS_INTERFACE_MAP_BEGIN(nsGeoPositionCoords)
57 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPositionCoords)
58 NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPositionCoords)
59 NS_INTERFACE_MAP_END
61 NS_IMPL_ADDREF(nsGeoPositionCoords)
62 NS_IMPL_RELEASE(nsGeoPositionCoords)
64 NS_IMETHODIMP
65 nsGeoPositionCoords::GetLatitude(double* aLatitude) {
66 *aLatitude = mLat;
67 return NS_OK;
70 NS_IMETHODIMP
71 nsGeoPositionCoords::GetLongitude(double* aLongitude) {
72 *aLongitude = mLong;
73 return NS_OK;
76 NS_IMETHODIMP
77 nsGeoPositionCoords::GetAltitude(double* aAltitude) {
78 *aAltitude = mAlt;
79 return NS_OK;
82 NS_IMETHODIMP
83 nsGeoPositionCoords::GetAccuracy(double* aAccuracy) {
84 *aAccuracy = mHError;
85 return NS_OK;
88 NS_IMETHODIMP
89 nsGeoPositionCoords::GetAltitudeAccuracy(double* aAltitudeAccuracy) {
90 *aAltitudeAccuracy = mVError;
91 return NS_OK;
94 NS_IMETHODIMP
95 nsGeoPositionCoords::GetHeading(double* aHeading) {
96 *aHeading = mHeading;
97 return NS_OK;
100 NS_IMETHODIMP
101 nsGeoPositionCoords::GetSpeed(double* aSpeed) {
102 *aSpeed = mSpeed;
103 return NS_OK;
106 ////////////////////////////////////////////////////
107 // nsGeoPosition
108 ////////////////////////////////////////////////////
110 nsGeoPosition::nsGeoPosition(double aLat, double aLong, double aAlt,
111 double aHError, double aVError, double aHeading,
112 double aSpeed, EpochTimeStamp aTimestamp)
113 : mTimestamp(aTimestamp) {
114 mCoords = new nsGeoPositionCoords(aLat, aLong, aAlt, aHError, aVError,
115 aHeading, aSpeed);
118 nsGeoPosition::nsGeoPosition(nsIDOMGeoPositionCoords* aCoords,
119 EpochTimeStamp aTimestamp)
120 : mTimestamp(aTimestamp), mCoords(aCoords) {}
122 nsGeoPosition::~nsGeoPosition() = default;
124 NS_INTERFACE_MAP_BEGIN(nsGeoPosition)
125 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPosition)
126 NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPosition)
127 NS_INTERFACE_MAP_END
129 NS_IMPL_ADDREF(nsGeoPosition)
130 NS_IMPL_RELEASE(nsGeoPosition)
132 NS_IMETHODIMP
133 nsGeoPosition::GetTimestamp(EpochTimeStamp* aTimestamp) {
134 *aTimestamp = mTimestamp;
135 return NS_OK;
138 NS_IMETHODIMP
139 nsGeoPosition::GetCoords(nsIDOMGeoPositionCoords** aCoords) {
140 NS_IF_ADDREF(*aCoords = mCoords);
141 return NS_OK;
144 namespace mozilla::dom {
146 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GeolocationPosition, mParent,
147 mCoordinates)
148 NS_IMPL_CYCLE_COLLECTING_ADDREF(GeolocationPosition)
149 NS_IMPL_CYCLE_COLLECTING_RELEASE(GeolocationPosition)
150 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GeolocationPosition)
151 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
152 NS_INTERFACE_MAP_ENTRY(nsISupports)
153 NS_INTERFACE_MAP_END
155 GeolocationPosition::GeolocationPosition(nsISupports* aParent,
156 nsIDOMGeoPosition* aGeoPosition)
157 : mParent(aParent), mGeoPosition(aGeoPosition) {}
159 GeolocationPosition::~GeolocationPosition() = default;
161 nsISupports* GeolocationPosition::GetParentObject() const { return mParent; }
163 JSObject* GeolocationPosition::WrapObject(JSContext* aCx,
164 JS::Handle<JSObject*> aGivenProto) {
165 return GeolocationPosition_Binding::Wrap(aCx, this, aGivenProto);
168 GeolocationCoordinates* GeolocationPosition::Coords() {
169 if (!mCoordinates) {
170 nsCOMPtr<nsIDOMGeoPositionCoords> coords;
171 mGeoPosition->GetCoords(getter_AddRefs(coords));
172 MOZ_ASSERT(coords, "coords should not be null");
174 mCoordinates = new GeolocationCoordinates(this, coords);
177 return mCoordinates;
180 uint64_t GeolocationPosition::Timestamp() const {
181 uint64_t rv;
183 mGeoPosition->GetTimestamp(&rv);
184 return rv;
187 } // namespace mozilla::dom