#ifndef Geolocation_h
#define Geolocation_h
#include "bindings/v8/ScriptWrappable.h"
#include "core/dom/ActiveDOMObject.h"
#include "heap/Handle.h"
#include "modules/geolocation/Geoposition.h"
#include "modules/geolocation/PositionCallback.h"
#include "modules/geolocation/PositionError.h"
#include "modules/geolocation/PositionErrorCallback.h"
#include "modules/geolocation/PositionOptions.h"
#include "platform/Timer.h"
namespace WebCore {
class Document;
class LocalFrame;
class GeolocationController;
class GeolocationError;
class GeolocationPosition;
class Page;
class ExecutionContext;
class Geolocation FINAL : public RefCountedWillBeGarbageCollectedFinalized<Geolocation>, public ScriptWrappable, public ActiveDOMObject
{
public:
static PassRefPtrWillBeRawPtr<Geolocation> create(ExecutionContext*);
virtual ~Geolocation();
void trace(Visitor*);
virtual void stop() OVERRIDE;
Document* document() const;
LocalFrame* frame() const;
void getCurrentPosition(PassOwnPtr<PositionCallback>, PassOwnPtr<PositionErrorCallback>, PassRefPtrWillBeRawPtr<PositionOptions>);
int watchPosition(PassOwnPtr<PositionCallback>, PassOwnPtr<PositionErrorCallback>, PassRefPtrWillBeRawPtr<PositionOptions>);
void clearWatch(int watchID);
void setIsAllowed(bool);
bool isAllowed() const { return m_allowGeolocation == Yes; }
void positionChanged();
void setError(GeolocationError*);
private:
Geoposition* lastPosition();
bool isDenied() const { return m_allowGeolocation == No; }
explicit Geolocation(ExecutionContext*);
Page* page() const;
class GeoNotifier : public RefCountedWillBeGarbageCollectedFinalized<GeoNotifier> {
public:
static PassRefPtrWillBeRawPtr<GeoNotifier> create(Geolocation* geolocation, PassOwnPtr<PositionCallback> positionCallback, PassOwnPtr<PositionErrorCallback> positionErrorCallback, PassRefPtrWillBeRawPtr<PositionOptions> options)
{
return adoptRefWillBeNoop(new GeoNotifier(geolocation, positionCallback, positionErrorCallback, options));
}
void trace(Visitor*);
PositionOptions* options() const { return m_options.get(); };
void setFatalError(PassRefPtrWillBeRawPtr<PositionError>);
bool useCachedPosition() const { return m_useCachedPosition; }
void setUseCachedPosition();
void runSuccessCallback(Geoposition*);
void runErrorCallback(PositionError*);
void startTimerIfNeeded();
void stopTimer();
void timerFired(Timer<GeoNotifier>*);
bool hasZeroTimeout() const;
private:
GeoNotifier(Geolocation*, PassOwnPtr<PositionCallback>, PassOwnPtr<PositionErrorCallback>, PassRefPtrWillBeRawPtr<PositionOptions>);
RefPtrWillBeMember<Geolocation> m_geolocation;
OwnPtr<PositionCallback> m_successCallback;
OwnPtr<PositionErrorCallback> m_errorCallback;
RefPtrWillBeMember<PositionOptions> m_options;
Timer<GeoNotifier> m_timer;
RefPtrWillBeMember<PositionError> m_fatalError;
bool m_useCachedPosition;
};
typedef WillBeHeapVector<RefPtrWillBeMember<GeoNotifier> > GeoNotifierVector;
typedef WillBeHeapHashSet<RefPtrWillBeMember<GeoNotifier> > GeoNotifierSet;
class Watchers {
public:
void trace(Visitor*);
bool add(int id, PassRefPtrWillBeRawPtr<GeoNotifier>);
GeoNotifier* find(int id);
void remove(int id);
void remove(GeoNotifier*);
bool contains(GeoNotifier*) const;
void clear();
bool isEmpty() const;
void getNotifiersVector(GeoNotifierVector&) const;
private:
typedef WillBeHeapHashMap<int, RefPtrWillBeMember<GeoNotifier> > IdToNotifierMap;
typedef WillBeHeapHashMap<RefPtrWillBeMember<GeoNotifier>, int> NotifierToIdMap;
IdToNotifierMap m_idToNotifierMap;
NotifierToIdMap m_notifierToIdMap;
};
bool hasListeners() const { return !m_oneShots.isEmpty() || !m_watchers.isEmpty(); }
void sendError(GeoNotifierVector&, PositionError*);
void sendPosition(GeoNotifierVector&, Geoposition*);
static void extractNotifiersWithCachedPosition(GeoNotifierVector& notifiers, GeoNotifierVector* cached);
static void copyToSet(const GeoNotifierVector& src, GeoNotifierSet& dest);
static void stopTimer(GeoNotifierVector&);
void stopTimersForOneShots();
void stopTimersForWatchers();
void stopTimers();
void cancelRequests(GeoNotifierVector&);
void cancelAllRequests();
void makeSuccessCallbacks();
void handleError(PositionError*);
void requestPermission();
bool startUpdating(GeoNotifier*);
void stopUpdating();
void handlePendingPermissionNotifiers();
void startRequest(GeoNotifier*);
void fatalErrorOccurred(GeoNotifier*);
void requestTimedOut(GeoNotifier*);
void requestUsesCachedPosition(GeoNotifier*);
bool haveSuitableCachedPosition(PositionOptions*);
void makeCachedPositionCallbacks();
GeoNotifierSet m_oneShots;
Watchers m_watchers;
GeoNotifierSet m_pendingForPermissionNotifiers;
RefPtrWillBeMember<Geoposition> m_lastPosition;
enum {
Unknown,
InProgress,
Yes,
No
} m_allowGeolocation;
GeoNotifierSet m_requestsAwaitingCachedPosition;
};
}
#endif