Chromium Code Reviews| Index: content/renderer/device_motion_event_pump.cc |
| diff --git a/content/renderer/device_motion_event_pump.cc b/content/renderer/device_motion_event_pump.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1b650655b233fc108570c5ed4c079a7a8efcb23e |
| --- /dev/null |
| +++ b/content/renderer/device_motion_event_pump.cc |
| @@ -0,0 +1,90 @@ |
| +// Copyright (c) 2013 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 "device_motion_event_pump.h" |
| + |
| +#include "base/bind.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/shared_memory.h" |
| +#include "device_motion_shared_memory_reader.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionData.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionHandler.h" |
| +#include "third_party/WebKit/Source/Platform/chromium/public/WebDeviceMotionListener.h" |
| + |
| +namespace content { |
| + |
| +DeviceMotionEventPump::DeviceMotionEventPump() |
| + : listener_(0), |
| + fire_pending_(false), |
| + pump_delay_(base::TimeDelta::FromMilliseconds(pump_delay_millis)) |
| +{ |
|
jamesr
2013/05/16 19:53:06
This is chromium - the { goes on the previous line
timvolodine
2013/05/20 18:18:48
Done.
|
| +} |
| + |
| +DeviceMotionEventPump::~DeviceMotionEventPump() |
| +{ |
| +} |
| + |
| +void DeviceMotionEventPump::startListeningToDeviceMotion( |
| + WebKit::WebDeviceMotionListener* listener) |
| +{ |
| + if (listener_) |
| + return; |
| + |
| + if (!reader_.get()) |
| + reader_.reset(new DeviceMotionSharedMemoryReader); |
| + |
| + reader_->startUpdating(); |
| + listener_ = listener; |
| + |
| + if (!fire_pending_) { |
| + // Only post a task if the pump is not still running. |
| + MessageLoop::current()->PostTask( |
| + FROM_HERE, |
| + base::Bind(&DeviceMotionEventPump::fire, base::Unretained(this))); |
|
jamesr
2013/05/16 19:53:06
unretained? what prevents this task from running a
timvolodine
2013/05/20 18:18:48
switched to RepeatedTimer, looks like this is exac
|
| + fire_pending_ = true; |
| + } |
| +} |
| + |
| +void DeviceMotionEventPump::stopListeningToDeviceMotion() |
| +{ |
| + LOG(INFO) << "stop listening\n"; |
|
jamesr
2013/05/16 19:53:06
This is way too useless to spam up the INFO log.
timvolodine
2013/05/20 18:18:48
Done.
|
| + reader_->stopUpdating(); |
| + listener_ = 0; |
| +} |
| + |
| +bool DeviceMotionEventPump::shouldFireEvent(WebKit::WebDeviceMotionData& data) |
| +{ |
| + // Device motion should probably be always fired, |
| + // however for device orientation a threshold can be provided. |
| + return true; |
|
jamesr
2013/05/16 19:53:06
why does this function take a data parameter if it
timvolodine
2013/05/20 18:18:48
Done.
|
| +} |
| + |
| +void DeviceMotionEventPump::fire() |
| +{ |
| + fire_pending_ = false; |
| + last_fire_event_time_ = base::TimeTicks::Now(); |
| + |
| + if (!listener_) |
| + return; |
| + |
| + WebKit::WebDeviceMotionData data; |
| + if (reader_->latestDeviceMotionData(data) && shouldFireEvent(data)) |
| + listener_->didChangeDeviceMotion(data); |
| + |
| + const base::TimeDelta dispatch_time = |
| + base::TimeTicks::Now() - last_fire_event_time_; |
| + const base::TimeDelta schedule_delay = |
| + (dispatch_time < pump_delay_) ? pump_delay_ - dispatch_time |
| + : base::TimeDelta(); |
| + |
| + MessageLoop::current()->PostDelayedTask( |
| + FROM_HERE, |
| + base::Bind(&DeviceMotionEventPump::fire, base::Unretained(this)), |
|
jamesr
2013/05/16 19:53:06
same thing here re the unretained
timvolodine
2013/05/20 18:18:48
Done.
|
| + schedule_delay); |
| + fire_pending_ = true; |
| +} |
| + |
| + |
| +} // namespace content |