WebCore/page/Geolocation.h
changeset 0 4f2f89ce4247
child 2 303757a437d3
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2008, 2009 Apple Inc. All Rights Reserved.
       
     3  * Copyright 2010, The Android Open Source Project
       
     4  *
       
     5  * Redistribution and use in source and binary forms, with or without
       
     6  * modification, are permitted provided that the following conditions
       
     7  * are met:
       
     8  * 1. Redistributions of source code must retain the above copyright
       
     9  *    notice, this list of conditions and the following disclaimer.
       
    10  * 2. Redistributions in binary form must reproduce the above copyright
       
    11  *    notice, this list of conditions and the following disclaimer in the
       
    12  *    documentation and/or other materials provided with the distribution.
       
    13  *
       
    14  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
       
    15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
       
    16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
       
    18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
       
    22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
       
    25  */
       
    26 
       
    27 #ifndef Geolocation_h
       
    28 #define Geolocation_h
       
    29 
       
    30 #include "GeolocationPositionCache.h"
       
    31 #include "Geoposition.h"
       
    32 #include "PositionCallback.h"
       
    33 #include "PositionError.h"
       
    34 #include "PositionErrorCallback.h"
       
    35 #include "PositionOptions.h"
       
    36 #include "Timer.h"
       
    37 
       
    38 #if !ENABLE(CLIENT_BASED_GEOLOCATION)
       
    39 #include "GeolocationService.h"
       
    40 #endif
       
    41 
       
    42 namespace WebCore {
       
    43 
       
    44 class Frame;
       
    45 
       
    46 #if ENABLE(CLIENT_BASED_GEOLOCATION)
       
    47 class GeolocationPosition;
       
    48 class GeolocationError;
       
    49 #endif
       
    50 
       
    51 class Geolocation : public RefCounted<Geolocation>
       
    52 #if !ENABLE(CLIENT_BASED_GEOLOCATION) && ENABLE(GEOLOCATION)
       
    53     , public GeolocationServiceClient
       
    54 #endif
       
    55 {
       
    56 public:
       
    57     static PassRefPtr<Geolocation> create(Frame* frame) { return adoptRef(new Geolocation(frame)); }
       
    58 
       
    59     ~Geolocation();
       
    60 
       
    61     void disconnectFrame();
       
    62     
       
    63     void getCurrentPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
       
    64     int watchPosition(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
       
    65     void clearWatch(int watchId);
       
    66 
       
    67     // These methods are used by Android.
       
    68     void suspend();
       
    69     void resume();
       
    70 
       
    71     void setIsAllowed(bool);
       
    72     Frame* frame() const { return m_frame; }
       
    73 
       
    74 #if ENABLE(CLIENT_BASED_GEOLOCATION)
       
    75     void positionChanged();
       
    76     void setError(GeolocationError*);
       
    77 #else
       
    78     GeolocationService* getGeolocationService() const { return m_service.get(); }
       
    79 #endif
       
    80 
       
    81 private:
       
    82     Geoposition* lastPosition();
       
    83 
       
    84     bool isAllowed() const { return m_allowGeolocation == Yes; }
       
    85     bool isDenied() const { return m_allowGeolocation == No; }
       
    86     
       
    87     Geolocation(Frame*);
       
    88 
       
    89     class GeoNotifier : public RefCounted<GeoNotifier> {
       
    90     public:
       
    91         static PassRefPtr<GeoNotifier> create(Geolocation* geolocation, PassRefPtr<PositionCallback> positionCallback, PassRefPtr<PositionErrorCallback> positionErrorCallback, PassRefPtr<PositionOptions> options) { return adoptRef(new GeoNotifier(geolocation, positionCallback, positionErrorCallback, options)); }
       
    92         
       
    93         void setFatalError(PassRefPtr<PositionError>);
       
    94         bool hasZeroTimeout() const;
       
    95         void setUseCachedPosition();
       
    96         void runSuccessCallback(Geoposition*);
       
    97         void startTimerIfNeeded();
       
    98         void timerFired(Timer<GeoNotifier>*);
       
    99         
       
   100         Geolocation* m_geolocation;
       
   101         RefPtr<PositionCallback> m_successCallback;
       
   102         RefPtr<PositionErrorCallback> m_errorCallback;
       
   103         RefPtr<PositionOptions> m_options;
       
   104         Timer<GeoNotifier> m_timer;
       
   105         RefPtr<PositionError> m_fatalError;
       
   106         bool m_useCachedPosition;
       
   107 
       
   108     private:
       
   109         GeoNotifier(Geolocation*, PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
       
   110     };
       
   111 
       
   112     class Watchers {
       
   113     public:
       
   114         void set(int id, PassRefPtr<GeoNotifier>);
       
   115         void remove(int id);
       
   116         void remove(GeoNotifier*);
       
   117         bool contains(GeoNotifier*) const;
       
   118         void clear();
       
   119         bool isEmpty() const;
       
   120         void getNotifiersVector(Vector<RefPtr<GeoNotifier> >&) const;
       
   121     private:
       
   122         typedef HashMap<int, RefPtr<GeoNotifier> > IdToNotifierMap;
       
   123         typedef HashMap<RefPtr<GeoNotifier>, int> NotifierToIdMap;
       
   124         IdToNotifierMap m_idToNotifierMap;
       
   125         NotifierToIdMap m_notifierToIdMap;
       
   126     };
       
   127 
       
   128     bool hasListeners() const { return !m_oneShots.isEmpty() || !m_watchers.isEmpty(); }
       
   129 
       
   130     void sendError(Vector<RefPtr<GeoNotifier> >&, PositionError*);
       
   131     void sendPosition(Vector<RefPtr<GeoNotifier> >&, Geoposition*);
       
   132 
       
   133     static void stopTimer(Vector<RefPtr<GeoNotifier> >&);
       
   134     void stopTimersForOneShots();
       
   135     void stopTimersForWatchers();
       
   136     void stopTimers();
       
   137 
       
   138     void positionChangedInternal();
       
   139     void makeSuccessCallbacks();
       
   140     void handleError(PositionError*);
       
   141 
       
   142     void requestPermission();
       
   143 
       
   144     bool startUpdating(GeoNotifier*);
       
   145     void stopUpdating();
       
   146 
       
   147 #if !ENABLE(CLIENT_BASED_GEOLOCATION) && ENABLE(GEOLOCATION)
       
   148     // GeolocationServiceClient
       
   149     virtual void geolocationServicePositionChanged(GeolocationService*);
       
   150     virtual void geolocationServiceErrorOccurred(GeolocationService*);
       
   151 #endif
       
   152 
       
   153     PassRefPtr<GeoNotifier> startRequest(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
       
   154 
       
   155     void fatalErrorOccurred(GeoNotifier*);
       
   156     void requestTimedOut(GeoNotifier*);
       
   157     void requestUsesCachedPosition(GeoNotifier*);
       
   158     bool haveSuitableCachedPosition(PositionOptions*);
       
   159     void makeCachedPositionCallbacks();
       
   160 
       
   161     typedef HashSet<RefPtr<GeoNotifier> > GeoNotifierSet;
       
   162     
       
   163     GeoNotifierSet m_oneShots;
       
   164     Watchers m_watchers;
       
   165     Frame* m_frame;
       
   166 #if !ENABLE(CLIENT_BASED_GEOLOCATION)
       
   167     OwnPtr<GeolocationService> m_service;
       
   168 #endif
       
   169 #if USE(PREEMPT_GEOLOCATION_PERMISSION)
       
   170     RefPtr<GeoNotifier> m_startRequestPermissionNotifier;
       
   171 #endif
       
   172     RefPtr<Geoposition> m_lastPosition;
       
   173 
       
   174     enum {
       
   175         Unknown,
       
   176         InProgress,
       
   177         Yes,
       
   178         No
       
   179     } m_allowGeolocation;
       
   180 
       
   181 #if ENABLE(GEOLOCATION)
       
   182     OwnPtr<GeolocationPositionCache> m_positionCache;
       
   183 #endif
       
   184     GeoNotifierSet m_requestsAwaitingCachedPosition;
       
   185 };
       
   186     
       
   187 } // namespace WebCore
       
   188 
       
   189 #endif // Geolocation_h