OLD | NEW |
| (Empty) |
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 "bindings/modules/v8/V8DeviceMotionEvent.h" | |
27 | |
28 #include "bindings/core/v8/V8Binding.h" | |
29 #include "modules/device_orientation/DeviceMotionData.h" | |
30 #include "v8/include/v8.h" | |
31 | |
32 namespace blink { | |
33 | |
34 namespace { | |
35 | |
36 DeviceMotionData::Acceleration* readAccelerationArgument( | |
37 v8::Local<v8::Value> value, | |
38 v8::Isolate* isolate) { | |
39 if (isUndefinedOrNull(value)) | |
40 return nullptr; | |
41 | |
42 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
43 v8::Local<v8::Object> object; | |
44 if (!value->ToObject(context).ToLocal(&object)) | |
45 return nullptr; | |
46 | |
47 v8::Local<v8::Value> xValue; | |
48 if (!object->Get(context, v8AtomicString(isolate, "x")).ToLocal(&xValue)) | |
49 return nullptr; | |
50 bool canProvideX = !isUndefinedOrNull(xValue); | |
51 double x; | |
52 if (!xValue->NumberValue(context).To(&x)) | |
53 return nullptr; | |
54 | |
55 v8::Local<v8::Value> yValue; | |
56 if (!object->Get(context, v8AtomicString(isolate, "y")).ToLocal(&yValue)) | |
57 return nullptr; | |
58 bool canProvideY = !isUndefinedOrNull(yValue); | |
59 double y; | |
60 if (!yValue->NumberValue(context).To(&y)) | |
61 return nullptr; | |
62 | |
63 v8::Local<v8::Value> zValue; | |
64 if (!object->Get(context, v8AtomicString(isolate, "z")).ToLocal(&zValue)) | |
65 return nullptr; | |
66 bool canProvideZ = !isUndefinedOrNull(zValue); | |
67 double z; | |
68 if (!zValue->NumberValue(context).To(&z)) | |
69 return nullptr; | |
70 | |
71 if (!canProvideX && !canProvideY && !canProvideZ) | |
72 return nullptr; | |
73 | |
74 return DeviceMotionData::Acceleration::create(canProvideX, x, canProvideY, y, | |
75 canProvideZ, z); | |
76 } | |
77 | |
78 DeviceMotionData::RotationRate* readRotationRateArgument( | |
79 v8::Local<v8::Value> value, | |
80 v8::Isolate* isolate) { | |
81 if (isUndefinedOrNull(value)) | |
82 return nullptr; | |
83 | |
84 v8::Local<v8::Context> context = isolate->GetCurrentContext(); | |
85 v8::Local<v8::Object> object; | |
86 if (!value->ToObject(context).ToLocal(&object)) | |
87 return nullptr; | |
88 | |
89 v8::Local<v8::Value> alphaValue; | |
90 if (!object->Get(context, v8AtomicString(isolate, "alpha")) | |
91 .ToLocal(&alphaValue)) | |
92 return nullptr; | |
93 bool canProvideAlpha = !isUndefinedOrNull(alphaValue); | |
94 double alpha; | |
95 if (!alphaValue->NumberValue(context).To(&alpha)) | |
96 return nullptr; | |
97 | |
98 v8::Local<v8::Value> betaValue; | |
99 if (!object->Get(context, v8AtomicString(isolate, "beta")) | |
100 .ToLocal(&betaValue)) | |
101 return nullptr; | |
102 bool canProvideBeta = !isUndefinedOrNull(betaValue); | |
103 double beta; | |
104 if (!betaValue->NumberValue(context).To(&beta)) | |
105 return nullptr; | |
106 | |
107 v8::Local<v8::Value> gammaValue; | |
108 if (!object->Get(context, v8AtomicString(isolate, "gamma")) | |
109 .ToLocal(&gammaValue)) | |
110 return nullptr; | |
111 bool canProvideGamma = !isUndefinedOrNull(gammaValue); | |
112 double gamma; | |
113 if (!gammaValue->NumberValue(context).To(&gamma)) | |
114 return nullptr; | |
115 | |
116 if (!canProvideAlpha && !canProvideBeta && !canProvideGamma) | |
117 return nullptr; | |
118 | |
119 return DeviceMotionData::RotationRate::create( | |
120 canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma); | |
121 } | |
122 | |
123 } // namespace | |
124 | |
125 void V8DeviceMotionEvent::initDeviceMotionEventMethodCustom( | |
126 const v8::FunctionCallbackInfo<v8::Value>& info) { | |
127 // TODO(haraken): This custom binding should mimic auto-generated code more. | |
128 ExceptionState exceptionState(info.GetIsolate(), | |
129 ExceptionState::ExecutionContext, | |
130 "DeviceMotionEvent", "initDeviceMotionEvent"); | |
131 DeviceMotionEvent* impl = V8DeviceMotionEvent::toImpl(info.Holder()); | |
132 v8::Isolate* isolate = info.GetIsolate(); | |
133 V8StringResource<> type(info[0]); | |
134 if (!type.prepare()) | |
135 return; | |
136 v8::Local<v8::Context> context = info.GetIsolate()->GetCurrentContext(); | |
137 bool bubbles = false; | |
138 if (!info[1]->BooleanValue(context).To(&bubbles)) | |
139 return; | |
140 bool cancelable = false; | |
141 if (!info[2]->BooleanValue(context).To(&cancelable)) | |
142 return; | |
143 DeviceMotionData::Acceleration* acceleration = | |
144 readAccelerationArgument(info[3], isolate); | |
145 DeviceMotionData::Acceleration* accelerationIncludingGravity = | |
146 readAccelerationArgument(info[4], isolate); | |
147 DeviceMotionData::RotationRate* rotationRate = | |
148 readRotationRateArgument(info[5], isolate); | |
149 bool intervalProvided = !isUndefinedOrNull(info[6]); | |
150 double interval = 0; | |
151 if (intervalProvided) { | |
152 interval = toRestrictedDouble(isolate, info[6], exceptionState); | |
153 if (exceptionState.hadException()) | |
154 return; | |
155 } | |
156 DeviceMotionData* deviceMotionData = | |
157 DeviceMotionData::create(acceleration, accelerationIncludingGravity, | |
158 rotationRate, intervalProvided, interval); | |
159 impl->initDeviceMotionEvent(type, bubbles, cancelable, deviceMotionData); | |
160 } | |
161 | |
162 } // namespace blink | |
OLD | NEW |