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