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(RenderThread* thread) | 18 DeviceLightEventPump::DeviceLightEventPump() |
19 : DeviceSensorEventPump<blink::WebDeviceLightListener>(thread), | 19 : DeviceSensorEventPump(kDefaultLightPumpDelayMillis), |
| 20 listener_(NULL), |
20 last_seen_data_(-1) { | 21 last_seen_data_(-1) { |
21 pump_delay_millis_ = kDefaultLightPumpDelayMillis; | 22 } |
| 23 |
| 24 DeviceLightEventPump::DeviceLightEventPump(int pump_delay_millis) |
| 25 : DeviceSensorEventPump(pump_delay_millis), |
| 26 listener_(NULL), |
| 27 last_seen_data_(-1) { |
22 } | 28 } |
23 | 29 |
24 DeviceLightEventPump::~DeviceLightEventPump() { | 30 DeviceLightEventPump::~DeviceLightEventPump() { |
25 } | 31 } |
26 | 32 |
| 33 bool DeviceLightEventPump::SetListener( |
| 34 blink::WebDeviceLightListener* listener) { |
| 35 listener_ = listener; |
| 36 return listener_ ? RequestStart() : Stop(); |
| 37 } |
| 38 |
27 bool DeviceLightEventPump::OnControlMessageReceived( | 39 bool DeviceLightEventPump::OnControlMessageReceived( |
28 const IPC::Message& message) { | 40 const IPC::Message& message) { |
29 bool handled = true; | 41 bool handled = true; |
30 IPC_BEGIN_MESSAGE_MAP(DeviceLightEventPump, message) | 42 IPC_BEGIN_MESSAGE_MAP(DeviceLightEventPump, message) |
31 IPC_MESSAGE_HANDLER(DeviceLightMsg_DidStartPolling, OnDidStart) | 43 IPC_MESSAGE_HANDLER(DeviceLightMsg_DidStartPolling, OnDidStart) |
32 IPC_MESSAGE_UNHANDLED(handled = false) | 44 IPC_MESSAGE_UNHANDLED(handled = false) |
33 IPC_END_MESSAGE_MAP() | 45 IPC_END_MESSAGE_MAP() |
34 return handled; | 46 return handled; |
35 } | 47 } |
36 | 48 |
37 void DeviceLightEventPump::FireEvent() { | 49 void DeviceLightEventPump::FireEvent() { |
38 DCHECK(listener()); | 50 DCHECK(listener_); |
39 DeviceLightData data; | 51 DeviceLightData data; |
40 bool did_return_light_data = reader_->GetLatestData(&data); | 52 bool did_return_light_data = reader_->GetLatestData(&data); |
41 if (did_return_light_data && data.value != last_seen_data_) { | 53 if (did_return_light_data && data.value != last_seen_data_) { |
42 last_seen_data_ = data.value; | 54 last_seen_data_ = data.value; |
43 listener()->didChangeDeviceLight(data.value); | 55 listener_->didChangeDeviceLight(data.value); |
44 } | 56 } |
45 } | 57 } |
46 | 58 |
47 bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) { | 59 bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) { |
48 if (!reader_) | 60 if (!reader_) |
49 reader_.reset(new DeviceLightSharedMemoryReader()); | 61 reader_.reset(new DeviceLightSharedMemoryReader()); |
50 return reader_->Initialize(handle); | 62 return reader_->Initialize(handle); |
51 } | 63 } |
52 | 64 |
53 void DeviceLightEventPump::SendStartMessage() { | 65 bool DeviceLightEventPump::SendStartMessage() { |
54 RenderThread::Get()->Send(new DeviceLightHostMsg_StartPolling()); | 66 return RenderThread::Get()->Send(new DeviceLightHostMsg_StartPolling()); |
55 } | 67 } |
56 | 68 |
57 void DeviceLightEventPump::SendStopMessage() { | 69 bool DeviceLightEventPump::SendStopMessage() { |
58 last_seen_data_ = -1; | 70 last_seen_data_ = -1; |
59 RenderThread::Get()->Send(new DeviceLightHostMsg_StopPolling()); | 71 return 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); | |
66 } | 72 } |
67 | 73 |
68 } // namespace content | 74 } // namespace content |
OLD | NEW |