JSGeolocationCustom.cpp [plain text]
#include "config.h"
#include "JSGeolocation.h"
#include "DOMWindow.h"
#include "ExceptionCode.h"
#include "Geolocation.h"
#include "GeolocationService.h"
#include "JSCustomPositionCallback.h"
#include "JSCustomPositionErrorCallback.h"
#include "JSDOMWindow.h"
#include "PositionOptions.h"
using namespace JSC;
namespace WebCore {
static PassRefPtr<PositionOptions> createPositionOptions(ExecState* exec, JSValue value)
{
if (!value.isObject())
return 0;
JSObject* object = asObject(value);
JSValue enableHighAccuracyValue = object->get(exec, Identifier(exec, "enableHighAccuracy"));
if (exec->hadException())
return 0;
bool enableHighAccuracy = enableHighAccuracyValue.toBoolean(exec);
if (exec->hadException())
return 0;
JSValue timeoutValue = object->get(exec, Identifier(exec, "timeout"));
if (exec->hadException())
return 0;
unsigned timeout = timeoutValue.toUInt32(exec);
if (exec->hadException())
return 0;
JSValue maximumAgeValue = object->get(exec, Identifier(exec, "maximumAge"));
if (exec->hadException())
return 0;
unsigned maximumAge = maximumAgeValue.toUInt32(exec);
if (exec->hadException())
return 0;
return PositionOptions::create(enableHighAccuracy, timeout, maximumAge);
}
JSValue JSGeolocation::getCurrentPosition(ExecState* exec, const ArgList& args)
{
RefPtr<PositionCallback> positionCallback;
JSObject* object = args.at(0).getObject();
if (exec->hadException())
return jsUndefined();
if (!object) {
setDOMException(exec, TYPE_MISMATCH_ERR);
return jsUndefined();
}
if (Frame* frame = toJSDOMWindow(exec->lexicalGlobalObject())->impl()->frame())
positionCallback = JSCustomPositionCallback::create(object, frame);
RefPtr<PositionErrorCallback> positionErrorCallback;
if (!args.at(1).isUndefinedOrNull()) {
JSObject* object = args.at(1).getObject();
if (!object) {
setDOMException(exec, TYPE_MISMATCH_ERR);
return jsUndefined();
}
if (Frame* frame = toJSDOMWindow(exec->lexicalGlobalObject())->impl()->frame())
positionErrorCallback = JSCustomPositionErrorCallback::create(object, frame);
}
RefPtr<PositionOptions> positionOptions;
if (!args.at(2).isUndefinedOrNull()) {
positionOptions = createPositionOptions(exec, args.at(2));
if (exec->hadException())
return jsUndefined();
}
m_impl->getCurrentPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
return jsUndefined();
}
JSValue JSGeolocation::watchPosition(ExecState* exec, const ArgList& args)
{
RefPtr<PositionCallback> positionCallback;
JSObject* object = args.at(0).getObject();
if (exec->hadException())
return jsUndefined();
if (!object) {
setDOMException(exec, TYPE_MISMATCH_ERR);
return jsUndefined();
}
if (Frame* frame = toJSDOMWindow(exec->lexicalGlobalObject())->impl()->frame())
positionCallback = JSCustomPositionCallback::create(object, frame);
RefPtr<PositionErrorCallback> positionErrorCallback;
if (!args.at(1).isUndefinedOrNull()) {
JSObject* object = args.at(1).getObject();
if (!object) {
setDOMException(exec, TYPE_MISMATCH_ERR);
return jsUndefined();
}
if (Frame* frame = toJSDOMWindow(exec->lexicalGlobalObject())->impl()->frame())
positionErrorCallback = JSCustomPositionErrorCallback::create(object, frame);
}
RefPtr<PositionOptions> positionOptions;
if (!args.at(2).isUndefinedOrNull()) {
positionOptions = createPositionOptions(exec, args.at(2));
if (exec->hadException())
return jsUndefined();
}
int watchID = m_impl->watchPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
return jsNumber(exec, watchID);
}
}