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

Side by Side Diff: content/browser/device_orientation/motion_message_filter.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/browser/device_orientation/motion_message_filter.h"
6
7 #include "base/memory/scoped_ptr.h"
8 #include "content/browser/device_orientation/motion.h"
9 #include "content/browser/device_orientation/provider.h"
10 #include "content/browser/renderer_host/render_view_host_impl.h"
11 #include "content/common/device_motion_messages.h"
12 #include "content/public/browser/browser_thread.h"
13
14 using content::BrowserThread;
15
16 namespace device_orientation {
17
18 MotionMessageFilter::MotionMessageFilter() : provider_(NULL) {
19 }
20
21 MotionMessageFilter::~MotionMessageFilter() {
22 }
23
24 class MotionMessageFilter::MotionObserverDelegate
25 : public base::RefCounted<MotionObserverDelegate>,
26 public Provider::MotionObserver {
27 public:
28 // Create MotionObserverDelegate that observes provider and forwards
29 // updates to render_view_id in process_id.
30 // Will stop observing provider when destructed.
31 MotionObserverDelegate(Provider* provider, int render_view_id,
32 IPC::Sender* sender);
33
34 // From Provider::MotionObserver.
35 virtual void OnMotionUpdate(const Motion& motion);
36
37 private:
38 friend class base::RefCounted<MotionObserverDelegate>;
39 virtual ~MotionObserverDelegate();
40
41 scoped_refptr<Provider> provider_;
42 int render_view_id_;
43 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
44
45 DISALLOW_COPY_AND_ASSIGN(MotionObserverDelegate);
46 };
47
48 MotionMessageFilter::MotionObserverDelegate::MotionObserverDelegate(
49 Provider* provider, int render_view_id, IPC::Sender* sender)
50 : provider_(provider),
51 render_view_id_(render_view_id),
52 sender_(sender) {
53 provider_->AddMotionObserver(this);
54 }
55
56 MotionMessageFilter::MotionObserverDelegate::~MotionObserverDelegate() {
57 provider_->RemoveMotionObserver(this);
58 }
59
60 void MotionMessageFilter::MotionObserverDelegate::OnMotionUpdate(
61 const Motion& motion) {
62 DeviceMotionMsg_Updated_Params params;
63
64 params.can_provide_acceleration_x = motion.canProvideAccelerationX();
65 params.acceleration_x = motion.accelerationX();
66 params.can_provide_acceleration_y = motion.canProvideAccelerationY();
67 params.acceleration_y = motion.accelerationY();
68 params.can_provide_acceleration_z = motion.canProvideAccelerationZ();
69 params.acceleration_z = motion.accelerationZ();
70
71 params.can_provide_acceleration_including_gravity_x =
72 motion.canProvideAccelerationIncludingGravityX();
73 params.acceleration_including_gravity_x =
74 motion.accelerationIncludingGravityX();
75 params.can_provide_acceleration_including_gravity_y =
76 motion.canProvideAccelerationIncludingGravityY();
77 params.acceleration_including_gravity_y =
78 motion.accelerationIncludingGravityY();
79 params.can_provide_acceleration_including_gravity_z =
80 motion.canProvideAccelerationIncludingGravityZ();
81 params.acceleration_including_gravity_z =
82 motion.accelerationIncludingGravityZ();
83
84 params.can_provide_rotation_rate_alpha =
85 motion.canProvideRotationRateAlpha();
86 params.rotation_rate_alpha = motion.rotationRateAlpha();
87 params.can_provide_rotation_rate_beta =
88 motion.canProvideRotationRateBeta();
89 params.rotation_rate_beta = motion.rotationRateBeta();
90 params.can_provide_rotation_rate_gamma =
91 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.
92 params.rotation_rate_gamma = motion.rotationRateGamma();
93
94 params.can_provide_interval = motion.canProvideInterval();
95 params.interval = motion.interval();
96
97 sender_->Send(new DeviceMotionMsg_Updated(render_view_id_, params));
98 }
99
100 bool MotionMessageFilter::OnMessageReceived(const IPC::Message& message,
101 bool* message_was_ok) {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
103 bool handled = true;
104 IPC_BEGIN_MESSAGE_MAP_EX(MotionMessageFilter, message, *message_was_ok)
105 IPC_MESSAGE_HANDLER(DeviceMotionHostMsg_StartUpdating, OnStartUpdating)
106 IPC_MESSAGE_HANDLER(DeviceMotionHostMsg_StopUpdating, OnStopUpdating)
107 IPC_MESSAGE_UNHANDLED(handled = false)
108 IPC_END_MESSAGE_MAP()
109 return handled;
110 }
111
112 void MotionMessageFilter::OnStartUpdating(int render_view_id) {
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
114
115 if (!provider_)
116 provider_ = Provider::GetInstance();
117
118 observers_map_[render_view_id] = new MotionObserverDelegate(provider_,
119 render_view_id,
120 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.
121 }
122
123 void MotionMessageFilter::OnStopUpdating(int render_view_id) {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
125
126 observers_map_.erase(render_view_id);
127 }
128
129 } // namespace device_motion
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698