Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / geolocation / GeoNotifier.cpp
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, 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)
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_options);
32     visitor->trace(m_fatalError);
33 }
34
35 void GeoNotifier::setFatalError(PositionError* error)
36 {
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
39     // spec.
40     if (m_fatalError)
41         return;
42
43     m_fatalError = error;
44     // An existing timer may not have a zero timeout.
45     m_timer.stop();
46     m_timer.startOneShot(0, FROM_HERE);
47 }
48
49 void GeoNotifier::setUseCachedPosition()
50 {
51     m_useCachedPosition = true;
52     m_timer.startOneShot(0, FROM_HERE);
53 }
54
55 void GeoNotifier::runSuccessCallback(Geoposition* position)
56 {
57     m_successCallback->handleEvent(position);
58 }
59
60 void GeoNotifier::runErrorCallback(PositionError* error)
61 {
62     if (m_errorCallback)
63         m_errorCallback->handleEvent(error);
64 }
65
66 void GeoNotifier::startTimer()
67 {
68     m_timer.startOneShot(m_options->timeout() / 1000.0, FROM_HERE);
69 }
70
71 void GeoNotifier::stopTimer()
72 {
73     m_timer.stop();
74 }
75
76 void GeoNotifier::timerFired(Timer<GeoNotifier>*)
77 {
78     m_timer.stop();
79
80     // Test for fatal error first. This is required for the case where the LocalFrame is
81     // disconnected and requests are cancelled.
82     if (m_fatalError) {
83         runErrorCallback(m_fatalError.get());
84         // This will cause this notifier to be deleted.
85         m_geolocation->fatalErrorOccurred(this);
86         return;
87     }
88
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);
94         return;
95     }
96
97     if (m_errorCallback)
98         m_errorCallback->handleEvent(PositionError::create(PositionError::TIMEOUT, "Timeout expired"));
99     m_geolocation->requestTimedOut(this);
100 }
101
102 } // namespace blink