WebKit/chromium/src/WebGeolocationServiceMock.cpp
changeset 0 4f2f89ce4247
equal deleted inserted replaced
-1:000000000000 0:4f2f89ce4247
       
     1 /*
       
     2  * Copyright (C) 2010 Google 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 are
       
     6  * met:
       
     7  *
       
     8  *     * Redistributions of source code must retain the above copyright
       
     9  * notice, this list of conditions and the following disclaimer.
       
    10  *     * Redistributions in binary form must reproduce the above
       
    11  * copyright notice, this list of conditions and the following disclaimer
       
    12  * in the documentation and/or other materials provided with the
       
    13  * distribution.
       
    14  *     * Neither the name of Google Inc. nor the names of its
       
    15  * contributors may be used to endorse or promote products derived from
       
    16  * this software without specific prior written permission.
       
    17  *
       
    18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    29  */
       
    30 
       
    31 #include "config.h"
       
    32 #include "WebGeolocationServiceMock.h"
       
    33 
       
    34 #include "GeolocationService.h"
       
    35 #include "GeolocationServiceChromium.h"
       
    36 #include "GeolocationServiceMock.h"
       
    37 #include "WebString.h"
       
    38 #include <wtf/CurrentTime.h>
       
    39 #include <wtf/HashMap.h>
       
    40 
       
    41 #if ENABLE(GEOLOCATION)
       
    42 
       
    43 using WebCore::Coordinates;
       
    44 using WebCore::Frame;
       
    45 using WebCore::Geolocation;
       
    46 using WebCore::GeolocationServiceBridge;
       
    47 using WebCore::GeolocationServiceChromium;
       
    48 using WebCore::GeolocationServiceClient;
       
    49 using WebCore::GeolocationServiceMock;
       
    50 using WebCore::Geoposition;
       
    51 using WebCore::PositionError;
       
    52 using WebCore::PositionOptions;
       
    53 using WebCore::String;
       
    54 
       
    55 namespace WebCore {
       
    56 class GeolocationServiceChromiumMock : public GeolocationServiceChromium, public GeolocationServiceClient {
       
    57 public:
       
    58     static GeolocationService* create(GeolocationServiceClient*);
       
    59     virtual bool startUpdating(PositionOptions*);
       
    60     virtual void stopUpdating();
       
    61     virtual Geoposition* lastPosition() const;
       
    62     virtual PositionError* lastError() const;
       
    63 
       
    64     virtual void geolocationServicePositionChanged(GeolocationService*);
       
    65     virtual void geolocationServiceErrorOccurred(GeolocationService*);
       
    66 
       
    67 private:
       
    68     explicit GeolocationServiceChromiumMock(GeolocationServiceClient*);
       
    69 
       
    70     GeolocationServiceClient* m_geolocationServiceClient;
       
    71     OwnPtr<GeolocationService> m_geolocationServiceMock;
       
    72 };
       
    73 
       
    74 GeolocationService* GeolocationServiceChromiumMock::create(GeolocationServiceClient* geolocationServiceClient)
       
    75 {
       
    76     return new GeolocationServiceChromiumMock(geolocationServiceClient);
       
    77 }
       
    78 
       
    79 GeolocationServiceChromiumMock::GeolocationServiceChromiumMock(GeolocationServiceClient* geolocationServiceClient)
       
    80     : GeolocationServiceChromium(geolocationServiceClient),
       
    81       m_geolocationServiceClient(geolocationServiceClient)
       
    82 {
       
    83       m_geolocationServiceMock.set(GeolocationServiceMock::create(this));
       
    84 }
       
    85 
       
    86 bool GeolocationServiceChromiumMock::startUpdating(PositionOptions* positionOptions)
       
    87 {
       
    88     GeolocationServiceChromium::startUpdating(positionOptions);
       
    89     return m_geolocationServiceMock->startUpdating(positionOptions);
       
    90 }
       
    91 
       
    92 void GeolocationServiceChromiumMock::stopUpdating()
       
    93 {
       
    94     GeolocationServiceChromium::stopUpdating();
       
    95     m_geolocationServiceMock->stopUpdating();
       
    96 }
       
    97 
       
    98 Geoposition* GeolocationServiceChromiumMock::lastPosition() const
       
    99 {
       
   100     return m_geolocationServiceMock->lastPosition();
       
   101 }
       
   102 
       
   103 PositionError* GeolocationServiceChromiumMock::lastError() const
       
   104 {
       
   105     return m_geolocationServiceMock->lastError();
       
   106 }
       
   107 
       
   108 void GeolocationServiceChromiumMock::geolocationServicePositionChanged(GeolocationService* geolocationService)
       
   109 {
       
   110     ASSERT_UNUSED(geolocationService, geolocationService == m_geolocationServiceMock);
       
   111     m_geolocationServiceClient->geolocationServicePositionChanged(this);
       
   112 
       
   113 }
       
   114 
       
   115 void GeolocationServiceChromiumMock::geolocationServiceErrorOccurred(GeolocationService* geolocationService)
       
   116 {
       
   117     ASSERT_UNUSED(geolocationService, geolocationService == m_geolocationServiceMock);
       
   118     m_geolocationServiceClient->geolocationServiceErrorOccurred(this);
       
   119 }
       
   120 
       
   121 } // namespace WebCore
       
   122 
       
   123 namespace WebKit {
       
   124 
       
   125 class WebGeolocationServiceMockImpl : public WebGeolocationServiceMock {
       
   126 public:
       
   127     virtual ~WebGeolocationServiceMockImpl() { }
       
   128     virtual void requestPermissionForFrame(int bridgeId, const WebURL& url);
       
   129     virtual int attachBridge(WebGeolocationServiceBridge*);
       
   130     virtual void detachBridge(int bridgeId);
       
   131 
       
   132 private:
       
   133     typedef HashMap<int, WebGeolocationServiceBridge*> IdToBridgeMap;
       
   134     IdToBridgeMap m_idToBridgeMap;
       
   135 };
       
   136 
       
   137 bool WebGeolocationServiceMock::s_mockGeolocationPermission = false;
       
   138 
       
   139 WebGeolocationServiceMock* WebGeolocationServiceMock::createWebGeolocationServiceMock()
       
   140 {
       
   141     return new WebGeolocationServiceMockImpl;
       
   142 }
       
   143 
       
   144 void WebGeolocationServiceMock::setMockGeolocationPermission(bool allowed)
       
   145 {
       
   146     s_mockGeolocationPermission = allowed;
       
   147 }
       
   148 
       
   149 void WebGeolocationServiceMock::setMockGeolocationPosition(double latitude, double longitude, double accuracy)
       
   150 {
       
   151     WebCore::GeolocationService::setCustomMockFactory(&WebCore::GeolocationServiceChromiumMock::create);
       
   152     RefPtr<Geoposition> geoposition = Geoposition::create(Coordinates::create(latitude, longitude, false, 0, accuracy, true, 0, false, 0, false, 0), currentTime() * 1000.0);
       
   153     GeolocationServiceMock::setPosition(geoposition);
       
   154 }
       
   155 
       
   156 void WebGeolocationServiceMock::setMockGeolocationError(int errorCode, const WebString& message)
       
   157 {
       
   158     WebCore::GeolocationService::setCustomMockFactory(&WebCore::GeolocationServiceChromiumMock::create);
       
   159     RefPtr<PositionError> positionError = PositionError::create(static_cast<PositionError::ErrorCode>(errorCode), message);
       
   160     GeolocationServiceMock::setError(positionError);
       
   161 }
       
   162 
       
   163 void WebGeolocationServiceMockImpl::requestPermissionForFrame(int bridgeId, const WebURL& url)
       
   164 {
       
   165     IdToBridgeMap::iterator iter = m_idToBridgeMap.find(bridgeId);
       
   166     if (iter == m_idToBridgeMap.end())
       
   167         return;
       
   168     iter->second->setIsAllowed(s_mockGeolocationPermission);
       
   169 }
       
   170 
       
   171 int WebGeolocationServiceMockImpl::attachBridge(WebGeolocationServiceBridge* bridge)
       
   172 {
       
   173     static int nextAvailableWatchId = 1;
       
   174     // In case of overflow, make sure the ID remains positive, but reuse the ID values.
       
   175     if (nextAvailableWatchId < 1)
       
   176         nextAvailableWatchId = 1;
       
   177     m_idToBridgeMap.set(nextAvailableWatchId, bridge);
       
   178     return nextAvailableWatchId++;
       
   179 }
       
   180 
       
   181 void WebGeolocationServiceMockImpl::detachBridge(int bridgeId)
       
   182 {
       
   183     m_idToBridgeMap.remove(bridgeId);
       
   184 }
       
   185 
       
   186 } // namespace WebKit
       
   187 
       
   188 #endif // ENABLE(GEOLOCATION)