1/*
2 * Copyright (C) 2008 Holger Hans Peter Freyther <zecke@selfish.org>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public License
15 * along with this library; see the file COPYING.LIB.  If not, write to
16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18 *
19 */
20
21#include "config.h"
22#include "GeolocationClientGtk.h"
23
24#if ENABLE(GEOLOCATION)
25
26#include "Chrome.h"
27#include "ChromeClient.h"
28#include "Geolocation.h"
29#include "GeolocationController.h"
30#include "GeolocationError.h"
31#include "GeolocationPosition.h"
32#include "webkitgeolocationpolicydecisionprivate.h"
33#include "webkitwebframeprivate.h"
34#include "webkitwebviewprivate.h"
35#include <glib/gi18n-lib.h>
36#include <wtf/gobject/GRefPtr.h>
37
38namespace WebKit {
39
40GeolocationClient::GeolocationClient(WebKitWebView* webView)
41    : m_webView(webView)
42    , m_provider(this)
43{
44}
45
46void GeolocationClient::geolocationDestroyed()
47{
48    delete this;
49}
50
51void GeolocationClient::startUpdating()
52{
53    m_provider.startUpdating();
54}
55
56void GeolocationClient::stopUpdating()
57{
58    m_provider.stopUpdating();
59}
60
61void GeolocationClient::setEnableHighAccuracy(bool enable)
62{
63    m_provider.setEnableHighAccuracy(enable);
64}
65
66WebCore::GeolocationPosition* GeolocationClient::lastPosition()
67{
68    return m_lastPosition.get();
69}
70
71void GeolocationClient::requestPermission(WebCore::Geolocation* geolocation)
72{
73    WebKitWebFrame* webFrame = kit(geolocation->frame());
74    GRefPtr<WebKitGeolocationPolicyDecision> policyDecision(adoptGRef(webkit_geolocation_policy_decision_new(webFrame, geolocation)));
75
76    gboolean isHandled = FALSE;
77    g_signal_emit_by_name(m_webView, "geolocation-policy-decision-requested", webFrame, policyDecision.get(), &isHandled);
78    if (!isHandled)
79        webkit_geolocation_policy_deny(policyDecision.get());
80}
81
82void GeolocationClient::cancelPermissionRequest(WebCore::Geolocation* geolocation)
83{
84    g_signal_emit_by_name(m_webView, "geolocation-policy-decision-cancelled", kit(geolocation->frame()));
85}
86
87void GeolocationClient::notifyPositionChanged(int timestamp, double latitude, double longitude, double altitude, double accuracy, double altitudeAccuracy)
88{
89    m_lastPosition = WebCore::GeolocationPosition::create(static_cast<double>(timestamp), latitude, longitude, accuracy,
90                                                          true, altitude, true, altitudeAccuracy, false, 0, false, 0);
91    WebCore::GeolocationController::from(core(m_webView))->positionChanged(m_lastPosition.get());
92}
93
94void GeolocationClient::notifyErrorOccurred(const char* message)
95{
96    RefPtr<WebCore::GeolocationError> error = WebCore::GeolocationError::create(WebCore::GeolocationError::PositionUnavailable, message);
97    WebCore::GeolocationController::from(core(m_webView))->errorOccurred(error.get());
98}
99
100}
101
102#endif // ENABLE(GEOLOCATION)
103