V8GeolocationCustom.cpp [plain text]
#include "config.h"
#include "V8Geolocation.h"
#include "Geolocation.h"
#include "V8Binding.h"
#include "V8CustomBinding.h"
#include "V8CustomPositionCallback.h"
#include "V8CustomPositionErrorCallback.h"
#include "V8Proxy.h"
using namespace std;
using namespace WTF;
namespace WebCore {
static const char typeMismatchError[] = "TYPE_MISMATCH_ERR: DOM Exception 17";
static void throwTypeMismatchException()
{
V8Proxy::throwError(V8Proxy::GeneralError, typeMismatchError);
}
static PassRefPtr<PositionCallback> createPositionCallback(v8::Local<v8::Value> value, bool& succeeded)
{
succeeded = true;
if (!value->IsFunction()) {
succeeded = false;
throwTypeMismatchException();
return 0;
}
Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
return V8CustomPositionCallback::create(value, frame);
}
static PassRefPtr<PositionErrorCallback> createPositionErrorCallback(v8::Local<v8::Value> value, bool& succeeded)
{
succeeded = true;
if (isUndefinedOrNull(value))
return 0;
if (!value->IsFunction()) {
succeeded = false;
throwTypeMismatchException();
return 0;
}
Frame* frame = V8Proxy::retrieveFrameForCurrentContext();
return V8CustomPositionErrorCallback::create(value, frame);
}
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 = createPositionCallback(args[0], succeeded);
if (!succeeded)
return v8::Undefined();
ASSERT(positionCallback);
RefPtr<PositionErrorCallback> positionErrorCallback = createPositionErrorCallback(args[1], succeeded);
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 = createPositionCallback(args[0], succeeded);
if (!succeeded)
return v8::Undefined();
ASSERT(positionCallback);
RefPtr<PositionErrorCallback> positionErrorCallback = createPositionErrorCallback(args[1], succeeded);
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);
}
}