This source file includes following definitions.
- createGeoposition
- createPositionError
- m_useCachedPosition
- trace
- setFatalError
- setUseCachedPosition
- hasZeroTimeout
- runSuccessCallback
- runErrorCallback
- startTimerIfNeeded
- stopTimer
- timerFired
- trace
- add
- find
- remove
- remove
- contains
- clear
- isEmpty
- getNotifiersVector
- create
- m_allowGeolocation
- trace
- document
- frame
- page
- stop
- lastPosition
- getCurrentPosition
- watchPosition
- startRequest
- fatalErrorOccurred
- requestUsesCachedPosition
- makeCachedPositionCallbacks
- requestTimedOut
- haveSuitableCachedPosition
- clearWatch
- setIsAllowed
- sendError
- sendPosition
- stopTimer
- stopTimersForOneShots
- stopTimersForWatchers
- stopTimers
- cancelRequests
- cancelAllRequests
- extractNotifiersWithCachedPosition
- copyToSet
- handleError
- requestPermission
- makeSuccessCallbacks
- positionChanged
- setError
- startUpdating
- stopUpdating
- handlePendingPermissionNotifiers
#include "config.h"
#include "modules/geolocation/Geolocation.h"
#include "core/dom/Document.h"
#include "wtf/CurrentTime.h"
#include "modules/geolocation/Coordinates.h"
#include "modules/geolocation/GeolocationController.h"
#include "modules/geolocation/GeolocationError.h"
#include "modules/geolocation/GeolocationPosition.h"
namespace WebCore {
static const char permissionDeniedErrorMessage[] = "User denied Geolocation";
static const char failedToStartServiceErrorMessage[] = "Failed to start Geolocation service";
static const char framelessDocumentErrorMessage[] = "Geolocation cannot be used in frameless documents";
static PassRefPtrWillBeRawPtr<Geoposition> createGeoposition(GeolocationPosition* position)
{
if (!position)
return nullptr;
RefPtrWillBeRawPtr<Coordinates> coordinates = Coordinates::create(
position->latitude(),
position->longitude(),
position->canProvideAltitude(),
position->altitude(),
position->accuracy(),
position->canProvideAltitudeAccuracy(),
position->altitudeAccuracy(),
position->canProvideHeading(),
position->heading(),
position->canProvideSpeed(),
position->speed());
return Geoposition::create(coordinates.release(), convertSecondsToDOMTimeStamp(position->timestamp()));
}
static PassRefPtrWillBeRawPtr<PositionError> createPositionError(GeolocationError* error)
{
PositionError::ErrorCode code = PositionError::POSITION_UNAVAILABLE;
switch (error->code()) {
case GeolocationError::PermissionDenied:
code = PositionError::PERMISSION_DENIED;
break;
case GeolocationError::PositionUnavailable:
code = PositionError::POSITION_UNAVAILABLE;
break;
}
return PositionError::create(code, error->message());
}
Geolocation::GeoNotifier::GeoNotifier(Geolocation* geolocation, PassOwnPtr<PositionCallback> successCallback, PassOwnPtr<PositionErrorCallback> errorCallback, PassRefPtrWillBeRawPtr<PositionOptions> options)
: m_geolocation(geolocation)
, m_successCallback(successCallback)
, m_errorCallback(errorCallback)
, m_options(options)
, m_timer(this, &Geolocation::GeoNotifier::timerFired)
, m_useCachedPosition(false)
{
ASSERT(m_geolocation);
ASSERT(m_successCallback);
ASSERT(m_options);
}
void Geolocation::GeoNotifier::trace(Visitor* visitor)
{
visitor->trace(m_geolocation);
visitor->trace(m_options);
visitor->trace(m_fatalError);
}
void Geolocation::GeoNotifier::setFatalError(PassRefPtrWillBeRawPtr<PositionError> error)
{
if (m_fatalError)
return;
m_fatalError = error;
m_timer.stop();
m_timer.startOneShot(0, FROM_HERE);
}
void Geolocation::GeoNotifier::setUseCachedPosition()
{
m_useCachedPosition = true;
m_timer.startOneShot(0, FROM_HERE);
}
bool Geolocation::GeoNotifier::hasZeroTimeout() const
{
return m_options->hasTimeout() && m_options->timeout() == 0;
}
void Geolocation::GeoNotifier::runSuccessCallback(Geoposition* position)
{
if (!m_geolocation->isAllowed())
CRASH();
m_successCallback->handleEvent(position);
}
void Geolocation::GeoNotifier::runErrorCallback(PositionError* error)
{
if (m_errorCallback)
m_errorCallback->handleEvent(error);
}
void Geolocation::GeoNotifier::startTimerIfNeeded()
{
if (m_options->hasTimeout())
m_timer.startOneShot(m_options->timeout() / 1000.0, FROM_HERE);
}
void Geolocation::GeoNotifier::stopTimer()
{
m_timer.stop();
}
void Geolocation::GeoNotifier::timerFired(Timer<GeoNotifier>*)
{
m_timer.stop();
RefPtrWillBeRawPtr<GeoNotifier> protect(this);
if (m_fatalError) {
runErrorCallback(m_fatalError.get());
m_geolocation->fatalErrorOccurred(this);
return;
}
if (m_useCachedPosition) {
m_useCachedPosition = false;
m_geolocation->requestUsesCachedPosition(this);
return;
}
if (m_errorCallback) {
RefPtrWillBeRawPtr<PositionError> error = PositionError::create(PositionError::TIMEOUT, "Timeout expired");
m_errorCallback->handleEvent(error.get());
}
m_geolocation->requestTimedOut(this);
}
void Geolocation::Watchers::trace(Visitor* visitor)
{
visitor->trace(m_idToNotifierMap);
visitor->trace(m_notifierToIdMap);
}
bool Geolocation::Watchers::add(int id, PassRefPtrWillBeRawPtr<GeoNotifier> prpNotifier)
{
ASSERT(id > 0);
RefPtrWillBeRawPtr<GeoNotifier> notifier = prpNotifier;
if (!m_idToNotifierMap.add(id, notifier.get()).isNewEntry)
return false;
m_notifierToIdMap.set(notifier.release(), id);
return true;
}
Geolocation::GeoNotifier* Geolocation::Watchers::find(int id)
{
ASSERT(id > 0);
IdToNotifierMap::iterator iter = m_idToNotifierMap.find(id);
if (iter == m_idToNotifierMap.end())
return 0;
return iter->value.get();
}
void Geolocation::Watchers::remove(int id)
{
ASSERT(id > 0);
IdToNotifierMap::iterator iter = m_idToNotifierMap.find(id);
if (iter == m_idToNotifierMap.end())
return;
m_notifierToIdMap.remove(iter->value);
m_idToNotifierMap.remove(iter);
}
void Geolocation::Watchers::remove(GeoNotifier* notifier)
{
NotifierToIdMap::iterator iter = m_notifierToIdMap.find(notifier);
if (iter == m_notifierToIdMap.end())
return;
m_idToNotifierMap.remove(iter->value);
m_notifierToIdMap.remove(iter);
}
bool Geolocation::Watchers::contains(GeoNotifier* notifier) const
{
return m_notifierToIdMap.contains(notifier);
}
void Geolocation::Watchers::clear()
{
m_idToNotifierMap.clear();
m_notifierToIdMap.clear();
}
bool Geolocation::Watchers::isEmpty() const
{
return m_idToNotifierMap.isEmpty();
}
void Geolocation::Watchers::getNotifiersVector(GeoNotifierVector& copy) const
{
copyValuesToVector(m_idToNotifierMap, copy);
}
PassRefPtrWillBeRawPtr<Geolocation> Geolocation::create(ExecutionContext* context)
{
RefPtrWillBeRawPtr<Geolocation> geolocation = adoptRefWillBeNoop(new Geolocation(context));
geolocation->suspendIfNeeded();
return geolocation.release();
}
Geolocation::Geolocation(ExecutionContext* context)
: ActiveDOMObject(context)
, m_allowGeolocation(Unknown)
{
ScriptWrappable::init(this);
}
Geolocation::~Geolocation()
{
ASSERT(m_allowGeolocation != InProgress);
}
void Geolocation::trace(Visitor* visitor)
{
visitor->trace(m_oneShots);
visitor->trace(m_watchers);
visitor->trace(m_pendingForPermissionNotifiers);
visitor->trace(m_lastPosition);
visitor->trace(m_requestsAwaitingCachedPosition);
}
Document* Geolocation::document() const
{
return toDocument(executionContext());
}
LocalFrame* Geolocation::frame() const
{
return document() ? document()->frame() : 0;
}
Page* Geolocation::page() const
{
return document() ? document()->page() : 0;
}
void Geolocation::stop()
{
Page* page = this->page();
if (page && m_allowGeolocation == InProgress)
GeolocationController::from(page)->cancelPermissionRequest(this);
m_allowGeolocation = Unknown;
cancelAllRequests();
stopUpdating();
m_pendingForPermissionNotifiers.clear();
}
Geoposition* Geolocation::lastPosition()
{
Page* page = this->page();
if (!page)
return 0;
m_lastPosition = createGeoposition(GeolocationController::from(page)->lastPosition());
return m_lastPosition.get();
}
void Geolocation::getCurrentPosition(PassOwnPtr<PositionCallback> successCallback, PassOwnPtr<PositionErrorCallback> errorCallback, PassRefPtrWillBeRawPtr<PositionOptions> options)
{
if (!frame())
return;
RefPtrWillBeRawPtr<GeoNotifier> notifier = GeoNotifier::create(this, successCallback, errorCallback, options);
startRequest(notifier.get());
m_oneShots.add(notifier);
}
int Geolocation::watchPosition(PassOwnPtr<PositionCallback> successCallback, PassOwnPtr<PositionErrorCallback> errorCallback, PassRefPtrWillBeRawPtr<PositionOptions> options)
{
if (!frame())
return 0;
RefPtrWillBeRawPtr<GeoNotifier> notifier = GeoNotifier::create(this, successCallback, errorCallback, options);
startRequest(notifier.get());
int watchID;
do {
watchID = executionContext()->circularSequentialID();
} while (!m_watchers.add(watchID, notifier));
return watchID;
}
void Geolocation::startRequest(GeoNotifier *notifier)
{
if (isDenied())
notifier->setFatalError(PositionError::create(PositionError::PERMISSION_DENIED, permissionDeniedErrorMessage));
else if (haveSuitableCachedPosition(notifier->options()))
notifier->setUseCachedPosition();
else if (notifier->hasZeroTimeout())
notifier->startTimerIfNeeded();
else if (!isAllowed()) {
m_pendingForPermissionNotifiers.add(notifier);
requestPermission();
} else if (startUpdating(notifier))
notifier->startTimerIfNeeded();
else
notifier->setFatalError(PositionError::create(PositionError::POSITION_UNAVAILABLE, failedToStartServiceErrorMessage));
}
void Geolocation::fatalErrorOccurred(Geolocation::GeoNotifier* notifier)
{
m_oneShots.remove(notifier);
m_watchers.remove(notifier);
if (!hasListeners())
stopUpdating();
}
void Geolocation::requestUsesCachedPosition(GeoNotifier* notifier)
{
if (isDenied()) {
notifier->setFatalError(PositionError::create(PositionError::PERMISSION_DENIED, permissionDeniedErrorMessage));
return;
}
m_requestsAwaitingCachedPosition.add(notifier);
if (isAllowed()) {
makeCachedPositionCallbacks();
return;
}
requestPermission();
}
void Geolocation::makeCachedPositionCallbacks()
{
GeoNotifierSet::const_iterator end = m_requestsAwaitingCachedPosition.end();
for (GeoNotifierSet::const_iterator iter = m_requestsAwaitingCachedPosition.begin(); iter != end; ++iter) {
GeoNotifier* notifier = iter->get();
notifier->runSuccessCallback(lastPosition());
if (m_oneShots.contains(notifier))
m_oneShots.remove(notifier);
else if (m_watchers.contains(notifier)) {
if (notifier->hasZeroTimeout() || startUpdating(notifier))
notifier->startTimerIfNeeded();
else
notifier->setFatalError(PositionError::create(PositionError::POSITION_UNAVAILABLE, failedToStartServiceErrorMessage));
}
}
m_requestsAwaitingCachedPosition.clear();
if (!hasListeners())
stopUpdating();
}
void Geolocation::requestTimedOut(GeoNotifier* notifier)
{
m_oneShots.remove(notifier);
if (!hasListeners())
stopUpdating();
}
bool Geolocation::haveSuitableCachedPosition(PositionOptions* options)
{
Geoposition* cachedPosition = lastPosition();
if (!cachedPosition)
return false;
if (!options->hasMaximumAge())
return true;
if (!options->maximumAge())
return false;
DOMTimeStamp currentTimeMillis = convertSecondsToDOMTimeStamp(currentTime());
return cachedPosition->timestamp() > currentTimeMillis - options->maximumAge();
}
void Geolocation::clearWatch(int watchID)
{
if (watchID <= 0)
return;
if (GeoNotifier* notifier = m_watchers.find(watchID))
m_pendingForPermissionNotifiers.remove(notifier);
m_watchers.remove(watchID);
if (!hasListeners())
stopUpdating();
}
void Geolocation::setIsAllowed(bool allowed)
{
RefPtrWillBeRawPtr<Geolocation> protect(this);
m_allowGeolocation = allowed ? Yes : No;
if (!m_pendingForPermissionNotifiers.isEmpty()) {
handlePendingPermissionNotifiers();
m_pendingForPermissionNotifiers.clear();
return;
}
if (!isAllowed()) {
RefPtrWillBeRawPtr<PositionError> error = PositionError::create(PositionError::PERMISSION_DENIED, permissionDeniedErrorMessage);
error->setIsFatal(true);
handleError(error.get());
m_requestsAwaitingCachedPosition.clear();
return;
}
if (lastPosition())
makeSuccessCallbacks();
else
makeCachedPositionCallbacks();
}
void Geolocation::sendError(GeoNotifierVector& notifiers, PositionError* error)
{
GeoNotifierVector::const_iterator end = notifiers.end();
for (GeoNotifierVector::const_iterator it = notifiers.begin(); it != end; ++it)
(*it)->runErrorCallback(error);
}
void Geolocation::sendPosition(GeoNotifierVector& notifiers, Geoposition* position)
{
GeoNotifierVector::const_iterator end = notifiers.end();
for (GeoNotifierVector::const_iterator it = notifiers.begin(); it != end; ++it)
(*it)->runSuccessCallback(position);
}
void Geolocation::stopTimer(GeoNotifierVector& notifiers)
{
GeoNotifierVector::const_iterator end = notifiers.end();
for (GeoNotifierVector::const_iterator it = notifiers.begin(); it != end; ++it)
(*it)->stopTimer();
}
void Geolocation::stopTimersForOneShots()
{
GeoNotifierVector copy;
copyToVector(m_oneShots, copy);
stopTimer(copy);
}
void Geolocation::stopTimersForWatchers()
{
GeoNotifierVector copy;
m_watchers.getNotifiersVector(copy);
stopTimer(copy);
}
void Geolocation::stopTimers()
{
stopTimersForOneShots();
stopTimersForWatchers();
}
void Geolocation::cancelRequests(GeoNotifierVector& notifiers)
{
GeoNotifierVector::const_iterator end = notifiers.end();
for (GeoNotifierVector::const_iterator it = notifiers.begin(); it != end; ++it)
(*it)->setFatalError(PositionError::create(PositionError::POSITION_UNAVAILABLE, framelessDocumentErrorMessage));
}
void Geolocation::cancelAllRequests()
{
GeoNotifierVector copy;
copyToVector(m_oneShots, copy);
cancelRequests(copy);
m_watchers.getNotifiersVector(copy);
cancelRequests(copy);
}
void Geolocation::extractNotifiersWithCachedPosition(GeoNotifierVector& notifiers, GeoNotifierVector* cached)
{
GeoNotifierVector nonCached;
GeoNotifierVector::iterator end = notifiers.end();
for (GeoNotifierVector::const_iterator it = notifiers.begin(); it != end; ++it) {
GeoNotifier* notifier = it->get();
if (notifier->useCachedPosition()) {
if (cached)
cached->append(notifier);
} else
nonCached.append(notifier);
}
notifiers.swap(nonCached);
}
void Geolocation::copyToSet(const GeoNotifierVector& src, GeoNotifierSet& dest)
{
GeoNotifierVector::const_iterator end = src.end();
for (GeoNotifierVector::const_iterator it = src.begin(); it != end; ++it) {
GeoNotifier* notifier = it->get();
dest.add(notifier);
}
}
void Geolocation::handleError(PositionError* error)
{
ASSERT(error);
GeoNotifierVector oneShotsCopy;
copyToVector(m_oneShots, oneShotsCopy);
GeoNotifierVector watchersCopy;
m_watchers.getNotifiersVector(watchersCopy);
GeoNotifierVector oneShotsWithCachedPosition;
m_oneShots.clear();
if (error->isFatal())
m_watchers.clear();
else {
extractNotifiersWithCachedPosition(oneShotsCopy, &oneShotsWithCachedPosition);
extractNotifiersWithCachedPosition(watchersCopy, 0);
}
sendError(oneShotsCopy, error);
sendError(watchersCopy, error);
if (!hasListeners())
stopUpdating();
copyToSet(oneShotsWithCachedPosition, m_oneShots);
}
void Geolocation::requestPermission()
{
if (m_allowGeolocation > Unknown)
return;
Page* page = this->page();
if (!page)
return;
m_allowGeolocation = InProgress;
GeolocationController::from(page)->requestPermission(this);
}
void Geolocation::makeSuccessCallbacks()
{
ASSERT(lastPosition());
ASSERT(isAllowed());
GeoNotifierVector oneShotsCopy;
copyToVector(m_oneShots, oneShotsCopy);
GeoNotifierVector watchersCopy;
m_watchers.getNotifiersVector(watchersCopy);
m_oneShots.clear();
m_requestsAwaitingCachedPosition.clear();
sendPosition(oneShotsCopy, lastPosition());
sendPosition(watchersCopy, lastPosition());
if (!hasListeners())
stopUpdating();
}
void Geolocation::positionChanged()
{
ASSERT(isAllowed());
stopTimers();
makeSuccessCallbacks();
}
void Geolocation::setError(GeolocationError* error)
{
RefPtrWillBeRawPtr<PositionError> positionError = createPositionError(error);
handleError(positionError.get());
}
bool Geolocation::startUpdating(GeoNotifier* notifier)
{
Page* page = this->page();
if (!page)
return false;
GeolocationController::from(page)->addObserver(this, notifier->options()->enableHighAccuracy());
return true;
}
void Geolocation::stopUpdating()
{
Page* page = this->page();
if (!page)
return;
GeolocationController::from(page)->removeObserver(this);
}
void Geolocation::handlePendingPermissionNotifiers()
{
GeoNotifierSet::const_iterator end = m_pendingForPermissionNotifiers.end();
for (GeoNotifierSet::const_iterator iter = m_pendingForPermissionNotifiers.begin(); iter != end; ++iter) {
GeoNotifier* notifier = iter->get();
if (isAllowed()) {
if (startUpdating(notifier))
notifier->startTimerIfNeeded();
else
notifier->setFatalError(PositionError::create(PositionError::POSITION_UNAVAILABLE, failedToStartServiceErrorMessage));
} else {
notifier->setFatalError(PositionError::create(PositionError::PERMISSION_DENIED, permissionDeniedErrorMessage));
}
}
}
}