GeolocationController.cpp [plain text]
#include "config.h"
#include "GeolocationController.h"
#if ENABLE(GEOLOCATION)
#include "GeolocationClient.h"
#include "GeolocationError.h"
#include "GeolocationPosition.h"
namespace WebCore {
GeolocationController::GeolocationController(Page& page, GeolocationClient& client)
: m_page(page)
, m_client(client)
{
m_page.addActivityStateChangeObserver(*this);
}
GeolocationController::~GeolocationController()
{
ASSERT(m_observers.isEmpty());
m_client.geolocationDestroyed();
}
void GeolocationController::addObserver(Geolocation& observer, bool enableHighAccuracy)
{
bool wasEmpty = m_observers.isEmpty();
m_observers.add(observer);
if (enableHighAccuracy)
m_highAccuracyObservers.add(observer);
if (enableHighAccuracy)
m_client.setEnableHighAccuracy(true);
if (wasEmpty && m_page.isVisible())
m_client.startUpdating();
}
void GeolocationController::removeObserver(Geolocation& observer)
{
if (!m_observers.contains(observer))
return;
m_observers.remove(observer);
m_highAccuracyObservers.remove(observer);
if (m_observers.isEmpty())
m_client.stopUpdating();
else if (m_highAccuracyObservers.isEmpty())
m_client.setEnableHighAccuracy(false);
}
void GeolocationController::requestPermission(Geolocation& geolocation)
{
if (!m_page.isVisible()) {
m_pendingPermissionRequest.add(geolocation);
return;
}
m_client.requestPermission(geolocation);
}
void GeolocationController::cancelPermissionRequest(Geolocation& geolocation)
{
if (m_pendingPermissionRequest.remove(geolocation))
return;
m_client.cancelPermissionRequest(geolocation);
}
void GeolocationController::positionChanged(const std::optional<GeolocationPosition>& position)
{
m_lastPosition = position;
Vector<Ref<Geolocation>> observersVector;
observersVector.reserveInitialCapacity(m_observers.size());
for (auto& observer : m_observers)
observersVector.uncheckedAppend(observer.copyRef());
for (auto& observer : observersVector)
observer->positionChanged();
}
void GeolocationController::errorOccurred(GeolocationError& error)
{
Vector<Ref<Geolocation>> observersVector;
observersVector.reserveInitialCapacity(m_observers.size());
for (auto& observer : m_observers)
observersVector.uncheckedAppend(observer.copyRef());
for (auto& observer : observersVector)
observer->setError(error);
}
std::optional<GeolocationPosition> GeolocationController::lastPosition()
{
if (m_lastPosition)
return m_lastPosition.value();
return m_client.lastPosition();
}
void GeolocationController::activityStateDidChange(ActivityState::Flags oldActivityState, ActivityState::Flags newActivityState)
{
ActivityState::Flags changed = oldActivityState ^ newActivityState;
if (changed & ActivityState::IsVisible && !m_observers.isEmpty()) {
if (newActivityState & ActivityState::IsVisible)
m_client.startUpdating();
else
m_client.stopUpdating();
}
if (!m_page.isVisible())
return;
auto pendedPermissionRequests = WTFMove(m_pendingPermissionRequest);
for (auto& permissionRequest : pendedPermissionRequests)
m_client.requestPermission(permissionRequest.get());
}
const char* GeolocationController::supplementName()
{
return "GeolocationController";
}
void provideGeolocationTo(Page* page, GeolocationClient& client)
{
ASSERT(page);
Supplement<Page>::provideTo(page, GeolocationController::supplementName(), std::make_unique<GeolocationController>(*page, client));
}
}
#endif // ENABLE(GEOLOCATION)