V8GeolocationCustom.cpp [plain text]
#include "config.h"
#include "V8Geolocation.h"
#if ENABLE(GEOLOCATION)
#include "Frame.h"
#include "Geolocation.h"
#include "V8Binding.h"
#include "V8PositionCallback.h"
#include "V8PositionErrorCallback.h"
#include "V8Utilities.h"
using namespace std;
using namespace WTF;
namespace WebCore {
static PassRefPtr<PositionOptions> createPositionOptions(v8::Local<v8::Value> value, bool& succeeded)
{
succeeded = true;
RefPtr<PositionOptions> options = PositionOptions::create();
if (isUndefinedOrNull(value)) {
return options.release();
}
v8::Local<v8::Object> object = value->ToObject();
v8::Local<v8::Value> enableHighAccuracyValue = object->Get(v8::String::New("enableHighAccuracy"));
if (enableHighAccuracyValue.IsEmpty()) {
succeeded = false;
return 0;
}
if (!enableHighAccuracyValue->IsUndefined()) {
v8::Local<v8::Boolean> enableHighAccuracyBoolean = enableHighAccuracyValue->ToBoolean();
if (enableHighAccuracyBoolean.IsEmpty()) {
succeeded = false;
return 0;
}
options->setEnableHighAccuracy(enableHighAccuracyBoolean->Value());
}
v8::Local<v8::Value> timeoutValue = object->Get(v8::String::New("timeout"));
if (timeoutValue.IsEmpty()) {
succeeded = false;
return 0;
}
if (!timeoutValue->IsUndefined()) {
v8::Local<v8::Number> timeoutNumber = timeoutValue->ToNumber();
if (timeoutNumber.IsEmpty()) {
succeeded = false;
return 0;
}
double timeoutDouble = timeoutNumber->Value();
if (!(isinf(timeoutDouble) && timeoutDouble > 0)) {
v8::Local<v8::Int32> timeoutInt32 = timeoutValue->ToInt32();
if (timeoutInt32.IsEmpty()) {
succeeded = false;
return 0;
}
options->setTimeout(max(0, timeoutInt32->Value()));
}
}
v8::Local<v8::Value> maximumAgeValue = object->Get(v8::String::New("maximumAge"));
if (maximumAgeValue.IsEmpty()) {
succeeded = false;
return 0;
}
if (!maximumAgeValue->IsUndefined()) {
v8::Local<v8::Number> maximumAgeNumber = maximumAgeValue->ToNumber();
if (maximumAgeNumber.IsEmpty()) {
succeeded = false;
return 0;
}
double maximumAgeDouble = maximumAgeNumber->Value();
if (isinf(maximumAgeDouble) && maximumAgeDouble > 0) {
options->clearMaximumAge();
} else {
v8::Local<v8::Int32> maximumAgeInt32 = maximumAgeValue->ToInt32();
if (maximumAgeInt32.IsEmpty()) {
succeeded = false;
return 0;
}
options->setMaximumAge(max(0, maximumAgeInt32->Value()));
}
}
return options.release();
}
v8::Handle<v8::Value> V8Geolocation::getCurrentPositionCallback(const v8::Arguments& args)
{
INC_STATS("DOM.Geolocation.getCurrentPosition()");
bool succeeded = false;
RefPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<V8PositionCallback>(args[0], succeeded);
if (!succeeded)
return v8::Undefined();
ASSERT(positionCallback);
RefPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<V8PositionErrorCallback>(args[1], succeeded, CallbackAllowUndefined | CallbackAllowNull);
if (!succeeded)
return v8::Undefined();
RefPtr<PositionOptions> positionOptions = createPositionOptions(args[2], succeeded);
if (!succeeded)
return v8::Undefined();
ASSERT(positionOptions);
Geolocation* geolocation = V8Geolocation::toNative(args.Holder());
geolocation->getCurrentPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
return v8::Undefined();
}
v8::Handle<v8::Value> V8Geolocation::watchPositionCallback(const v8::Arguments& args)
{
INC_STATS("DOM.Geolocation.watchPosition()");
bool succeeded = false;
RefPtr<PositionCallback> positionCallback = createFunctionOnlyCallback<V8PositionCallback>(args[0], succeeded);
if (!succeeded)
return v8::Undefined();
ASSERT(positionCallback);
RefPtr<PositionErrorCallback> positionErrorCallback = createFunctionOnlyCallback<V8PositionErrorCallback>(args[1], succeeded, CallbackAllowUndefined | CallbackAllowNull);
if (!succeeded)
return v8::Undefined();
RefPtr<PositionOptions> positionOptions = createPositionOptions(args[2], succeeded);
if (!succeeded)
return v8::Undefined();
ASSERT(positionOptions);
Geolocation* geolocation = V8Geolocation::toNative(args.Holder());
int watchId = geolocation->watchPosition(positionCallback.release(), positionErrorCallback.release(), positionOptions.release());
return v8::Number::New(watchId);
}
}
#endif // ENABLE(GEOLOCATION)