WebGeolocationServiceMock.cpp 9.13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
 * Copyright (C) 2010 Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *     * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "WebGeolocationServiceMock.h"

#include "GeolocationService.h"
#include "GeolocationServiceChromium.h"
#include "GeolocationServiceMock.h"
37
#include "WebGeolocationServiceBridge.h"
38 39
#include "WebString.h"
#include <wtf/CurrentTime.h>
40
#include <wtf/HashMap.h>
41
#include <wtf/Vector.h>
42 43 44 45 46 47 48 49 50 51 52 53

#if ENABLE(GEOLOCATION)
using WebCore::Coordinates;
using WebCore::Frame;
using WebCore::Geolocation;
using WebCore::GeolocationServiceBridge;
using WebCore::GeolocationServiceChromium;
using WebCore::GeolocationServiceClient;
using WebCore::GeolocationServiceMock;
using WebCore::Geoposition;
using WebCore::PositionError;
using WebCore::PositionOptions;
54
using WTF::String;
55
using WTF::Vector;
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126

namespace WebCore {
class GeolocationServiceChromiumMock : public GeolocationServiceChromium, public GeolocationServiceClient {
public:
    static GeolocationService* create(GeolocationServiceClient*);
    virtual bool startUpdating(PositionOptions*);
    virtual void stopUpdating();
    virtual Geoposition* lastPosition() const;
    virtual PositionError* lastError() const;

    virtual void geolocationServicePositionChanged(GeolocationService*);
    virtual void geolocationServiceErrorOccurred(GeolocationService*);

private:
    explicit GeolocationServiceChromiumMock(GeolocationServiceClient*);

    GeolocationServiceClient* m_geolocationServiceClient;
    OwnPtr<GeolocationService> m_geolocationServiceMock;
};

GeolocationService* GeolocationServiceChromiumMock::create(GeolocationServiceClient* geolocationServiceClient)
{
    return new GeolocationServiceChromiumMock(geolocationServiceClient);
}

GeolocationServiceChromiumMock::GeolocationServiceChromiumMock(GeolocationServiceClient* geolocationServiceClient)
    : GeolocationServiceChromium(geolocationServiceClient),
      m_geolocationServiceClient(geolocationServiceClient)
{
      m_geolocationServiceMock.set(GeolocationServiceMock::create(this));
}

bool GeolocationServiceChromiumMock::startUpdating(PositionOptions* positionOptions)
{
    GeolocationServiceChromium::startUpdating(positionOptions);
    return m_geolocationServiceMock->startUpdating(positionOptions);
}

void GeolocationServiceChromiumMock::stopUpdating()
{
    GeolocationServiceChromium::stopUpdating();
    m_geolocationServiceMock->stopUpdating();
}

Geoposition* GeolocationServiceChromiumMock::lastPosition() const
{
    return m_geolocationServiceMock->lastPosition();
}

PositionError* GeolocationServiceChromiumMock::lastError() const
{
    return m_geolocationServiceMock->lastError();
}

void GeolocationServiceChromiumMock::geolocationServicePositionChanged(GeolocationService* geolocationService)
{
    ASSERT_UNUSED(geolocationService, geolocationService == m_geolocationServiceMock);
    m_geolocationServiceClient->geolocationServicePositionChanged(this);

}

void GeolocationServiceChromiumMock::geolocationServiceErrorOccurred(GeolocationService* geolocationService)
{
    ASSERT_UNUSED(geolocationService, geolocationService == m_geolocationServiceMock);
    m_geolocationServiceClient->geolocationServiceErrorOccurred(this);
}

} // namespace WebCore

namespace WebKit {

127 128
class WebGeolocationServiceMockImpl : public WebGeolocationServiceMock {
public:
129 130 131 132 133
    WebGeolocationServiceMockImpl();
    virtual ~WebGeolocationServiceMockImpl();
    static void setMockGeolocationPermission(bool allowed);

    // WebGeolocationService
134 135 136 137 138
    virtual void requestPermissionForFrame(int bridgeId, const WebURL& url);
    virtual int attachBridge(WebGeolocationServiceBridge*);
    virtual void detachBridge(int bridgeId);

private:
139 140
    void notifyPendingPermissions();

141 142
    typedef HashMap<int, WebGeolocationServiceBridge*> IdToBridgeMap;
    IdToBridgeMap m_idToBridgeMap;
143 144 145 146 147 148 149 150 151 152
    Vector<int> m_pendingPermissionRequests;

    // In addition to the singleton instance pointer, we need to keep the setMockGeolocationPermission() state
    // as a static (not object members) as this call may come in before the service has been created.
    static enum PermissionState {
        PermissionStateUnset,
        PermissionStateAllowed,
        PermissionStateDenied,
    } s_permissionState;
    static WebGeolocationServiceMockImpl* s_instance;
153 154
};

155 156
WebGeolocationServiceMockImpl::PermissionState WebGeolocationServiceMockImpl::s_permissionState = WebGeolocationServiceMockImpl::PermissionStateUnset;
WebGeolocationServiceMockImpl* WebGeolocationServiceMockImpl::s_instance = 0;
157

158 159 160 161 162
WebGeolocationServiceMock* WebGeolocationServiceMock::createWebGeolocationServiceMock()
{
    return new WebGeolocationServiceMockImpl;
}

163 164
void WebGeolocationServiceMock::setMockGeolocationPermission(bool allowed)
{
165
    WebGeolocationServiceMockImpl::setMockGeolocationPermission(allowed);
166 167
}

168 169 170 171 172 173 174 175 176 177 178 179 180 181
void WebGeolocationServiceMock::setMockGeolocationPosition(double latitude, double longitude, double accuracy)
{
    WebCore::GeolocationService::setCustomMockFactory(&WebCore::GeolocationServiceChromiumMock::create);
    RefPtr<Geoposition> geoposition = Geoposition::create(Coordinates::create(latitude, longitude, false, 0, accuracy, true, 0, false, 0, false, 0), currentTime() * 1000.0);
    GeolocationServiceMock::setPosition(geoposition);
}

void WebGeolocationServiceMock::setMockGeolocationError(int errorCode, const WebString& message)
{
    WebCore::GeolocationService::setCustomMockFactory(&WebCore::GeolocationServiceChromiumMock::create);
    RefPtr<PositionError> positionError = PositionError::create(static_cast<PositionError::ErrorCode>(errorCode), message);
    GeolocationServiceMock::setError(positionError);
}

182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
WebGeolocationServiceMockImpl::WebGeolocationServiceMockImpl()
{
    ASSERT(!s_instance);
    s_instance = this;
}

WebGeolocationServiceMockImpl::~WebGeolocationServiceMockImpl()
{
    ASSERT(this == s_instance);
    s_instance = 0;
    // Reset the permission state, so any future service instance (e.g. running
    // multiple tests in a single DRT run) will see a clean call sequence.
    s_permissionState = PermissionStateUnset;
    for (IdToBridgeMap::iterator it = m_idToBridgeMap.begin(); it != m_idToBridgeMap.end(); ++it)
        it->second->didDestroyGeolocationService();
}

void WebGeolocationServiceMockImpl::setMockGeolocationPermission(bool allowed)
{
    s_permissionState = allowed ? PermissionStateAllowed : PermissionStateDenied;
    if (s_instance)
        s_instance->notifyPendingPermissions();
}

206
void WebGeolocationServiceMockImpl::requestPermissionForFrame(int bridgeId, const WebURL& url)
207
{
208 209 210
    m_pendingPermissionRequests.append(bridgeId);
    if (s_permissionState != PermissionStateUnset)
        notifyPendingPermissions();
211 212
}

213
int WebGeolocationServiceMockImpl::attachBridge(WebGeolocationServiceBridge* bridge)
214 215 216 217 218 219 220 221 222
{
    static int nextAvailableWatchId = 1;
    // In case of overflow, make sure the ID remains positive, but reuse the ID values.
    if (nextAvailableWatchId < 1)
        nextAvailableWatchId = 1;
    m_idToBridgeMap.set(nextAvailableWatchId, bridge);
    return nextAvailableWatchId++;
}

223
void WebGeolocationServiceMockImpl::detachBridge(int bridgeId)
224 225 226 227
{
    m_idToBridgeMap.remove(bridgeId);
}

228 229 230 231 232 233 234 235 236 237 238 239 240
void WebGeolocationServiceMockImpl::notifyPendingPermissions()
{
    ASSERT(s_permissionState == PermissionStateAllowed || s_permissionState ==  PermissionStateDenied);
    Vector<int> pendingPermissionRequests;
    pendingPermissionRequests.swap(m_pendingPermissionRequests);
    for (Vector<int>::const_iterator it = pendingPermissionRequests.begin(); it != pendingPermissionRequests.end(); ++it) {
        ASSERT(*it > 0);
        IdToBridgeMap::iterator iter = m_idToBridgeMap.find(*it);
        if (iter != m_idToBridgeMap.end())
            iter->second->setIsAllowed(s_permissionState == PermissionStateAllowed);
    }
}

241 242
} // namespace WebKit
#endif // ENABLE(GEOLOCATION)