GeolocationProviderMock.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2012 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "GeolocationProviderMock.h"
  27. #include <WebKit2/WKGeolocationManager.h>
  28. #include <wtf/Assertions.h>
  29. #include <wtf/CurrentTime.h>
  30. namespace WTR {
  31. static void startUpdatingCallback(WKGeolocationManagerRef geolocationManager, const void* clientInfo)
  32. {
  33. GeolocationProviderMock* geolocationProvider = static_cast<GeolocationProviderMock*>(const_cast<void*>(clientInfo));
  34. geolocationProvider->startUpdating(geolocationManager);
  35. }
  36. static void stopUpdatingCallback(WKGeolocationManagerRef geolocationManager, const void* clientInfo)
  37. {
  38. GeolocationProviderMock* geolocationProvider = static_cast<GeolocationProviderMock*>(const_cast<void*>(clientInfo));
  39. geolocationProvider->stopUpdating(geolocationManager);
  40. }
  41. GeolocationProviderMock::GeolocationProviderMock(WKContextRef context)
  42. : m_isActive(false)
  43. , m_hasError(false)
  44. {
  45. m_geolocationManager = WKContextGetGeolocationManager(context);
  46. WKGeolocationProvider providerCallback = {
  47. kWKGeolocationProviderCurrentVersion,
  48. this,
  49. startUpdatingCallback,
  50. stopUpdatingCallback };
  51. WKGeolocationManagerSetProvider(m_geolocationManager, &providerCallback);
  52. }
  53. GeolocationProviderMock::~GeolocationProviderMock()
  54. {
  55. WKGeolocationManagerSetProvider(m_geolocationManager, 0);
  56. }
  57. void GeolocationProviderMock::setPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed)
  58. {
  59. m_position.adopt(WKGeolocationPositionCreate_b(currentTime(), latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed));
  60. m_hasError = false;
  61. m_errorMessage.clear();
  62. sendPositionIfNeeded();
  63. }
  64. void GeolocationProviderMock::setPositionUnavailableError(WKStringRef errorMessage)
  65. {
  66. m_errorMessage = errorMessage;
  67. m_hasError = true;
  68. m_position.clear();
  69. sendErrorIfNeeded();
  70. }
  71. void GeolocationProviderMock::startUpdating(WKGeolocationManagerRef geolocationManager)
  72. {
  73. ASSERT_UNUSED(geolocationManager, geolocationManager == m_geolocationManager);
  74. m_isActive = true;
  75. sendPositionIfNeeded();
  76. sendErrorIfNeeded();
  77. }
  78. void GeolocationProviderMock::stopUpdating(WKGeolocationManagerRef geolocationManager)
  79. {
  80. ASSERT_UNUSED(geolocationManager, geolocationManager == m_geolocationManager);
  81. m_isActive = false;
  82. }
  83. void GeolocationProviderMock::sendPositionIfNeeded()
  84. {
  85. if (m_isActive && m_position)
  86. WKGeolocationManagerProviderDidChangePosition(m_geolocationManager, m_position.get());
  87. }
  88. void GeolocationProviderMock::sendErrorIfNeeded()
  89. {
  90. if (m_isActive && m_hasError)
  91. WKGeolocationManagerProviderDidFailToDeterminePositionWithErrorMessage(m_geolocationManager, m_errorMessage.get());
  92. }
  93. } // namespace WTR