Chromium Code Reviews| Index: content/browser/device_orientation/motion_message_filter.cc |
| diff --git a/content/browser/device_orientation/motion_message_filter.cc b/content/browser/device_orientation/motion_message_filter.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..4339827ba2199c1993ccbd72b5167dcb4ea8566a |
| --- /dev/null |
| +++ b/content/browser/device_orientation/motion_message_filter.cc |
| @@ -0,0 +1,129 @@ |
| +// 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/browser/device_orientation/motion_message_filter.h" |
| + |
| +#include "base/memory/scoped_ptr.h" |
| +#include "content/browser/device_orientation/motion.h" |
| +#include "content/browser/device_orientation/provider.h" |
| +#include "content/browser/renderer_host/render_view_host_impl.h" |
| +#include "content/common/device_motion_messages.h" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +using content::BrowserThread; |
| + |
| +namespace device_orientation { |
| + |
| +MotionMessageFilter::MotionMessageFilter() : provider_(NULL) { |
| +} |
| + |
| +MotionMessageFilter::~MotionMessageFilter() { |
| +} |
| + |
| +class MotionMessageFilter::MotionObserverDelegate |
| + : public base::RefCounted<MotionObserverDelegate>, |
| + public Provider::MotionObserver { |
| + public: |
| + // Create MotionObserverDelegate that observes provider and forwards |
| + // updates to render_view_id in process_id. |
| + // Will stop observing provider when destructed. |
| + MotionObserverDelegate(Provider* provider, int render_view_id, |
| + IPC::Sender* sender); |
| + |
| + // From Provider::MotionObserver. |
| + virtual void OnMotionUpdate(const Motion& motion); |
| + |
| + private: |
| + friend class base::RefCounted<MotionObserverDelegate>; |
| + virtual ~MotionObserverDelegate(); |
| + |
| + scoped_refptr<Provider> provider_; |
| + int render_view_id_; |
| + IPC::Sender* sender_; // Weak pointer. |
|
bulach
2012/07/02 12:47:56
not sure how is this a weak pointer in this contex
aousterh
2012/07/04 13:31:55
The sender isn't owned by the MotionMessageFilter
|
| + |
| + DISALLOW_COPY_AND_ASSIGN(MotionObserverDelegate); |
| +}; |
| + |
| +MotionMessageFilter::MotionObserverDelegate::MotionObserverDelegate( |
| + Provider* provider, int render_view_id, IPC::Sender* sender) |
| + : provider_(provider), |
| + render_view_id_(render_view_id), |
| + sender_(sender) { |
| + provider_->AddMotionObserver(this); |
| +} |
| + |
| +MotionMessageFilter::MotionObserverDelegate::~MotionObserverDelegate() { |
| + provider_->RemoveMotionObserver(this); |
| +} |
| + |
| +void MotionMessageFilter::MotionObserverDelegate::OnMotionUpdate( |
| + const Motion& motion) { |
| + DeviceMotionMsg_Updated_Params params; |
| + |
| + params.can_provide_acceleration_x = motion.canProvideAccelerationX(); |
| + params.acceleration_x = motion.accelerationX(); |
| + params.can_provide_acceleration_y = motion.canProvideAccelerationY(); |
| + params.acceleration_y = motion.accelerationY(); |
| + params.can_provide_acceleration_z = motion.canProvideAccelerationZ(); |
| + params.acceleration_z = motion.accelerationZ(); |
| + |
| + params.can_provide_acceleration_including_gravity_x = |
| + motion.canProvideAccelerationIncludingGravityX(); |
| + params.acceleration_including_gravity_x = |
| + motion.accelerationIncludingGravityX(); |
| + params.can_provide_acceleration_including_gravity_y = |
| + motion.canProvideAccelerationIncludingGravityY(); |
| + params.acceleration_including_gravity_y = |
| + motion.accelerationIncludingGravityY(); |
| + params.can_provide_acceleration_including_gravity_z = |
| + motion.canProvideAccelerationIncludingGravityZ(); |
| + params.acceleration_including_gravity_z = |
| + motion.accelerationIncludingGravityZ(); |
| + |
| + params.can_provide_rotation_rate_alpha = |
| + motion.canProvideRotationRateAlpha(); |
| + params.rotation_rate_alpha = motion.rotationRateAlpha(); |
| + params.can_provide_rotation_rate_beta = |
| + motion.canProvideRotationRateBeta(); |
| + params.rotation_rate_beta = motion.rotationRateBeta(); |
| + params.can_provide_rotation_rate_gamma = |
| + motion.canProvideRotationRateGamma(); |
|
bulach
2012/07/02 12:47:56
nit: from 72-91, need an extra indent for the cont
aousterh
2012/07/04 13:31:55
Done.
|
| + params.rotation_rate_gamma = motion.rotationRateGamma(); |
| + |
| + params.can_provide_interval = motion.canProvideInterval(); |
| + params.interval = motion.interval(); |
| + |
| + sender_->Send(new DeviceMotionMsg_Updated(render_view_id_, params)); |
| +} |
| + |
| +bool MotionMessageFilter::OnMessageReceived(const IPC::Message& message, |
| + bool* message_was_ok) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP_EX(MotionMessageFilter, message, *message_was_ok) |
| + IPC_MESSAGE_HANDLER(DeviceMotionHostMsg_StartUpdating, OnStartUpdating) |
| + IPC_MESSAGE_HANDLER(DeviceMotionHostMsg_StopUpdating, OnStopUpdating) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void MotionMessageFilter::OnStartUpdating(int render_view_id) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + if (!provider_) |
| + provider_ = Provider::GetInstance(); |
| + |
| + observers_map_[render_view_id] = new MotionObserverDelegate(provider_, |
| + render_view_id, |
| + this); |
|
bulach
2012/07/02 12:47:56
nit: move this line to 119, and indent it just by
aousterh
2012/07/04 13:31:55
Done.
|
| +} |
| + |
| +void MotionMessageFilter::OnStopUpdating(int render_view_id) { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| + |
| + observers_map_.erase(render_view_id); |
| +} |
| + |
| +} // namespace device_motion |