GeolocationClientBlackBerry.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include "config.h"
  19. #include "GeolocationClientBlackBerry.h"
  20. #include "Chrome.h"
  21. #include "Frame.h"
  22. #include "Geolocation.h"
  23. #include "GeolocationController.h"
  24. #include "GeolocationError.h"
  25. #include "GeolocationPosition.h"
  26. #include "Page.h"
  27. #include "SecurityOrigin.h"
  28. #include "WebPage_p.h"
  29. #include <BlackBerryPlatformString.h>
  30. using namespace WebCore;
  31. static String getOrigin(Frame* frame)
  32. {
  33. String origin;
  34. SecurityOrigin* securityOrigin = frame->document()->securityOrigin();
  35. // Special case for file.
  36. if (securityOrigin->protocol() == "file")
  37. origin = securityOrigin->fileSystemPath();
  38. else
  39. origin = securityOrigin->toString();
  40. return origin;
  41. }
  42. GeolocationClientBlackBerry::GeolocationClientBlackBerry(BlackBerry::WebKit::WebPagePrivate* webPagePrivate)
  43. : m_webPagePrivate(webPagePrivate)
  44. , m_accuracy(false)
  45. , m_isActive(false)
  46. {
  47. }
  48. void GeolocationClientBlackBerry::geolocationDestroyed()
  49. {
  50. BlackBerry::Platform::GeolocationHandler::instance()->unregisterFromPermissionTracking(this);
  51. delete this;
  52. }
  53. void GeolocationClientBlackBerry::startUpdating()
  54. {
  55. if (!m_isActive)
  56. BlackBerry::Platform::GeolocationHandler::instance()->addListener(this);
  57. m_isActive = true;
  58. }
  59. void GeolocationClientBlackBerry::stopUpdating()
  60. {
  61. if (m_isActive)
  62. BlackBerry::Platform::GeolocationHandler::instance()->removeListener(this);
  63. m_isActive = false;
  64. }
  65. GeolocationPosition* GeolocationClientBlackBerry::lastPosition()
  66. {
  67. return m_lastPosition.get();
  68. }
  69. void GeolocationClientBlackBerry::requestPermission(Geolocation* location)
  70. {
  71. Frame* frame = location->frame();
  72. if (!frame)
  73. return;
  74. if (!m_webPagePrivate->m_webSettings->isGeolocationEnabled()) {
  75. location->setIsAllowed(false);
  76. return;
  77. }
  78. const String origin = getOrigin(frame);
  79. // Special case for documents with the isUnique flag on. (ie. sandboxed iframes)
  80. if (origin == "null")
  81. location->setIsAllowed(false);
  82. // Check global location setting, if it is off then we request an infobar that invokes a location settings card.
  83. // If it's on, then we request an infobar that allows the site to have permission to use geolocation.
  84. if (!BlackBerry::Platform::GeolocationHandler::instance()->isGlobalServiceActive()) {
  85. // We only want to ask them once per session. If we get here again, automatically fail the request.
  86. if (!BlackBerry::Platform::GeolocationHandler::instance()->didAskUserForGlobalPermission()) {
  87. m_webPagePrivate->m_client->requestGlobalLocalServicePermission(this, origin);
  88. BlackBerry::Platform::GeolocationHandler::instance()->setAskedUserForGlobalPermission();
  89. } else
  90. location->setIsAllowed(false);
  91. return;
  92. }
  93. // Register the listener with the GeolocationHandler to receive permissions.
  94. if (m_geolocationRequestMap.isEmpty())
  95. BlackBerry::Platform::GeolocationHandler::instance()->registerPermissionTracking(this);
  96. // Add this geolocation permission request to our request map.
  97. Vector<RefPtr<Geolocation> > geoRequestsForOrigin;
  98. HashMap<String, Vector<RefPtr<Geolocation> > >::AddResult result = m_geolocationRequestMap.add(origin, geoRequestsForOrigin);
  99. result.iterator->value.append(location);
  100. // Avoid sending another request if the vector already has another geolocation pending for this origin in this page.
  101. if (result.isNewEntry)
  102. m_webPagePrivate->m_client->requestGeolocationPermission(this, origin);
  103. }
  104. void GeolocationClientBlackBerry::cancelPermissionRequest(Geolocation* location)
  105. {
  106. Frame* frame = location->frame();
  107. if (!frame)
  108. return;
  109. const String origin = getOrigin(frame);
  110. // Remove the geolocation from the pending permission map.
  111. HashMap<String, Vector<RefPtr<Geolocation> > >::iterator it = m_geolocationRequestMap.find(origin);
  112. if (it == m_geolocationRequestMap.end())
  113. return;
  114. Vector<RefPtr<Geolocation> >* result = &(it->value);
  115. size_t geolocationCount = result->size();
  116. for (size_t i = 0; i < geolocationCount; ++i) {
  117. if ((*result)[i].get() == location) {
  118. result->remove(i);
  119. // Remove this vector from the pending permission map is it doesn't contain anymore geo objects
  120. if (result->isEmpty())
  121. m_geolocationRequestMap.remove(origin);
  122. break;
  123. }
  124. }
  125. if (m_geolocationRequestMap.isEmpty())
  126. BlackBerry::Platform::GeolocationHandler::instance()->unregisterFromPermissionTracking(this);
  127. m_webPagePrivate->m_client->cancelGeolocationPermission();
  128. }
  129. void GeolocationClientBlackBerry::onLocationUpdate(double timestamp, double latitude, double longitude, double accuracy, double altitude, bool altitudeValid,
  130. double altitudeAccuracy, bool altitudeAccuracyValid, double speed, bool speedValid, double heading, bool headingValid)
  131. {
  132. m_lastPosition = GeolocationPosition::create(timestamp, latitude, longitude, accuracy, altitudeValid, altitude, altitudeAccuracyValid,
  133. altitudeAccuracy, headingValid, heading, speedValid, speed);
  134. GeolocationController::from(m_webPagePrivate->m_page)->positionChanged(m_lastPosition.get());
  135. }
  136. void GeolocationClientBlackBerry::onLocationError(const char* errorStr)
  137. {
  138. RefPtr<GeolocationError> error = GeolocationError::create(GeolocationError::PositionUnavailable, String::fromUTF8(errorStr));
  139. GeolocationController::from(m_webPagePrivate->m_page)->errorOccurred(error.get());
  140. }
  141. void GeolocationClientBlackBerry::onPermission(const BlackBerry::Platform::String& origin, bool isAllowed)
  142. {
  143. Vector<RefPtr<Geolocation> > pendingPermissionGeolocation = m_geolocationRequestMap.get(origin);
  144. if (pendingPermissionGeolocation.isEmpty())
  145. return;
  146. size_t numberOfGeolocations = pendingPermissionGeolocation.size();
  147. for (size_t i = 0; i < numberOfGeolocations; ++i)
  148. pendingPermissionGeolocation[i]->setIsAllowed(isAllowed);
  149. m_geolocationRequestMap.remove(origin);
  150. if (m_geolocationRequestMap.isEmpty())
  151. BlackBerry::Platform::GeolocationHandler::instance()->unregisterFromPermissionTracking(this);
  152. }
  153. void GeolocationClientBlackBerry::setEnableHighAccuracy(bool newAccuracy)
  154. {
  155. if (m_accuracy != newAccuracy) {
  156. m_accuracy = newAccuracy;
  157. BlackBerry::Platform::GeolocationHandler::instance()->switchAccuracy(this);
  158. }
  159. }