1/* 2 * Copyright (C) 2010, 2011, 2012 Research In Motion Limited. All rights reserved. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18 19#include "config.h" 20#include "GeolocationClientBlackBerry.h" 21 22#include "Chrome.h" 23#include "Frame.h" 24#include "Geolocation.h" 25#include "GeolocationController.h" 26#include "GeolocationError.h" 27#include "GeolocationPosition.h" 28#include "Page.h" 29#include "SecurityOrigin.h" 30#include "WebPage_p.h" 31 32#include <BlackBerryPlatformString.h> 33 34using namespace WebCore; 35 36static String getOrigin(Frame* frame) 37{ 38 String origin; 39 SecurityOrigin* securityOrigin = frame->document()->securityOrigin(); 40 41 // Special case for file. 42 if (securityOrigin->protocol() == "file") 43 origin = securityOrigin->fileSystemPath(); 44 else 45 origin = securityOrigin->toString(); 46 47 return origin; 48} 49 50GeolocationClientBlackBerry::GeolocationClientBlackBerry(BlackBerry::WebKit::WebPagePrivate* webPagePrivate) 51 : m_webPagePrivate(webPagePrivate) 52 , m_accuracy(false) 53 , m_isActive(false) 54{ 55} 56 57void GeolocationClientBlackBerry::geolocationDestroyed() 58{ 59 BlackBerry::Platform::GeolocationHandler::instance()->unregisterFromPermissionTracking(this); 60 61 delete this; 62} 63 64void GeolocationClientBlackBerry::startUpdating() 65{ 66 if (!m_isActive) 67 BlackBerry::Platform::GeolocationHandler::instance()->addListener(this); 68 m_isActive = true; 69} 70 71void GeolocationClientBlackBerry::stopUpdating() 72{ 73 if (m_isActive) 74 BlackBerry::Platform::GeolocationHandler::instance()->removeListener(this); 75 m_isActive = false; 76} 77 78GeolocationPosition* GeolocationClientBlackBerry::lastPosition() 79{ 80 return m_lastPosition.get(); 81} 82 83void GeolocationClientBlackBerry::requestPermission(Geolocation* location) 84{ 85 Frame* frame = location->frame(); 86 if (!frame) 87 return; 88 89 if (!m_webPagePrivate->m_webSettings->isGeolocationEnabled()) { 90 location->setIsAllowed(false); 91 return; 92 } 93 94 const String origin = getOrigin(frame); 95 96 // Special case for documents with the isUnique flag on. (ie. sandboxed iframes) 97 if (origin == "null") 98 location->setIsAllowed(false); 99 100 // Check global location setting, if it is off then we request an infobar that invokes a location settings card. 101 // If it's on, then we request an infobar that allows the site to have permission to use geolocation. 102 if (!BlackBerry::Platform::GeolocationHandler::instance()->isGlobalServiceActive()) { 103 // We only want to ask them once per session. If we get here again, automatically fail the request. 104 if (!BlackBerry::Platform::GeolocationHandler::instance()->didAskUserForGlobalPermission()) { 105 m_webPagePrivate->m_client->requestGlobalLocalServicePermission(this, origin); 106 BlackBerry::Platform::GeolocationHandler::instance()->setAskedUserForGlobalPermission(); 107 } else 108 location->setIsAllowed(false); 109 return; 110 } 111 112 // Register the listener with the GeolocationHandler to receive permissions. 113 if (m_geolocationRequestMap.isEmpty()) 114 BlackBerry::Platform::GeolocationHandler::instance()->registerPermissionTracking(this); 115 116 // Add this geolocation permission request to our request map. 117 Vector<RefPtr<Geolocation> > geoRequestsForOrigin; 118 HashMap<String, Vector<RefPtr<Geolocation> > >::AddResult result = m_geolocationRequestMap.add(origin, geoRequestsForOrigin); 119 result.iterator->value.append(location); 120 121 // Avoid sending another request if the vector already has another geolocation pending for this origin in this page. 122 if (result.isNewEntry) 123 m_webPagePrivate->m_client->requestGeolocationPermission(this, origin); 124} 125 126void GeolocationClientBlackBerry::cancelPermissionRequest(Geolocation* location) 127{ 128 Frame* frame = location->frame(); 129 if (!frame) 130 return; 131 132 const String origin = getOrigin(frame); 133 134 // Remove the geolocation from the pending permission map. 135 HashMap<String, Vector<RefPtr<Geolocation> > >::iterator it = m_geolocationRequestMap.find(origin); 136 if (it == m_geolocationRequestMap.end()) 137 return; 138 139 Vector<RefPtr<Geolocation> >* result = &(it->value); 140 141 size_t geolocationCount = result->size(); 142 for (size_t i = 0; i < geolocationCount; ++i) { 143 if ((*result)[i].get() == location) { 144 result->remove(i); 145 // Remove this vector from the pending permission map is it doesn't contain anymore geo objects 146 if (result->isEmpty()) 147 m_geolocationRequestMap.remove(origin); 148 break; 149 } 150 } 151 152 if (m_geolocationRequestMap.isEmpty()) 153 BlackBerry::Platform::GeolocationHandler::instance()->unregisterFromPermissionTracking(this); 154 155 m_webPagePrivate->m_client->cancelGeolocationPermission(); 156} 157 158void GeolocationClientBlackBerry::onLocationUpdate(double timestamp, double latitude, double longitude, double accuracy, double altitude, bool altitudeValid, 159 double altitudeAccuracy, bool altitudeAccuracyValid, double speed, bool speedValid, double heading, bool headingValid) 160{ 161 m_lastPosition = GeolocationPosition::create(timestamp, latitude, longitude, accuracy, altitudeValid, altitude, altitudeAccuracyValid, 162 altitudeAccuracy, headingValid, heading, speedValid, speed); 163 GeolocationController::from(m_webPagePrivate->m_page)->positionChanged(m_lastPosition.get()); 164} 165 166void GeolocationClientBlackBerry::onLocationError(const char* errorStr) 167{ 168 RefPtr<GeolocationError> error = GeolocationError::create(GeolocationError::PositionUnavailable, String::fromUTF8(errorStr)); 169 GeolocationController::from(m_webPagePrivate->m_page)->errorOccurred(error.get()); 170} 171 172void GeolocationClientBlackBerry::onPermission(const BlackBerry::Platform::String& origin, bool isAllowed) 173{ 174 Vector<RefPtr<Geolocation> > pendingPermissionGeolocation = m_geolocationRequestMap.get(origin); 175 176 if (pendingPermissionGeolocation.isEmpty()) 177 return; 178 179 size_t numberOfGeolocations = pendingPermissionGeolocation.size(); 180 for (size_t i = 0; i < numberOfGeolocations; ++i) 181 pendingPermissionGeolocation[i]->setIsAllowed(isAllowed); 182 m_geolocationRequestMap.remove(origin); 183 184 if (m_geolocationRequestMap.isEmpty()) 185 BlackBerry::Platform::GeolocationHandler::instance()->unregisterFromPermissionTracking(this); 186} 187 188void GeolocationClientBlackBerry::setEnableHighAccuracy(bool newAccuracy) 189{ 190 if (m_accuracy != newAccuracy) { 191 m_accuracy = newAccuracy; 192 BlackBerry::Platform::GeolocationHandler::instance()->switchAccuracy(this); 193 } 194} 195 196