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, RefPtr<PositionCallback>&& successCallback, RefPtr<PositionErrorCallback>&& errorCallback, RefPtr<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)
45 ASSERT(m_successCallback);
46 // If no options were supplied from JS, we should have created a default set
47 // of options in JSGeolocationCustom.cpp.
51 void GeoNotifier::setFatalError(RefPtr<PositionError>&& error)
53 // If a fatal error has already been set, stick with it. This makes sure that
54 // when permission is denied, this is the error reported, as required by the
59 m_fatalError = WTFMove(error);
60 // An existing timer may not have a zero timeout.
62 m_timer.startOneShot(0);
65 void GeoNotifier::setUseCachedPosition()
67 m_useCachedPosition = true;
68 m_timer.startOneShot(0);
71 bool GeoNotifier::hasZeroTimeout() const
73 return m_options->hasTimeout() && !m_options->timeout();
76 void GeoNotifier::runSuccessCallback(Geoposition* position)
78 // If we are here and the Geolocation permission is not approved, something has
79 // gone horribly wrong.
80 if (!m_geolocation->isAllowed())
83 m_successCallback->handleEvent(position);
86 void GeoNotifier::runErrorCallback(PositionError* error)
89 m_errorCallback->handleEvent(error);
92 void GeoNotifier::startTimerIfNeeded()
94 if (m_options->hasTimeout())
95 m_timer.startOneShot(m_options->timeout() / 1000.0);
98 void GeoNotifier::stopTimer()
103 void GeoNotifier::timerFired()
107 // Protect this GeoNotifier object, since it
108 // could be deleted by a call to clearWatch in a callback.
109 Ref<GeoNotifier> protectedThis(*this);
111 // Test for fatal error first. This is required for the case where the Frame is
112 // disconnected and requests are cancelled.
114 runErrorCallback(m_fatalError.get());
115 // This will cause this notifier to be deleted.
116 m_geolocation->fatalErrorOccurred(this);
120 if (m_useCachedPosition) {
121 // Clear the cached position flag in case this is a watch request, which
122 // will continue to run.
123 m_useCachedPosition = false;
124 m_geolocation->requestUsesCachedPosition(this);
128 if (m_errorCallback) {
129 RefPtr<PositionError> error = PositionError::create(PositionError::TIMEOUT, ASCIILiteral("Timeout expired"));
130 m_errorCallback->handleEvent(error.get());
132 m_geolocation->requestTimedOut(this);
135 } // namespace WebCore
137 #endif // ENABLE(GEOLOCATION)