1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/dom/src/geolocation/nsGeoPosition.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,264 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "nsGeoPosition.h" 1.10 + 1.11 +#include "mozilla/dom/PositionBinding.h" 1.12 +#include "mozilla/dom/CoordinatesBinding.h" 1.13 + 1.14 +//////////////////////////////////////////////////// 1.15 +// nsGeoPositionCoords 1.16 +//////////////////////////////////////////////////// 1.17 +nsGeoPositionCoords::nsGeoPositionCoords(double aLat, double aLong, 1.18 + double aAlt, double aHError, 1.19 + double aVError, double aHeading, 1.20 + double aSpeed) 1.21 + : mLat(aLat) 1.22 + , mLong(aLong) 1.23 + , mAlt(aAlt) 1.24 + , mHError(aHError) 1.25 + , mVError(aVError) 1.26 + , mHeading(aHeading) 1.27 + , mSpeed(aSpeed) 1.28 +{ 1.29 +} 1.30 + 1.31 +nsGeoPositionCoords::~nsGeoPositionCoords() 1.32 +{ 1.33 +} 1.34 + 1.35 +NS_INTERFACE_MAP_BEGIN(nsGeoPositionCoords) 1.36 +NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPositionCoords) 1.37 +NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPositionCoords) 1.38 +NS_INTERFACE_MAP_END 1.39 + 1.40 +NS_IMPL_ADDREF(nsGeoPositionCoords) 1.41 +NS_IMPL_RELEASE(nsGeoPositionCoords) 1.42 + 1.43 +NS_IMETHODIMP 1.44 +nsGeoPositionCoords::GetLatitude(double *aLatitude) 1.45 +{ 1.46 + *aLatitude = mLat; 1.47 + return NS_OK; 1.48 +} 1.49 + 1.50 +NS_IMETHODIMP 1.51 +nsGeoPositionCoords::GetLongitude(double *aLongitude) 1.52 +{ 1.53 + *aLongitude = mLong; 1.54 + return NS_OK; 1.55 +} 1.56 + 1.57 +NS_IMETHODIMP 1.58 +nsGeoPositionCoords::GetAltitude(double *aAltitude) 1.59 +{ 1.60 + *aAltitude = mAlt; 1.61 + return NS_OK; 1.62 +} 1.63 + 1.64 +NS_IMETHODIMP 1.65 +nsGeoPositionCoords::GetAccuracy(double *aAccuracy) 1.66 +{ 1.67 + *aAccuracy = mHError; 1.68 + return NS_OK; 1.69 +} 1.70 + 1.71 +NS_IMETHODIMP 1.72 +nsGeoPositionCoords::GetAltitudeAccuracy(double *aAltitudeAccuracy) 1.73 +{ 1.74 + *aAltitudeAccuracy = mVError; 1.75 + return NS_OK; 1.76 +} 1.77 + 1.78 +NS_IMETHODIMP 1.79 +nsGeoPositionCoords::GetHeading(double *aHeading) 1.80 +{ 1.81 + *aHeading = mHeading; 1.82 + return NS_OK; 1.83 +} 1.84 + 1.85 +NS_IMETHODIMP 1.86 +nsGeoPositionCoords::GetSpeed(double *aSpeed) 1.87 +{ 1.88 + *aSpeed = mSpeed; 1.89 + return NS_OK; 1.90 +} 1.91 + 1.92 +//////////////////////////////////////////////////// 1.93 +// nsGeoPosition 1.94 +//////////////////////////////////////////////////// 1.95 + 1.96 +nsGeoPosition::nsGeoPosition(double aLat, double aLong, 1.97 + double aAlt, double aHError, 1.98 + double aVError, double aHeading, 1.99 + double aSpeed, long long aTimestamp) : 1.100 + mTimestamp(aTimestamp) 1.101 +{ 1.102 + mCoords = new nsGeoPositionCoords(aLat, aLong, 1.103 + aAlt, aHError, 1.104 + aVError, aHeading, 1.105 + aSpeed); 1.106 +} 1.107 + 1.108 +nsGeoPosition::nsGeoPosition(nsIDOMGeoPositionCoords *aCoords, 1.109 + long long aTimestamp) : 1.110 + mTimestamp(aTimestamp), 1.111 + mCoords(aCoords) 1.112 +{ 1.113 +} 1.114 + 1.115 +nsGeoPosition::nsGeoPosition(nsIDOMGeoPositionCoords *aCoords, 1.116 + DOMTimeStamp aTimestamp) : 1.117 + mTimestamp(aTimestamp), 1.118 + mCoords(aCoords) 1.119 +{ 1.120 +} 1.121 + 1.122 +nsGeoPosition::~nsGeoPosition() 1.123 +{ 1.124 +} 1.125 + 1.126 +NS_INTERFACE_MAP_BEGIN(nsGeoPosition) 1.127 +NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPosition) 1.128 +NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPosition) 1.129 +NS_INTERFACE_MAP_END 1.130 + 1.131 +NS_IMPL_ADDREF(nsGeoPosition) 1.132 +NS_IMPL_RELEASE(nsGeoPosition) 1.133 + 1.134 +NS_IMETHODIMP 1.135 +nsGeoPosition::GetTimestamp(DOMTimeStamp* aTimestamp) 1.136 +{ 1.137 + *aTimestamp = mTimestamp; 1.138 + return NS_OK; 1.139 +} 1.140 + 1.141 +NS_IMETHODIMP 1.142 +nsGeoPosition::GetCoords(nsIDOMGeoPositionCoords * *aCoords) 1.143 +{ 1.144 + NS_IF_ADDREF(*aCoords = mCoords); 1.145 + return NS_OK; 1.146 +} 1.147 + 1.148 +namespace mozilla { 1.149 +namespace dom { 1.150 + 1.151 + 1.152 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(Position, mParent, mCoordinates) 1.153 +NS_IMPL_CYCLE_COLLECTING_ADDREF(Position) 1.154 +NS_IMPL_CYCLE_COLLECTING_RELEASE(Position) 1.155 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Position) 1.156 + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 1.157 + NS_INTERFACE_MAP_ENTRY(nsISupports) 1.158 +NS_INTERFACE_MAP_END 1.159 + 1.160 +Position::Position(nsISupports* aParent, nsIDOMGeoPosition* aGeoPosition) 1.161 + : mParent(aParent) 1.162 + , mGeoPosition(aGeoPosition) 1.163 +{ 1.164 + SetIsDOMBinding(); 1.165 +} 1.166 + 1.167 +Position::~Position() 1.168 +{ 1.169 +} 1.170 + 1.171 +nsISupports* 1.172 +Position::GetParentObject() const 1.173 +{ 1.174 + return mParent; 1.175 +} 1.176 + 1.177 +JSObject* 1.178 +Position::WrapObject(JSContext* aCx) 1.179 +{ 1.180 + return PositionBinding::Wrap(aCx, this); 1.181 +} 1.182 + 1.183 +Coordinates* 1.184 +Position::Coords() 1.185 +{ 1.186 + if (!mCoordinates) { 1.187 + nsCOMPtr<nsIDOMGeoPositionCoords> coords; 1.188 + mGeoPosition->GetCoords(getter_AddRefs(coords)); 1.189 + MOZ_ASSERT(coords, "coords should not be null"); 1.190 + 1.191 + mCoordinates = new Coordinates(this, coords); 1.192 + } 1.193 + 1.194 + return mCoordinates; 1.195 +} 1.196 + 1.197 +uint64_t 1.198 +Position::Timestamp() const 1.199 +{ 1.200 + uint64_t rv; 1.201 + 1.202 + mGeoPosition->GetTimestamp(&rv); 1.203 + return rv; 1.204 +} 1.205 + 1.206 +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(Coordinates, mPosition) 1.207 +NS_IMPL_CYCLE_COLLECTING_ADDREF(Coordinates) 1.208 +NS_IMPL_CYCLE_COLLECTING_RELEASE(Coordinates) 1.209 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Coordinates) 1.210 + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY 1.211 + NS_INTERFACE_MAP_ENTRY(nsISupports) 1.212 +NS_INTERFACE_MAP_END 1.213 + 1.214 +Coordinates::Coordinates(Position* aPosition, nsIDOMGeoPositionCoords* aCoords) 1.215 + : mPosition(aPosition) 1.216 + , mCoords(aCoords) 1.217 +{ 1.218 + SetIsDOMBinding(); 1.219 +} 1.220 + 1.221 +Coordinates::~Coordinates() 1.222 +{ 1.223 +} 1.224 + 1.225 +Position* 1.226 +Coordinates::GetParentObject() const 1.227 +{ 1.228 + return mPosition; 1.229 +} 1.230 + 1.231 +JSObject* 1.232 +Coordinates::WrapObject(JSContext* aCx) 1.233 +{ 1.234 + return CoordinatesBinding::Wrap(aCx, this); 1.235 +} 1.236 + 1.237 +#define GENERATE_COORDS_WRAPPED_GETTER(name) \ 1.238 +double \ 1.239 +Coordinates::name() const \ 1.240 +{ \ 1.241 + double rv; \ 1.242 + mCoords->Get##name(&rv); \ 1.243 + return rv; \ 1.244 +} 1.245 + 1.246 +#define GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(name) \ 1.247 +Nullable<double> \ 1.248 +Coordinates::Get##name() const \ 1.249 +{ \ 1.250 + double rv; \ 1.251 + mCoords->Get##name(&rv); \ 1.252 + return Nullable<double>(rv); \ 1.253 +} 1.254 + 1.255 +GENERATE_COORDS_WRAPPED_GETTER(Latitude) 1.256 +GENERATE_COORDS_WRAPPED_GETTER(Longitude) 1.257 +GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Altitude) 1.258 +GENERATE_COORDS_WRAPPED_GETTER(Accuracy) 1.259 +GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(AltitudeAccuracy) 1.260 +GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Heading) 1.261 +GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Speed) 1.262 + 1.263 +#undef GENERATE_COORDS_WRAPPED_GETTER 1.264 +#undef GENERATE_COORDS_WRAPPED_GETTER_NULLABLE 1.265 + 1.266 +} // namespace dom 1.267 +} // namespace mozilla