void
nsGeolocationRequest::SendLocation(nsIDOMGeoPosition* aPosition, bool aCachePosition)
{
  if (mCleared || !mAllowed) {
    return;
  }

  if (mTimeoutTimer) {
    mTimeoutTimer->Cancel();
    mTimeoutTimer = nullptr;
  }

  nsRefPtr<Position> wrapped, cachedWrapper = mLocator->GetCachedPosition();
  if (cachedWrapper && aPosition == cachedWrapper->GetWrappedGeoPosition()) {
    wrapped = cachedWrapper;
  } else if (aPosition) {
    nsCOMPtr<nsIDOMGeoPositionCoords> coords;
    aPosition->GetCoords(getter_AddRefs(coords));
    if (coords) {
      wrapped = new Position(mLocator, aPosition);
    }
  }

  if (!wrapped) {
    NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
    return;
  }

  if (aCachePosition) {
    mLocator->SetCachedPosition(wrapped);
  }

  // Ensure that the proper context is on the stack (bug 452762)
  nsCxPusher pusher;
  pusher.PushNull();
  nsAutoMicroTask mt;
  if (mCallback.HasWebIDLCallback()) {
    ErrorResult err;
    PositionCallback* callback = mCallback.GetWebIDLCallback();

    MOZ_ASSERT(callback);
    callback->Call(*wrapped, err);
  } else {
    nsIDOMGeoPositionCallback* callback = mCallback.GetXPCOMCallback();

    MOZ_ASSERT(callback);
    callback->HandleEvent(aPosition);
  }

  if (mIsWatchPositionRequest) {
    SetTimeoutTimer();
  }
}
示例#2
0
void
nsGeolocationRequest::SendLocation(nsIDOMGeoPosition* aPosition)
{
    if (mShutdown) {
        // Ignore SendLocationEvents issued before we were cleared.
        return;
    }

    nsRefPtr<Position> wrapped, cachedWrapper = mLocator->GetCachedPosition();
    if (cachedWrapper && aPosition == cachedWrapper->GetWrappedGeoPosition()) {
        wrapped = cachedWrapper;
    } else if (aPosition) {
        nsCOMPtr<nsIDOMGeoPositionCoords> coords;
        aPosition->GetCoords(getter_AddRefs(coords));
        if (coords) {
            wrapped = new Position(ToSupports(mLocator), aPosition);
        }
    }

    if (!wrapped) {
        NotifyError(nsIDOMGeoPositionError::POSITION_UNAVAILABLE);
        return;
    }

    mLocator->SetCachedPosition(wrapped);

    // Ensure that the proper context is on the stack (bug 452762)
    nsCxPusher pusher;
    pusher.PushNull();
    nsAutoMicroTask mt;
    if (mCallback.HasWebIDLCallback()) {
        ErrorResult err;
        PositionCallback* callback = mCallback.GetWebIDLCallback();

        MOZ_ASSERT(callback);
        callback->Call(*wrapped, err);
    } else {
        nsIDOMGeoPositionCallback* callback = mCallback.GetXPCOMCallback();

        MOZ_ASSERT(callback);
        callback->HandleEvent(aPosition);
    }

    if (!mIsWatchPositionRequest) {
        Shutdown();
    } else if (!mShutdown) { // The handler may have called clearWatch
        SetTimeoutTimer();
    }
}