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/process/process_handle.h" |
7 #include "base/time/time.h" | 8 #include "base/time/time.h" |
8 #include "content/common/device_sensors/device_light_messages.h" | 9 #include "content/public/common/service_registry.h" |
9 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
10 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" | 11 #include "third_party/WebKit/public/platform/WebDeviceLightListener.h" |
11 | 12 |
12 namespace { | 13 namespace { |
13 // Default rate for firing of DeviceLight events. | 14 // Default rate for firing of DeviceLight events. |
14 const int kDefaultLightPumpFrequencyHz = 5; | 15 const int kDefaultLightPumpFrequencyHz = 5; |
15 const int kDefaultLightPumpDelayMicroseconds = | 16 const int kDefaultLightPumpDelayMicroseconds = |
16 base::Time::kMicrosecondsPerSecond / kDefaultLightPumpFrequencyHz; | 17 base::Time::kMicrosecondsPerSecond / kDefaultLightPumpFrequencyHz; |
17 } // namespace | 18 } // namespace |
18 | 19 |
19 namespace content { | 20 namespace content { |
20 | 21 |
21 DeviceLightEventPump::DeviceLightEventPump(RenderThread* thread) | 22 DeviceLightEventPump::DeviceLightEventPump( |
22 : DeviceSensorEventPump<blink::WebDeviceLightListener>(thread), | 23 blink::WebDeviceLightListener* listener) |
23 last_seen_data_(-1) { | 24 : last_seen_data_(-1), listener_(listener) { |
24 pump_delay_microseconds_ = kDefaultLightPumpDelayMicroseconds; | 25 pump_delay_microseconds_ = kDefaultLightPumpDelayMicroseconds; |
| 26 LOG(WARNING) << "DeviceLightEventPump::DeviceLightEventPump"; |
| 27 if (ServiceRegistry* registry = RenderThread::Get()->GetServiceRegistry()) { |
| 28 // registry can be null during testing. |
| 29 registry->ConnectToRemoteService(&service_); |
| 30 service_->StartPolling( |
| 31 static_cast<int32>(base::GetCurrentProcId()), |
| 32 base::Bind(&DeviceLightEventPump::OnDidStart, base::Unretained(this))); |
| 33 } |
25 } | 34 } |
26 | 35 |
27 DeviceLightEventPump::~DeviceLightEventPump() { | 36 DeviceLightEventPump::~DeviceLightEventPump() { |
28 } | 37 } |
29 | 38 |
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() { | 39 void DeviceLightEventPump::FireEvent() { |
41 DCHECK(listener()); | 40 LOG(WARNING) << "DeviceLightEventPump::FireEvent"; |
42 DeviceLightData data; | 41 DCHECK(listener_); |
| 42 device::DeviceLightData data; |
43 if (reader_->GetLatestData(&data) && ShouldFireEvent(data.value)) { | 43 if (reader_->GetLatestData(&data) && ShouldFireEvent(data.value)) { |
44 last_seen_data_ = data.value; | 44 last_seen_data_ = data.value; |
45 listener()->didChangeDeviceLight(data.value); | 45 listener_->didChangeDeviceLight(data.value); |
46 } | 46 } |
47 } | 47 } |
48 | 48 |
49 bool DeviceLightEventPump::ShouldFireEvent(double lux) const { | 49 bool DeviceLightEventPump::ShouldFireEvent(double lux) const { |
50 if (lux < 0) | 50 if (lux < 0) |
51 return false; | 51 return false; |
52 | 52 |
53 if (lux == std::numeric_limits<double>::infinity()) { | 53 if (lux == std::numeric_limits<double>::infinity()) { |
54 // no sensor data can be provided, fire an Infinity event to Blink. | 54 // no sensor data can be provided, fire an Infinity event to Blink. |
55 return true; | 55 return true; |
56 } | 56 } |
57 | 57 |
58 return lux != last_seen_data_; | 58 return lux != last_seen_data_; |
59 } | 59 } |
60 | 60 |
61 bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) { | 61 bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) { |
62 if (!reader_) | 62 if (!reader_) |
63 reader_.reset(new DeviceLightSharedMemoryReader()); | 63 reader_.reset(new DeviceLightSharedMemoryReader()); |
64 return reader_->Initialize(handle); | 64 return reader_->Initialize(handle); |
65 } | 65 } |
66 | 66 |
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) { | 67 void DeviceLightEventPump::SendFakeDataForTesting(void* fake_data) { |
77 double data = *static_cast<double*>(fake_data); | 68 double data = *static_cast<double*>(fake_data); |
78 | 69 |
79 listener()->didChangeDeviceLight(data); | 70 listener_->didChangeDeviceLight(data); |
80 } | 71 } |
81 | 72 |
82 } // namespace content | 73 } // namespace content |
OLD | NEW |