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 PassOwnPtr<GeolocationService> GeolocationServiceMock::create(GeolocationServiceClient* client)
44 {
45     initStatics();
46     return adoptPtr(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 
startUpdating(PositionOptions *)83 bool GeolocationServiceMock::startUpdating(PositionOptions*)
84 {
85     m_isActive = true;
86     m_timer.startOneShot(0);
87     return true;
88 }
89 
stopUpdating()90 void GeolocationServiceMock::stopUpdating()
91 {
92     m_isActive = false;
93 }
94 
timerFired(Timer<GeolocationServiceMock> * timer)95 void GeolocationServiceMock::timerFired(Timer<GeolocationServiceMock>* timer)
96 {
97     ASSERT_UNUSED(timer, timer == &m_timer);
98     makeGeolocationCallback();
99 }
100 
makeGeolocationCallbackFromAllInstances()101 void GeolocationServiceMock::makeGeolocationCallbackFromAllInstances()
102 {
103     GeolocationServiceSet::const_iterator end = s_instances->end();
104     for (GeolocationServiceSet::const_iterator iter = s_instances->begin(); iter != end; ++iter)
105         (*iter)->makeGeolocationCallback();
106 }
107 
makeGeolocationCallback()108 void GeolocationServiceMock::makeGeolocationCallback()
109 {
110     if (!m_isActive)
111         return;
112 
113     if (*s_lastPosition)
114         positionChanged();
115     else if (*s_lastError)
116         errorOccurred();
117 }
118 
initStatics()119 void GeolocationServiceMock::initStatics()
120 {
121     if (s_instances == 0) {
122         s_instances = new GeolocationServiceSet;
123         s_lastPosition = new RefPtr<Geoposition>;
124         s_lastError = new RefPtr<PositionError>;
125     }
126 }
127 
cleanUpStatics()128 void GeolocationServiceMock::cleanUpStatics()
129 {
130     if (s_instances->size() == 0) {
131         delete s_instances;
132         s_instances = 0;
133         delete s_lastPosition;
134         delete s_lastError;
135     }
136 }
137 
138 } // namespace WebCore
139 
140 #endif
141