nsGeoPositionIPCSerialiser.h 4.2 KB

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