blob: f66c3c4e4ac24cbf66ff95cf8c87fe114f4cac46 [file] [log] [blame]
/*
* Copyright (C) 2010 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#if ENABLE(DEVICE_ORIENTATION)
#include "JSDeviceMotionEvent.h"
#include "DeviceMotionData.h"
#include "DeviceMotionEvent.h"
#include <heap/HeapInlines.h>
#include <runtime/AuxiliaryBarrierInlines.h>
#include <runtime/IdentifierInlines.h>
#include <runtime/JSCJSValueInlines.h>
#include <runtime/ObjectConstructor.h>
#include <runtime/StructureInlines.h>
using namespace JSC;
namespace WebCore {
static RefPtr<DeviceMotionData::Acceleration> readAccelerationArgument(JSValue value, ExecState& state)
{
if (value.isUndefinedOrNull())
return nullptr;
VM& vm = state.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
// Given the above test, this will always yield an object.
JSObject* object = value.toObject(&state);
ASSERT(!scope.exception());
JSValue xValue = object->get(&state, Identifier::fromString(&state, "x"));
RETURN_IF_EXCEPTION(scope, nullptr);
bool canProvideX = !xValue.isUndefinedOrNull();
double x = xValue.toNumber(&state);
RETURN_IF_EXCEPTION(scope, nullptr);
JSValue yValue = object->get(&state, Identifier::fromString(&state, "y"));
RETURN_IF_EXCEPTION(scope, nullptr);
bool canProvideY = !yValue.isUndefinedOrNull();
double y = yValue.toNumber(&state);
RETURN_IF_EXCEPTION(scope, nullptr);
JSValue zValue = object->get(&state, Identifier::fromString(&state, "z"));
RETURN_IF_EXCEPTION(scope, nullptr);
bool canProvideZ = !zValue.isUndefinedOrNull();
double z = zValue.toNumber(&state);
RETURN_IF_EXCEPTION(scope, nullptr);
if (!canProvideX && !canProvideY && !canProvideZ)
return nullptr;
return DeviceMotionData::Acceleration::create(canProvideX, x, canProvideY, y, canProvideZ, z);
}
static RefPtr<DeviceMotionData::RotationRate> readRotationRateArgument(JSValue value, ExecState& state)
{
if (value.isUndefinedOrNull())
return nullptr;
VM& vm = state.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
// Given the above test, this will always yield an object.
JSObject* object = value.toObject(&state);
ASSERT(!scope.exception());
JSValue alphaValue = object->get(&state, Identifier::fromString(&state, "alpha"));
RETURN_IF_EXCEPTION(scope, nullptr);
bool canProvideAlpha = !alphaValue.isUndefinedOrNull();
double alpha = alphaValue.toNumber(&state);
RETURN_IF_EXCEPTION(scope, nullptr);
JSValue betaValue = object->get(&state, Identifier::fromString(&state, "beta"));
RETURN_IF_EXCEPTION(scope, nullptr);
bool canProvideBeta = !betaValue.isUndefinedOrNull();
double beta = betaValue.toNumber(&state);
RETURN_IF_EXCEPTION(scope, nullptr);
JSValue gammaValue = object->get(&state, Identifier::fromString(&state, "gamma"));
RETURN_IF_EXCEPTION(scope, nullptr);
bool canProvideGamma = !gammaValue.isUndefinedOrNull();
double gamma = gammaValue.toNumber(&state);
RETURN_IF_EXCEPTION(scope, nullptr);
if (!canProvideAlpha && !canProvideBeta && !canProvideGamma)
return nullptr;
return DeviceMotionData::RotationRate::create(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma);
}
static JSObject* createAccelerationObject(const DeviceMotionData::Acceleration* acceleration, ExecState& state)
{
JSObject* object = constructEmptyObject(&state);
object->putDirect(state.vm(), Identifier::fromString(&state, "x"), acceleration->canProvideX() ? jsNumber(acceleration->x()) : jsNull());
object->putDirect(state.vm(), Identifier::fromString(&state, "y"), acceleration->canProvideY() ? jsNumber(acceleration->y()) : jsNull());
object->putDirect(state.vm(), Identifier::fromString(&state, "z"), acceleration->canProvideZ() ? jsNumber(acceleration->z()) : jsNull());
return object;
}
static JSObject* createRotationRateObject(const DeviceMotionData::RotationRate* rotationRate, ExecState& state)
{
JSObject* object = constructEmptyObject(&state);
object->putDirect(state.vm(), Identifier::fromString(&state, "alpha"), rotationRate->canProvideAlpha() ? jsNumber(rotationRate->alpha()) : jsNull());
object->putDirect(state.vm(), Identifier::fromString(&state, "beta"), rotationRate->canProvideBeta() ? jsNumber(rotationRate->beta()) : jsNull());
object->putDirect(state.vm(), Identifier::fromString(&state, "gamma"), rotationRate->canProvideGamma() ? jsNumber(rotationRate->gamma()) : jsNull());
return object;
}
JSValue JSDeviceMotionEvent::acceleration(ExecState& state) const
{
DeviceMotionEvent& imp = wrapped();
if (!imp.deviceMotionData()->acceleration())
return jsNull();
return createAccelerationObject(imp.deviceMotionData()->acceleration(), state);
}
JSValue JSDeviceMotionEvent::accelerationIncludingGravity(ExecState& state) const
{
DeviceMotionEvent& imp = wrapped();
if (!imp.deviceMotionData()->accelerationIncludingGravity())
return jsNull();
return createAccelerationObject(imp.deviceMotionData()->accelerationIncludingGravity(), state);
}
JSValue JSDeviceMotionEvent::rotationRate(ExecState& state) const
{
DeviceMotionEvent& imp = wrapped();
if (!imp.deviceMotionData()->rotationRate())
return jsNull();
return createRotationRateObject(imp.deviceMotionData()->rotationRate(), state);
}
JSValue JSDeviceMotionEvent::interval(ExecState&) const
{
DeviceMotionEvent& imp = wrapped();
if (!imp.deviceMotionData()->canProvideInterval())
return jsNull();
return jsNumber(imp.deviceMotionData()->interval());
}
JSValue JSDeviceMotionEvent::initDeviceMotionEvent(ExecState& state)
{
VM& vm = state.vm();
auto scope = DECLARE_THROW_SCOPE(vm);
const String type = state.argument(0).toString(&state)->value(&state);
bool bubbles = state.argument(1).toBoolean(&state);
bool cancelable = state.argument(2).toBoolean(&state);
// If any of the parameters are null or undefined, mark them as not provided.
// Otherwise, use the standard JavaScript conversion.
RefPtr<DeviceMotionData::Acceleration> acceleration = readAccelerationArgument(state.argument(3), state);
RETURN_IF_EXCEPTION(scope, JSValue());
RefPtr<DeviceMotionData::Acceleration> accelerationIncludingGravity = readAccelerationArgument(state.argument(4), state);
RETURN_IF_EXCEPTION(scope, JSValue());
RefPtr<DeviceMotionData::RotationRate> rotationRate = readRotationRateArgument(state.argument(5), state);
RETURN_IF_EXCEPTION(scope, JSValue());
bool intervalProvided = !state.argument(6).isUndefinedOrNull();
double interval = state.argument(6).toNumber(&state);
auto deviceMotionData = DeviceMotionData::create(WTFMove(acceleration), WTFMove(accelerationIncludingGravity), WTFMove(rotationRate), intervalProvided, interval);
wrapped().initDeviceMotionEvent(type, bubbles, cancelable, deviceMotionData.ptr());
return jsUndefined();
}
} // namespace WebCore
#endif // ENABLE(DEVICE_ORIENTATION)