1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
* 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 <runtime/ObjectConstructor.h>
using namespace JSC;
namespace WebCore {
static PassRefPtr<DeviceMotionData::Acceleration> readAccelerationArgument(JSValue value, ExecState* exec)
{
if (value.isUndefinedOrNull())
return 0;
// Given the above test, this will always yield an object.
JSObject* object = value.toObject(exec);
JSValue xValue = object->get(exec, Identifier(exec, "x"));
if (exec->hadException())
return 0;
bool canProvideX = !xValue.isUndefinedOrNull();
double x = xValue.toNumber(exec);
if (exec->hadException())
return 0;
JSValue yValue = object->get(exec, Identifier(exec, "y"));
if (exec->hadException())
return 0;
bool canProvideY = !yValue.isUndefinedOrNull();
double y = yValue.toNumber(exec);
if (exec->hadException())
return 0;
JSValue zValue = object->get(exec, Identifier(exec, "z"));
if (exec->hadException())
return 0;
bool canProvideZ = !zValue.isUndefinedOrNull();
double z = zValue.toNumber(exec);
if (exec->hadException())
return 0;
if (!canProvideX && !canProvideY && !canProvideZ)
return 0;
return DeviceMotionData::Acceleration::create(canProvideX, x, canProvideY, y, canProvideZ, z);
}
static PassRefPtr<DeviceMotionData::RotationRate> readRotationRateArgument(JSValue value, ExecState* exec)
{
if (value.isUndefinedOrNull())
return 0;
// Given the above test, this will always yield an object.
JSObject* object = value.toObject(exec);
JSValue alphaValue = object->get(exec, Identifier(exec, "alpha"));
if (exec->hadException())
return 0;
bool canProvideAlpha = !alphaValue.isUndefinedOrNull();
double alpha = alphaValue.toNumber(exec);
if (exec->hadException())
return 0;
JSValue betaValue = object->get(exec, Identifier(exec, "beta"));
if (exec->hadException())
return 0;
bool canProvideBeta = !betaValue.isUndefinedOrNull();
double beta = betaValue.toNumber(exec);
if (exec->hadException())
return 0;
JSValue gammaValue = object->get(exec, Identifier(exec, "gamma"));
if (exec->hadException())
return 0;
bool canProvideGamma = !gammaValue.isUndefinedOrNull();
double gamma = gammaValue.toNumber(exec);
if (exec->hadException())
return 0;
if (!canProvideAlpha && !canProvideBeta && !canProvideGamma)
return 0;
return DeviceMotionData::RotationRate::create(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma);
}
static JSObject* createAccelerationObject(const DeviceMotionData::Acceleration* acceleration, ExecState* exec)
{
JSObject* object = constructEmptyObject(exec);
object->putDirect(exec->vm(), Identifier(exec, "x"), acceleration->canProvideX() ? jsNumber(acceleration->x()) : jsNull());
object->putDirect(exec->vm(), Identifier(exec, "y"), acceleration->canProvideY() ? jsNumber(acceleration->y()) : jsNull());
object->putDirect(exec->vm(), Identifier(exec, "z"), acceleration->canProvideZ() ? jsNumber(acceleration->z()) : jsNull());
return object;
}
static JSObject* createRotationRateObject(const DeviceMotionData::RotationRate* rotationRate, ExecState* exec)
{
JSObject* object = constructEmptyObject(exec);
object->putDirect(exec->vm(), Identifier(exec, "alpha"), rotationRate->canProvideAlpha() ? jsNumber(rotationRate->alpha()) : jsNull());
object->putDirect(exec->vm(), Identifier(exec, "beta"), rotationRate->canProvideBeta() ? jsNumber(rotationRate->beta()) : jsNull());
object->putDirect(exec->vm(), Identifier(exec, "gamma"), rotationRate->canProvideGamma() ? jsNumber(rotationRate->gamma()) : jsNull());
return object;
}
JSValue JSDeviceMotionEvent::acceleration(ExecState* exec) const
{
DeviceMotionEvent* imp = static_cast<DeviceMotionEvent*>(impl());
if (!imp->deviceMotionData()->acceleration())
return jsNull();
return createAccelerationObject(imp->deviceMotionData()->acceleration(), exec);
}
JSValue JSDeviceMotionEvent::accelerationIncludingGravity(ExecState* exec) const
{
DeviceMotionEvent* imp = static_cast<DeviceMotionEvent*>(impl());
if (!imp->deviceMotionData()->accelerationIncludingGravity())
return jsNull();
return createAccelerationObject(imp->deviceMotionData()->accelerationIncludingGravity(), exec);
}
JSValue JSDeviceMotionEvent::rotationRate(ExecState* exec) const
{
DeviceMotionEvent* imp = static_cast<DeviceMotionEvent*>(impl());
if (!imp->deviceMotionData()->rotationRate())
return jsNull();
return createRotationRateObject(imp->deviceMotionData()->rotationRate(), exec);
}
JSValue JSDeviceMotionEvent::interval(ExecState*) const
{
DeviceMotionEvent* imp = static_cast<DeviceMotionEvent*>(impl());
if (!imp->deviceMotionData()->canProvideInterval())
return jsNull();
return jsNumber(imp->deviceMotionData()->interval());
}
JSValue JSDeviceMotionEvent::initDeviceMotionEvent(ExecState* exec)
{
const String type = exec->argument(0).toString(exec)->value(exec);
bool bubbles = exec->argument(1).toBoolean(exec);
bool cancelable = exec->argument(2).toBoolean(exec);
// 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(exec->argument(3), exec);
if (exec->hadException())
return jsUndefined();
RefPtr<DeviceMotionData::Acceleration> accelerationIncludingGravity = readAccelerationArgument(exec->argument(4), exec);
if (exec->hadException())
return jsUndefined();
RefPtr<DeviceMotionData::RotationRate> rotationRate = readRotationRateArgument(exec->argument(5), exec);
if (exec->hadException())
return jsUndefined();
bool intervalProvided = !exec->argument(6).isUndefinedOrNull();
double interval = exec->argument(6).toNumber(exec);
RefPtr<DeviceMotionData> deviceMotionData = DeviceMotionData::create(acceleration, accelerationIncludingGravity, rotationRate, intervalProvided, interval);
DeviceMotionEvent* imp = static_cast<DeviceMotionEvent*>(impl());
imp->initDeviceMotionEvent(type, bubbles, cancelable, deviceMotionData.get());
return jsUndefined();
}
} // namespace WebCore
#endif // ENABLE(DEVICE_ORIENTATION)
|