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/GeolocationCoordinates.h"
8 
9 #include "mozilla/FloatingPoint.h"
10 #include "mozilla/dom/GeolocationPositionBinding.h"
11 #include "mozilla/dom/GeolocationCoordinatesBinding.h"
12 
13 namespace mozilla::dom {
14 
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GeolocationCoordinates,mPosition)15 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(GeolocationCoordinates, mPosition)
16 NS_IMPL_CYCLE_COLLECTING_ADDREF(GeolocationCoordinates)
17 NS_IMPL_CYCLE_COLLECTING_RELEASE(GeolocationCoordinates)
18 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(GeolocationCoordinates)
19   NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
20   NS_INTERFACE_MAP_ENTRY(nsISupports)
21 NS_INTERFACE_MAP_END
22 
23 GeolocationCoordinates::GeolocationCoordinates(GeolocationPosition* aPosition,
24                                                nsIDOMGeoPositionCoords* aCoords)
25     : mPosition(aPosition), mCoords(aCoords) {}
26 
27 GeolocationCoordinates::~GeolocationCoordinates() = default;
28 
GetParentObject() const29 GeolocationPosition* GeolocationCoordinates::GetParentObject() const {
30   return mPosition;
31 }
32 
WrapObject(JSContext * aCx,JS::Handle<JSObject * > aGivenProto)33 JSObject* GeolocationCoordinates::WrapObject(
34     JSContext* aCx, JS::Handle<JSObject*> aGivenProto) {
35   return GeolocationCoordinates_Binding::Wrap(aCx, this, aGivenProto);
36 }
37 
38 #define GENERATE_COORDS_WRAPPED_GETTER(name)    \
39   double GeolocationCoordinates::name() const { \
40     double rv;                                  \
41     mCoords->Get##name(&rv);                    \
42     return rv;                                  \
43   }
44 
45 #define GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(name)          \
46   Nullable<double> GeolocationCoordinates::Get##name() const { \
47     double value;                                              \
48     mCoords->Get##name(&value);                                \
49     Nullable<double> rv;                                       \
50     if (!IsNaN(value)) {                                       \
51       rv.SetValue(value);                                      \
52     }                                                          \
53     return rv;                                                 \
54   }
55 
56 GENERATE_COORDS_WRAPPED_GETTER(Latitude)
57 GENERATE_COORDS_WRAPPED_GETTER(Longitude)
58 GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Altitude)
59 GENERATE_COORDS_WRAPPED_GETTER(Accuracy)
60 GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(AltitudeAccuracy)
61 GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Heading)
62 GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Speed)
63 
64 #undef GENERATE_COORDS_WRAPPED_GETTER
65 #undef GENERATE_COORDS_WRAPPED_GETTER_NULLABLE
66 
67 }  // namespace mozilla::dom
68