1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
* Copyright (C) 2014 Samsung Electronics. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "GeolocationProviderGeoclue.h"
#if ENABLE(GEOLOCATION) && USE(GEOCLUE2)
#include <wtf/gobject/GUniquePtr.h>
#include <wtf/gobject/GlibUtilities.h>
#include <wtf/text/CString.h>
const char* gGeoclueBusName = "org.freedesktop.GeoClue2";
const char* gGeoclueManagerPath = "/org/freedesktop/GeoClue2/Manager";
using namespace WebCore;
typedef enum {
GeoclueAccuracyLevelCountry = 1,
GeoclueAccuracyLevelCity = 4,
GeoclueAccuracyLevelStreet = 6,
GeoclueAccuracyLevelExact = 8,
} GeoclueAccuracyLevel;
GeolocationProviderGeoclue::GeolocationProviderGeoclue(GeolocationProviderGeoclueClient* client)
: m_client(client)
, m_latitude(0)
, m_longitude(0)
, m_altitude(0)
, m_accuracy(0)
, m_altitudeAccuracy(0)
, m_timestamp(0)
, m_enableHighAccuracy(false)
, m_isUpdating(false)
{
ASSERT(m_client);
}
GeolocationProviderGeoclue::~GeolocationProviderGeoclue()
{
stopUpdating();
}
void GeolocationProviderGeoclue::startUpdating()
{
m_isUpdating = true;
if (!m_managerProxy) {
geoclue_manager_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, gGeoclueBusName, gGeoclueManagerPath, nullptr,
reinterpret_cast<GAsyncReadyCallback>(createGeoclueManagerProxyCallback), this);
return;
}
geoclue_manager_call_get_client(m_managerProxy.get(), nullptr, reinterpret_cast<GAsyncReadyCallback>(getGeoclueClientCallback), this);
}
void GeolocationProviderGeoclue::stopUpdating()
{
if (m_clientProxy) {
geoclue_client_call_stop(m_clientProxy.get(), nullptr, nullptr, nullptr);
g_signal_handlers_disconnect_by_func(m_clientProxy.get(), reinterpret_cast<gpointer>(locationUpdatedCallback), this);
m_clientProxy = nullptr;
}
m_isUpdating = false;
}
void GeolocationProviderGeoclue::setEnableHighAccuracy(bool enable)
{
if (m_enableHighAccuracy == enable)
return;
m_enableHighAccuracy = enable;
// If we're already updating we should report the new requirements in order
// to change to a more suitable provider if needed. If not, return.
if (!m_isUpdating)
return;
updateClientRequirements();
}
void GeolocationProviderGeoclue::createGeoclueManagerProxyCallback(GObject*, GAsyncResult* result, GeolocationProviderGeoclue* provider)
{
GUniqueOutPtr<GError> error;
provider->m_managerProxy = adoptGRef(geoclue_manager_proxy_new_for_bus_finish(result, &error.outPtr()));
if (error) {
provider->errorOccurred(error->message);
return;
}
geoclue_manager_call_get_client(provider->m_managerProxy.get(), nullptr, reinterpret_cast<GAsyncReadyCallback>(getGeoclueClientCallback), provider);
}
void GeolocationProviderGeoclue::getGeoclueClientCallback(GObject* sourceObject, GAsyncResult* result, GeolocationProviderGeoclue* provider)
{
GUniqueOutPtr<GError> error;
GUniqueOutPtr<gchar> path;
if (!geoclue_manager_call_get_client_finish(GEOCLUE_MANAGER(sourceObject), &path.outPtr(), result, &error.outPtr())) {
provider->errorOccurred(error->message);
return;
}
geoclue_client_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, gGeoclueBusName, path.get(), nullptr,
reinterpret_cast<GAsyncReadyCallback>(createGeoclueClientProxyCallback), provider);
}
void GeolocationProviderGeoclue::createGeoclueClientProxyCallback(GObject*, GAsyncResult* result, GeolocationProviderGeoclue* provider)
{
GUniqueOutPtr<GError> error;
provider->m_clientProxy = adoptGRef(geoclue_client_proxy_new_for_bus_finish(result, &error.outPtr()));
if (error) {
provider->errorOccurred(error->message);
return;
}
// Geoclue2 requires the client to provide a desktop ID for security
// reasons, which should identify the application requesting the location.
// FIXME: We provide the program name as the desktop ID for now but, in an ideal world,
// we should provide a proper "application ID" (normally the name of a .desktop file).
// https://bugs.webkit.org/show_bug.cgi?id=129879
geoclue_client_set_desktop_id(provider->m_clientProxy.get(), g_get_prgname());
provider->startGeoclueClient();
}
void GeolocationProviderGeoclue::startClientCallback(GObject* sourceObject, GAsyncResult* result, GeolocationProviderGeoclue* provider)
{
GUniqueOutPtr<GError> error;
if (!geoclue_client_call_start_finish(GEOCLUE_CLIENT(sourceObject), result, &error.outPtr()))
static_cast<GeolocationProviderGeoclue*>(provider)->errorOccurred(error->message);
}
void GeolocationProviderGeoclue::locationUpdatedCallback(GeoclueClient*, const gchar*, const gchar* newPath, GeolocationProviderGeoclue* provider)
{
geoclue_location_proxy_new_for_bus(G_BUS_TYPE_SYSTEM, G_DBUS_PROXY_FLAGS_NONE, gGeoclueBusName, newPath, nullptr,
reinterpret_cast<GAsyncReadyCallback>(createLocationProxyCallback), provider);
}
void GeolocationProviderGeoclue::createLocationProxyCallback(GObject*, GAsyncResult* result, GeolocationProviderGeoclue* provider)
{
GUniqueOutPtr<GError> error;
GRefPtr<GeoclueLocation> locationProxy = adoptGRef(geoclue_location_proxy_new_for_bus_finish(result, &error.outPtr()));
if (error) {
provider->errorOccurred(error->message);
return;
}
provider->updateLocation(locationProxy.get());
}
void GeolocationProviderGeoclue::startGeoclueClient()
{
// Set the requirement for the client.
updateClientRequirements();
g_signal_connect(m_clientProxy.get(), "location-updated", G_CALLBACK(locationUpdatedCallback), this);
geoclue_client_call_start(m_clientProxy.get(), nullptr, reinterpret_cast<GAsyncReadyCallback>(startClientCallback), this);
}
void GeolocationProviderGeoclue::updateLocation(GeoclueLocation* locationProxy)
{
GTimeVal timeValue;
g_get_current_time(&timeValue);
m_timestamp = timeValue.tv_sec;
m_latitude = geoclue_location_get_latitude(locationProxy);
m_longitude = geoclue_location_get_longitude(locationProxy);
m_accuracy = geoclue_location_get_accuracy(locationProxy);
m_client->notifyPositionChanged(m_timestamp, m_latitude, m_longitude, m_altitude, m_accuracy, m_altitudeAccuracy);
}
void GeolocationProviderGeoclue::errorOccurred(const char* message)
{
m_isUpdating = false;
m_client->notifyErrorOccurred(message);
}
void GeolocationProviderGeoclue::updateClientRequirements()
{
if (!m_clientProxy)
return;
GeoclueAccuracyLevel accuracyLevel = m_enableHighAccuracy ? GeoclueAccuracyLevelExact : GeoclueAccuracyLevelCity;
geoclue_client_set_requested_accuracy_level(m_clientProxy.get(), accuracyLevel);
}
#endif // ENABLE(GEOLOCATION) && USE(GEOCLUE2)
|