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
;
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
)
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
36 mHeading((aHeading
>= 0 && aHeading
< 360 && aSpeed
> 0) ? aHeading
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
)
61 NS_IMPL_ADDREF(nsGeoPositionCoords
)
62 NS_IMPL_RELEASE(nsGeoPositionCoords
)
65 nsGeoPositionCoords::GetLatitude(double* aLatitude
) {
71 nsGeoPositionCoords::GetLongitude(double* aLongitude
) {
77 nsGeoPositionCoords::GetAltitude(double* aAltitude
) {
83 nsGeoPositionCoords::GetAccuracy(double* aAccuracy
) {
89 nsGeoPositionCoords::GetAltitudeAccuracy(double* aAltitudeAccuracy
) {
90 *aAltitudeAccuracy
= mVError
;
95 nsGeoPositionCoords::GetHeading(double* aHeading
) {
101 nsGeoPositionCoords::GetSpeed(double* aSpeed
) {
106 ////////////////////////////////////////////////////
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
,
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
)
129 NS_IMPL_ADDREF(nsGeoPosition
)
130 NS_IMPL_RELEASE(nsGeoPosition
)
133 nsGeoPosition::GetTimestamp(EpochTimeStamp
* aTimestamp
) {
134 *aTimestamp
= mTimestamp
;
139 nsGeoPosition::GetCoords(nsIDOMGeoPositionCoords
** aCoords
) {
140 NS_IF_ADDREF(*aCoords
= mCoords
);
144 namespace mozilla::dom
{
146 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GeolocationPosition
, mParent
,
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
)
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() {
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
);
180 uint64_t GeolocationPosition::Timestamp() const {
183 mGeoPosition
->GetTimestamp(&rv
);
187 } // namespace mozilla::dom