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. 4 5 #include "content/renderer/device_sensors/device_light_event_pump.h" 6 7 #include "content/common/device_sensors/device_light_messages.h" 8 #include "content/public/renderer/render_thread.h" 9 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" 10 11 namespace { 12 // Default delay between subsequent firing of DeviceLight events. 13 const int kDefaultLightPumpDelayMillis = 200; 14 } // namespace 15 16 namespace content { 17 DeviceLightEventPump(RenderThread * thread)18DeviceLightEventPump::DeviceLightEventPump(RenderThread* thread) 19 : DeviceSensorEventPump<blink::WebDeviceLightListener>(thread), 20 last_seen_data_(-1) { 21 pump_delay_millis_ = kDefaultLightPumpDelayMillis; 22 } 23 ~DeviceLightEventPump()24DeviceLightEventPump::~DeviceLightEventPump() { 25 } 26 OnControlMessageReceived(const IPC::Message & message)27bool DeviceLightEventPump::OnControlMessageReceived( 28 const IPC::Message& message) { 29 bool handled = true; 30 IPC_BEGIN_MESSAGE_MAP(DeviceLightEventPump, message) 31 IPC_MESSAGE_HANDLER(DeviceLightMsg_DidStartPolling, OnDidStart) 32 IPC_MESSAGE_UNHANDLED(handled = false) 33 IPC_END_MESSAGE_MAP() 34 return handled; 35 } 36 FireEvent()37void DeviceLightEventPump::FireEvent() { 38 DCHECK(listener()); 39 DeviceLightData data; 40 if (reader_->GetLatestData(&data) && ShouldFireEvent(data.value)) { 41 last_seen_data_ = data.value; 42 listener()->didChangeDeviceLight(data.value); 43 } 44 } 45 ShouldFireEvent(double lux) const46bool DeviceLightEventPump::ShouldFireEvent(double lux) const { 47 if (lux < 0) 48 return false; 49 50 if (lux == std::numeric_limits<double>::infinity()) { 51 // no sensor data can be provided, fire an Infinity event to Blink. 52 return true; 53 } 54 55 return lux != last_seen_data_; 56 } 57 InitializeReader(base::SharedMemoryHandle handle)58bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) { 59 if (!reader_) 60 reader_.reset(new DeviceLightSharedMemoryReader()); 61 return reader_->Initialize(handle); 62 } 63 SendStartMessage()64void DeviceLightEventPump::SendStartMessage() { 65 RenderThread::Get()->Send(new DeviceLightHostMsg_StartPolling()); 66 } 67 SendStopMessage()68void DeviceLightEventPump::SendStopMessage() { 69 last_seen_data_ = -1; 70 RenderThread::Get()->Send(new DeviceLightHostMsg_StopPolling()); 71 } 72 SendFakeDataForTesting(void * fake_data)73void DeviceLightEventPump::SendFakeDataForTesting(void* fake_data) { 74 double data = *static_cast<double*>(fake_data); 75 76 listener()->didChangeDeviceLight(data); 77 } 78 79 } // namespace content 80