WebKit/chromium/src/WebGeolocationServiceBridgeImpl.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 "WebGeolocationServiceBridgeImpl.h"
       
    33 
       
    34 #include "Chrome.h"
       
    35 #include "ChromeClientImpl.h"
       
    36 #include "Frame.h"
       
    37 #include "Geolocation.h"
       
    38 #include "GeolocationServiceChromium.h"
       
    39 #include "Geoposition.h"
       
    40 #include "Page.h"
       
    41 #include "PositionError.h"
       
    42 #include "PositionOptions.h"
       
    43 #include "WebFrame.h"
       
    44 #include "WebFrameImpl.h"
       
    45 #include "WebGeolocationService.h"
       
    46 #include "WebGeolocationServiceBridge.h"
       
    47 #include "WebViewClient.h"
       
    48 #include "WebViewImpl.h"
       
    49 
       
    50 #if ENABLE(GEOLOCATION)
       
    51 
       
    52 using WebCore::Coordinates;
       
    53 using WebCore::Frame;
       
    54 using WebCore::Geolocation;
       
    55 using WebCore::GeolocationServiceBridge;
       
    56 using WebCore::GeolocationServiceChromium;
       
    57 using WebCore::GeolocationServiceClient;
       
    58 using WebCore::Geoposition;
       
    59 using WebCore::PositionError;
       
    60 using WebCore::PositionOptions;
       
    61 using WebCore::String;
       
    62 
       
    63 namespace WebKit {
       
    64 
       
    65 class WebGeolocationServiceBridgeImpl : public GeolocationServiceBridge, public WebGeolocationServiceBridge {
       
    66 public:
       
    67     explicit WebGeolocationServiceBridgeImpl(GeolocationServiceChromium*);
       
    68     virtual ~WebGeolocationServiceBridgeImpl();
       
    69 
       
    70     // GeolocationServiceBridge
       
    71     virtual bool startUpdating(PositionOptions*);
       
    72     virtual void stopUpdating();
       
    73     virtual void suspend();
       
    74     virtual void resume();
       
    75     virtual int getBridgeId() const;
       
    76     virtual void attachBridgeIfNeeded();
       
    77 
       
    78     // WebGeolocationServiceBridge
       
    79     virtual void setIsAllowed(bool allowed);
       
    80     virtual void setLastPosition(double latitude, double longitude, bool providesAltitude, double altitude, double accuracy, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed, long long timestamp);
       
    81     virtual void setLastError(int errorCode, const WebString& message);
       
    82 
       
    83 private:
       
    84     WebViewClient* getWebViewClient();
       
    85 
       
    86     // GeolocationServiceChromium owns us, we only have a pointer back to it.
       
    87     GeolocationServiceChromium* m_GeolocationServiceChromium;
       
    88     int m_bridgeId;
       
    89 };
       
    90 
       
    91 GeolocationServiceBridge* createGeolocationServiceBridgeImpl(GeolocationServiceChromium* geolocationServiceChromium)
       
    92 {
       
    93     return new WebGeolocationServiceBridgeImpl(geolocationServiceChromium);
       
    94 }
       
    95 
       
    96 WebGeolocationServiceBridgeImpl::WebGeolocationServiceBridgeImpl(GeolocationServiceChromium* geolocationServiceChromium)
       
    97     : m_GeolocationServiceChromium(geolocationServiceChromium)
       
    98     , m_bridgeId(0)
       
    99 {
       
   100 }
       
   101 
       
   102 WebGeolocationServiceBridgeImpl::~WebGeolocationServiceBridgeImpl()
       
   103 {
       
   104     WebKit::WebViewClient* webViewClient = getWebViewClient();
       
   105     // Geolocation has an OwnPtr to us, and it's destroyed after the frame has
       
   106     // been potentially disconnected. In this case, it calls stopUpdating()
       
   107     // has been called and we have already detached ourselves.
       
   108     if (!webViewClient)
       
   109         ASSERT(!m_bridgeId);
       
   110     else if (m_bridgeId)
       
   111         webViewClient->geolocationService()->detachBridge(m_bridgeId);
       
   112 }
       
   113 
       
   114 bool WebGeolocationServiceBridgeImpl::startUpdating(PositionOptions* positionOptions)
       
   115 {
       
   116     attachBridgeIfNeeded();
       
   117     getWebViewClient()->geolocationService()->startUpdating(m_bridgeId, m_GeolocationServiceChromium->frame()->document()->url(), positionOptions->enableHighAccuracy());
       
   118     return true;
       
   119 }
       
   120 
       
   121 void WebGeolocationServiceBridgeImpl::stopUpdating()
       
   122 {
       
   123     WebViewClient* webViewClient = getWebViewClient();
       
   124     if (m_bridgeId && webViewClient) {
       
   125         WebGeolocationService* geolocationService = webViewClient->geolocationService();
       
   126         geolocationService->stopUpdating(m_bridgeId);
       
   127         geolocationService->detachBridge(m_bridgeId);
       
   128     }
       
   129     m_bridgeId = 0;
       
   130 }
       
   131 
       
   132 void WebGeolocationServiceBridgeImpl::suspend()
       
   133 {
       
   134     getWebViewClient()->geolocationService()->suspend(m_bridgeId);
       
   135 }
       
   136 
       
   137 void WebGeolocationServiceBridgeImpl::resume()
       
   138 {
       
   139     getWebViewClient()->geolocationService()->resume(m_bridgeId);
       
   140 }
       
   141 
       
   142 int WebGeolocationServiceBridgeImpl::getBridgeId() const
       
   143 {
       
   144     return m_bridgeId;
       
   145 }
       
   146 
       
   147 void WebGeolocationServiceBridgeImpl::attachBridgeIfNeeded()
       
   148 {
       
   149     if (!m_bridgeId)
       
   150         m_bridgeId = getWebViewClient()->geolocationService()->attachBridge(this);
       
   151 }
       
   152 
       
   153 void WebGeolocationServiceBridgeImpl::setIsAllowed(bool allowed)
       
   154 {
       
   155     m_GeolocationServiceChromium->setIsAllowed(allowed);
       
   156 }
       
   157 
       
   158 void WebGeolocationServiceBridgeImpl::setLastPosition(double latitude, double longitude, bool providesAltitude, double altitude, double accuracy, bool providesAltitudeAccuracy, double altitudeAccuracy, bool providesHeading, double heading, bool providesSpeed, double speed, long long timestamp)
       
   159 {
       
   160     RefPtr<Geoposition> geoposition = Geoposition::create(Coordinates::create(latitude, longitude, providesAltitude, altitude, accuracy, providesAltitudeAccuracy, altitudeAccuracy, providesHeading, heading, providesSpeed, speed), timestamp);
       
   161     m_GeolocationServiceChromium->setLastPosition(geoposition);
       
   162 }
       
   163 
       
   164 void WebGeolocationServiceBridgeImpl::setLastError(int errorCode, const WebString& message)
       
   165 {
       
   166     m_GeolocationServiceChromium->setLastError(errorCode, message);
       
   167 }
       
   168 
       
   169 WebViewClient* WebGeolocationServiceBridgeImpl::getWebViewClient()
       
   170 {
       
   171     Frame* frame = m_GeolocationServiceChromium->frame();
       
   172     if (!frame || !frame->page())
       
   173         return 0;
       
   174     WebKit::ChromeClientImpl* chromeClientImpl = static_cast<WebKit::ChromeClientImpl*>(frame->page()->chrome()->client());
       
   175     WebKit::WebViewClient* webViewClient = chromeClientImpl->webView()->client();
       
   176     return webViewClient;
       
   177 }
       
   178 
       
   179 } // namespace WebKit
       
   180 
       
   181 #endif // ENABLE(GEOLOCATION)