Home | History | Annotate | Download | only in page
      1 /*
      2  * Copyright (C) 2009 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  *
     13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
     14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
     15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
     17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
     23  * THE POSSIBILITY OF SUCH DAMAGE.
     24  */
     25 
     26 #include "config.h"
     27 #include "GeolocationController.h"
     28 #include "GeolocationPosition.h"
     29 
     30 #if ENABLE(CLIENT_BASED_GEOLOCATION)
     31 
     32 #include "GeolocationClient.h"
     33 
     34 namespace WebCore {
     35 
     36 GeolocationController::GeolocationController(Page* page, GeolocationClient* client)
     37     : m_page(page)
     38     , m_client(client)
     39 {
     40 }
     41 
     42 GeolocationController::~GeolocationController()
     43 {
     44     ASSERT(m_observers.isEmpty());
     45 
     46     if (m_client)
     47         m_client->geolocationDestroyed();
     48 }
     49 
     50 void GeolocationController::addObserver(Geolocation* observer, bool enableHighAccuracy)
     51 {
     52     // This may be called multiple times with the same observer, though removeObserver()
     53     // is called only once with each.
     54     bool wasEmpty = m_observers.isEmpty();
     55     m_observers.add(observer);
     56     if (enableHighAccuracy)
     57         m_highAccuracyObservers.add(observer);
     58 
     59     if (m_client) {
     60         if (enableHighAccuracy)
     61             m_client->setEnableHighAccuracy(true);
     62 // See https://bugs.webkit.org/show_bug.cgi?id=87030
     63 #if !PLATFORM(ANDROID)
     64         if (wasEmpty)
     65 #endif
     66             m_client->startUpdating();
     67     }
     68 }
     69 
     70 void GeolocationController::removeObserver(Geolocation* observer)
     71 {
     72     if (!m_observers.contains(observer))
     73         return;
     74 
     75     m_observers.remove(observer);
     76     m_highAccuracyObservers.remove(observer);
     77 
     78     if (m_client) {
     79         if (m_observers.isEmpty())
     80             m_client->stopUpdating();
     81         else if (m_highAccuracyObservers.isEmpty())
     82             m_client->setEnableHighAccuracy(false);
     83     }
     84 }
     85 
     86 void GeolocationController::requestPermission(Geolocation* geolocation)
     87 {
     88     if (m_client)
     89         m_client->requestPermission(geolocation);
     90 }
     91 
     92 void GeolocationController::cancelPermissionRequest(Geolocation* geolocation)
     93 {
     94     if (m_client)
     95         m_client->cancelPermissionRequest(geolocation);
     96 }
     97 
     98 void GeolocationController::positionChanged(GeolocationPosition* position)
     99 {
    100     m_lastPosition = position;
    101     Vector<RefPtr<Geolocation> > observersVector;
    102     copyToVector(m_observers, observersVector);
    103     for (size_t i = 0; i < observersVector.size(); ++i)
    104         observersVector[i]->positionChanged();
    105 }
    106 
    107 void GeolocationController::errorOccurred(GeolocationError* error)
    108 {
    109     Vector<RefPtr<Geolocation> > observersVector;
    110     copyToVector(m_observers, observersVector);
    111     for (size_t i = 0; i < observersVector.size(); ++i)
    112         observersVector[i]->setError(error);
    113 }
    114 
    115 GeolocationPosition* GeolocationController::lastPosition()
    116 {
    117     if (m_lastPosition.get())
    118         return m_lastPosition.get();
    119 
    120     if (!m_client)
    121         return 0;
    122 
    123     return m_client->lastPosition();
    124 }
    125 
    126 } // namespace WebCore
    127 
    128 #endif // ENABLE(CLIENT_BASED_GEOLOCATION)
    129