1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "device_orientation_event_pump.h"
9 #include "content/common/device_orientation/device_orientation_messages.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h"
15 const double DeviceOrientationEventPump::kOrientationThreshold
= 0.1;
17 DeviceOrientationEventPump::DeviceOrientationEventPump()
18 : DeviceSensorEventPump(), listener_(0) {
21 DeviceOrientationEventPump::DeviceOrientationEventPump(int pump_delay_millis
)
22 : DeviceSensorEventPump(pump_delay_millis
), listener_(0) {
25 DeviceOrientationEventPump::~DeviceOrientationEventPump() {
28 bool DeviceOrientationEventPump::SetListener(
29 WebKit::WebDeviceOrientationListener
* listener
) {
31 return listener_
? RequestStart() : Stop();
34 bool DeviceOrientationEventPump::OnControlMessageReceived(
35 const IPC::Message
& message
) {
37 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationEventPump
, message
)
38 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_DidStartPolling
, OnDidStart
)
39 IPC_MESSAGE_UNHANDLED(handled
= false)
44 void DeviceOrientationEventPump::FireEvent() {
46 WebKit::WebDeviceOrientationData data
;
47 if (reader_
->GetLatestData(&data
) && ShouldFireEvent(data
)) {
48 memcpy(&data_
, &data
, sizeof(data
));
49 listener_
->didChangeDeviceOrientation(data
);
53 static bool IsSignificantlyDifferent(bool hasAngle1
, double angle1
,
54 bool hasAngle2
, double angle2
) {
55 if (hasAngle1
!= hasAngle2
)
57 return (hasAngle1
&& std::fabs(angle1
- angle2
) >=
58 DeviceOrientationEventPump::kOrientationThreshold
);
61 bool DeviceOrientationEventPump::ShouldFireEvent(
62 const WebKit::WebDeviceOrientationData
& data
) const {
63 return data
.allAvailableSensorsAreActive
&&
64 (IsSignificantlyDifferent(
65 data_
.hasAlpha
, data_
.alpha
, data
.hasAlpha
, data
.alpha
) ||
66 IsSignificantlyDifferent(
67 data_
.hasBeta
, data_
.beta
, data
.hasBeta
, data
.beta
) ||
68 IsSignificantlyDifferent(
69 data_
.hasGamma
, data_
.gamma
, data
.hasGamma
, data
.gamma
));
72 bool DeviceOrientationEventPump::InitializeReader(
73 base::SharedMemoryHandle handle
) {
74 memset(&data_
, 0, sizeof(data_
));
76 reader_
.reset(new DeviceOrientationSharedMemoryReader());
77 return reader_
->Initialize(handle
);
80 bool DeviceOrientationEventPump::SendStartMessage() {
81 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling());
84 bool DeviceOrientationEventPump::SendStopMessage() {
85 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling());
88 } // namespace content