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