2 * Copyright (C) 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "V8DeviceMotionEvent.h"
29 #if ENABLE(DEVICE_ORIENTATION)
31 #include "DeviceMotionData.h"
32 #include "V8Binding.h"
33 #include "V8BindingMacros.h"
42 v8::Handle<v8::Value> createAccelerationObject(const DeviceMotionData::Acceleration* acceleration)
44 v8::Local<v8::Object> object = v8::Object::New();
45 object->Set(v8::String::New("x"), acceleration->canProvideX() ? v8::Number::New(acceleration->x()) : v8::Null());
46 object->Set(v8::String::New("y"), acceleration->canProvideY() ? v8::Number::New(acceleration->y()) : v8::Null());
47 object->Set(v8::String::New("z"), acceleration->canProvideZ() ? v8::Number::New(acceleration->z()) : v8::Null());
51 v8::Handle<v8::Value> createRotationRateObject(const DeviceMotionData::RotationRate* rotationRate)
53 v8::Local<v8::Object> object = v8::Object::New();
54 object->Set(v8::String::New("alpha"), rotationRate->canProvideAlpha() ? v8::Number::New(rotationRate->alpha()) : v8::Null());
55 object->Set(v8::String::New("beta"), rotationRate->canProvideBeta() ? v8::Number::New(rotationRate->beta()) : v8::Null());
56 object->Set(v8::String::New("gamma"), rotationRate->canProvideGamma() ? v8::Number::New(rotationRate->gamma()) : v8::Null());
60 RefPtr<DeviceMotionData::Acceleration> readAccelerationArgument(v8::Local<v8::Value> value)
62 if (isUndefinedOrNull(value))
65 // Given the test above, this will always yield an object.
66 v8::Local<v8::Object> object = value->ToObject();
68 v8::Local<v8::Value> xValue = object->Get(v8::String::New("x"));
71 bool canProvideX = !isUndefinedOrNull(xValue);
72 double x = xValue->NumberValue();
74 v8::Local<v8::Value> yValue = object->Get(v8::String::New("y"));
77 bool canProvideY = !isUndefinedOrNull(yValue);
78 double y = yValue->NumberValue();
80 v8::Local<v8::Value> zValue = object->Get(v8::String::New("z"));
83 bool canProvideZ = !isUndefinedOrNull(zValue);
84 double z = zValue->NumberValue();
86 if (!canProvideX && !canProvideY && !canProvideZ)
89 return DeviceMotionData::Acceleration::create(canProvideX, x, canProvideY, y, canProvideZ, z);
92 RefPtr<DeviceMotionData::RotationRate> readRotationRateArgument(v8::Local<v8::Value> value)
94 if (isUndefinedOrNull(value))
97 // Given the test above, this will always yield an object.
98 v8::Local<v8::Object> object = value->ToObject();
100 v8::Local<v8::Value> alphaValue = object->Get(v8::String::New("alpha"));
101 if (alphaValue.IsEmpty())
103 bool canProvideAlpha = !isUndefinedOrNull(alphaValue);
104 double alpha = alphaValue->NumberValue();
106 v8::Local<v8::Value> betaValue = object->Get(v8::String::New("beta"));
107 if (betaValue.IsEmpty())
109 bool canProvideBeta = !isUndefinedOrNull(betaValue);
110 double beta = betaValue->NumberValue();
112 v8::Local<v8::Value> gammaValue = object->Get(v8::String::New("gamma"));
113 if (gammaValue.IsEmpty())
115 bool canProvideGamma = !isUndefinedOrNull(gammaValue);
116 double gamma = gammaValue->NumberValue();
118 if (!canProvideAlpha && !canProvideBeta && !canProvideGamma)
121 return DeviceMotionData::RotationRate::create(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma);
126 v8::Handle<v8::Value> V8DeviceMotionEvent::accelerationAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
128 INC_STATS("DOM.DeviceMotionEvent.acceleration._get");
129 v8::Handle<v8::Object> holder = info.Holder();
130 DeviceMotionEvent* imp = V8DeviceMotionEvent::toNative(holder);
131 if (!imp->deviceMotionData()->acceleration())
133 return createAccelerationObject(imp->deviceMotionData()->acceleration());
136 v8::Handle<v8::Value> V8DeviceMotionEvent::accelerationIncludingGravityAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
138 INC_STATS("DOM.DeviceMotionEvent.accelerationIncludingGravity._get");
139 v8::Handle<v8::Object> holder = info.Holder();
140 DeviceMotionEvent* imp = V8DeviceMotionEvent::toNative(holder);
141 if (!imp->deviceMotionData()->accelerationIncludingGravity())
143 return createAccelerationObject(imp->deviceMotionData()->accelerationIncludingGravity());
146 v8::Handle<v8::Value> V8DeviceMotionEvent::rotationRateAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
148 INC_STATS("DOM.DeviceMotionEvent.rotationRate._get");
149 v8::Handle<v8::Object> holder = info.Holder();
150 DeviceMotionEvent* imp = V8DeviceMotionEvent::toNative(holder);
151 if (!imp->deviceMotionData()->rotationRate())
153 return createRotationRateObject(imp->deviceMotionData()->rotationRate());
156 v8::Handle<v8::Value> V8DeviceMotionEvent::intervalAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
158 INC_STATS("DOM.DeviceMotionEvent.interval._get");
159 v8::Handle<v8::Object> holder = info.Holder();
160 DeviceMotionEvent* imp = V8DeviceMotionEvent::toNative(holder);
161 if (!imp->deviceMotionData()->canProvideInterval())
163 return v8::Number::New(imp->deviceMotionData()->interval());
166 v8::Handle<v8::Value> V8DeviceMotionEvent::initDeviceMotionEventCallback(const v8::Arguments& args)
168 DeviceMotionEvent* imp = V8DeviceMotionEvent::toNative(args.Holder());
169 STRING_TO_V8PARAMETER_EXCEPTION_BLOCK(V8Parameter<>, type, args[0]);
170 bool bubbles = args[1]->BooleanValue();
171 bool cancelable = args[2]->BooleanValue();
172 RefPtr<DeviceMotionData::Acceleration> acceleration = readAccelerationArgument(args[3]);
173 RefPtr<DeviceMotionData::Acceleration> accelerationIncludingGravity = readAccelerationArgument(args[4]);
174 RefPtr<DeviceMotionData::RotationRate> rotationRate = readRotationRateArgument(args[5]);
175 bool intervalProvided = !isUndefinedOrNull(args[6]);
176 double interval = args[6]->NumberValue();
177 RefPtr<DeviceMotionData> deviceMotionData = DeviceMotionData::create(acceleration, accelerationIncludingGravity, rotationRate, intervalProvided, interval);
178 imp->initDeviceMotionEvent(type, bubbles, cancelable, deviceMotionData.get());
179 return v8::Handle<v8::Value>();
182 } // namespace WebCore
184 #endif // ENABLE(DEVICE_ORIENTATION)