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.
5 #include "device_sensor_event_pump.h"
7 #include "base/logging.h"
8 #include "content/public/renderer/render_thread.h"
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;
17 int DeviceSensorEventPump::GetDelayMillis() const {
18 return pump_delay_millis_
;
21 DeviceSensorEventPump::DeviceSensorEventPump()
22 : pump_delay_millis_(kDefaultPumpDelayMillis
),
26 DeviceSensorEventPump::DeviceSensorEventPump(int pump_delay_millis
)
27 : pump_delay_millis_(pump_delay_millis
),
29 DCHECK_GE(pump_delay_millis_
, 0);
32 DeviceSensorEventPump::~DeviceSensorEventPump() {
35 bool DeviceSensorEventPump::RequestStart() {
36 DVLOG(2) << "requested start";
38 if (state_
!= STOPPED
)
41 DCHECK(!timer_
.IsRunning());
43 if (SendStartMessage()) {
44 state_
= PENDING_START
;
50 bool DeviceSensorEventPump::Stop() {
53 if (state_
== STOPPED
)
56 DCHECK((state_
== PENDING_START
&& !timer_
.IsRunning()) ||
57 (state_
== RUNNING
&& timer_
.IsRunning()));
59 if (timer_
.IsRunning())
66 void DeviceSensorEventPump::Attach(RenderThread
* thread
) {
69 thread
->AddObserver(this);
72 void DeviceSensorEventPump::OnDidStart(base::SharedMemoryHandle handle
) {
73 DVLOG(2) << "did start sensor event pump";
75 if (state_
!= PENDING_START
)
78 DCHECK(!timer_
.IsRunning());
80 if (InitializeReader(handle
)) {
81 timer_
.Start(FROM_HERE
, base::TimeDelta::FromMilliseconds(GetDelayMillis()),
82 this, &DeviceSensorEventPump::FireEvent
);
87 } // namespace content