| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device_motion_event_pump.h" | 5 #include "device_motion_event_pump.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "content/common/device_orientation/device_motion_messages.h" |
| 8 #include "base/logging.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/memory/shared_memory.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "content/common/device_motion_messages.h" | |
| 13 #include "content/public/renderer/render_thread.h" | 8 #include "content/public/renderer/render_thread.h" |
| 14 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" | 9 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" |
| 15 | 10 |
| 16 namespace content { | 11 namespace content { |
| 17 | 12 |
| 18 const double DeviceMotionEventPump::kPumpDelayMillis = 40; | 13 DeviceMotionEventPump::DeviceMotionEventPump() |
| 19 | 14 : DeviceSensorEventPump(), listener_(0) { |
| 20 double DeviceMotionEventPump::GetDelayMillis() { | |
| 21 return kPumpDelayMillis; | |
| 22 } | 15 } |
| 23 | 16 |
| 24 DeviceMotionEventPump::DeviceMotionEventPump() | 17 DeviceMotionEventPump::DeviceMotionEventPump(int pump_delay_millis) |
| 25 : listener_(0), state_(STOPPED) { | 18 : DeviceSensorEventPump(pump_delay_millis), listener_(0) { |
| 26 } | 19 } |
| 27 | 20 |
| 28 DeviceMotionEventPump::~DeviceMotionEventPump() { | 21 DeviceMotionEventPump::~DeviceMotionEventPump() { |
| 29 } | 22 } |
| 30 | 23 |
| 31 bool DeviceMotionEventPump::SetListener( | 24 bool DeviceMotionEventPump::SetListener( |
| 32 WebKit::WebDeviceMotionListener* listener) { | 25 WebKit::WebDeviceMotionListener* listener) { |
| 33 listener_ = listener; | 26 listener_ = listener; |
| 34 if (listener_) | 27 return listener_ ? RequestStart() : Stop(); |
| 35 return StartFetchingDeviceMotion(); | |
| 36 return StopFetchingDeviceMotion(); | |
| 37 } | 28 } |
| 38 | 29 |
| 39 void DeviceMotionEventPump::SetDeviceMotionReader( | 30 bool DeviceMotionEventPump::OnControlMessageReceived( |
| 40 scoped_ptr<DeviceMotionSharedMemoryReader> reader) { | 31 const IPC::Message& message) { |
| 41 reader_.reset(reader.release()); | 32 bool handled = true; |
| 42 } | 33 IPC_BEGIN_MESSAGE_MAP(DeviceMotionEventPump, message) |
| 43 | 34 IPC_MESSAGE_HANDLER(DeviceMotionMsg_DidStartPolling, OnDidStart) |
| 44 bool DeviceMotionEventPump::StartFetchingDeviceMotion() { | 35 IPC_MESSAGE_UNHANDLED(handled = false) |
| 45 DVLOG(2) << "start fetching device motion"; | 36 IPC_END_MESSAGE_MAP() |
| 46 | 37 return handled; |
| 47 if (state_ != STOPPED) | |
| 48 return false; | |
| 49 | |
| 50 DCHECK(!timer_.IsRunning()); | |
| 51 | |
| 52 if (RenderThread::Get()->Send(new DeviceMotionHostMsg_StartPolling())) { | |
| 53 state_ = PENDING_START; | |
| 54 return true; | |
| 55 } | |
| 56 return false; | |
| 57 } | |
| 58 | |
| 59 bool DeviceMotionEventPump::StopFetchingDeviceMotion() { | |
| 60 DVLOG(2) << "stop fetching device motion"; | |
| 61 | |
| 62 if (state_ == STOPPED) | |
| 63 return true; | |
| 64 | |
| 65 DCHECK((state_ == PENDING_START && !timer_.IsRunning()) || | |
| 66 (state_ == RUNNING && timer_.IsRunning())); | |
| 67 | |
| 68 if (timer_.IsRunning()) | |
| 69 timer_.Stop(); | |
| 70 RenderThread::Get()->Send(new DeviceMotionHostMsg_StopPolling()); | |
| 71 state_ = STOPPED; | |
| 72 return true; | |
| 73 } | 38 } |
| 74 | 39 |
| 75 void DeviceMotionEventPump::FireEvent() { | 40 void DeviceMotionEventPump::FireEvent() { |
| 76 DCHECK(listener_); | 41 DCHECK(listener_); |
| 77 WebKit::WebDeviceMotionData data; | 42 WebKit::WebDeviceMotionData data; |
| 78 if (reader_->GetLatestData(&data) && data.allAvailableSensorsAreActive) | 43 if (reader_->GetLatestData(&data) && data.allAvailableSensorsAreActive) |
| 79 listener_->didChangeDeviceMotion(data); | 44 listener_->didChangeDeviceMotion(data); |
| 80 } | 45 } |
| 81 | 46 |
| 82 void DeviceMotionEventPump::Attach(RenderThread* thread) { | 47 bool DeviceMotionEventPump::InitializeReader(base::SharedMemoryHandle handle) { |
| 83 if (!thread) | 48 if (!reader_) |
| 84 return; | 49 reader_.reset(new DeviceMotionSharedMemoryReader()); |
| 85 thread->AddObserver(this); | 50 return reader_->Initialize(handle); |
| 86 } | 51 } |
| 87 | 52 |
| 88 void DeviceMotionEventPump::OnDidStartDeviceMotion( | 53 bool DeviceMotionEventPump::SendStartMessage() { |
| 89 base::SharedMemoryHandle renderer_handle) { | 54 return RenderThread::Get()->Send(new DeviceMotionHostMsg_StartPolling()); |
| 90 DVLOG(2) << "did start fetching device motion"; | |
| 91 | |
| 92 if (state_ != PENDING_START) | |
| 93 return; | |
| 94 | |
| 95 DCHECK(!timer_.IsRunning()); | |
| 96 if (!reader_) | |
| 97 reader_.reset(new DeviceMotionSharedMemoryReader()); | |
| 98 | |
| 99 if (reader_->Initialize(renderer_handle)) { | |
| 100 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(kPumpDelayMillis), | |
| 101 this, &DeviceMotionEventPump::FireEvent); | |
| 102 state_ = RUNNING; | |
| 103 } | |
| 104 } | 55 } |
| 105 | 56 |
| 106 bool DeviceMotionEventPump::OnControlMessageReceived( | 57 |
| 107 const IPC::Message& message) { | 58 bool DeviceMotionEventPump::SendStopMessage() { |
| 108 bool handled = true; | 59 return RenderThread::Get()->Send(new DeviceMotionHostMsg_StopPolling()); |
| 109 IPC_BEGIN_MESSAGE_MAP(DeviceMotionEventPump, message) | |
| 110 IPC_MESSAGE_HANDLER(DeviceMotionMsg_DidStartPolling, | |
| 111 OnDidStartDeviceMotion) | |
| 112 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 113 IPC_END_MESSAGE_MAP() | |
| 114 return handled; | |
| 115 } | 60 } |
| 116 | 61 |
| 117 } // namespace content | 62 } // namespace content |
| OLD | NEW |