| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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_orientation/device_orientation_messages.h" | 9 #include "content/common/device_orientation/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() | 17 DeviceOrientationEventPump::DeviceOrientationEventPump() |
| 18 : DeviceSensorEventPump(), listener_(0) { | 18 : DeviceSensorEventPump(), listener_(0) { |
| 19 } | 19 } |
| 20 | 20 |
| 21 DeviceOrientationEventPump::DeviceOrientationEventPump(int pump_delay_millis) | 21 DeviceOrientationEventPump::DeviceOrientationEventPump(int pump_delay_millis) |
| 22 : DeviceSensorEventPump(pump_delay_millis), listener_(0) { | 22 : DeviceSensorEventPump(pump_delay_millis), listener_(0) { |
| 23 } | 23 } |
| 24 | 24 |
| 25 DeviceOrientationEventPump::~DeviceOrientationEventPump() { | 25 DeviceOrientationEventPump::~DeviceOrientationEventPump() { |
| 26 } | 26 } |
| 27 | 27 |
| 28 bool DeviceOrientationEventPump::SetListener( | 28 bool DeviceOrientationEventPump::SetListener( |
| 29 WebKit::WebDeviceOrientationListener* listener) { | 29 blink::WebDeviceOrientationListener* listener) { |
| 30 listener_ = listener; | 30 listener_ = listener; |
| 31 return listener_ ? RequestStart() : Stop(); | 31 return listener_ ? RequestStart() : Stop(); |
| 32 } | 32 } |
| 33 | 33 |
| 34 bool DeviceOrientationEventPump::OnControlMessageReceived( | 34 bool DeviceOrientationEventPump::OnControlMessageReceived( |
| 35 const IPC::Message& message) { | 35 const IPC::Message& message) { |
| 36 bool handled = true; | 36 bool handled = true; |
| 37 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationEventPump, message) | 37 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationEventPump, message) |
| 38 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_DidStartPolling, OnDidStart) | 38 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_DidStartPolling, OnDidStart) |
| 39 IPC_MESSAGE_UNHANDLED(handled = false) | 39 IPC_MESSAGE_UNHANDLED(handled = false) |
| 40 IPC_END_MESSAGE_MAP() | 40 IPC_END_MESSAGE_MAP() |
| 41 return handled; | 41 return handled; |
| 42 } | 42 } |
| 43 | 43 |
| 44 void DeviceOrientationEventPump::FireEvent() { | 44 void DeviceOrientationEventPump::FireEvent() { |
| 45 DCHECK(listener_); | 45 DCHECK(listener_); |
| 46 WebKit::WebDeviceOrientationData data; | 46 blink::WebDeviceOrientationData data; |
| 47 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { | 47 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { |
| 48 memcpy(&data_, &data, sizeof(data)); | 48 memcpy(&data_, &data, sizeof(data)); |
| 49 listener_->didChangeDeviceOrientation(data); | 49 listener_->didChangeDeviceOrientation(data); |
| 50 } | 50 } |
| 51 } | 51 } |
| 52 | 52 |
| 53 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, | 53 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, |
| 54 bool hasAngle2, double angle2) { | 54 bool hasAngle2, double angle2) { |
| 55 if (hasAngle1 != hasAngle2) | 55 if (hasAngle1 != hasAngle2) |
| 56 return true; | 56 return true; |
| 57 return (hasAngle1 && std::fabs(angle1 - angle2) >= | 57 return (hasAngle1 && std::fabs(angle1 - angle2) >= |
| 58 DeviceOrientationEventPump::kOrientationThreshold); | 58 DeviceOrientationEventPump::kOrientationThreshold); |
| 59 } | 59 } |
| 60 | 60 |
| 61 bool DeviceOrientationEventPump::ShouldFireEvent( | 61 bool DeviceOrientationEventPump::ShouldFireEvent( |
| 62 const WebKit::WebDeviceOrientationData& data) const { | 62 const blink::WebDeviceOrientationData& data) const { |
| 63 return data.allAvailableSensorsAreActive && | 63 return data.allAvailableSensorsAreActive && |
| 64 (IsSignificantlyDifferent( | 64 (IsSignificantlyDifferent( |
| 65 data_.hasAlpha, data_.alpha, data.hasAlpha, data.alpha) || | 65 data_.hasAlpha, data_.alpha, data.hasAlpha, data.alpha) || |
| 66 IsSignificantlyDifferent( | 66 IsSignificantlyDifferent( |
| 67 data_.hasBeta, data_.beta, data.hasBeta, data.beta) || | 67 data_.hasBeta, data_.beta, data.hasBeta, data.beta) || |
| 68 IsSignificantlyDifferent( | 68 IsSignificantlyDifferent( |
| 69 data_.hasGamma, data_.gamma, data.hasGamma, data.gamma)); | 69 data_.hasGamma, data_.gamma, data.hasGamma, data.gamma)); |
| 70 } | 70 } |
| 71 | 71 |
| 72 bool DeviceOrientationEventPump::InitializeReader( | 72 bool DeviceOrientationEventPump::InitializeReader( |
| 73 base::SharedMemoryHandle handle) { | 73 base::SharedMemoryHandle handle) { |
| 74 memset(&data_, 0, sizeof(data_)); | 74 memset(&data_, 0, sizeof(data_)); |
| 75 if (!reader_) | 75 if (!reader_) |
| 76 reader_.reset(new DeviceOrientationSharedMemoryReader()); | 76 reader_.reset(new DeviceOrientationSharedMemoryReader()); |
| 77 return reader_->Initialize(handle); | 77 return reader_->Initialize(handle); |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool DeviceOrientationEventPump::SendStartMessage() { | 80 bool DeviceOrientationEventPump::SendStartMessage() { |
| 81 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling()); | 81 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling()); |
| 82 } | 82 } |
| 83 | 83 |
| 84 bool DeviceOrientationEventPump::SendStopMessage() { | 84 bool DeviceOrientationEventPump::SendStopMessage() { |
| 85 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling()); | 85 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling()); |
| 86 } | 86 } |
| 87 | 87 |
| 88 } // namespace content | 88 } // namespace content |
| OLD | NEW |