OLD | NEW |
| (Empty) |
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 "base/time/time.h" | |
8 #include "content/public/renderer/render_thread.h" | |
9 #include "device/sensors/public/cpp/device_sensors_consts.h" | |
10 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" | |
11 | |
12 namespace { | |
13 // Default rate for firing of DeviceLight events. | |
14 const int kDefaultLightPumpDelayMicroseconds = | |
15 base::Time::kMicrosecondsPerSecond / | |
16 device::kDefaultAmbientLightFrequencyHz; | |
17 } // namespace | |
18 | |
19 namespace content { | |
20 | |
21 DeviceLightEventPump::DeviceLightEventPump(RenderThread* thread) | |
22 : DeviceSensorMojoClientMixin< | |
23 DeviceSensorEventPump<blink::WebDeviceLightListener>, | |
24 device::mojom::LightSensor>(thread), | |
25 last_seen_data_(-1) { | |
26 pump_delay_microseconds_ = kDefaultLightPumpDelayMicroseconds; | |
27 } | |
28 | |
29 DeviceLightEventPump::~DeviceLightEventPump() { | |
30 } | |
31 | |
32 void DeviceLightEventPump::FireEvent() { | |
33 DCHECK(listener()); | |
34 device::DeviceLightData data; | |
35 if (reader_->GetLatestData(&data) && ShouldFireEvent(data.value)) { | |
36 last_seen_data_ = data.value; | |
37 listener()->DidChangeDeviceLight(data.value); | |
38 } | |
39 } | |
40 | |
41 bool DeviceLightEventPump::ShouldFireEvent(double lux) const { | |
42 if (lux < 0) | |
43 return false; | |
44 | |
45 if (lux == std::numeric_limits<double>::infinity()) { | |
46 // no sensor data can be provided, fire an Infinity event to Blink. | |
47 return true; | |
48 } | |
49 | |
50 return lux != last_seen_data_; | |
51 } | |
52 | |
53 bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) { | |
54 if (!reader_) | |
55 reader_.reset(new DeviceLightSharedMemoryReader()); | |
56 return reader_->Initialize(handle); | |
57 } | |
58 | |
59 void DeviceLightEventPump::SendFakeDataForTesting(void* fake_data) { | |
60 double data = *static_cast<double*>(fake_data); | |
61 | |
62 listener()->DidChangeDeviceLight(data); | |
63 } | |
64 | |
65 } // namespace content | |
OLD | NEW |