Chromium Code Reviews| Index: content/renderer/device_motion_dispatcher.cc |
| diff --git a/content/renderer/device_motion_dispatcher.cc b/content/renderer/device_motion_dispatcher.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..70bb8f416acccd586036ddea1a35b72504c60f03 |
| --- /dev/null |
| +++ b/content/renderer/device_motion_dispatcher.cc |
| @@ -0,0 +1,87 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "content/renderer/device_motion_dispatcher.h" |
| + |
| +#include "content/common/device_motion_messages.h" |
| +#include "content/renderer/render_view_impl.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionData.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionDetectorClient.h" |
| + |
| +DeviceMotionDispatcher::DeviceMotionDispatcher( |
| + RenderViewImpl* render_view) |
| + : content::RenderViewObserver(render_view), |
| + client_(NULL), |
| + started_(false) { |
| +} |
| + |
| +DeviceMotionDispatcher::~DeviceMotionDispatcher() { |
| + if (started_) |
| + stopUpdating(); |
| +} |
| + |
| +bool DeviceMotionDispatcher::OnMessageReceived(const IPC::Message& msg) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(DeviceMotionDispatcher, msg) |
| + IPC_MESSAGE_HANDLER(DeviceMotionMsg_Updated, OnDeviceMotionUpdated) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void DeviceMotionDispatcher::startUpdating( |
| + WebKit::WebDeviceMotionDetectorClient* client) { |
| + client_.reset(client); |
| + Send(new DeviceMotionHostMsg_StartUpdating(routing_id())); |
| + started_ = true; |
| +} |
| + |
| +void DeviceMotionDispatcher::stopUpdating() { |
| + Send(new DeviceMotionHostMsg_StopUpdating(routing_id())); |
| + started_ = false; |
| +} |
| + |
| +WebKit::WebDeviceMotionData DeviceMotionDispatcher::lastMotion() |
| + const { |
| + return last_motion_; |
| +} |
| + |
| +void DeviceMotionDispatcher::OnDeviceMotionUpdated( |
| + const DeviceMotionMsg_Updated_Params& p) { |
| + if (client_ == NULL) |
| + return; |
| + |
|
hans
2012/08/16 16:38:40
should last_motion_ be reset here before we start
aousterh
2012/08/16 17:21:05
Good point. Done.
|
| + if (p.can_provide_acceleration_x || p.can_provide_acceleration_y || |
| + p.can_provide_acceleration_z) |
| + last_motion_.initializeAcceleration(p.can_provide_acceleration_x, |
| + p.acceleration_x, |
| + p.can_provide_acceleration_y, |
| + p.acceleration_y, |
| + p.can_provide_acceleration_z, |
| + p.acceleration_z); |
| + |
| + if (p.can_provide_acceleration_including_gravity_x || |
| + p.can_provide_acceleration_including_gravity_y || |
| + p.can_provide_acceleration_including_gravity_z) |
| + last_motion_.initializeAccelerationIncludingGravity( |
| + p.can_provide_acceleration_including_gravity_x, |
| + p.acceleration_including_gravity_x, |
| + p.can_provide_acceleration_including_gravity_y, |
| + p.acceleration_including_gravity_y, |
| + p.can_provide_acceleration_including_gravity_z, |
| + p.acceleration_including_gravity_z); |
| + |
| + if (p.can_provide_rotation_rate_alpha || p.can_provide_rotation_rate_beta || |
| + p.can_provide_rotation_rate_gamma) |
| + last_motion_.initializeRotationRate(p.can_provide_rotation_rate_alpha, |
| + p.rotation_rate_alpha, |
| + p.can_provide_rotation_rate_beta, |
| + p.rotation_rate_beta, |
| + p.can_provide_rotation_rate_gamma, |
| + p.rotation_rate_gamma); |
| + if (p.can_provide_interval) |
| + last_motion_.initializeInterval(p.interval); |
| + |
| + client_->didDetectDeviceMotion(last_motion_); |
| +} |