• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include "config.h"
27 #include "GeolocationServiceMock.h"
28 
29 #if ENABLE(GEOLOCATION)
30 
31 #include "Logging.h"
32 #include "Geolocation.h"
33 #include "Geoposition.h"
34 #include "PositionError.h"
35 #include "PositionOptions.h"
36 
37 namespace WebCore {
38 
39 GeolocationServiceMock::GeolocationServiceSet* GeolocationServiceMock::s_instances = 0;
40 RefPtr<Geoposition>* GeolocationServiceMock::s_lastPosition;
41 RefPtr<PositionError>* GeolocationServiceMock::s_lastError;
42 
create(GeolocationServiceClient * client)43 GeolocationService* GeolocationServiceMock::create(GeolocationServiceClient* client)
44 {
45     initStatics();
46     return new GeolocationServiceMock(client);
47 }
48 
GeolocationServiceMock(GeolocationServiceClient * client)49 GeolocationServiceMock::GeolocationServiceMock(GeolocationServiceClient* client)
50     : GeolocationService(client)
51     , m_timer(this, &GeolocationServiceMock::timerFired)
52     , m_isActive(false)
53 {
54     s_instances->add(this);
55 }
56 
~GeolocationServiceMock()57 GeolocationServiceMock::~GeolocationServiceMock()
58 {
59     GeolocationServiceSet::iterator iter = s_instances->find(this);
60     ASSERT(iter != s_instances->end());
61     s_instances->remove(iter);
62     cleanUpStatics();
63 }
64 
setPosition(PassRefPtr<Geoposition> position)65 void GeolocationServiceMock::setPosition(PassRefPtr<Geoposition> position)
66 {
67     initStatics();
68     GeolocationService::useMock();
69     *s_lastPosition = position;
70     *s_lastError = 0;
71     makeGeolocationCallbackFromAllInstances();
72 }
73 
setError(PassRefPtr<PositionError> error)74 void GeolocationServiceMock::setError(PassRefPtr<PositionError> error)
75 {
76     initStatics();
77     GeolocationService::useMock();
78     *s_lastError = error;
79     *s_lastPosition = 0;
80     makeGeolocationCallbackFromAllInstances();
81 }
82 
83 #if PLATFORM(ANDROID)
84 // TODO: Upstream to webkit.org. See https://bugs.webkit.org/show_bug.cgi?id=34082
startUpdating(PositionOptions *,bool)85 bool GeolocationServiceMock::startUpdating(PositionOptions*, bool /* suspend */)
86 #else
87 bool GeolocationServiceMock::startUpdating(PositionOptions*)
88 #endif
89 {
90     m_isActive = true;
91     m_timer.startOneShot(0);
92     return true;
93 }
94 
stopUpdating()95 void GeolocationServiceMock::stopUpdating()
96 {
97     m_isActive = false;
98 }
99 
timerFired(Timer<GeolocationServiceMock> * timer)100 void GeolocationServiceMock::timerFired(Timer<GeolocationServiceMock>* timer)
101 {
102     ASSERT_UNUSED(timer, timer == &m_timer);
103     makeGeolocationCallback();
104 }
105 
makeGeolocationCallbackFromAllInstances()106 void GeolocationServiceMock::makeGeolocationCallbackFromAllInstances()
107 {
108     GeolocationServiceSet::const_iterator end = s_instances->end();
109     for (GeolocationServiceSet::const_iterator iter = s_instances->begin(); iter != end; ++iter)
110         (*iter)->makeGeolocationCallback();
111 }
112 
makeGeolocationCallback()113 void GeolocationServiceMock::makeGeolocationCallback()
114 {
115     if (!m_isActive)
116         return;
117 
118     if (*s_lastPosition)
119         positionChanged();
120     else if (*s_lastError)
121         errorOccurred();
122 }
123 
initStatics()124 void GeolocationServiceMock::initStatics()
125 {
126     if (s_instances == 0) {
127         s_instances = new GeolocationServiceSet;
128         s_lastPosition = new RefPtr<Geoposition>;
129         s_lastError = new RefPtr<PositionError>;
130     }
131 }
132 
cleanUpStatics()133 void GeolocationServiceMock::cleanUpStatics()
134 {
135     if (s_instances->size() == 0) {
136         delete s_instances;
137         s_instances = 0;
138         delete s_lastPosition;
139         delete s_lastError;
140     }
141 }
142 
143 } // namespace WebCore
144 
145 #endif
146