#include "config.h"
#include "Geolocation.h"
#include "Chrome.h"
#include "Document.h"
#include "Frame.h"
#include "Page.h"
#include "PositionError.h"
namespace WebCore {
Geolocation::GeoNotifier::GeoNotifier(PassRefPtr<PositionCallback> successCallback, PassRefPtr<PositionErrorCallback> errorCallback, PassRefPtr<PositionOptions> options)
: m_successCallback(successCallback)
, m_errorCallback(errorCallback)
, m_options(options)
, m_timer(this, &Geolocation::GeoNotifier::timerFired)
{
}
void Geolocation::GeoNotifier::startTimer()
{
if (m_errorCallback && m_options)
m_timer.startOneShot(m_options->timeout() / 1000.0);
}
void Geolocation::GeoNotifier::timerFired(Timer<GeoNotifier>*)
{
ASSERT(m_errorCallback);
m_timer.stop();
RefPtr<PositionError> error = PositionError::create(PositionError::TIMEOUT, "Timed out");
m_errorCallback->handleEvent(error.get());
}
Geolocation::Geolocation(Frame* frame)
: m_frame(frame)
, m_service(GeolocationService::create(this))
, m_allowGeolocation(Unknown)
, m_shouldClearCache(false)
{
if (!m_frame)
return;
ASSERT(m_frame->document());
m_frame->document()->setUsingGeolocation(true);
}
void Geolocation::disconnectFrame()
{
m_service->stopUpdating();
if (m_frame && m_frame->document())
m_frame->document()->setUsingGeolocation(false);
m_frame = 0;
}
void Geolocation::getCurrentPosition(PassRefPtr<PositionCallback> successCallback, PassRefPtr<PositionErrorCallback> errorCallback, PassRefPtr<PositionOptions> options)
{
RefPtr<GeoNotifier> notifier = GeoNotifier::create(successCallback, errorCallback, options);
if (!m_service->startUpdating(notifier->m_options.get())) {
if (notifier->m_errorCallback) {
RefPtr<PositionError> error = PositionError::create(PositionError::PERMISSION_DENIED, "Unable to Start");
notifier->m_errorCallback->handleEvent(error.get());
}
return;
}
m_oneShots.add(notifier);
}
int Geolocation::watchPosition(PassRefPtr<PositionCallback> successCallback, PassRefPtr<PositionErrorCallback> errorCallback, PassRefPtr<PositionOptions> options)
{
RefPtr<GeoNotifier> notifier = GeoNotifier::create(successCallback, errorCallback, options);
if (!m_service->startUpdating(notifier->m_options.get())) {
if (notifier->m_errorCallback) {
RefPtr<PositionError> error = PositionError::create(PositionError::PERMISSION_DENIED, "Unable to Start");
notifier->m_errorCallback->handleEvent(error.get());
}
return 0;
}
static int sIdentifier = 0;
m_watchers.set(++sIdentifier, notifier);
return sIdentifier;
}
void Geolocation::clearWatch(int watchId)
{
m_watchers.remove(watchId);
if (!hasListeners())
m_service->stopUpdating();
}
void Geolocation::suspend()
{
if (hasListeners())
m_service->suspend();
}
void Geolocation::resume()
{
if (hasListeners())
m_service->resume();
}
void Geolocation::setIsAllowed(bool isAllowed)
{
m_allowGeolocation = isAllowed ? Yes : No;
if (m_allowGeolocation == Yes)
geolocationServicePositionChanged(m_service.get());
else {
WTF::RefPtr<WebCore::PositionError> error = WebCore::PositionError::create(PositionError::PERMISSION_DENIED, "User disallowed GeoLocation");
handleError(error.get());
}
}
void Geolocation::sendError(Vector<RefPtr<GeoNotifier> >& notifiers, PositionError* error)
{
Vector<RefPtr<GeoNotifier> >::const_iterator end = notifiers.end();
for (Vector<RefPtr<GeoNotifier> >::const_iterator it = notifiers.begin(); it != end; ++it) {
RefPtr<GeoNotifier> notifier = *it;
if (notifier->m_errorCallback)
notifier->m_errorCallback->handleEvent(error);
}
}
void Geolocation::sendErrorToOneShots(PositionError* error)
{
Vector<RefPtr<GeoNotifier> > copy;
copyToVector(m_oneShots, copy);
sendError(copy, error);
}
void Geolocation::sendErrorToWatchers(PositionError* error)
{
Vector<RefPtr<GeoNotifier> > copy;
copyValuesToVector(m_watchers, copy);
sendError(copy, error);
}
void Geolocation::sendPosition(Vector<RefPtr<GeoNotifier> >& notifiers, Geoposition* position)
{
Vector<RefPtr<GeoNotifier> >::const_iterator end = notifiers.end();
for (Vector<RefPtr<GeoNotifier> >::const_iterator it = notifiers.begin(); it != end; ++it) {
RefPtr<GeoNotifier> notifier = *it;
ASSERT(notifier->m_successCallback);
notifier->m_timer.stop();
bool shouldCallErrorCallback = false;
notifier->m_successCallback->handleEvent(position, shouldCallErrorCallback);
if (shouldCallErrorCallback) {
RefPtr<PositionError> error = PositionError::create(PositionError::UNKNOWN_ERROR, "An exception was thrown");
handleError(error.get());
}
}
}
void Geolocation::sendPositionToOneShots(Geoposition* position)
{
Vector<RefPtr<GeoNotifier> > copy;
copyToVector(m_oneShots, copy);
sendPosition(copy, position);
}
void Geolocation::sendPositionToWatchers(Geoposition* position)
{
Vector<RefPtr<GeoNotifier> > copy;
copyValuesToVector(m_watchers, copy);
sendPosition(copy, position);
}
void Geolocation::startTimer(Vector<RefPtr<GeoNotifier> >& notifiers)
{
Vector<RefPtr<GeoNotifier> >::const_iterator end = notifiers.end();
for (Vector<RefPtr<GeoNotifier> >::const_iterator it = notifiers.begin(); it != end; ++it) {
RefPtr<GeoNotifier> notifier = *it;
notifier->startTimer();
}
}
void Geolocation::startTimersForOneShots()
{
Vector<RefPtr<GeoNotifier> > copy;
copyToVector(m_oneShots, copy);
startTimer(copy);
}
void Geolocation::startTimersForWatchers()
{
Vector<RefPtr<GeoNotifier> > copy;
copyValuesToVector(m_watchers, copy);
startTimer(copy);
}
void Geolocation::startTimers()
{
startTimersForOneShots();
startTimersForWatchers();
}
void Geolocation::handleError(PositionError* error)
{
ASSERT(error);
sendErrorToOneShots(error);
sendErrorToWatchers(error);
m_oneShots.clear();
if (!hasListeners())
m_service->stopUpdating();
}
void Geolocation::requestPermission()
{
if (m_allowGeolocation > Unknown)
return;
if (!m_frame)
return;
Page* page = m_frame->page();
if (!page)
return;
bool result = page->chrome()->requestGeolocationPermissionForFrame(m_frame, this);
setIsAllowed(result);
if (result)
startTimers();
}
void Geolocation::geolocationServicePositionChanged(GeolocationService* service)
{
ASSERT(service->lastPosition());
requestPermission();
if (!isAllowed())
return;
sendPositionToOneShots(service->lastPosition());
sendPositionToWatchers(service->lastPosition());
m_oneShots.clear();
if (!hasListeners())
m_service->stopUpdating();
}
void Geolocation::geolocationServiceErrorOccurred(GeolocationService* service)
{
ASSERT(service->lastError());
handleError(service->lastError());
}
void Geolocation::geolocationServiceCachePolicyChanged(GeolocationService* service)
{
setShouldClearCache(service->shouldClearCache());
m_allowGeolocation = Unknown;
}
}