nsGeoPositionIPCSerialiser.h (4054B)
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 #ifndef dom_src_geolocation_IPC_serialiser 8 #define dom_src_geolocation_IPC_serialiser 9 10 #include "ipc/IPCMessageUtils.h" 11 #include "mozilla/dom/GeolocationPosition.h" 12 #include "nsIDOMGeoPosition.h" 13 14 namespace IPC { 15 16 template <> 17 struct ParamTraits<nsIDOMGeoPositionCoords*> { 18 // Function to serialize a geoposition 19 static void Write(MessageWriter* aWriter, nsIDOMGeoPositionCoords* aParam) { 20 bool isNull = !aParam; 21 WriteParam(aWriter, isNull); 22 // If it is a null object, then we are done 23 if (isNull) return; 24 25 double coordData; 26 27 aParam->GetLatitude(&coordData); 28 WriteParam(aWriter, coordData); 29 30 aParam->GetLongitude(&coordData); 31 WriteParam(aWriter, coordData); 32 33 aParam->GetAltitude(&coordData); 34 WriteParam(aWriter, coordData); 35 36 aParam->GetAccuracy(&coordData); 37 WriteParam(aWriter, coordData); 38 39 aParam->GetAltitudeAccuracy(&coordData); 40 WriteParam(aWriter, coordData); 41 42 aParam->GetHeading(&coordData); 43 WriteParam(aWriter, coordData); 44 45 aParam->GetSpeed(&coordData); 46 WriteParam(aWriter, coordData); 47 } 48 49 // Function to de-serialize a geoposition 50 static bool Read(MessageReader* aReader, 51 RefPtr<nsIDOMGeoPositionCoords>* aResult) { 52 // Check if it is the null pointer we have transfered 53 bool isNull; 54 if (!ReadParam(aReader, &isNull)) return false; 55 56 if (isNull) { 57 *aResult = nullptr; 58 return true; 59 } 60 61 double latitude; 62 double longitude; 63 double altitude; 64 double accuracy; 65 double altitudeAccuracy; 66 double heading; 67 double speed; 68 69 // It's not important to us where it fails, but rather if it fails 70 if (!(ReadParam(aReader, &latitude) && ReadParam(aReader, &longitude) && 71 ReadParam(aReader, &altitude) && ReadParam(aReader, &accuracy) && 72 ReadParam(aReader, &altitudeAccuracy) && 73 ReadParam(aReader, &heading) && ReadParam(aReader, &speed))) 74 return false; 75 76 // We now have all the data 77 *aResult = new nsGeoPositionCoords(latitude, /* aLat */ 78 longitude, /* aLong */ 79 altitude, /* aAlt */ 80 accuracy, /* aHError */ 81 altitudeAccuracy, /* aVError */ 82 heading, /* aHeading */ 83 speed /* aSpeed */ 84 ); 85 return true; 86 } 87 }; 88 89 template <> 90 struct ParamTraits<nsIDOMGeoPosition*> { 91 // Function to serialize a geoposition 92 static void Write(MessageWriter* aWriter, nsIDOMGeoPosition* aParam) { 93 bool isNull = !aParam; 94 WriteParam(aWriter, isNull); 95 // If it is a null object, then we are done 96 if (isNull) return; 97 98 EpochTimeStamp timeStamp; 99 aParam->GetTimestamp(&timeStamp); 100 WriteParam(aWriter, timeStamp); 101 102 nsCOMPtr<nsIDOMGeoPositionCoords> coords; 103 aParam->GetCoords(getter_AddRefs(coords)); 104 WriteParam(aWriter, coords); 105 } 106 107 // Function to de-serialize a geoposition 108 static bool Read(MessageReader* aReader, RefPtr<nsIDOMGeoPosition>* aResult) { 109 // Check if it is the null pointer we have transfered 110 bool isNull; 111 if (!ReadParam(aReader, &isNull)) return false; 112 113 if (isNull) { 114 *aResult = nullptr; 115 return true; 116 } 117 118 EpochTimeStamp timeStamp; 119 RefPtr<nsIDOMGeoPositionCoords> coords; 120 121 // It's not important to us where it fails, but rather if it fails 122 if (!ReadParam(aReader, &timeStamp) || !ReadParam(aReader, &coords)) { 123 return false; 124 } 125 126 *aResult = new nsGeoPosition(coords, timeStamp); 127 128 return true; 129 }; 130 }; 131 132 } // namespace IPC 133 134 #endif