OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/device_sensors/device_light_event_pump.h" | 5 #include "content/renderer/device_sensors/device_light_event_pump.h" |
6 | 6 |
7 #include "base/time/time.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" | 8 #include "content/public/renderer/render_thread.h" |
10 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" | 9 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" |
11 | 10 |
12 namespace { | 11 namespace { |
13 // Default rate for firing of DeviceLight events. | 12 // Default rate for firing of DeviceLight events. |
14 const int kDefaultLightPumpFrequencyHz = 5; | 13 const int kDefaultLightPumpFrequencyHz = 5; |
15 const int kDefaultLightPumpDelayMicroseconds = | 14 const int kDefaultLightPumpDelayMicroseconds = |
16 base::Time::kMicrosecondsPerSecond / kDefaultLightPumpFrequencyHz; | 15 base::Time::kMicrosecondsPerSecond / kDefaultLightPumpFrequencyHz; |
17 } // namespace | 16 } // namespace |
18 | 17 |
19 namespace content { | 18 namespace content { |
20 | 19 |
21 DeviceLightEventPump::DeviceLightEventPump(RenderThread* thread) | 20 DeviceLightEventPump::DeviceLightEventPump(RenderThread* thread) |
22 : DeviceSensorEventPump<blink::WebDeviceLightListener>(thread), | 21 : DeviceMojoClientMixin< |
| 22 DeviceSensorEventPump<blink::WebDeviceLightListener>, |
| 23 device::mojom::LightSensor>(thread), |
23 last_seen_data_(-1) { | 24 last_seen_data_(-1) { |
24 pump_delay_microseconds_ = kDefaultLightPumpDelayMicroseconds; | 25 pump_delay_microseconds_ = kDefaultLightPumpDelayMicroseconds; |
25 } | 26 } |
26 | 27 |
27 DeviceLightEventPump::~DeviceLightEventPump() { | 28 DeviceLightEventPump::~DeviceLightEventPump() { |
28 } | 29 } |
29 | 30 |
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; | |
38 } | |
39 | |
40 void DeviceLightEventPump::FireEvent() { | 31 void DeviceLightEventPump::FireEvent() { |
41 DCHECK(listener()); | 32 DCHECK(listener()); |
42 DeviceLightData data; | 33 DeviceLightData data; |
43 if (reader_->GetLatestData(&data) && ShouldFireEvent(data.value)) { | 34 if (reader_->GetLatestData(&data) && ShouldFireEvent(data.value)) { |
44 last_seen_data_ = data.value; | 35 last_seen_data_ = data.value; |
45 listener()->didChangeDeviceLight(data.value); | 36 listener()->didChangeDeviceLight(data.value); |
46 } | 37 } |
47 } | 38 } |
48 | 39 |
49 bool DeviceLightEventPump::ShouldFireEvent(double lux) const { | 40 bool DeviceLightEventPump::ShouldFireEvent(double lux) const { |
50 if (lux < 0) | 41 if (lux < 0) |
51 return false; | 42 return false; |
52 | 43 |
53 if (lux == std::numeric_limits<double>::infinity()) { | 44 if (lux == std::numeric_limits<double>::infinity()) { |
54 // no sensor data can be provided, fire an Infinity event to Blink. | 45 // no sensor data can be provided, fire an Infinity event to Blink. |
55 return true; | 46 return true; |
56 } | 47 } |
57 | 48 |
58 return lux != last_seen_data_; | 49 return lux != last_seen_data_; |
59 } | 50 } |
60 | 51 |
61 bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) { | 52 bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) { |
62 if (!reader_) | 53 if (!reader_) |
63 reader_.reset(new DeviceLightSharedMemoryReader()); | 54 reader_.reset(new DeviceLightSharedMemoryReader()); |
64 return reader_->Initialize(handle); | 55 return reader_->Initialize(handle); |
65 } | 56 } |
66 | 57 |
67 void DeviceLightEventPump::SendStartMessage() { | |
68 RenderThread::Get()->Send(new DeviceLightHostMsg_StartPolling()); | |
69 } | |
70 | |
71 void DeviceLightEventPump::SendStopMessage() { | |
72 last_seen_data_ = -1; | |
73 RenderThread::Get()->Send(new DeviceLightHostMsg_StopPolling()); | |
74 } | |
75 | |
76 void DeviceLightEventPump::SendFakeDataForTesting(void* fake_data) { | 58 void DeviceLightEventPump::SendFakeDataForTesting(void* fake_data) { |
77 double data = *static_cast<double*>(fake_data); | 59 double data = *static_cast<double*>(fake_data); |
78 | 60 |
79 listener()->didChangeDeviceLight(data); | 61 listener()->didChangeDeviceLight(data); |
80 } | 62 } |
81 | 63 |
82 } // namespace content | 64 } // namespace content |
OLD | NEW |