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