Home | History | Annotate | Download | only in geolocation
      1 // Copyright 2014 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "config.h"
      6 #include "modules/geolocation/GeoNotifier.h"
      7 
      8 #include "modules/geolocation/Geolocation.h"
      9 #include "modules/geolocation/PositionError.h"
     10 #include "modules/geolocation/PositionOptions.h"
     11 
     12 namespace blink {
     13 
     14 GeoNotifier::GeoNotifier(Geolocation* geolocation, PositionCallback* successCallback, PositionErrorCallback* errorCallback, PositionOptions* options)
     15     // FIXME : m_geolocation should be removed, it makes circular dependancy.
     16     : m_geolocation(geolocation)
     17     , m_successCallback(successCallback)
     18     , m_errorCallback(errorCallback)
     19     , m_options(options)
     20     , m_timer(this, &GeoNotifier::timerFired)
     21     , m_useCachedPosition(false)
     22 {
     23     ASSERT(m_geolocation);
     24     ASSERT(m_successCallback);
     25     ASSERT(m_options);
     26 }
     27 
     28 void GeoNotifier::trace(Visitor* visitor)
     29 {
     30     visitor->trace(m_geolocation);
     31     visitor->trace(m_successCallback);
     32     visitor->trace(m_errorCallback);
     33     visitor->trace(m_options);
     34     visitor->trace(m_fatalError);
     35 }
     36 
     37 void GeoNotifier::setFatalError(PositionError* error)
     38 {
     39     // If a fatal error has already been set, stick with it. This makes sure that
     40     // when permission is denied, this is the error reported, as required by the
     41     // spec.
     42     if (m_fatalError)
     43         return;
     44 
     45     m_fatalError = error;
     46     // An existing timer may not have a zero timeout.
     47     m_timer.stop();
     48     m_timer.startOneShot(0, FROM_HERE);
     49 }
     50 
     51 void GeoNotifier::setUseCachedPosition()
     52 {
     53     m_useCachedPosition = true;
     54     m_timer.startOneShot(0, FROM_HERE);
     55 }
     56 
     57 void GeoNotifier::runSuccessCallback(Geoposition* position)
     58 {
     59     m_successCallback->handleEvent(position);
     60 }
     61 
     62 void GeoNotifier::runErrorCallback(PositionError* error)
     63 {
     64     if (m_errorCallback)
     65         m_errorCallback->handleEvent(error);
     66 }
     67 
     68 void GeoNotifier::startTimer()
     69 {
     70     m_timer.startOneShot(m_options->timeout() / 1000.0, FROM_HERE);
     71 }
     72 
     73 void GeoNotifier::stopTimer()
     74 {
     75     m_timer.stop();
     76 }
     77 
     78 void GeoNotifier::timerFired(Timer<GeoNotifier>*)
     79 {
     80     m_timer.stop();
     81 
     82     // Test for fatal error first. This is required for the case where the LocalFrame is
     83     // disconnected and requests are cancelled.
     84     if (m_fatalError) {
     85         runErrorCallback(m_fatalError.get());
     86         // This will cause this notifier to be deleted.
     87         m_geolocation->fatalErrorOccurred(this);
     88         return;
     89     }
     90 
     91     if (m_useCachedPosition) {
     92         // Clear the cached position flag in case this is a watch request, which
     93         // will continue to run.
     94         m_useCachedPosition = false;
     95         m_geolocation->requestUsesCachedPosition(this);
     96         return;
     97     }
     98 
     99     if (m_errorCallback)
    100         m_errorCallback->handleEvent(PositionError::create(PositionError::TIMEOUT, "Timeout expired"));
    101     m_geolocation->requestTimedOut(this);
    102 }
    103 
    104 } // namespace blink
    105