Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / modules / device_orientation / DeviceMotionData.cpp
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 "modules/device_orientation/DeviceMotionData.h"
28 #include "public/platform/WebDeviceMotionData.h"
29
30 namespace WebCore {
31
32 DEFINE_GC_INFO(DeviceMotionData::Acceleration);
33 DEFINE_GC_INFO(DeviceMotionData::RotationRate);
34 DEFINE_GC_INFO(DeviceMotionData);
35
36 PassRefPtrWillBeRawPtr<DeviceMotionData::Acceleration> DeviceMotionData::Acceleration::create(
37     bool canProvideX, double x, bool canProvideY, double y, bool canProvideZ, double z)
38 {
39     return adoptRefWillBeNoop(new DeviceMotionData::Acceleration(canProvideX, x, canProvideY, y, canProvideZ, z));
40 }
41
42 DeviceMotionData::Acceleration::Acceleration(bool canProvideX, double x, bool canProvideY, double y, bool canProvideZ, double z)
43     : m_x(x)
44     , m_y(y)
45     , m_z(z)
46     , m_canProvideX(canProvideX)
47     , m_canProvideY(canProvideY)
48     , m_canProvideZ(canProvideZ)
49
50 {
51 }
52
53 PassRefPtrWillBeRawPtr<DeviceMotionData::RotationRate> DeviceMotionData::RotationRate::create(
54     bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
55 {
56     return adoptRefWillBeNoop(new DeviceMotionData::RotationRate(canProvideAlpha, alpha, canProvideBeta, beta, canProvideGamma, gamma));
57 }
58
59 DeviceMotionData::RotationRate::RotationRate(bool canProvideAlpha, double alpha, bool canProvideBeta, double beta, bool canProvideGamma, double gamma)
60     : m_alpha(alpha)
61     , m_beta(beta)
62     , m_gamma(gamma)
63     , m_canProvideAlpha(canProvideAlpha)
64     , m_canProvideBeta(canProvideBeta)
65     , m_canProvideGamma(canProvideGamma)
66 {
67 }
68
69 PassRefPtrWillBeRawPtr<DeviceMotionData> DeviceMotionData::create()
70 {
71     return adoptRefWillBeNoop(new DeviceMotionData);
72 }
73
74 PassRefPtrWillBeRawPtr<DeviceMotionData> DeviceMotionData::create(
75     PassRefPtrWillBeRawPtr<Acceleration> acceleration,
76     PassRefPtrWillBeRawPtr<Acceleration> accelerationIncludingGravity,
77     PassRefPtrWillBeRawPtr<RotationRate> rotationRate,
78     bool canProvideInterval,
79     double interval)
80 {
81     return adoptRefWillBeNoop(new DeviceMotionData(acceleration, accelerationIncludingGravity, rotationRate, canProvideInterval, interval));
82 }
83
84 PassRefPtrWillBeRawPtr<DeviceMotionData> DeviceMotionData::create(const blink::WebDeviceMotionData& data)
85 {
86     return DeviceMotionData::create(
87         DeviceMotionData::Acceleration::create(
88             data.hasAccelerationX, data.accelerationX,
89             data.hasAccelerationY, data.accelerationY,
90             data.hasAccelerationZ, data.accelerationZ),
91         DeviceMotionData::Acceleration::create(
92             data.hasAccelerationIncludingGravityX, data.accelerationIncludingGravityX,
93             data.hasAccelerationIncludingGravityY, data.accelerationIncludingGravityY,
94             data.hasAccelerationIncludingGravityZ, data.accelerationIncludingGravityZ),
95         DeviceMotionData::RotationRate::create(
96             data.hasRotationRateAlpha, data.rotationRateAlpha,
97             data.hasRotationRateBeta, data.rotationRateBeta,
98             data.hasRotationRateGamma, data.rotationRateGamma),
99         true, data.interval);
100 }
101
102 DeviceMotionData::DeviceMotionData()
103     : m_canProvideInterval(false)
104     , m_interval(0)
105 {
106 }
107
108 DeviceMotionData::DeviceMotionData(
109     PassRefPtrWillBeRawPtr<Acceleration> acceleration,
110     PassRefPtrWillBeRawPtr<Acceleration> accelerationIncludingGravity,
111     PassRefPtrWillBeRawPtr<RotationRate> rotationRate,
112     bool canProvideInterval,
113     double interval)
114     : m_acceleration(acceleration)
115     , m_accelerationIncludingGravity(accelerationIncludingGravity)
116     , m_rotationRate(rotationRate)
117     , m_canProvideInterval(canProvideInterval)
118     , m_interval(interval)
119 {
120 }
121
122 void DeviceMotionData::trace(Visitor* visitor)
123 {
124     visitor->trace(m_acceleration);
125     visitor->trace(m_accelerationIncludingGravity);
126     visitor->trace(m_rotationRate);
127 }
128
129 bool DeviceMotionData::canProvideEventData() const
130 {
131     const bool hasAcceleration = m_acceleration && (m_acceleration->canProvideX() || m_acceleration->canProvideY() || m_acceleration->canProvideZ());
132     const bool hasAccelerationIncludingGravity = m_accelerationIncludingGravity && (m_accelerationIncludingGravity->canProvideX() || m_accelerationIncludingGravity->canProvideY() || m_accelerationIncludingGravity->canProvideZ());
133     const bool hasRotationRate = m_rotationRate && (m_rotationRate->canProvideAlpha() || m_rotationRate->canProvideBeta() || m_rotationRate->canProvideGamma());
134
135     return hasAcceleration || hasAccelerationIncludingGravity || hasRotationRate;
136 }
137
138 } // namespace WebCore