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