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
)
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
35 mHeading((aHeading
>= 0 && aHeading
< 360 && aSpeed
> 0) ? aHeading
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
)
60 NS_IMPL_ADDREF(nsGeoPositionCoords
)
61 NS_IMPL_RELEASE(nsGeoPositionCoords
)
64 nsGeoPositionCoords::GetLatitude(double* aLatitude
) {
70 nsGeoPositionCoords::GetLongitude(double* aLongitude
) {
76 nsGeoPositionCoords::GetAltitude(double* aAltitude
) {
82 nsGeoPositionCoords::GetAccuracy(double* aAccuracy
) {
88 nsGeoPositionCoords::GetAltitudeAccuracy(double* aAltitudeAccuracy
) {
89 *aAltitudeAccuracy
= mVError
;
94 nsGeoPositionCoords::GetHeading(double* aHeading
) {
100 nsGeoPositionCoords::GetSpeed(double* aSpeed
) {
105 ////////////////////////////////////////////////////
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
,
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
)
128 NS_IMPL_ADDREF(nsGeoPosition
)
129 NS_IMPL_RELEASE(nsGeoPosition
)
132 nsGeoPosition::GetTimestamp(EpochTimeStamp
* aTimestamp
) {
133 *aTimestamp
= mTimestamp
;
138 nsGeoPosition::GetCoords(nsIDOMGeoPositionCoords
** aCoords
) {
139 NS_IF_ADDREF(*aCoords
= mCoords
);
143 namespace mozilla::dom
{
145 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GeolocationPosition
, mParent
,
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
)
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() {
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
);
179 uint64_t GeolocationPosition::Timestamp() const {
182 mGeoPosition
->GetTimestamp(&rv
);
186 } // namespace mozilla::dom