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.
6 #include "modules/geolocation/GeoNotifier.h"
8 #include "modules/geolocation/Geolocation.h"
9 #include "modules/geolocation/PositionError.h"
10 #include "modules/geolocation/PositionOptions.h"
14 GeoNotifier::GeoNotifier(Geolocation* geolocation, PassOwnPtr<PositionCallback> successCallback, PassOwnPtr<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)
20 , m_timer(this, &GeoNotifier::timerFired)
21 , m_useCachedPosition(false)
23 ASSERT(m_geolocation);
24 ASSERT(m_successCallback);
28 void GeoNotifier::trace(Visitor* visitor)
30 visitor->trace(m_geolocation);
31 visitor->trace(m_options);
32 visitor->trace(m_fatalError);
35 void GeoNotifier::setFatalError(PositionError* error)
37 // If a fatal error has already been set, stick with it. This makes sure that
38 // when permission is denied, this is the error reported, as required by the
44 // An existing timer may not have a zero timeout.
46 m_timer.startOneShot(0, FROM_HERE);
49 void GeoNotifier::setUseCachedPosition()
51 m_useCachedPosition = true;
52 m_timer.startOneShot(0, FROM_HERE);
55 void GeoNotifier::runSuccessCallback(Geoposition* position)
57 m_successCallback->handleEvent(position);
60 void GeoNotifier::runErrorCallback(PositionError* error)
63 m_errorCallback->handleEvent(error);
66 void GeoNotifier::startTimer()
68 m_timer.startOneShot(m_options->timeout() / 1000.0, FROM_HERE);
71 void GeoNotifier::stopTimer()
76 void GeoNotifier::timerFired(Timer<GeoNotifier>*)
80 // Test for fatal error first. This is required for the case where the LocalFrame is
81 // disconnected and requests are cancelled.
83 runErrorCallback(m_fatalError.get());
84 // This will cause this notifier to be deleted.
85 m_geolocation->fatalErrorOccurred(this);
89 if (m_useCachedPosition) {
90 // Clear the cached position flag in case this is a watch request, which
91 // will continue to run.
92 m_useCachedPosition = false;
93 m_geolocation->requestUsesCachedPosition(this);
98 m_errorCallback->handleEvent(PositionError::create(PositionError::TIMEOUT, "Timeout expired"));
99 m_geolocation->requestTimedOut(this);