michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsGeoPosition.h" michael@0: michael@0: #include "mozilla/dom/PositionBinding.h" michael@0: #include "mozilla/dom/CoordinatesBinding.h" michael@0: michael@0: //////////////////////////////////////////////////// michael@0: // nsGeoPositionCoords michael@0: //////////////////////////////////////////////////// michael@0: nsGeoPositionCoords::nsGeoPositionCoords(double aLat, double aLong, michael@0: double aAlt, double aHError, michael@0: double aVError, double aHeading, michael@0: double aSpeed) michael@0: : mLat(aLat) michael@0: , mLong(aLong) michael@0: , mAlt(aAlt) michael@0: , mHError(aHError) michael@0: , mVError(aVError) michael@0: , mHeading(aHeading) michael@0: , mSpeed(aSpeed) michael@0: { michael@0: } michael@0: michael@0: nsGeoPositionCoords::~nsGeoPositionCoords() michael@0: { michael@0: } michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(nsGeoPositionCoords) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPositionCoords) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPositionCoords) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_ADDREF(nsGeoPositionCoords) michael@0: NS_IMPL_RELEASE(nsGeoPositionCoords) michael@0: michael@0: NS_IMETHODIMP michael@0: nsGeoPositionCoords::GetLatitude(double *aLatitude) michael@0: { michael@0: *aLatitude = mLat; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGeoPositionCoords::GetLongitude(double *aLongitude) michael@0: { michael@0: *aLongitude = mLong; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGeoPositionCoords::GetAltitude(double *aAltitude) michael@0: { michael@0: *aAltitude = mAlt; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGeoPositionCoords::GetAccuracy(double *aAccuracy) michael@0: { michael@0: *aAccuracy = mHError; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGeoPositionCoords::GetAltitudeAccuracy(double *aAltitudeAccuracy) michael@0: { michael@0: *aAltitudeAccuracy = mVError; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGeoPositionCoords::GetHeading(double *aHeading) michael@0: { michael@0: *aHeading = mHeading; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGeoPositionCoords::GetSpeed(double *aSpeed) michael@0: { michael@0: *aSpeed = mSpeed; michael@0: return NS_OK; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////// michael@0: // nsGeoPosition michael@0: //////////////////////////////////////////////////// michael@0: michael@0: nsGeoPosition::nsGeoPosition(double aLat, double aLong, michael@0: double aAlt, double aHError, michael@0: double aVError, double aHeading, michael@0: double aSpeed, long long aTimestamp) : michael@0: mTimestamp(aTimestamp) michael@0: { michael@0: mCoords = new nsGeoPositionCoords(aLat, aLong, michael@0: aAlt, aHError, michael@0: aVError, aHeading, michael@0: aSpeed); michael@0: } michael@0: michael@0: nsGeoPosition::nsGeoPosition(nsIDOMGeoPositionCoords *aCoords, michael@0: long long aTimestamp) : michael@0: mTimestamp(aTimestamp), michael@0: mCoords(aCoords) michael@0: { michael@0: } michael@0: michael@0: nsGeoPosition::nsGeoPosition(nsIDOMGeoPositionCoords *aCoords, michael@0: DOMTimeStamp aTimestamp) : michael@0: mTimestamp(aTimestamp), michael@0: mCoords(aCoords) michael@0: { michael@0: } michael@0: michael@0: nsGeoPosition::~nsGeoPosition() michael@0: { michael@0: } michael@0: michael@0: NS_INTERFACE_MAP_BEGIN(nsGeoPosition) michael@0: NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMGeoPosition) michael@0: NS_INTERFACE_MAP_ENTRY(nsIDOMGeoPosition) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: NS_IMPL_ADDREF(nsGeoPosition) michael@0: NS_IMPL_RELEASE(nsGeoPosition) michael@0: michael@0: NS_IMETHODIMP michael@0: nsGeoPosition::GetTimestamp(DOMTimeStamp* aTimestamp) michael@0: { michael@0: *aTimestamp = mTimestamp; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsGeoPosition::GetCoords(nsIDOMGeoPositionCoords * *aCoords) michael@0: { michael@0: NS_IF_ADDREF(*aCoords = mCoords); michael@0: return NS_OK; michael@0: } michael@0: michael@0: namespace mozilla { michael@0: namespace dom { michael@0: michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_2(Position, mParent, mCoordinates) michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(Position) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(Position) michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Position) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: Position::Position(nsISupports* aParent, nsIDOMGeoPosition* aGeoPosition) michael@0: : mParent(aParent) michael@0: , mGeoPosition(aGeoPosition) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: Position::~Position() michael@0: { michael@0: } michael@0: michael@0: nsISupports* michael@0: Position::GetParentObject() const michael@0: { michael@0: return mParent; michael@0: } michael@0: michael@0: JSObject* michael@0: Position::WrapObject(JSContext* aCx) michael@0: { michael@0: return PositionBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: Coordinates* michael@0: Position::Coords() michael@0: { michael@0: if (!mCoordinates) { michael@0: nsCOMPtr coords; michael@0: mGeoPosition->GetCoords(getter_AddRefs(coords)); michael@0: MOZ_ASSERT(coords, "coords should not be null"); michael@0: michael@0: mCoordinates = new Coordinates(this, coords); michael@0: } michael@0: michael@0: return mCoordinates; michael@0: } michael@0: michael@0: uint64_t michael@0: Position::Timestamp() const michael@0: { michael@0: uint64_t rv; michael@0: michael@0: mGeoPosition->GetTimestamp(&rv); michael@0: return rv; michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(Coordinates, mPosition) michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(Coordinates) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE(Coordinates) michael@0: NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Coordinates) michael@0: NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY michael@0: NS_INTERFACE_MAP_ENTRY(nsISupports) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: Coordinates::Coordinates(Position* aPosition, nsIDOMGeoPositionCoords* aCoords) michael@0: : mPosition(aPosition) michael@0: , mCoords(aCoords) michael@0: { michael@0: SetIsDOMBinding(); michael@0: } michael@0: michael@0: Coordinates::~Coordinates() michael@0: { michael@0: } michael@0: michael@0: Position* michael@0: Coordinates::GetParentObject() const michael@0: { michael@0: return mPosition; michael@0: } michael@0: michael@0: JSObject* michael@0: Coordinates::WrapObject(JSContext* aCx) michael@0: { michael@0: return CoordinatesBinding::Wrap(aCx, this); michael@0: } michael@0: michael@0: #define GENERATE_COORDS_WRAPPED_GETTER(name) \ michael@0: double \ michael@0: Coordinates::name() const \ michael@0: { \ michael@0: double rv; \ michael@0: mCoords->Get##name(&rv); \ michael@0: return rv; \ michael@0: } michael@0: michael@0: #define GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(name) \ michael@0: Nullable \ michael@0: Coordinates::Get##name() const \ michael@0: { \ michael@0: double rv; \ michael@0: mCoords->Get##name(&rv); \ michael@0: return Nullable(rv); \ michael@0: } michael@0: michael@0: GENERATE_COORDS_WRAPPED_GETTER(Latitude) michael@0: GENERATE_COORDS_WRAPPED_GETTER(Longitude) michael@0: GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Altitude) michael@0: GENERATE_COORDS_WRAPPED_GETTER(Accuracy) michael@0: GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(AltitudeAccuracy) michael@0: GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Heading) michael@0: GENERATE_COORDS_WRAPPED_GETTER_NULLABLE(Speed) michael@0: michael@0: #undef GENERATE_COORDS_WRAPPED_GETTER michael@0: #undef GENERATE_COORDS_WRAPPED_GETTER_NULLABLE michael@0: michael@0: } // namespace dom michael@0: } // namespace mozilla