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