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/. */
6
7 #include "mozilla/dom/GeolocationPosition.h"
8 #include "mozilla/dom/GeolocationCoordinates.h"
9
10 #include "mozilla/FloatingPoint.h"
11 #include "mozilla/dom/GeolocationPositionBinding.h"
12 #include "mozilla/dom/GeolocationCoordinatesBinding.h"
13
14 using mozilla::EqualOrBothNaN;
15 using mozilla::IsNaN;
16
17 // NaN() is a more convenient function name.
NaN()18 inline double NaN() { return mozilla::UnspecifiedNaN<double>(); }
19
20 ////////////////////////////////////////////////////
21 // nsGeoPositionCoords
22 ////////////////////////////////////////////////////
nsGeoPositionCoords(double aLat,double aLong,double aAlt,double aHError,double aVError,double aHeading,double aSpeed)23 nsGeoPositionCoords::nsGeoPositionCoords(double aLat, double aLong, double aAlt,
24 double aHError, double aVError,
25 double aHeading, double aSpeed)
26 : mLat(aLat),
27 mLong(aLong),
28 mAlt(aAlt),
29 mHError((aHError >= 0) ? aHError : 0)
30 // altitudeAccuracy without an altitude doesn't make any sense.
31 ,
32 mVError((aVError >= 0 && !IsNaN(aAlt)) ? aVError : NaN())
33 // If the hosting device is stationary (i.e. the value of the speed
34 // attribute is 0), then the value of the heading attribute must be NaN
35 // (or null).
36 ,
37 mHeading((aHeading >= 0 && aHeading < 360 && aSpeed > 0) ? aHeading
38 : NaN()),
39 mSpeed(aSpeed >= 0 ? aSpeed : NaN()) {
40 // Sanity check the location provider's results in debug builds. If the
41 // location provider is returning bogus results, we'd like to know, but
42 // we prefer to return some position data to JavaScript over a
43 // POSITION_UNAVAILABLE error.
44 MOZ_ASSERT(aLat >= -90 && aLat <= 90);
45 MOZ_ASSERT(aLong >= -180 && aLong <= 180);
46 MOZ_ASSERT(!(aLat == 0 && aLong == 0)); // valid but probably a bug
47
48 MOZ_ASSERT(EqualOrBothNaN(mAlt, aAlt));
49 MOZ_ASSERT(mHError == aHError);
50 MOZ_ASSERT(EqualOrBothNaN(mVError, aVError));
51 MOZ_ASSERT(EqualOrBothNaN(mHeading, aHeading));
52 MOZ_ASSERT(EqualOrBothNaN(mSpeed, aSpeed));
53 }
54
55 nsGeoPositionCoords::~nsGeoPositionCoords() = default;
56
57 NS_INTERFACE_MAP_BEGIN(nsGeoPositionCoords)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports,nsIDOMGeoPositionCoords)58 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPositionCoords)
59 NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPositionCoords)
60 NS_INTERFACE_MAP_END
61
62 NS_IMPL_ADDREF(nsGeoPositionCoords)
63 NS_IMPL_RELEASE(nsGeoPositionCoords)
64
65 NS_IMETHODIMP
66 nsGeoPositionCoords::GetLatitude(double* aLatitude) {
67 *aLatitude = mLat;
68 return NS_OK;
69 }
70
71 NS_IMETHODIMP
GetLongitude(double * aLongitude)72 nsGeoPositionCoords::GetLongitude(double* aLongitude) {
73 *aLongitude = mLong;
74 return NS_OK;
75 }
76
77 NS_IMETHODIMP
GetAltitude(double * aAltitude)78 nsGeoPositionCoords::GetAltitude(double* aAltitude) {
79 *aAltitude = mAlt;
80 return NS_OK;
81 }
82
83 NS_IMETHODIMP
GetAccuracy(double * aAccuracy)84 nsGeoPositionCoords::GetAccuracy(double* aAccuracy) {
85 *aAccuracy = mHError;
86 return NS_OK;
87 }
88
89 NS_IMETHODIMP
GetAltitudeAccuracy(double * aAltitudeAccuracy)90 nsGeoPositionCoords::GetAltitudeAccuracy(double* aAltitudeAccuracy) {
91 *aAltitudeAccuracy = mVError;
92 return NS_OK;
93 }
94
95 NS_IMETHODIMP
GetHeading(double * aHeading)96 nsGeoPositionCoords::GetHeading(double* aHeading) {
97 *aHeading = mHeading;
98 return NS_OK;
99 }
100
101 NS_IMETHODIMP
GetSpeed(double * aSpeed)102 nsGeoPositionCoords::GetSpeed(double* aSpeed) {
103 *aSpeed = mSpeed;
104 return NS_OK;
105 }
106
107 ////////////////////////////////////////////////////
108 // nsGeoPosition
109 ////////////////////////////////////////////////////
110
nsGeoPosition(double aLat,double aLong,double aAlt,double aHError,double aVError,double aHeading,double aSpeed,DOMTimeStamp aTimestamp)111 nsGeoPosition::nsGeoPosition(double aLat, double aLong, double aAlt,
112 double aHError, double aVError, double aHeading,
113 double aSpeed, DOMTimeStamp aTimestamp)
114 : mTimestamp(aTimestamp) {
115 mCoords = new nsGeoPositionCoords(aLat, aLong, aAlt, aHError, aVError,
116 aHeading, aSpeed);
117 }
118
nsGeoPosition(nsIDOMGeoPositionCoords * aCoords,DOMTimeStamp aTimestamp)119 nsGeoPosition::nsGeoPosition(nsIDOMGeoPositionCoords* aCoords,
120 DOMTimeStamp aTimestamp)
121 : mTimestamp(aTimestamp), mCoords(aCoords) {}
122
123 nsGeoPosition::~nsGeoPosition() = default;
124
125 NS_INTERFACE_MAP_BEGIN(nsGeoPosition)
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports,nsIDOMGeoPosition)126 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPosition)
127 NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPosition)
128 NS_INTERFACE_MAP_END
129
130 NS_IMPL_ADDREF(nsGeoPosition)
131 NS_IMPL_RELEASE(nsGeoPosition)
132
133 NS_IMETHODIMP
134 nsGeoPosition::GetTimestamp(DOMTimeStamp* aTimestamp) {
135 *aTimestamp = mTimestamp;
136 return NS_OK;
137 }
138
139 NS_IMETHODIMP
GetCoords(nsIDOMGeoPositionCoords ** aCoords)140 nsGeoPosition::GetCoords(nsIDOMGeoPositionCoords** aCoords) {
141 NS_IF_ADDREF(*aCoords = mCoords);
142 return NS_OK;
143 }
144
145 namespace mozilla::dom {
146
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GeolocationPosition,mParent,mCoordinates)147 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GeolocationPosition, mParent,
148 mCoordinates)
149 NS_IMPL_CYCLE_COLLECTING_ADDREF(GeolocationPosition)
150 NS_IMPL_CYCLE_COLLECTING_RELEASE(GeolocationPosition)
151 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GeolocationPosition)
152 NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
153 NS_INTERFACE_MAP_ENTRY(nsISupports)
154 NS_INTERFACE_MAP_END
155
156 GeolocationPosition::GeolocationPosition(nsISupports* aParent,
157 nsIDOMGeoPosition* aGeoPosition)
158 : mParent(aParent), mGeoPosition(aGeoPosition) {}
159
160 GeolocationPosition::~GeolocationPosition() = default;
161
GetParentObject() const162 nsISupports* GeolocationPosition::GetParentObject() const { return mParent; }
163
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)164 JSObject* GeolocationPosition::WrapObject(JSContext* aCx,
165 JS::Handle<JSObject*> aGivenProto) {
166 return GeolocationPosition_Binding::Wrap(aCx, this, aGivenProto);
167 }
168
Coords()169 GeolocationCoordinates* GeolocationPosition::Coords() {
170 if (!mCoordinates) {
171 nsCOMPtr<nsIDOMGeoPositionCoords> coords;
172 mGeoPosition->GetCoords(getter_AddRefs(coords));
173 MOZ_ASSERT(coords, "coords should not be null");
174
175 mCoordinates = new GeolocationCoordinates(this, coords);
176 }
177
178 return mCoordinates;
179 }
180
Timestamp() const181 uint64_t GeolocationPosition::Timestamp() const {
182 uint64_t rv;
183
184 mGeoPosition->GetTimestamp(&rv);
185 return rv;
186 }
187
188 } // namespace mozilla::dom
189