1a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block/*
2a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * Copyright (C) 2009 Apple Inc. All Rights Reserved.
3a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block *
4a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * Redistribution and use in source and binary forms, with or without
5a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * modification, are permitted provided that the following conditions
6a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * are met:
7a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * 1. Redistributions of source code must retain the above copyright
8a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block *    notice, this list of conditions and the following disclaimer.
9a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * 2. Redistributions in binary form must reproduce the above copyright
10a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block *    notice, this list of conditions and the following disclaimer in the
11a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block *    documentation and/or other materials provided with the distribution.
12a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block *
13a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block */
25a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
26a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block#include "config.h"
279c3710c76ea425248782469af80633fd63cb6f8dSteve Block#include "GeolocationServiceMock.h"
28a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
29dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block#if ENABLE(GEOLOCATION)
30dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block
319c3710c76ea425248782469af80633fd63cb6f8dSteve Block#include "Logging.h"
329c3710c76ea425248782469af80633fd63cb6f8dSteve Block#include "Geolocation.h"
33a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block#include "Geoposition.h"
34a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block#include "PositionError.h"
35a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block#include "PositionOptions.h"
36a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
37a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Blocknamespace WebCore {
38a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
399c3710c76ea425248782469af80633fd63cb6f8dSteve BlockGeolocationServiceMock::GeolocationServiceSet* GeolocationServiceMock::s_instances = 0;
409c3710c76ea425248782469af80633fd63cb6f8dSteve BlockRefPtr<Geoposition>* GeolocationServiceMock::s_lastPosition;
419c3710c76ea425248782469af80633fd63cb6f8dSteve BlockRefPtr<PositionError>* GeolocationServiceMock::s_lastError;
42a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
439c3710c76ea425248782469af80633fd63cb6f8dSteve BlockGeolocationService* GeolocationServiceMock::create(GeolocationServiceClient* client)
44a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block{
45666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    initStatics();
469c3710c76ea425248782469af80633fd63cb6f8dSteve Block    return new GeolocationServiceMock(client);
47a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block}
48a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
499c3710c76ea425248782469af80633fd63cb6f8dSteve BlockGeolocationServiceMock::GeolocationServiceMock(GeolocationServiceClient* client)
50a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block    : GeolocationService(client)
519c3710c76ea425248782469af80633fd63cb6f8dSteve Block    , m_timer(this, &GeolocationServiceMock::timerFired)
52feee28e61b0337792c7129c54623c71adffc8ed1Steve Block    , m_isActive(false)
53a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block{
54666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    s_instances->add(this);
55a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block}
56a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
579c3710c76ea425248782469af80633fd63cb6f8dSteve BlockGeolocationServiceMock::~GeolocationServiceMock()
58a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block{
599c3710c76ea425248782469af80633fd63cb6f8dSteve Block    GeolocationServiceSet::iterator iter = s_instances->find(this);
60666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    ASSERT(iter != s_instances->end());
61666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    s_instances->remove(iter);
62666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    cleanUpStatics();
63a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block}
64a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
659c3710c76ea425248782469af80633fd63cb6f8dSteve Blockvoid GeolocationServiceMock::setPosition(PassRefPtr<Geoposition> position)
66a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block{
67666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    initStatics();
68f7d703686fcbad876e7ce6d21a52d9a6cd71d2e8Steve Block    GeolocationService::useMock();
69666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    *s_lastPosition = position;
70666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    *s_lastError = 0;
71a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block    makeGeolocationCallbackFromAllInstances();
72a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block}
73a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
749c3710c76ea425248782469af80633fd63cb6f8dSteve Blockvoid GeolocationServiceMock::setError(PassRefPtr<PositionError> error)
75a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block{
76666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    initStatics();
77f7d703686fcbad876e7ce6d21a52d9a6cd71d2e8Steve Block    GeolocationService::useMock();
78666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    *s_lastError = error;
79666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    *s_lastPosition = 0;
80a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block    makeGeolocationCallbackFromAllInstances();
81a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block}
82a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
839c3710c76ea425248782469af80633fd63cb6f8dSteve Blockbool GeolocationServiceMock::startUpdating(PositionOptions*)
84a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block{
85feee28e61b0337792c7129c54623c71adffc8ed1Steve Block    m_isActive = true;
86a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block    m_timer.startOneShot(0);
87a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block    return true;
88a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block}
89a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
909c3710c76ea425248782469af80633fd63cb6f8dSteve Blockvoid GeolocationServiceMock::stopUpdating()
91feee28e61b0337792c7129c54623c71adffc8ed1Steve Block{
92feee28e61b0337792c7129c54623c71adffc8ed1Steve Block    m_isActive = false;
93feee28e61b0337792c7129c54623c71adffc8ed1Steve Block}
94feee28e61b0337792c7129c54623c71adffc8ed1Steve Block
959c3710c76ea425248782469af80633fd63cb6f8dSteve Blockvoid GeolocationServiceMock::timerFired(Timer<GeolocationServiceMock>* timer)
96a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block{
97f7d703686fcbad876e7ce6d21a52d9a6cd71d2e8Steve Block    ASSERT_UNUSED(timer, timer == &m_timer);
98a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block    makeGeolocationCallback();
99a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block}
100a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
1019c3710c76ea425248782469af80633fd63cb6f8dSteve Blockvoid GeolocationServiceMock::makeGeolocationCallbackFromAllInstances()
102a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block{
1039c3710c76ea425248782469af80633fd63cb6f8dSteve Block    GeolocationServiceSet::const_iterator end = s_instances->end();
1049c3710c76ea425248782469af80633fd63cb6f8dSteve Block    for (GeolocationServiceSet::const_iterator iter = s_instances->begin(); iter != end; ++iter)
105a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block        (*iter)->makeGeolocationCallback();
106a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block}
107a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
1089c3710c76ea425248782469af80633fd63cb6f8dSteve Blockvoid GeolocationServiceMock::makeGeolocationCallback()
109a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block{
110feee28e61b0337792c7129c54623c71adffc8ed1Steve Block    if (!m_isActive)
111feee28e61b0337792c7129c54623c71adffc8ed1Steve Block        return;
112feee28e61b0337792c7129c54623c71adffc8ed1Steve Block
1139c3710c76ea425248782469af80633fd63cb6f8dSteve Block    if (*s_lastPosition)
114a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block        positionChanged();
1159c3710c76ea425248782469af80633fd63cb6f8dSteve Block    else if (*s_lastError)
116a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block        errorOccurred();
117a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block}
118a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block
1199c3710c76ea425248782469af80633fd63cb6f8dSteve Blockvoid GeolocationServiceMock::initStatics()
120666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block{
1219c3710c76ea425248782469af80633fd63cb6f8dSteve Block    if (s_instances == 0) {
1229c3710c76ea425248782469af80633fd63cb6f8dSteve Block        s_instances = new GeolocationServiceSet;
123666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block        s_lastPosition = new RefPtr<Geoposition>;
124666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block        s_lastError = new RefPtr<PositionError>;
125666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    }
126666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block}
127666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block
1289c3710c76ea425248782469af80633fd63cb6f8dSteve Blockvoid GeolocationServiceMock::cleanUpStatics()
129666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block{
130666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    if (s_instances->size() == 0) {
131666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block        delete s_instances;
132666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block        s_instances = 0;
133666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block        delete s_lastPosition;
134666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block        delete s_lastError;
135666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block    }
136666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block}
137666ce90734aced48a6dbbce4b87ae20b6f32d065Steve Block
138a47a1c89e31d5fd26298656c3e15c09e841bce83Steve Block} // namespace WebCore
139dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block
140dcc8cf2e65d1aa555cce12431a16547e66b469eeSteve Block#endif
141