Home | History | Annotate | Download | only in custom
      1 /*
      2  * Copyright (C) 2010 Apple Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions
      6  * are met:
      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.
     12  *
     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.
     24  */
     25 
     26 #include "config.h"
     27 #include "V8DeviceMotionEvent.h"
     28 
     29 #if ENABLE(DEVICE_ORIENTATION)
     30 
     31 #include "DeviceMotionData.h"
     32 #include "V8Binding.h"
     33 #include "V8BindingMacros.h"
     34 #include "V8Proxy.h"
     35 
     36 #include <v8.h>
     37 
     38 namespace WebCore {
     39 
     40 namespace {
     41 
     42 v8::Handle<v8::Value> createAccelerationObject(const DeviceMotionData::Acceleration* acceleration)
     43 {
     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());
     48     return object;
     49 }
     50 
     51 v8::Handle<v8::Value> createRotationRateObject(const DeviceMotionData::RotationRate* rotationRate)
     52 {
     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());
     57     return object;
     58 }
     59 
     60 RefPtr<DeviceMotionData::Acceleration> readAccelerationArgument(v8::Local<v8::Value> value)
     61 {
     62     if (isUndefinedOrNull(value))
     63         return 0;
     64 
     65     // Given the test above, this will always yield an object.
     66     v8::Local<v8::Object> object = value->ToObject();
     67 
     68     v8::Local<v8::Value> xValue = object->Get(v8::String::New("x"));
     69     if (xValue.IsEmpty())
     70         return 0;
     71     bool canProvideX = !isUndefinedOrNull(xValue);
     72     double x = xValue->NumberValue();
     73 
     74     v8::Local<v8::Value> yValue = object->Get(v8::String::New("y"));
     75     if (yValue.IsEmpty())
     76         return 0;
     77     bool canProvideY = !isUndefinedOrNull(yValue);
     78     double y = yValue->NumberValue();
     79 
     80     v8::Local<v8::Value> zValue = object->Get(v8::String::New("z"));
     81     if (zValue.IsEmpty())
     82         return 0;
     83     bool canProvideZ = !isUndefinedOrNull(zValue);
     84     double z = zValue->NumberValue();
     85 
     86     if (!canProvideX && !canProvideY && !canProvideZ)
     87         return 0;
     88 
     89     return DeviceMotionData::Acceleration::create(canProvideX, x, canProvideY, y, canProvideZ, z);
     90 }
     91 
     92 RefPtr<DeviceMotionData::RotationRate> readRotationRateArgument(v8::Local<v8::Value> value)
     93 {
     94     if (isUndefinedOrNull(value))
     95         return 0;
     96 
     97     // Given the test above, this will always yield an object.
     98     v8::Local<v8::Object> object = value->ToObject();
     99 
    100     v8::Local<v8::Value> alphaValue = object->Get(v8::String::New("alpha"));
    101     if (alphaValue.IsEmpty())
    102         return 0;
    103     bool canProvideAlpha = !isUndefinedOrNull(alphaValue);
    104     double alpha = alphaValue->NumberValue();
    105 
    106     v8::Local<v8::Value> betaValue = object->Get(v8::String::New("beta"));
    107     if (betaValue.IsEmpty())
    108         return 0;
    109     bool canProvideBeta = !isUndefinedOrNull(betaValue);
    110     double beta = betaValue->NumberValue();
    111 
    112     v8::Local<v8::Value> gammaValue = object->Get(v8::String::New("gamma"));
    113     if (gammaValue.IsEmpty())
    114         return 0;
    115     bool canProvideGamma = !isUndefinedOrNull(gammaValue);
    116     double gamma = gammaValue->NumberValue();
    117 
    118     if (!canProvideAlpha && !canProvideBeta && !canProvideGamma)
    119         return 0;
    120 
    121     return DeviceMotionData::RotationRate::create(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma);
    122 }
    123 
    124 } // namespace
    125 
    126 v8::Handle<v8::Value> V8DeviceMotionEvent::accelerationAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
    127 {
    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())
    132         return v8::Null();
    133     return createAccelerationObject(imp->deviceMotionData()->acceleration());
    134 }
    135 
    136 v8::Handle<v8::Value> V8DeviceMotionEvent::accelerationIncludingGravityAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
    137 {
    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())
    142         return v8::Null();
    143     return createAccelerationObject(imp->deviceMotionData()->accelerationIncludingGravity());
    144 }
    145 
    146 v8::Handle<v8::Value> V8DeviceMotionEvent::rotationRateAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
    147 {
    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())
    152         return v8::Null();
    153     return createRotationRateObject(imp->deviceMotionData()->rotationRate());
    154 }
    155 
    156 v8::Handle<v8::Value> V8DeviceMotionEvent::intervalAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
    157 {
    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())
    162         return v8::Null();
    163     return v8::Number::New(imp->deviceMotionData()->interval());
    164 }
    165 
    166 v8::Handle<v8::Value> V8DeviceMotionEvent::initDeviceMotionEventCallback(const v8::Arguments& args)
    167 {
    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>();
    180 }
    181 
    182 } // namespace WebCore
    183 
    184 #endif // ENABLE(DEVICE_ORIENTATION)
    185