2 * Copyright (C) 2008-2011, 2015 Apple Inc. All Rights Reserved.
3 * Copyright (C) 2009 Torch Mobile, Inc.
4 * Copyright 2010, The Android Open Source Project
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "GeoNotifier.h"
31 #if ENABLE(GEOLOCATION)
33 #include "Geolocation.h"
37 GeoNotifier::GeoNotifier(Geolocation& geolocation, Ref<PositionCallback>&& successCallback, RefPtr<PositionErrorCallback>&& errorCallback, PositionOptions&& options)
38 : m_geolocation(geolocation)
39 , m_successCallback(WTFMove(successCallback))
40 , m_errorCallback(WTFMove(errorCallback))
41 , m_options(WTFMove(options))
42 , m_timer(*this, &GeoNotifier::timerFired)
43 , m_useCachedPosition(false)
47 void GeoNotifier::setFatalError(RefPtr<PositionError>&& error)
49 // If a fatal error has already been set, stick with it. This makes sure that
50 // when permission is denied, this is the error reported, as required by the
55 m_fatalError = WTFMove(error);
56 // An existing timer may not have a zero timeout.
58 m_timer.startOneShot(0);
61 void GeoNotifier::setUseCachedPosition()
63 m_useCachedPosition = true;
64 m_timer.startOneShot(0);
67 bool GeoNotifier::hasZeroTimeout() const
69 return !m_options.timeout;
72 void GeoNotifier::runSuccessCallback(Geoposition* position)
74 // If we are here and the Geolocation permission is not approved, something has
75 // gone horribly wrong.
76 if (!m_geolocation->isAllowed())
79 m_successCallback->handleEvent(position);
82 void GeoNotifier::runErrorCallback(PositionError* error)
85 m_errorCallback->handleEvent(error);
88 void GeoNotifier::startTimerIfNeeded()
90 m_timer.startOneShot(m_options.timeout / 1000.0);
93 void GeoNotifier::stopTimer()
98 void GeoNotifier::timerFired()
102 // Protect this GeoNotifier object, since it
103 // could be deleted by a call to clearWatch in a callback.
104 Ref<GeoNotifier> protectedThis(*this);
106 // Test for fatal error first. This is required for the case where the Frame is
107 // disconnected and requests are cancelled.
109 runErrorCallback(m_fatalError.get());
110 // This will cause this notifier to be deleted.
111 m_geolocation->fatalErrorOccurred(this);
115 if (m_useCachedPosition) {
116 // Clear the cached position flag in case this is a watch request, which
117 // will continue to run.
118 m_useCachedPosition = false;
119 m_geolocation->requestUsesCachedPosition(this);
123 if (m_errorCallback) {
124 RefPtr<PositionError> error = PositionError::create(PositionError::TIMEOUT, ASCIILiteral("Timeout expired"));
125 m_errorCallback->handleEvent(error.get());
127 m_geolocation->requestTimedOut(this);
130 } // namespace WebCore
132 #endif // ENABLE(GEOLOCATION)