Chromium Code Reviews| Index: content/renderer/device_orientation/device_motion_event_pump.cc |
| diff --git a/content/renderer/device_orientation/device_motion_event_pump.cc b/content/renderer/device_orientation/device_motion_event_pump.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..18042c595bd37ce346112dc95691c0ad3d4b3143 |
| --- /dev/null |
| +++ b/content/renderer/device_orientation/device_motion_event_pump.cc |
| @@ -0,0 +1,119 @@ |
| +// 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/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/message_loop.h" |
| +#include "base/shared_memory.h" |
| +#include "content/common/device_motion_messages.h" |
| +#include "content/public/renderer/render_thread.h" |
| +#include "device_motion_shared_memory_reader.h" |
| +#include "third_party/WebKit/public/platform/WebDeviceMotionData.h" |
| +#include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" |
| + |
| +namespace content { |
| + |
| +const double DeviceMotionEventPump::kPumpDelayMillis = 40; |
| + |
| +DeviceMotionEventPump::DeviceMotionEventPump() |
| + : listener_(0), state_(STOPPED) { |
| +} |
| + |
| +DeviceMotionEventPump::~DeviceMotionEventPump() { |
| +} |
| + |
| +bool DeviceMotionEventPump::SetListener( |
| + WebKit::WebDeviceMotionListener* listener) { |
| + listener_ = listener; |
| + if (listener_) |
| + return StartFetchingDeviceMotion(); |
| + return StopFetchingDeviceMotion(); |
| +} |
| + |
| +void DeviceMotionEventPump::SetDeviceMotionReader( |
| + DeviceMotionSharedMemoryReader* reader) { |
| + reader_.reset(reader); |
|
darin (slow to review)
2013/06/17 05:27:46
since this is a transfer of ownership, the argumen
timvolodine
2013/06/17 20:10:55
Done.
|
| +} |
| + |
| +bool DeviceMotionEventPump::StartFetchingDeviceMotion() { |
| + DVLOG(2) << "start fetching device motion"; |
| + LOG(INFO) << "PUMP start fetching"; |
|
darin (slow to review)
2013/06/17 05:27:46
nit: are both of these log statements necessary?
timvolodine
2013/06/17 20:10:55
sorry this was for debugging, removed all extra lo
|
| + |
| + if (state_ != STOPPED) |
| + return false; |
| + |
| + DCHECK(!timer_.IsRunning()); |
| + |
| + if (RenderThread::Get()->Send(new DeviceMotionHostMsg_StartPolling())) { |
| + state_ = PENDING_START; |
| + return true; |
| + } |
| + return false; |
| +} |
| + |
| +bool DeviceMotionEventPump::StopFetchingDeviceMotion() { |
| + DVLOG(2) << "stop fetching device motion"; |
| + LOG(INFO) << "PUMP stop fetching"; |
|
darin (slow to review)
2013/06/17 05:27:46
ditto: can you do with only one log statement? do
timvolodine
2013/06/17 20:10:55
Done.
|
| + |
| + if (state_ == STOPPED) |
| + return true; |
| + |
| + DCHECK((state_ == PENDING_START && !timer_.IsRunning()) |
|
darin (slow to review)
2013/06/17 05:27:46
nit: chromium style is to put the "||" operator at
timvolodine
2013/06/17 20:10:55
Done.
|
| + || (state_ == RUNNING && timer_.IsRunning())); |
| + |
| + if (timer_.IsRunning()); |
| + timer_.Stop(); |
| + RenderThread::Get()->Send(new DeviceMotionHostMsg_StopPolling()); |
| + state_ = STOPPED; |
| + return true; |
| +} |
| + |
| +void DeviceMotionEventPump::FireEvent() { |
| + DCHECK(listener_); |
| + LOG(INFO) << "PUMP fire"; |
|
darin (slow to review)
2013/06/17 05:27:46
nit: it seems like there could be a lot of these l
timvolodine
2013/06/17 20:10:55
Done.
|
| + WebKit::WebDeviceMotionData data; |
| + if (reader_->GetLatestDeviceMotionData(data)) |
| + listener_->didChangeDeviceMotion(data); |
| +} |
| + |
| +void DeviceMotionEventPump::Attach(RenderThread* thread) { |
| + if (!thread) |
| + return; |
| + thread->AddObserver(this); |
| +} |
| + |
| +void DeviceMotionEventPump::OnDidStartDeviceMotion( |
| + base::SharedMemoryHandle renderer_handle) { |
| + DVLOG(2) << "did start fetching device motion"; |
| + LOG(INFO) << "PUMP did start fetching"; |
| + |
| + if (state_ != PENDING_START) |
| + return; |
| + |
| + DCHECK(!timer_.IsRunning()); |
| + if (!reader_) |
| + SetDeviceMotionReader(new DeviceMotionSharedMemoryReader); |
|
darin (slow to review)
2013/06/17 05:27:46
nit: the code might be clearer if you just directl
timvolodine
2013/06/17 20:10:55
Done.
|
| + |
| + if (reader_->Initialize(renderer_handle)) { |
| + timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kPumpDelayMillis), |
| + this, &DeviceMotionEventPump::FireEvent); |
| + state_ = RUNNING; |
| + } |
| +} |
| + |
| +bool DeviceMotionEventPump::OnControlMessageReceived( |
| + const IPC::Message& message) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(DeviceMotionEventPump, message) |
| + IPC_MESSAGE_HANDLER(DeviceMotionMsg_DidStartPolling, |
| + OnDidStartDeviceMotion) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +} // namespace content |