OSDN Git Service

Refactors Geolocation to avoid duplication in getCurrentPosition and watchPosition.
authorSteve Block <steveblock@google.com>
Tue, 25 Aug 2009 11:10:30 +0000 (12:10 +0100)
committerSteve Block <steveblock@google.com>
Thu, 3 Sep 2009 17:24:20 +0000 (18:24 +0100)
This change is being submitted to WebKit as part of https://bugs.webkit.org/show_bug.cgi?id=27944.

WebCore/page/Geolocation.cpp
WebCore/page/Geolocation.h

index e57a8b5..5d908a3 100644 (file)
@@ -105,13 +105,33 @@ void Geolocation::disconnectFrame()
 
 void Geolocation::getCurrentPosition(PassRefPtr<PositionCallback> successCallback, PassRefPtr<PositionErrorCallback> errorCallback, PassRefPtr<PositionOptions> options)
 {
+    RefPtr<GeoNotifier> notifier = makeRequest(successCallback, errorCallback, options);
+    if (!notifier)
+        return;
+
+    m_oneShots.add(notifier);
+}
+
+int Geolocation::watchPosition(PassRefPtr<PositionCallback> successCallback, PassRefPtr<PositionErrorCallback> errorCallback, PassRefPtr<PositionOptions> options)
+{
+    RefPtr<GeoNotifier> notifier = makeRequest(successCallback, errorCallback, options);
+    if (!notifier)
+        return 0;
+
+    static int sIdentifier = 0;
+    m_watchers.set(++sIdentifier, notifier);
+
+    return sIdentifier;
+}
+
+PassRefPtr<Geolocation::GeoNotifier> Geolocation::makeRequest(PassRefPtr<PositionCallback> successCallback, PassRefPtr<PositionErrorCallback> errorCallback, PassRefPtr<PositionOptions> options)
+{
     RefPtr<GeoNotifier> notifier = GeoNotifier::create(this, successCallback, errorCallback, options);
 
     // Check whether permissions have already been denied. Note that if this is the case,
     // the permission state can not change again in the lifetime of this page.
     if (isDenied()) {
-        RefPtr<PositionError> error = PositionError::create(PositionError::PERMISSION_DENIED, permissionDeniedErrorMessage);
-        notifier->setFatalError(error.release());
+        notifier->setFatalError(PositionError::create(PositionError::PERMISSION_DENIED, permissionDeniedErrorMessage));
     } else {
         if (m_service->startUpdating(notifier->m_options.get()))
             notifier->startTimerIfNeeded();
@@ -120,12 +140,11 @@ void Geolocation::getCurrentPosition(PassRefPtr<PositionCallback> successCallbac
                 RefPtr<PositionError> error = PositionError::create(PositionError::PERMISSION_DENIED, "Unable to Start");
                 notifier->m_errorCallback->handleEvent(error.get());
             }
-            return;
+            return 0;
         }
     }
 
-    m_oneShots.add(notifier);
-
+    return notifier.release();
 }
 
 void Geolocation::fatalErrorOccurred(Geolocation::GeoNotifier* notifier)
@@ -143,34 +162,6 @@ void Geolocation::fatalErrorOccurred(Geolocation::GeoNotifier* notifier)
         m_service->stopUpdating();
 }
 
-int Geolocation::watchPosition(PassRefPtr<PositionCallback> successCallback, PassRefPtr<PositionErrorCallback> errorCallback, PassRefPtr<PositionOptions> options)
-{
-    RefPtr<GeoNotifier> notifier = GeoNotifier::create(this, successCallback, errorCallback, options);
-
-    // Check whether permissions have already been denied. Note that if this is the case,
-    // the permission state can not change again in the lifetime of this page.
-    if (isDenied()) {
-        RefPtr<PositionError> error = PositionError::create(PositionError::PERMISSION_DENIED, permissionDeniedErrorMessage);
-        notifier->setFatalError(error.release());
-    } else {
-        if (m_service->startUpdating(notifier->m_options.get()))
-            notifier->startTimerIfNeeded();
-        else {
-            if (notifier->m_errorCallback) {
-                RefPtr<PositionError> error = PositionError::create(PositionError::PERMISSION_DENIED, "Unable to Start");
-                notifier->m_errorCallback->handleEvent(error.get());
-            }
-            return 0;
-        }
-    }
-    
-    static int sIdentifier = 0;
-    
-    m_watchers.set(++sIdentifier, notifier);
-
-    return sIdentifier;
-}
-
 void Geolocation::requestTimedOut(GeoNotifier* notifier)
 {
     // If this is a one-shot request, stop it.
index 6681592..18c2be3 100644 (file)
@@ -111,6 +111,7 @@ private:
     void handleError(PositionError*);
 
     void requestPermission();
+    PassRefPtr<GeoNotifier> makeRequest(PassRefPtr<PositionCallback>, PassRefPtr<PositionErrorCallback>, PassRefPtr<PositionOptions>);
 
     // GeolocationServiceClient
     virtual void geolocationServicePositionChanged(GeolocationService*);