• 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 #include "Logging.h"
30 #include "Geolocation.h"
31 #include "Geoposition.h"
32 #include "PositionError.h"
33 #include "PositionOptions.h"
34 
35 namespace WebCore {
36 
37 GeolocationServiceMock::GeolocationServiceSet* GeolocationServiceMock::s_instances = 0;
38 RefPtr<Geoposition>* GeolocationServiceMock::s_lastPosition;
39 RefPtr<PositionError>* GeolocationServiceMock::s_lastError;
40 
create(GeolocationServiceClient * client)41 GeolocationService* GeolocationServiceMock::create(GeolocationServiceClient* client)
42 {
43     initStatics();
44     return new GeolocationServiceMock(client);
45 }
46 
GeolocationServiceMock(GeolocationServiceClient * client)47 GeolocationServiceMock::GeolocationServiceMock(GeolocationServiceClient* client)
48     : GeolocationService(client)
49     , m_timer(this, &GeolocationServiceMock::timerFired)
50     , m_isActive(false)
51 {
52     s_instances->add(this);
53 }
54 
~GeolocationServiceMock()55 GeolocationServiceMock::~GeolocationServiceMock()
56 {
57     GeolocationServiceSet::iterator iter = s_instances->find(this);
58     ASSERT(iter != s_instances->end());
59     s_instances->remove(iter);
60     cleanUpStatics();
61 }
62 
setPosition(PassRefPtr<Geoposition> position)63 void GeolocationServiceMock::setPosition(PassRefPtr<Geoposition> position)
64 {
65     initStatics();
66     GeolocationService::useMock();
67     *s_lastPosition = position;
68     *s_lastError = 0;
69     makeGeolocationCallbackFromAllInstances();
70 }
71 
setError(PassRefPtr<PositionError> error)72 void GeolocationServiceMock::setError(PassRefPtr<PositionError> error)
73 {
74     initStatics();
75     GeolocationService::useMock();
76     *s_lastError = error;
77     *s_lastPosition = 0;
78     makeGeolocationCallbackFromAllInstances();
79 }
80 
startUpdating(PositionOptions *)81 bool GeolocationServiceMock::startUpdating(PositionOptions*)
82 {
83     m_isActive = true;
84     m_timer.startOneShot(0);
85     return true;
86 }
87 
stopUpdating()88 void GeolocationServiceMock::stopUpdating()
89 {
90     m_isActive = false;
91 }
92 
timerFired(Timer<GeolocationServiceMock> * timer)93 void GeolocationServiceMock::timerFired(Timer<GeolocationServiceMock>* timer)
94 {
95     ASSERT_UNUSED(timer, timer == &m_timer);
96     makeGeolocationCallback();
97 }
98 
makeGeolocationCallbackFromAllInstances()99 void GeolocationServiceMock::makeGeolocationCallbackFromAllInstances()
100 {
101     GeolocationServiceSet::const_iterator end = s_instances->end();
102     for (GeolocationServiceSet::const_iterator iter = s_instances->begin(); iter != end; ++iter)
103         (*iter)->makeGeolocationCallback();
104 }
105 
makeGeolocationCallback()106 void GeolocationServiceMock::makeGeolocationCallback()
107 {
108     if (!m_isActive)
109         return;
110 
111     if (*s_lastPosition)
112         positionChanged();
113     else if (*s_lastError)
114         errorOccurred();
115 }
116 
initStatics()117 void GeolocationServiceMock::initStatics()
118 {
119     if (s_instances == 0) {
120         s_instances = new GeolocationServiceSet;
121         s_lastPosition = new RefPtr<Geoposition>;
122         s_lastError = new RefPtr<PositionError>;
123     }
124 }
125 
cleanUpStatics()126 void GeolocationServiceMock::cleanUpStatics()
127 {
128     if (s_instances->size() == 0) {
129         delete s_instances;
130         s_instances = 0;
131         delete s_lastPosition;
132         delete s_lastError;
133     }
134 }
135 
136 } // namespace WebCore
137