1 // Copyright 2014 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 "device_motion_event_pump.h" 6 7 #include "content/common/device_sensors/device_motion_messages.h" 8 #include "content/public/renderer/render_thread.h" 9 #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" 10 11 namespace content { 12 13 DeviceMotionEventPump::DeviceMotionEventPump(RenderThread* thread) 14 : DeviceSensorEventPump<blink::WebDeviceMotionListener>(thread) { 15 } 16 17 DeviceMotionEventPump::~DeviceMotionEventPump() { 18 } 19 20 bool DeviceMotionEventPump::OnControlMessageReceived( 21 const IPC::Message& message) { 22 bool handled = true; 23 IPC_BEGIN_MESSAGE_MAP(DeviceMotionEventPump, message) 24 IPC_MESSAGE_HANDLER(DeviceMotionMsg_DidStartPolling, OnDidStart) 25 IPC_MESSAGE_UNHANDLED(handled = false) 26 IPC_END_MESSAGE_MAP() 27 return handled; 28 } 29 30 void DeviceMotionEventPump::FireEvent() { 31 DCHECK(listener()); 32 blink::WebDeviceMotionData data; 33 if (reader_->GetLatestData(&data) && data.allAvailableSensorsAreActive) 34 listener()->didChangeDeviceMotion(data); 35 } 36 37 bool DeviceMotionEventPump::InitializeReader(base::SharedMemoryHandle handle) { 38 if (!reader_) 39 reader_.reset(new DeviceMotionSharedMemoryReader()); 40 return reader_->Initialize(handle); 41 } 42 43 void DeviceMotionEventPump::SendStartMessage() { 44 RenderThread::Get()->Send(new DeviceMotionHostMsg_StartPolling()); 45 } 46 47 void DeviceMotionEventPump::SendStopMessage() { 48 RenderThread::Get()->Send(new DeviceMotionHostMsg_StopPolling()); 49 } 50 51 void DeviceMotionEventPump::SendFakeDataForTesting(void* fake_data) { 52 blink::WebDeviceMotionData data = 53 *static_cast<blink::WebDeviceMotionData*>(fake_data); 54 55 listener()->didChangeDeviceMotion(data); 56 } 57 58 } // namespace content 59