src/3rdparty/webkit/WebCore/page/Geolocation.h
changeset 30 5dc02b23752f
parent 3 41300fa6a67c
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
     1 /*
     1 /*
     2  * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
     2  * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
       
     3  * Copyright 2010, The Android Open Source Project
     3  *
     4  *
     4  * Redistribution and use in source and binary forms, with or without
     5  * Redistribution and use in source and binary forms, with or without
     5  * modification, are permitted provided that the following conditions
     6  * modification, are permitted provided that the following conditions
     6  * are met:
     7  * are met:
     7  * 1. Redistributions of source code must retain the above copyright
     8  * 1. Redistributions of source code must retain the above copyright
    24  */
    25  */
    25 
    26 
    26 #ifndef Geolocation_h
    27 #ifndef Geolocation_h
    27 #define Geolocation_h
    28 #define Geolocation_h
    28 
    29 
       
    30 #include "GeolocationPositionCache.h"
    29 #include "GeolocationService.h"
    31 #include "GeolocationService.h"
       
    32 #include "Geoposition.h"
    30 #include "PositionCallback.h"
    33 #include "PositionCallback.h"
    31 #include "PositionError.h"
    34 #include "PositionError.h"
    32 #include "PositionErrorCallback.h"
    35 #include "PositionErrorCallback.h"
    33 #include "PositionOptions.h"
    36 #include "PositionOptions.h"
    34 #include "Timer.h"
    37 #include "Timer.h"
    35 #include <wtf/Platform.h>
       
    36 #include <wtf/HashMap.h>
       
    37 #include <wtf/HashSet.h>
       
    38 #include <wtf/OwnPtr.h>
       
    39 #include <wtf/PassRefPtr.h>
       
    40 #include <wtf/RefCounted.h>
       
    41 #include <wtf/RefPtr.h>
       
    42 #include <wtf/Vector.h>
       
    43 
    38 
    44 namespace WebCore {
    39 namespace WebCore {
    45 
    40 
    46 class Frame;
    41 class Frame;
    47 class Geoposition;
       
    48 
    42 
    49 class Geolocation : public RefCounted<Geolocation>, public GeolocationServiceClient {
    43 #if ENABLE(CLIENT_BASED_GEOLOCATION)
       
    44 class GeolocationPosition;
       
    45 class GeolocationError;
       
    46 #endif
       
    47 
       
    48 class Geolocation : public RefCounted<Geolocation>
       
    49 #if !ENABLE(CLIENT_BASED_GEOLOCATION) && ENABLE(GEOLOCATION)
       
    50     , public GeolocationServiceClient
       
    51 #endif
       
    52 {
    50 public:
    53 public:
    51     static PassRefPtr<Geolocation> create(Frame* frame) { return adoptRef(new Geolocation(frame)); }
    54     static PassRefPtr<Geolocation> create(Frame* frame) { return adoptRef(new Geolocation(frame)); }
    52 
    55 
    53     virtual ~Geolocation() {}
    56     ~Geolocation();
    54 
    57 
    55     void disconnectFrame();
    58     void disconnectFrame();
    56     
    59     
    57     Geoposition* lastPosition() const { return m_service->lastPosition(); }
       
    58 
       
    59     void getCurrentPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
    60     void getCurrentPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
    60     int watchPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
    61     int watchPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
    61     void clearWatch(int watchId);
    62     void clearWatch(int watchId);
    62 
    63 
    63     void suspend();
       
    64     void resume();
       
    65     
       
    66     void setIsAllowed(bool);
    64     void setIsAllowed(bool);
       
    65     Frame* frame() const { return m_frame; }
       
    66 
       
    67 #if ENABLE(CLIENT_BASED_GEOLOCATION)
       
    68     void setPosition(GeolocationPosition*);
       
    69     void setError(GeolocationError*);
       
    70 #else
       
    71     GeolocationService* getGeolocationService() const { return m_service.get(); }
       
    72 #endif
       
    73 
       
    74 private:
       
    75     Geoposition* lastPosition();
       
    76 
    67     bool isAllowed() const { return m_allowGeolocation == Yes; }
    77     bool isAllowed() const { return m_allowGeolocation == Yes; }
    68     bool isDenied() const { return m_allowGeolocation == No; }
    78     bool isDenied() const { return m_allowGeolocation == No; }
    69     
    79     
    70     void setShouldClearCache(bool shouldClearCache) { m_shouldClearCache = shouldClearCache; }
       
    71     bool shouldClearCache() const { return m_shouldClearCache; }
       
    72 
       
    73 private:
       
    74     Geolocation(Frame*);
    80     Geolocation(Frame*);
    75 
    81 
    76     class GeoNotifier : public RefCounted<GeoNotifier> {
    82     class GeoNotifier : public RefCounted<GeoNotifier> {
    77     public:
    83     public:
    78         static PassRefPtr<GeoNotifier> create(Geolocation* geolocation, PassRefPtr<PositionCallback> positionCallback, PassRefPtr<PositionErrorCallback> positionErrorCallback, PassRefPtr<PositionOptions> options) { return adoptRef(new GeoNotifier(geolocation, positionCallback, positionErrorCallback, options)); }
    84         static PassRefPtr<GeoNotifier> create(Geolocation* geolocation, PassRefPtr<PositionCallback> positionCallback, PassRefPtr<PositionErrorCallback> positionErrorCallback, PassRefPtr<PositionOptions> options) { return adoptRef(new GeoNotifier(geolocation, positionCallback, positionErrorCallback, options)); }
    79         
    85         
    80         void setFatalError(PassRefPtr<PositionError>);
    86         void setFatalError(PassRefPtr<PositionError>);
    81         bool hasZeroTimeout() const;
    87         bool hasZeroTimeout() const;
       
    88         void setUseCachedPosition();
       
    89         void runSuccessCallback(Geoposition*);
    82         void startTimerIfNeeded();
    90         void startTimerIfNeeded();
    83         void timerFired(Timer<GeoNotifier>*);
    91         void timerFired(Timer<GeoNotifier>*);
    84         
    92         
    85         Geolocation* m_geolocation;
    93         Geolocation* m_geolocation;
    86         RefPtr<PositionCallback> m_successCallback;
    94         RefPtr<PositionCallback> m_successCallback;
    87         RefPtr<PositionErrorCallback> m_errorCallback;
    95         RefPtr<PositionErrorCallback> m_errorCallback;
    88         RefPtr<PositionOptions> m_options;
    96         RefPtr<PositionOptions> m_options;
    89         Timer<GeoNotifier> m_timer;
    97         Timer<GeoNotifier> m_timer;
    90         RefPtr<PositionError> m_fatalError;
    98         RefPtr<PositionError> m_fatalError;
       
    99         bool m_useCachedPosition;
    91 
   100 
    92     private:
   101     private:
    93         GeoNotifier(Geolocation*, PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
   102         GeoNotifier(Geolocation*, PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
    94     };
   103     };
    95 
   104 
    96     class Watchers {
   105     class Watchers {
    97     public:
   106     public:
    98         void set(int id, PassRefPtr<GeoNotifier>);
   107         void set(int id, PassRefPtr<GeoNotifier>);
    99         void remove(int id);
   108         void remove(int id);
   100         void remove(GeoNotifier*);
   109         void remove(GeoNotifier*);
       
   110         bool contains(GeoNotifier*) const;
   101         void clear();
   111         void clear();
   102         bool isEmpty() const;
   112         bool isEmpty() const;
   103         void getNotifiersVector(Vector<RefPtr<GeoNotifier> >&) const;
   113         void getNotifiersVector(Vector<RefPtr<GeoNotifier> >&) const;
   104     private:
   114     private:
   105         typedef HashMap<int, RefPtr<GeoNotifier> > IdToNotifierMap;
   115         typedef HashMap<int, RefPtr<GeoNotifier> > IdToNotifierMap;
   116     static void stopTimer(Vector<RefPtr<GeoNotifier> >&);
   126     static void stopTimer(Vector<RefPtr<GeoNotifier> >&);
   117     void stopTimersForOneShots();
   127     void stopTimersForOneShots();
   118     void stopTimersForWatchers();
   128     void stopTimersForWatchers();
   119     void stopTimers();
   129     void stopTimers();
   120     
   130     
       
   131     void positionChanged(PassRefPtr<Geoposition>);
   121     void makeSuccessCallbacks();
   132     void makeSuccessCallbacks();
   122     void handleError(PositionError*);
   133     void handleError(PositionError*);
   123 
   134 
   124     void requestPermission();
   135     void requestPermission();
   125 
   136 
       
   137     bool startUpdating(GeoNotifier*);
       
   138     void stopUpdating();
       
   139 
       
   140 #if !ENABLE(CLIENT_BASED_GEOLOCATION) && ENABLE(GEOLOCATION)
   126     // GeolocationServiceClient
   141     // GeolocationServiceClient
   127     virtual void geolocationServicePositionChanged(GeolocationService*);
   142     virtual void geolocationServicePositionChanged(GeolocationService*);
   128     virtual void geolocationServiceErrorOccurred(GeolocationService*);
   143     virtual void geolocationServiceErrorOccurred(GeolocationService*);
       
   144 #endif
   129 
   145 
   130     PassRefPtr<GeoNotifier> startRequest(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
   146     PassRefPtr<GeoNotifier> startRequest(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
   131 
   147 
   132     void fatalErrorOccurred(GeoNotifier*);
   148     void fatalErrorOccurred(GeoNotifier*);
   133     void requestTimedOut(GeoNotifier*);
   149     void requestTimedOut(GeoNotifier*);
       
   150     void requestUsesCachedPosition(GeoNotifier*);
       
   151     bool haveSuitableCachedPosition(PositionOptions*);
       
   152     void makeCachedPositionCallbacks();
   134 
   153 
   135     typedef HashSet<RefPtr<GeoNotifier> > GeoNotifierSet;
   154     typedef HashSet<RefPtr<GeoNotifier> > GeoNotifierSet;
   136     
   155     
   137     GeoNotifierSet m_oneShots;
   156     GeoNotifierSet m_oneShots;
   138     Watchers m_watchers;
   157     Watchers m_watchers;
   139     Frame* m_frame;
   158     Frame* m_frame;
       
   159 #if !ENABLE(CLIENT_BASED_GEOLOCATION)
   140     OwnPtr<GeolocationService> m_service;
   160     OwnPtr<GeolocationService> m_service;
       
   161 #else
       
   162     RefPtr<GeoNotifier> m_startRequestPermissionNotifier;
       
   163 #endif
       
   164     RefPtr<Geoposition> m_lastPosition;
       
   165     RefPtr<Geoposition> m_currentPosition;
   141 
   166 
   142     enum {
   167     enum {
   143         Unknown,
   168         Unknown,
   144         InProgress,
   169         InProgress,
   145         Yes,
   170         Yes,
   146         No
   171         No
   147     } m_allowGeolocation;
   172     } m_allowGeolocation;
   148     bool m_shouldClearCache;
   173     bool m_shouldClearCache;
       
   174 
       
   175 #if ENABLE(GEOLOCATION)
       
   176     OwnPtr<GeolocationPositionCache> m_positionCache;
       
   177 #endif
       
   178     GeoNotifierSet m_requestsAwaitingCachedPosition;
   149 };
   179 };
   150     
   180     
   151 } // namespace WebCore
   181 } // namespace WebCore
   152 
   182 
   153 #endif // Geolocation_h
   183 #endif // Geolocation_h