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