1/*
2 * Copyright (C) 2012, 2013 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
26#include "config.h"
27#include "GeolocationProviderMock.h"
28
29#include <WebKit/WKGeolocationManager.h>
30#include <string.h>
31#include <wtf/Assertions.h>
32#include <wtf/WallTime.h>
33
34namespace WTR {
35
36static void startUpdatingCallback(WKGeolocationManagerRef geolocationManager, const void* clientInfo)
37{
38 GeolocationProviderMock* geolocationProvider = static_cast<GeolocationProviderMock*>(const_cast<void*>(clientInfo));
39 geolocationProvider->startUpdating(geolocationManager);
40}
41
42static void stopUpdatingCallback(WKGeolocationManagerRef geolocationManager, const void* clientInfo)
43{
44 GeolocationProviderMock* geolocationProvider = static_cast<GeolocationProviderMock*>(const_cast<void*>(clientInfo));
45 geolocationProvider->stopUpdating(geolocationManager);
46}
47
48GeolocationProviderMock::GeolocationProviderMock(WKContextRef context)
49 : m_context(context)
50 , m_geolocationManager(WKContextGetGeolocationManager(context))
51{
52 WKGeolocationProviderV1 providerCallback;
53 memset(&providerCallback, 0, sizeof(WKGeolocationProviderV1));
54 providerCallback.base.version = 1;
55 providerCallback.base.clientInfo = this;
56 providerCallback.startUpdating = startUpdatingCallback;
57 providerCallback.stopUpdating = stopUpdatingCallback;
58 WKGeolocationManagerSetProvider(m_geolocationManager, &providerCallback.base);
59}
60
61GeolocationProviderMock::~GeolocationProviderMock()
62{
63 WKGeolocationManagerSetProvider(m_geolocationManager, 0);
64}
65
66void GeolocationProviderMock::setPosition(double latitude, double longitude, double accuracy, bool providesAltitude, double altitude, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed, bool providesFloorLevel, double floorLevel)
67{
68 m_position.adopt(WKGeolocationPositionCreate_c(WallTime::now().secondsSinceEpoch().seconds(), latitude, longitude, accuracy, providesAltitude, altitude, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed, providesFloorLevel, floorLevel));
69
70 m_hasError = false;
71 m_errorMessage.clear();
72
73 sendPositionIfNeeded();
74}
75
76void GeolocationProviderMock::setPositionUnavailableError(WKStringRef errorMessage)
77{
78 m_errorMessage = errorMessage;
79 m_hasError = true;
80
81 m_position.clear();
82
83 sendErrorIfNeeded();
84}
85
86void GeolocationProviderMock::startUpdating(WKGeolocationManagerRef geolocationManager)
87{
88 ASSERT_UNUSED(geolocationManager, geolocationManager == m_geolocationManager);
89
90 m_isActive = true;
91 sendPositionIfNeeded();
92 sendErrorIfNeeded();
93}
94
95void GeolocationProviderMock::stopUpdating(WKGeolocationManagerRef geolocationManager)
96{
97 ASSERT_UNUSED(geolocationManager, geolocationManager == m_geolocationManager);
98
99 m_isActive = false;
100}
101
102void GeolocationProviderMock::sendPositionIfNeeded()
103{
104 if (m_isActive && m_position)
105 WKGeolocationManagerProviderDidChangePosition(m_geolocationManager, m_position.get());
106}
107
108void GeolocationProviderMock::sendErrorIfNeeded()
109{
110 if (m_isActive && m_hasError)
111 WKGeolocationManagerProviderDidFailToDeterminePositionWithErrorMessage(m_geolocationManager, m_errorMessage.get());
112}
113
114} // namespace WTR
115