JSGeolocationCustom.cpp [plain text]
#include "config.h"
#include "JSGeolocation.h"
#if ENABLE(GEOLOCATION)
#include "CallbackFunction.h"
#include "DOMWindow.h"
#include "Geolocation.h"
#include "JSCustomPositionCallback.h"
#include "JSCustomPositionErrorCallback.h"
#include "JSDOMWindow.h"
#include "PositionOptions.h"
#if !ENABLE(CLIENT_BASED_GEOLOCATION)
#include "GeolocationService.h"
#endif
using namespace JSC;
using namespace std;
namespace WebCore {
static PassRefPtr<PositionOptions> createPositionOptions(ExecState* exec, JSValue value)
{
RefPtr<PositionOptions> options = PositionOptions::create();
if (value.isUndefinedOrNull()) {
return options.release();
}
JSObject* object = value.toObject(exec);
JSValue enableHighAccuracyValue = object->get(exec, Identifier(exec, "enableHighAccuracy"));
if (exec->hadException())
return 0;
if (!enableHighAccuracyValue.isUndefined()) {
options->setEnableHighAccuracy(enableHighAccuracyValue.toBoolean(exec));
if (exec->hadException())
return 0;
}
JSValue timeoutValue = object->get(exec, Identifier(exec, "timeout"));
if (exec->hadException())
return 0;
if (!timeoutValue.isUndefined()) {
double timeoutNumber = timeoutValue.toNumber(exec);
if (exec->hadException())
return 0;
if (!(isinf(timeoutNumber) && (timeoutNumber > 0))) {
options->setTimeout(max(0, timeoutValue.toInt32(exec)));
if (exec->hadException())
return 0;
}
}
JSValue maximumAgeValue = object->get(exec, Identifier(exec, "maximumAge"));
if (exec->hadException())
return 0;
if (!maximumAgeValue.isUndefined()) {
double maximumAgeNumber = maximumAgeValue.toNumber(exec);
if (exec->hadException())
return 0;
if (isinf(maximumAgeNumber) && (maximumAgeNumber > 0)) {
options->clearMaximumAge();
} else {
options->setMaximumAge(max(0, maximumAgeValue.toInt32(exec)));
if (exec->hadException())
return 0;
}
}
return options.release();
}
JSValue JSGeolocation::getCurrentPosition(ExecState* exec)
{
RefPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<JSCustomPositionCallback>(exec, static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject()), exec->argument(0));
if (exec->hadException())
return jsUndefined();
ASSERT(positionCallback);
RefPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<JSCustomPositionErrorCallback>(exec, static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject()), exec->argument(1), CallbackAllowUndefined | CallbackAllowNull);
if (exec->hadException())
return jsUndefined();
RefPtr<PositionOptions> positionOptions = createPositionOptions(exec, exec->argument(2));
if (exec->hadException())
return jsUndefined();
ASSERT(positionOptions);
m_impl->getCurrentPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
return jsUndefined();
}
JSValue JSGeolocation::watchPosition(ExecState* exec)
{
RefPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<JSCustomPositionCallback>(exec, static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject()), exec->argument(0));
if (exec->hadException())
return jsUndefined();
ASSERT(positionCallback);
RefPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<JSCustomPositionErrorCallback>(exec, static_cast<JSDOMGlobalObject*>(exec->lexicalGlobalObject()), exec->argument(1), CallbackAllowUndefined | CallbackAllowNull);
if (exec->hadException())
return jsUndefined();
RefPtr<PositionOptions> positionOptions = createPositionOptions(exec, exec->argument(2));
if (exec->hadException())
return jsUndefined();
ASSERT(positionOptions);
int watchID = m_impl->watchPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
return jsNumber(watchID);
}
}
#endif // ENABLE(GEOLOCATION)