1/*
2 * Copyright (C) 2012 Igalia S.L.
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 "WebKitGeolocationProvider.h"
28
29#include "WebGeolocationManagerProxy.h"
30#include "WebGeolocationPosition.h"
31
32using namespace WebKit;
33
34#if ENABLE(GEOLOCATION)
35
36static inline WebKitGeolocationProvider* toGeolocationProvider(const void* clientInfo)
37{
38    return static_cast<WebKitGeolocationProvider*>(const_cast<void*>(clientInfo));
39}
40
41static void startUpdatingCallback(WKGeolocationManagerRef, const void* clientInfo)
42{
43    toGeolocationProvider(clientInfo)->startUpdating();
44}
45
46static void stopUpdatingCallback(WKGeolocationManagerRef, const void* clientInfo)
47{
48    toGeolocationProvider(clientInfo)->stopUpdating();
49}
50
51WebKitGeolocationProvider::~WebKitGeolocationProvider()
52{
53    m_provider.stopUpdating();
54}
55
56PassRefPtr<WebKitGeolocationProvider> WebKitGeolocationProvider::create(WebGeolocationManagerProxy* geolocationManager)
57{
58    return adoptRef(new WebKitGeolocationProvider(geolocationManager));
59}
60
61WebKitGeolocationProvider::WebKitGeolocationProvider(WebGeolocationManagerProxy* geolocationManager)
62    : m_geolocationManager(geolocationManager)
63    , m_provider(this)
64{
65    ASSERT(geolocationManager);
66
67    WKGeolocationProviderV1 wkGeolocationProvider = {
68        {
69            1, // version
70            this, // clientInfo
71        },
72        startUpdatingCallback,
73        stopUpdatingCallback,
74        0 // setEnableHighAccuracy
75    };
76    WKGeolocationManagerSetProvider(toAPI(geolocationManager), &wkGeolocationProvider.base);
77}
78
79void WebKitGeolocationProvider::startUpdating()
80{
81    m_provider.startUpdating();
82}
83
84void WebKitGeolocationProvider::stopUpdating()
85{
86    m_provider.stopUpdating();
87}
88
89void WebKitGeolocationProvider::notifyPositionChanged(int timestamp, double latitude, double longitude, double altitude, double accuracy, double altitudeAccuracy)
90{
91    RefPtr<WebGeolocationPosition> position = WebGeolocationPosition::create(timestamp, latitude, longitude, accuracy, true, altitude, true, altitudeAccuracy, false, 0, false, 0);
92    m_geolocationManager->providerDidChangePosition(position.get());
93}
94
95void WebKitGeolocationProvider::notifyErrorOccurred(const char* /* message */)
96{
97    m_geolocationManager->providerDidFailToDeterminePosition();
98}
99
100#endif // ENABLE(GEOLOCATION)
101