Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / device_sensors / device_light_event_pump.cc
blob53567441df879f54439b7b8d5527e6b12f2df0f8
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"
12 namespace {
13 // Default rate for firing of DeviceLight events.
14 const int kDefaultLightPumpFrequencyHz = 5;
15 const int kDefaultLightPumpDelayMicroseconds =
16 base::Time::kMicrosecondsPerSecond / kDefaultLightPumpFrequencyHz;
17 } // namespace
19 namespace content {
21 DeviceLightEventPump::DeviceLightEventPump(RenderThread* thread)
22 : DeviceSensorEventPump<blink::WebDeviceLightListener>(thread),
23 last_seen_data_(-1) {
24 pump_delay_microseconds_ = kDefaultLightPumpDelayMicroseconds;
27 DeviceLightEventPump::~DeviceLightEventPump() {
30 bool DeviceLightEventPump::OnControlMessageReceived(
31 const IPC::Message& message) {
32 bool handled = true;
33 IPC_BEGIN_MESSAGE_MAP(DeviceLightEventPump, message)
34 IPC_MESSAGE_HANDLER(DeviceLightMsg_DidStartPolling, OnDidStart)
35 IPC_MESSAGE_UNHANDLED(handled = false)
36 IPC_END_MESSAGE_MAP()
37 return handled;
40 void DeviceLightEventPump::FireEvent() {
41 DCHECK(listener());
42 DeviceLightData data;
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 {
50 if (lux < 0)
51 return false;
53 if (lux == std::numeric_limits<double>::infinity()) {
54 // no sensor data can be provided, fire an Infinity event to Blink.
55 return true;
58 return lux != last_seen_data_;
61 bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) {
62 if (!reader_)
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() {
72 last_seen_data_ = -1;
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