OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "device_orientation_event_pump.h" | |
6 | |
7 #include <cmath> | |
8 | |
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" | |
12 | |
13 namespace content { | |
14 | |
15 const double DeviceOrientationEventPump::kOrientationThreshold = 0.1; | |
16 | |
17 DeviceOrientationEventPump::DeviceOrientationEventPump() | |
18 : DeviceSensorEventPump(), listener_(0) { | |
19 } | |
20 | |
21 DeviceOrientationEventPump::DeviceOrientationEventPump(int pump_delay_millis) | |
22 : DeviceSensorEventPump(pump_delay_millis), listener_(0) { | |
23 } | |
24 | |
25 DeviceOrientationEventPump::~DeviceOrientationEventPump() { | |
26 } | |
27 | |
28 bool DeviceOrientationEventPump::SetListener( | |
29 blink::WebDeviceOrientationListener* listener) { | |
30 listener_ = listener; | |
31 return listener_ ? RequestStart() : Stop(); | |
32 } | |
33 | |
34 bool DeviceOrientationEventPump::OnControlMessageReceived( | |
35 const IPC::Message& message) { | |
36 bool handled = true; | |
37 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationEventPump, message) | |
38 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_DidStartPolling, OnDidStart) | |
39 IPC_MESSAGE_UNHANDLED(handled = false) | |
40 IPC_END_MESSAGE_MAP() | |
41 return handled; | |
42 } | |
43 | |
44 void DeviceOrientationEventPump::FireEvent() { | |
45 DCHECK(listener_); | |
46 blink::WebDeviceOrientationData data; | |
47 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { | |
48 memcpy(&data_, &data, sizeof(data)); | |
49 listener_->didChangeDeviceOrientation(data); | |
50 } | |
51 } | |
52 | |
53 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, | |
54 bool hasAngle2, double angle2) { | |
55 if (hasAngle1 != hasAngle2) | |
56 return true; | |
57 return (hasAngle1 && std::fabs(angle1 - angle2) >= | |
58 DeviceOrientationEventPump::kOrientationThreshold); | |
59 } | |
60 | |
61 bool DeviceOrientationEventPump::ShouldFireEvent( | |
62 const blink::WebDeviceOrientationData& data) const { | |
63 if (!data.allAvailableSensorsAreActive) | |
64 return false; | |
65 | |
66 if (!data.hasAlpha && !data.hasBeta && !data.hasGamma) { | |
67 // no data can be provided, this is an all-null event. | |
68 return true; | |
69 } | |
70 | |
71 return IsSignificantlyDifferent( | |
72 data_.hasAlpha, data_.alpha, data.hasAlpha, data.alpha) || | |
73 IsSignificantlyDifferent( | |
74 data_.hasBeta, data_.beta, data.hasBeta, data.beta) || | |
75 IsSignificantlyDifferent( | |
76 data_.hasGamma, data_.gamma, data.hasGamma, data.gamma); | |
77 } | |
78 | |
79 bool DeviceOrientationEventPump::InitializeReader( | |
80 base::SharedMemoryHandle handle) { | |
81 memset(&data_, 0, sizeof(data_)); | |
82 if (!reader_) | |
83 reader_.reset(new DeviceOrientationSharedMemoryReader()); | |
84 return reader_->Initialize(handle); | |
85 } | |
86 | |
87 bool DeviceOrientationEventPump::SendStartMessage() { | |
88 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling()); | |
89 } | |
90 | |
91 bool DeviceOrientationEventPump::SendStopMessage() { | |
92 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling()); | |
93 } | |
94 | |
95 } // namespace content | |
OLD | NEW |