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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <cmath> | 9 #include <cmath> |
10 | 10 |
11 #include "content/public/renderer/render_thread.h" | 11 #include "content/public/renderer/render_thread.h" |
12 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eOrientationListener.h" | 12 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eOrientationListener.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 const double DeviceOrientationEventPumpBase::kOrientationThreshold = 0.1; | 16 const double DeviceOrientationEventPumpBase::kOrientationThreshold = 0.1; |
17 | 17 |
18 DeviceOrientationEventPumpBase::DeviceOrientationEventPumpBase( | 18 DeviceOrientationEventPumpBase::DeviceOrientationEventPumpBase( |
19 RenderThread* thread) | 19 RenderThread* thread) |
20 : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread) {} | 20 : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread) {} |
21 | 21 |
22 DeviceOrientationEventPumpBase::~DeviceOrientationEventPumpBase() {} | 22 DeviceOrientationEventPumpBase::~DeviceOrientationEventPumpBase() {} |
23 | 23 |
24 void DeviceOrientationEventPumpBase::FireEvent() { | 24 void DeviceOrientationEventPumpBase::FireEvent() { |
25 DCHECK(listener()); | 25 DCHECK(listener()); |
26 blink::WebDeviceOrientationData data; | 26 device::OrientationData data; |
27 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { | 27 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { |
28 memcpy(&data_, &data, sizeof(data)); | 28 memcpy(&data_, &data, sizeof(data)); |
29 listener()->didChangeDeviceOrientation(data); | 29 listener()->didChangeDeviceOrientation(data); |
30 } | 30 } |
31 } | 31 } |
32 | 32 |
33 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, | 33 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, |
34 bool hasAngle2, double angle2) { | 34 bool hasAngle2, double angle2) { |
35 if (hasAngle1 != hasAngle2) | 35 if (hasAngle1 != hasAngle2) |
36 return true; | 36 return true; |
37 return (hasAngle1 && | 37 return (hasAngle1 && |
38 std::fabs(angle1 - angle2) >= | 38 std::fabs(angle1 - angle2) >= |
39 DeviceOrientationEventPumpBase::kOrientationThreshold); | 39 DeviceOrientationEventPumpBase::kOrientationThreshold); |
40 } | 40 } |
41 | 41 |
42 bool DeviceOrientationEventPumpBase::ShouldFireEvent( | 42 bool DeviceOrientationEventPumpBase::ShouldFireEvent( |
43 const blink::WebDeviceOrientationData& data) const { | 43 const device::OrientationData& data) const { |
44 if (!data.allAvailableSensorsAreActive) | 44 if (!data.allAvailableSensorsAreActive) |
45 return false; | 45 return false; |
46 | 46 |
47 if (!data.hasAlpha && !data.hasBeta && !data.hasGamma) { | 47 if (!data.hasAlpha && !data.hasBeta && !data.hasGamma) { |
48 // no data can be provided, this is an all-null event. | 48 // no data can be provided, this is an all-null event. |
49 return true; | 49 return true; |
50 } | 50 } |
51 | 51 |
52 return IsSignificantlyDifferent( | 52 return IsSignificantlyDifferent( |
53 data_.hasAlpha, data_.alpha, data.hasAlpha, data.alpha) || | 53 data_.hasAlpha, data_.alpha, data.hasAlpha, data.alpha) || |
54 IsSignificantlyDifferent( | 54 IsSignificantlyDifferent( |
55 data_.hasBeta, data_.beta, data.hasBeta, data.beta) || | 55 data_.hasBeta, data_.beta, data.hasBeta, data.beta) || |
56 IsSignificantlyDifferent( | 56 IsSignificantlyDifferent( |
57 data_.hasGamma, data_.gamma, data.hasGamma, data.gamma); | 57 data_.hasGamma, data_.gamma, data.hasGamma, data.gamma); |
58 } | 58 } |
59 | 59 |
60 bool DeviceOrientationEventPumpBase::InitializeReader( | 60 bool DeviceOrientationEventPumpBase::InitializeReader( |
61 base::SharedMemoryHandle handle) { | 61 base::SharedMemoryHandle handle) { |
62 memset(&data_, 0, sizeof(data_)); | 62 memset(&data_, 0, sizeof(data_)); |
63 if (!reader_) | 63 if (!reader_) |
64 reader_.reset(new DeviceOrientationSharedMemoryReader()); | 64 reader_.reset(new DeviceOrientationSharedMemoryReader()); |
65 return reader_->Initialize(handle); | 65 return reader_->Initialize(handle); |
66 } | 66 } |
67 | 67 |
68 void DeviceOrientationEventPumpBase::SendFakeDataForTesting(void* fake_data) { | 68 void DeviceOrientationEventPumpBase::SendFakeDataForTesting(void* fake_data) { |
69 blink::WebDeviceOrientationData data = | 69 device::OrientationData data = |
70 *static_cast<blink::WebDeviceOrientationData*>(fake_data); | 70 *static_cast<device::OrientationData*>(fake_data); |
71 | 71 |
72 listener()->didChangeDeviceOrientation(data); | 72 listener()->didChangeDeviceOrientation(data); |
73 } | 73 } |
74 | 74 |
75 } // namespace content | 75 } // namespace content |
OLD | NEW |