Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(134)

Side by Side Diff: content/renderer/device_motion_dispatcher.cc

Issue 10698046: Implements part of Device Motion in the Renderer (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/renderer/device_motion_dispatcher.h"
6
7 #include "content/common/device_motion_messages.h"
8 #include "content/renderer/render_view_impl.h"
9 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceMotionData.h "
10 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceMotionContro ller.h"
11
12 DeviceMotionDispatcher::DeviceMotionDispatcher(
13 RenderViewImpl* render_view)
14 : content::RenderViewObserver(render_view),
15 controller_(NULL),
16 started_(false) {
17 }
18
19 DeviceMotionDispatcher::~DeviceMotionDispatcher() {
20 if (started_)
21 stopUpdating();
22 }
23
24 bool DeviceMotionDispatcher::OnMessageReceived(const IPC::Message& msg) {
25 bool handled = true;
26 IPC_BEGIN_MESSAGE_MAP(DeviceMotionDispatcher, msg)
27 IPC_MESSAGE_HANDLER(DeviceMotionMsg_Updated,
28 OnDeviceMotionUpdated)
29 IPC_MESSAGE_UNHANDLED(handled = false)
30 IPC_END_MESSAGE_MAP()
31 return handled;
32 }
33
34 void DeviceMotionDispatcher::setController(
35 WebKit::WebDeviceMotionController& controller) {
36 controller_ = controller;
37 }
38
39 void DeviceMotionDispatcher::startUpdating() {
40 Send(new DeviceMotionHostMsg_StartUpdating(routing_id()));
41 started_ = true;
42 }
43
44 void DeviceMotionDispatcher::stopUpdating() {
45 Send(new DeviceMotionHostMsg_StopUpdating(routing_id()));
46 started_ = false;
47 }
48
49 WebKit::WebDeviceMotionData DeviceMotionDispatcher::lastMotion()
50 const {
51 return last_motion_;
52 }
53
54 namespace {
55 bool MotionsEqual(const DeviceMotionMsg_Updated_Params& a,
56 WebKit::WebDeviceMotionData* b) {
57 if (a.can_provide_acceleration_x != b->canProvideAccelerationX())
58 return false;
59 if (a.can_provide_acceleration_x && a.acceleration_x != b->accelerationX())
60 return false;
61 if (a.can_provide_acceleration_y != b->canProvideAccelerationY())
62 return false;
63 if (a.can_provide_acceleration_y && a.acceleration_y != b->accelerationY())
64 return false;
65 if (a.can_provide_acceleration_z != b->canProvideAccelerationZ())
66 return false;
67 if (a.can_provide_acceleration_z && a.acceleration_z != b->accelerationZ())
68 return false;
69
70 if (a.can_provide_acceleration_including_gravity_x !=
71 b->canProvideAccelerationIncludingGravityX())
72 return false;
73 if (a.can_provide_acceleration_including_gravity_x &&
74 a.acceleration_including_gravity_x != b->accelerationIncludingGravityX())
75 return false;
76 if (a.can_provide_acceleration_including_gravity_y !=
77 b->canProvideAccelerationIncludingGravityY())
78 return false;
79 if (a.can_provide_acceleration_including_gravity_y &&
80 a.acceleration_including_gravity_y != b->accelerationIncludingGravityY())
81 return false;
82 if (a.can_provide_acceleration_including_gravity_z !=
83 b->canProvideAccelerationIncludingGravityZ())
84 return false;
85 if (a.can_provide_acceleration_including_gravity_z &&
86 a.acceleration_including_gravity_z != b->accelerationIncludingGravityZ())
87 return false;
88
89 if (a.can_provide_rotation_rate_alpha != b->canProvideRotationRateAlpha())
90 return false;
91 if (a.can_provide_rotation_rate_alpha &&
92 a.rotation_rate_alpha != b->rotationRateAlpha())
93 return false;
94 if (a.can_provide_rotation_rate_beta != b->canProvideRotationRateBeta())
95 return false;
96 if (a.can_provide_rotation_rate_beta &&
97 a.rotation_rate_beta != b->rotationRateBeta())
98 return false;
99 if (a.can_provide_rotation_rate_gamma != b->canProvideRotationRateGamma())
100 return false;
101 if (a.can_provide_rotation_rate_gamma &&
102 a.rotation_rate_gamma != b->rotationRateGamma())
103 return false;
104
105 if (a.can_provide_interval != b->canProvideInterval())
106 return false;
107 if (a.can_provide_interval && a.interval != b->interval())
108 return false;
109
110 return true;
111 }
112 } // namespace
113
114 void DeviceMotionDispatcher::OnDeviceMotionUpdated(
115 const DeviceMotionMsg_Updated_Params& p) {
116 if (!last_motion_.isNull() && MotionsEqual(p, &last_motion_))
117 return;
118
119 last_motion_.setNull(false);
120 if (p.can_provide_acceleration_x)
121 last_motion_.setAccelerationX(p.acceleration_x);
122 if (p.can_provide_acceleration_y)
123 last_motion_.setAccelerationY(p.acceleration_y);
124 if (p.can_provide_acceleration_z)
125 last_motion_.setAccelerationZ(p.acceleration_z);
126
127 if (p.can_provide_acceleration_including_gravity_x) {
128 last_motion_.setAccelerationIncludingGravityX(
129 p.acceleration_including_gravity_x);
130 }
131 if (p.can_provide_acceleration_including_gravity_y) {
132 last_motion_.setAccelerationIncludingGravityY(
133 p.acceleration_including_gravity_y);
134 }
135 if (p.can_provide_acceleration_including_gravity_z) {
136 last_motion_.setAccelerationIncludingGravityZ(
137 p.acceleration_including_gravity_z);
138 }
139
140 if (p.can_provide_rotation_rate_alpha)
141 last_motion_.setRotationRateAlpha(p.rotation_rate_alpha);
142 if (p.can_provide_rotation_rate_beta)
143 last_motion_.setRotationRateBeta(p.rotation_rate_beta);
144 if (p.can_provide_rotation_rate_gamma)
145 last_motion_.setRotationRateGamma(p.rotation_rate_gamma);
146
147 if (p.can_provide_interval)
148 last_motion_.setInterval(p.interval);
149
150 controller_.didChangeDeviceMotion(last_motion_);
151 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698