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 "content/renderer/device_sensors/device_light_event_pump.h"
7 #include "base/time/time.h"
8 #include "content/common/device_sensors/device_light_messages.h"
9 #include "content/public/renderer/render_thread.h"
10 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h"
13 // Default rate for firing of DeviceLight events.
14 const int kDefaultLightPumpFrequencyHz
= 5;
15 const int kDefaultLightPumpDelayMicroseconds
=
16 base::Time::kMicrosecondsPerSecond
/ kDefaultLightPumpFrequencyHz
;
21 DeviceLightEventPump::DeviceLightEventPump(RenderThread
* thread
)
22 : DeviceSensorEventPump
<blink::WebDeviceLightListener
>(thread
),
24 pump_delay_microseconds_
= kDefaultLightPumpDelayMicroseconds
;
27 DeviceLightEventPump::~DeviceLightEventPump() {
30 bool DeviceLightEventPump::OnControlMessageReceived(
31 const IPC::Message
& message
) {
33 IPC_BEGIN_MESSAGE_MAP(DeviceLightEventPump
, message
)
34 IPC_MESSAGE_HANDLER(DeviceLightMsg_DidStartPolling
, OnDidStart
)
35 IPC_MESSAGE_UNHANDLED(handled
= false)
40 void DeviceLightEventPump::FireEvent() {
43 if (reader_
->GetLatestData(&data
) && ShouldFireEvent(data
.value
)) {
44 last_seen_data_
= data
.value
;
45 listener()->didChangeDeviceLight(data
.value
);
49 bool DeviceLightEventPump::ShouldFireEvent(double lux
) const {
53 if (lux
== std::numeric_limits
<double>::infinity()) {
54 // no sensor data can be provided, fire an Infinity event to Blink.
58 return lux
!= last_seen_data_
;
61 bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle
) {
63 reader_
.reset(new DeviceLightSharedMemoryReader());
64 return reader_
->Initialize(handle
);
67 void DeviceLightEventPump::SendStartMessage() {
68 RenderThread::Get()->Send(new DeviceLightHostMsg_StartPolling());
71 void DeviceLightEventPump::SendStopMessage() {
73 RenderThread::Get()->Send(new DeviceLightHostMsg_StopPolling());
76 void DeviceLightEventPump::SendFakeDataForTesting(void* fake_data
) {
77 double data
= *static_cast<double*>(fake_data
);
79 listener()->didChangeDeviceLight(data
);
82 } // namespace content