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 3 // found in the LICENSE file. 4 5 #include "content/browser/device_orientation/device_motion_message_filter.h" 6 7 #include "content/browser/device_orientation/device_inertial_sensor_service.h" 8 #include "content/common/device_orientation/device_motion_messages.h" 9 10 namespace content { 11 DeviceMotionMessageFilter()12DeviceMotionMessageFilter::DeviceMotionMessageFilter() 13 : is_started_(false) { 14 } 15 ~DeviceMotionMessageFilter()16DeviceMotionMessageFilter::~DeviceMotionMessageFilter() { 17 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 18 if (is_started_) 19 DeviceInertialSensorService::GetInstance()->RemoveConsumer( 20 CONSUMER_TYPE_MOTION); 21 } 22 OnMessageReceived(const IPC::Message & message,bool * message_was_ok)23bool DeviceMotionMessageFilter::OnMessageReceived( 24 const IPC::Message& message, 25 bool* message_was_ok) { 26 bool handled = true; 27 IPC_BEGIN_MESSAGE_MAP_EX(DeviceMotionMessageFilter, 28 message, 29 *message_was_ok) 30 IPC_MESSAGE_HANDLER(DeviceMotionHostMsg_StartPolling, 31 OnDeviceMotionStartPolling) 32 IPC_MESSAGE_HANDLER(DeviceMotionHostMsg_StopPolling, 33 OnDeviceMotionStopPolling) 34 IPC_MESSAGE_UNHANDLED(handled = false) 35 IPC_END_MESSAGE_MAP_EX() 36 return handled; 37 } 38 OnDeviceMotionStartPolling()39void DeviceMotionMessageFilter::OnDeviceMotionStartPolling() { 40 DCHECK(!is_started_); 41 if (is_started_) 42 return; 43 is_started_ = true; 44 DeviceInertialSensorService::GetInstance()->AddConsumer( 45 CONSUMER_TYPE_MOTION); 46 DidStartDeviceMotionPolling(); 47 } 48 OnDeviceMotionStopPolling()49void DeviceMotionMessageFilter::OnDeviceMotionStopPolling() { 50 DCHECK(is_started_); 51 if (!is_started_) 52 return; 53 is_started_ = false; 54 DeviceInertialSensorService::GetInstance()->RemoveConsumer( 55 CONSUMER_TYPE_MOTION); 56 } 57 DidStartDeviceMotionPolling()58void DeviceMotionMessageFilter::DidStartDeviceMotionPolling() { 59 Send(new DeviceMotionMsg_DidStartPolling( 60 DeviceInertialSensorService::GetInstance()-> 61 GetSharedMemoryHandleForProcess( 62 CONSUMER_TYPE_MOTION, PeerHandle()))); 63 } 64 65 } // namespace content 66