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 "device_orientation_event_pump.h" | 5 #include "device_orientation_event_pump.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "content/common/device_sensors/device_orientation_messages.h" | 9 #include "content/common/device_sensors/device_orientation_messages.h" |
10 #include "content/public/renderer/render_thread.h" | 10 #include "content/public/renderer/render_thread.h" |
11 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h" | 11 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
15 const double DeviceOrientationEventPump::kOrientationThreshold = 0.1; | 15 const double DeviceOrientationEventPump::kOrientationThreshold = 0.1; |
16 | 16 |
17 DeviceOrientationEventPump::DeviceOrientationEventPump() | 17 DeviceOrientationEventPump::DeviceOrientationEventPump(RenderThread* thread) |
18 : DeviceSensorEventPump(), listener_(0) { | 18 : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread) { |
19 } | |
20 | |
21 DeviceOrientationEventPump::DeviceOrientationEventPump(int pump_delay_millis) | |
22 : DeviceSensorEventPump(pump_delay_millis), listener_(0) { | |
23 } | 19 } |
24 | 20 |
25 DeviceOrientationEventPump::~DeviceOrientationEventPump() { | 21 DeviceOrientationEventPump::~DeviceOrientationEventPump() { |
26 } | 22 } |
27 | 23 |
28 bool DeviceOrientationEventPump::SetListener( | |
29 blink::WebDeviceOrientationListener* listener) { | |
30 listener_ = listener; | |
31 return listener_ ? RequestStart() : Stop(); | |
32 } | |
33 | |
34 bool DeviceOrientationEventPump::OnControlMessageReceived( | 24 bool DeviceOrientationEventPump::OnControlMessageReceived( |
35 const IPC::Message& message) { | 25 const IPC::Message& message) { |
36 bool handled = true; | 26 bool handled = true; |
37 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationEventPump, message) | 27 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationEventPump, message) |
38 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_DidStartPolling, OnDidStart) | 28 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_DidStartPolling, OnDidStart) |
39 IPC_MESSAGE_UNHANDLED(handled = false) | 29 IPC_MESSAGE_UNHANDLED(handled = false) |
40 IPC_END_MESSAGE_MAP() | 30 IPC_END_MESSAGE_MAP() |
41 return handled; | 31 return handled; |
42 } | 32 } |
43 | 33 |
44 void DeviceOrientationEventPump::FireEvent() { | 34 void DeviceOrientationEventPump::FireEvent() { |
45 DCHECK(listener_); | 35 DCHECK(listener()); |
46 blink::WebDeviceOrientationData data; | 36 blink::WebDeviceOrientationData data; |
47 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { | 37 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { |
48 memcpy(&data_, &data, sizeof(data)); | 38 memcpy(&data_, &data, sizeof(data)); |
49 listener_->didChangeDeviceOrientation(data); | 39 listener()->didChangeDeviceOrientation(data); |
50 } | 40 } |
51 } | 41 } |
52 | 42 |
53 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, | 43 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, |
54 bool hasAngle2, double angle2) { | 44 bool hasAngle2, double angle2) { |
55 if (hasAngle1 != hasAngle2) | 45 if (hasAngle1 != hasAngle2) |
56 return true; | 46 return true; |
57 return (hasAngle1 && std::fabs(angle1 - angle2) >= | 47 return (hasAngle1 && std::fabs(angle1 - angle2) >= |
58 DeviceOrientationEventPump::kOrientationThreshold); | 48 DeviceOrientationEventPump::kOrientationThreshold); |
59 } | 49 } |
(...skipping 17 matching lines...) Expand all Loading... |
77 } | 67 } |
78 | 68 |
79 bool DeviceOrientationEventPump::InitializeReader( | 69 bool DeviceOrientationEventPump::InitializeReader( |
80 base::SharedMemoryHandle handle) { | 70 base::SharedMemoryHandle handle) { |
81 memset(&data_, 0, sizeof(data_)); | 71 memset(&data_, 0, sizeof(data_)); |
82 if (!reader_) | 72 if (!reader_) |
83 reader_.reset(new DeviceOrientationSharedMemoryReader()); | 73 reader_.reset(new DeviceOrientationSharedMemoryReader()); |
84 return reader_->Initialize(handle); | 74 return reader_->Initialize(handle); |
85 } | 75 } |
86 | 76 |
87 bool DeviceOrientationEventPump::SendStartMessage() { | 77 void DeviceOrientationEventPump::SendStartMessage() { |
88 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling()); | 78 RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling()); |
89 } | 79 } |
90 | 80 |
91 bool DeviceOrientationEventPump::SendStopMessage() { | 81 void DeviceOrientationEventPump::SendStopMessage() { |
92 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling()); | 82 RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling()); |
| 83 } |
| 84 |
| 85 void DeviceOrientationEventPump::SendFakeDataForTesting(void* fake_data) { |
| 86 blink::WebDeviceOrientationData data = |
| 87 *static_cast<blink::WebDeviceOrientationData*>(fake_data); |
| 88 |
| 89 listener()->didChangeDeviceOrientation(data); |
93 } | 90 } |
94 | 91 |
95 } // namespace content | 92 } // namespace content |
OLD | NEW |