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 "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 const int DeviceSensorEventPump::kDefaultPumpDelayMillis = 40; 13 14 int DeviceSensorEventPump::GetDelayMillis() const { 15 return pump_delay_millis_; 16 } 17 18 DeviceSensorEventPump::DeviceSensorEventPump() 19 : pump_delay_millis_(kDefaultPumpDelayMillis), 20 state_(STOPPED) { 21 } 22 23 DeviceSensorEventPump::DeviceSensorEventPump(int pump_delay_millis) 24 : pump_delay_millis_(pump_delay_millis), 25 state_(STOPPED) { 26 DCHECK(pump_delay_millis_ > 0); 27 } 28 29 DeviceSensorEventPump::~DeviceSensorEventPump() { 30 } 31 32 bool DeviceSensorEventPump::RequestStart() { 33 DVLOG(2) << "requested start"; 34 35 if (state_ != STOPPED) 36 return false; 37 38 DCHECK(!timer_.IsRunning()); 39 40 if (SendStartMessage()) { 41 state_ = PENDING_START; 42 return true; 43 } 44 return false; 45 } 46 47 bool DeviceSensorEventPump::Stop() { 48 DVLOG(2) << "stop"; 49 50 if (state_ == STOPPED) 51 return true; 52 53 DCHECK((state_ == PENDING_START && !timer_.IsRunning()) || 54 (state_ == RUNNING && timer_.IsRunning())); 55 56 if (timer_.IsRunning()) 57 timer_.Stop(); 58 SendStopMessage(); 59 state_ = STOPPED; 60 return true; 61 } 62 63 void DeviceSensorEventPump::Attach(RenderThread* thread) { 64 if (!thread) 65 return; 66 thread->AddObserver(this); 67 } 68 69 void DeviceSensorEventPump::OnDidStart(base::SharedMemoryHandle handle) { 70 DVLOG(2) << "did start sensor event pump"; 71 72 if (state_ != PENDING_START) 73 return; 74 75 DCHECK(!timer_.IsRunning()); 76 77 if (InitializeReader(handle)) { 78 timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(GetDelayMillis()), 79 this, &DeviceSensorEventPump::FireEvent); 80 state_ = RUNNING; 81 } 82 } 83 84 } // namespace content 85