nsGeoPosition.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsGeoPosition.h"
  6. #include "mozilla/dom/PositionBinding.h"
  7. #include "mozilla/dom/CoordinatesBinding.h"
  8. ////////////////////////////////////////////////////
  9. // nsGeoPositionCoords
  10. ////////////////////////////////////////////////////
  11. nsGeoPositionCoords::nsGeoPositionCoords(double aLat, double aLong,
  12. double aAlt, double aHError,
  13. double aVError, double aHeading,
  14. double aSpeed)
  15. : mLat(aLat)
  16. , mLong(aLong)
  17. , mAlt(aAlt)
  18. , mHError(aHError)
  19. , mVError(aVError)
  20. , mHeading(aHeading)
  21. , mSpeed(aSpeed)
  22. {
  23. }
  24. nsGeoPositionCoords::~nsGeoPositionCoords()
  25. {
  26. }
  27. NS_INTERFACE_MAP_BEGIN(nsGeoPositionCoords)
  28. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPositionCoords)
  29. NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPositionCoords)
  30. NS_INTERFACE_MAP_END
  31. NS_IMPL_ADDREF(nsGeoPositionCoords)
  32. NS_IMPL_RELEASE(nsGeoPositionCoords)
  33. NS_IMETHODIMP
  34. nsGeoPositionCoords::GetLatitude(double *aLatitude)
  35. {
  36. *aLatitude = mLat;
  37. return NS_OK;
  38. }
  39. NS_IMETHODIMP
  40. nsGeoPositionCoords::GetLongitude(double *aLongitude)
  41. {
  42. *aLongitude = mLong;
  43. return NS_OK;
  44. }
  45. NS_IMETHODIMP
  46. nsGeoPositionCoords::GetAltitude(double *aAltitude)
  47. {
  48. *aAltitude = mAlt;
  49. return NS_OK;
  50. }
  51. NS_IMETHODIMP
  52. nsGeoPositionCoords::GetAccuracy(double *aAccuracy)
  53. {
  54. *aAccuracy = mHError;
  55. return NS_OK;
  56. }
  57. NS_IMETHODIMP
  58. nsGeoPositionCoords::GetAltitudeAccuracy(double *aAltitudeAccuracy)
  59. {
  60. *aAltitudeAccuracy = mVError;
  61. return NS_OK;
  62. }
  63. NS_IMETHODIMP
  64. nsGeoPositionCoords::GetHeading(double *aHeading)
  65. {
  66. *aHeading = mHeading;
  67. return NS_OK;
  68. }
  69. NS_IMETHODIMP
  70. nsGeoPositionCoords::GetSpeed(double *aSpeed)
  71. {
  72. *aSpeed = mSpeed;
  73. return NS_OK;
  74. }
  75. ////////////////////////////////////////////////////
  76. // nsGeoPosition
  77. ////////////////////////////////////////////////////
  78. nsGeoPosition::nsGeoPosition(double aLat, double aLong,
  79. double aAlt, double aHError,
  80. double aVError, double aHeading,
  81. double aSpeed, long long aTimestamp) :
  82. mTimestamp(aTimestamp)
  83. {
  84. mCoords = new nsGeoPositionCoords(aLat, aLong,
  85. aAlt, aHError,
  86. aVError, aHeading,
  87. aSpeed);
  88. }
  89. nsGeoPosition::nsGeoPosition(nsIDOMGeoPositionCoords *aCoords,
  90. long long aTimestamp) :
  91. mTimestamp(aTimestamp),
  92. mCoords(aCoords)
  93. {
  94. }
  95. nsGeoPosition::nsGeoPosition(nsIDOMGeoPositionCoords *aCoords,
  96. DOMTimeStamp aTimestamp) :
  97. mTimestamp(aTimestamp),
  98. mCoords(aCoords)
  99. {
  100. }
  101. nsGeoPosition::~nsGeoPosition()
  102. {
  103. }
  104. NS_INTERFACE_MAP_BEGIN(nsGeoPosition)
  105. NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPosition)
  106. NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPosition)
  107. NS_INTERFACE_MAP_END
  108. NS_IMPL_ADDREF(nsGeoPosition)
  109. NS_IMPL_RELEASE(nsGeoPosition)
  110. NS_IMETHODIMP
  111. nsGeoPosition::GetTimestamp(DOMTimeStamp* aTimestamp)
  112. {
  113. *aTimestamp = mTimestamp;
  114. return NS_OK;
  115. }
  116. NS_IMETHODIMP
  117. nsGeoPosition::GetCoords(nsIDOMGeoPositionCoords * *aCoords)
  118. {
  119. NS_IF_ADDREF(*aCoords = mCoords);
  120. return NS_OK;
  121. }
  122. namespace mozilla {
  123. namespace dom {
  124. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Position, mParent, mCoordinates)
  125. NS_IMPL_CYCLE_COLLECTING_ADDREF(Position)
  126. NS_IMPL_CYCLE_COLLECTING_RELEASE(Position)
  127. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Position)
  128. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  129. NS_INTERFACE_MAP_ENTRY(nsISupports)
  130. NS_INTERFACE_MAP_END
  131. Position::Position(nsISupports* aParent, nsIDOMGeoPosition* aGeoPosition)
  132. : mParent(aParent)
  133. , mGeoPosition(aGeoPosition)
  134. {
  135. }
  136. Position::~Position()
  137. {
  138. }
  139. nsISupports*
  140. Position::GetParentObject() const
  141. {
  142. return mParent;
  143. }
  144. JSObject*
  145. Position::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  146. {
  147. return PositionBinding::Wrap(aCx, this, aGivenProto);
  148. }
  149. Coordinates*
  150. Position::Coords()
  151. {
  152. if (!mCoordinates) {
  153. nsCOMPtr<nsIDOMGeoPositionCoords> coords;
  154. mGeoPosition->GetCoords(getter_AddRefs(coords));
  155. MOZ_ASSERT(coords, "coords should not be null");
  156. mCoordinates = new Coordinates(this, coords);
  157. }
  158. return mCoordinates;
  159. }
  160. uint64_t
  161. Position::Timestamp() const
  162. {
  163. uint64_t rv;
  164. mGeoPosition->GetTimestamp(&rv);
  165. return rv;
  166. }
  167. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Coordinates, mPosition)
  168. NS_IMPL_CYCLE_COLLECTING_ADDREF(Coordinates)
  169. NS_IMPL_CYCLE_COLLECTING_RELEASE(Coordinates)
  170. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Coordinates)
  171. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  172. NS_INTERFACE_MAP_ENTRY(nsISupports)
  173. NS_INTERFACE_MAP_END
  174. Coordinates::Coordinates(Position* aPosition, nsIDOMGeoPositionCoords* aCoords)
  175. : mPosition(aPosition)
  176. , mCoords(aCoords)
  177. {
  178. }
  179. Coordinates::~Coordinates()
  180. {
  181. }
  182. Position*
  183. Coordinates::GetParentObject() const
  184. {
  185. return mPosition;
  186. }
  187. JSObject*
  188. Coordinates::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  189. {
  190. return CoordinatesBinding::Wrap(aCx, this, aGivenProto);
  191. }
  192. #define GENERATE_COORDS_WRAPPED_GETTER(name) \
  193. double \
  194. Coordinates::name() const \
  195. { \
  196. double rv; \
  197. mCoords->Get##name(&rv); \
  198. return rv; \
  199. }
  200. #define GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(name) \
  201. Nullable<double> \
  202. Coordinates::Get##name() const \
  203. { \
  204. double rv; \
  205. mCoords->Get##name(&rv); \
  206. return Nullable<double>(rv); \
  207. }
  208. GENERATE_COORDS_WRAPPED_GETTER(Latitude)
  209. GENERATE_COORDS_WRAPPED_GETTER(Longitude)
  210. GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Altitude)
  211. GENERATE_COORDS_WRAPPED_GETTER(Accuracy)
  212. GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(AltitudeAccuracy)
  213. GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Heading)
  214. GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Speed)
  215. #undef GENERATE_COORDS_WRAPPED_GETTER
  216. #undef GENERATE_COORDS_WRAPPED_GETTER_NULLABLE
  217. } // namespace dom
  218. } // namespace mozilla