1/*
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
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
14 *
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.
26 */
27
28#include "config.h"
29#include "GeoNotifier.h"
30
31#if ENABLE(GEOLOCATION)
32
33#include "Geolocation.h"
34
35namespace WebCore {
36
37GeoNotifier::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)
44{
45}
46
47void GeoNotifier::setFatalError(RefPtr<PositionError>&& error)
48{
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
51 // spec.
52 if (m_fatalError)
53 return;
54
55 m_fatalError = WTFMove(error);
56 // An existing timer may not have a zero timeout.
57 m_timer.stop();
58 m_timer.startOneShot(0_s);
59}
60
61void GeoNotifier::setUseCachedPosition()
62{
63 m_useCachedPosition = true;
64 m_timer.startOneShot(0_s);
65}
66
67bool GeoNotifier::hasZeroTimeout() const
68{
69 return !m_options.timeout;
70}
71
72void GeoNotifier::runSuccessCallback(Geoposition* position)
73{
74 // If we are here and the Geolocation permission is not approved, something has
75 // gone horribly wrong.
76 if (!m_geolocation->isAllowed())
77 CRASH();
78
79 m_successCallback->handleEvent(position);
80}
81
82void GeoNotifier::runErrorCallback(PositionError& error)
83{
84 if (m_errorCallback)
85 m_errorCallback->handleEvent(error);
86}
87
88void GeoNotifier::startTimerIfNeeded()
89{
90 m_timer.startOneShot(1_ms * m_options.timeout);
91}
92
93void GeoNotifier::stopTimer()
94{
95 m_timer.stop();
96}
97
98void GeoNotifier::timerFired()
99{
100 m_timer.stop();
101
102 // Protect this GeoNotifier object, since it
103 // could be deleted by a call to clearWatch in a callback.
104 Ref<GeoNotifier> protectedThis(*this);
105
106 // Test for fatal error first. This is required for the case where the Frame is
107 // disconnected and requests are cancelled.
108 if (m_fatalError) {
109 runErrorCallback(*m_fatalError);
110 // This will cause this notifier to be deleted.
111 m_geolocation->fatalErrorOccurred(this);
112 return;
113 }
114
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);
120 return;
121 }
122
123 if (m_errorCallback) {
124 auto error = PositionError::create(PositionError::TIMEOUT, "Timeout expired"_s);
125 m_errorCallback->handleEvent(error);
126 }
127 m_geolocation->requestTimedOut(this);
128}
129
130} // namespace WebCore
131
132#endif // ENABLE(GEOLOCATION)
133