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_sensor_event_pump.h" 6 7 #include "base/logging.h" 8 #include "content/public/renderer/render_thread.h" 9 10 namespace content { 11 12 // Default interval between successive polls, should take into account the 13 // value of |kInertialSensorIntervalMillis| in 14 // content/browser/device_sensors/inertial_sensor_consts.h. 15 const int DeviceSensorEventPump::kDefaultPumpDelayMillis = 50; 16 GetDelayMillis() const17int DeviceSensorEventPump::GetDelayMillis() const { 18 return pump_delay_millis_; 19 } 20 DeviceSensorEventPump()21DeviceSensorEventPump::DeviceSensorEventPump() 22 : pump_delay_millis_(kDefaultPumpDelayMillis), 23 state_(STOPPED) { 24 } 25 DeviceSensorEventPump(int pump_delay_millis)26DeviceSensorEventPump::DeviceSensorEventPump(int pump_delay_millis) 27 : pump_delay_millis_(pump_delay_millis), 28 state_(STOPPED) { 29 DCHECK_GE(pump_delay_millis_, 0); 30 } 31 ~DeviceSensorEventPump()32DeviceSensorEventPump::~DeviceSensorEventPump() { 33 } 34 RequestStart()35bool DeviceSensorEventPump::RequestStart() { 36 DVLOG(2) << "requested start"; 37 38 if (state_ != STOPPED) 39 return false; 40 41 DCHECK(!timer_.IsRunning()); 42 43 if (SendStartMessage()) { 44 state_ = PENDING_START; 45 return true; 46 } 47 return false; 48 } 49 Stop()50bool DeviceSensorEventPump::Stop() { 51 DVLOG(2) << "stop"; 52 53 if (state_ == STOPPED) 54 return true; 55 56 DCHECK((state_ == PENDING_START && !timer_.IsRunning()) || 57 (state_ == RUNNING && timer_.IsRunning())); 58 59 if (timer_.IsRunning()) 60 timer_.Stop(); 61 SendStopMessage(); 62 state_ = STOPPED; 63 return true; 64 } 65 Attach(RenderThread * thread)66void DeviceSensorEventPump::Attach(RenderThread* thread) { 67 if (!thread) 68 return; 69 thread->AddObserver(this); 70 } 71 OnDidStart(base::SharedMemoryHandle handle)72void DeviceSensorEventPump::OnDidStart(base::SharedMemoryHandle handle) { 73 DVLOG(2) << "did start sensor event pump"; 74 75 if (state_ != PENDING_START) 76 return; 77 78 DCHECK(!timer_.IsRunning()); 79 80 if (InitializeReader(handle)) { 81 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(GetDelayMillis()), 82 this, &DeviceSensorEventPump::FireEvent); 83 state_ = RUNNING; 84 } 85 } 86 87 } // namespace content 88