| 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..085c8df3a21d734d4ce35b231b3646fe575bec6e
|
| --- /dev/null
|
| +++ b/content/renderer/device_motion_dispatcher.cc
|
| @@ -0,0 +1,151 @@
|
| +// 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/WebKit/chromium/public/WebDeviceMotionData.h"
|
| +#include "third_party/WebKit/Source/WebKit/chromium/public/WebDeviceMotionController.h"
|
| +
|
| +DeviceMotionDispatcher::DeviceMotionDispatcher(
|
| + RenderViewImpl* render_view)
|
| + : content::RenderViewObserver(render_view),
|
| + controller_(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::setController(
|
| + WebKit::WebDeviceMotionController& controller) {
|
| + controller_ = controller;
|
| +}
|
| +
|
| +void DeviceMotionDispatcher::startUpdating() {
|
| + 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_;
|
| +}
|
| +
|
| +namespace {
|
| +bool MotionsEqual(const DeviceMotionMsg_Updated_Params& a,
|
| + WebKit::WebDeviceMotionData* b) {
|
| + if (a.can_provide_acceleration_x != b->canProvideAccelerationX())
|
| + return false;
|
| + if (a.can_provide_acceleration_x && a.acceleration_x != b->accelerationX())
|
| + return false;
|
| + if (a.can_provide_acceleration_y != b->canProvideAccelerationY())
|
| + return false;
|
| + if (a.can_provide_acceleration_y && a.acceleration_y != b->accelerationY())
|
| + return false;
|
| + if (a.can_provide_acceleration_z != b->canProvideAccelerationZ())
|
| + return false;
|
| + if (a.can_provide_acceleration_z && a.acceleration_z != b->accelerationZ())
|
| + return false;
|
| +
|
| + if (a.can_provide_acceleration_including_gravity_x !=
|
| + b->canProvideAccelerationIncludingGravityX())
|
| + return false;
|
| + if (a.can_provide_acceleration_including_gravity_x &&
|
| + a.acceleration_including_gravity_x != b->accelerationIncludingGravityX())
|
| + return false;
|
| + if (a.can_provide_acceleration_including_gravity_y !=
|
| + b->canProvideAccelerationIncludingGravityY())
|
| + return false;
|
| + if (a.can_provide_acceleration_including_gravity_y &&
|
| + a.acceleration_including_gravity_y != b->accelerationIncludingGravityY())
|
| + return false;
|
| + if (a.can_provide_acceleration_including_gravity_z !=
|
| + b->canProvideAccelerationIncludingGravityZ())
|
| + return false;
|
| + if (a.can_provide_acceleration_including_gravity_z &&
|
| + a.acceleration_including_gravity_z != b->accelerationIncludingGravityZ())
|
| + return false;
|
| +
|
| + if (a.can_provide_rotation_rate_alpha != b->canProvideRotationRateAlpha())
|
| + return false;
|
| + if (a.can_provide_rotation_rate_alpha &&
|
| + a.rotation_rate_alpha != b->rotationRateAlpha())
|
| + return false;
|
| + if (a.can_provide_rotation_rate_beta != b->canProvideRotationRateBeta())
|
| + return false;
|
| + if (a.can_provide_rotation_rate_beta &&
|
| + a.rotation_rate_beta != b->rotationRateBeta())
|
| + return false;
|
| + if (a.can_provide_rotation_rate_gamma != b->canProvideRotationRateGamma())
|
| + return false;
|
| + if (a.can_provide_rotation_rate_gamma &&
|
| + a.rotation_rate_gamma != b->rotationRateGamma())
|
| + return false;
|
| +
|
| + if (a.can_provide_interval != b->canProvideInterval())
|
| + return false;
|
| + if (a.can_provide_interval && a.interval != b->interval())
|
| + return false;
|
| +
|
| + return true;
|
| +}
|
| +} // namespace
|
| +
|
| +void DeviceMotionDispatcher::OnDeviceMotionUpdated(
|
| + const DeviceMotionMsg_Updated_Params& p) {
|
| + if (!last_motion_.isNull() && MotionsEqual(p, &last_motion_))
|
| + return;
|
| +
|
| + last_motion_.setNull(false);
|
| + if (p.can_provide_acceleration_x)
|
| + last_motion_.setAccelerationX(p.acceleration_x);
|
| + if (p.can_provide_acceleration_y)
|
| + last_motion_.setAccelerationY(p.acceleration_y);
|
| + if (p.can_provide_acceleration_z)
|
| + last_motion_.setAccelerationZ(p.acceleration_z);
|
| +
|
| + if (p.can_provide_acceleration_including_gravity_x) {
|
| + last_motion_.setAccelerationIncludingGravityX(
|
| + p.acceleration_including_gravity_x);
|
| + }
|
| + if (p.can_provide_acceleration_including_gravity_y) {
|
| + last_motion_.setAccelerationIncludingGravityY(
|
| + p.acceleration_including_gravity_y);
|
| + }
|
| + if (p.can_provide_acceleration_including_gravity_z) {
|
| + last_motion_.setAccelerationIncludingGravityZ(
|
| + p.acceleration_including_gravity_z);
|
| + }
|
| +
|
| + if (p.can_provide_rotation_rate_alpha)
|
| + last_motion_.setRotationRateAlpha(p.rotation_rate_alpha);
|
| + if (p.can_provide_rotation_rate_beta)
|
| + last_motion_.setRotationRateBeta(p.rotation_rate_beta);
|
| + if (p.can_provide_rotation_rate_gamma)
|
| + last_motion_.setRotationRateGamma(p.rotation_rate_gamma);
|
| +
|
| + if (p.can_provide_interval)
|
| + last_motion_.setInterval(p.interval);
|
| +
|
| + controller_.didChangeDeviceMotion(last_motion_);
|
| +}
|
|
|