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 WebKit::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 WebKit::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 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)); |
| 70 } |
| 71 |
| 72 bool DeviceOrientationEventPump::InitializeReader( |
| 73 base::SharedMemoryHandle handle) { |
| 74 memset(&data_, 0, sizeof(data_)); |
| 75 if (!reader_) |
| 76 reader_.reset(new DeviceOrientationSharedMemoryReader()); |
| 77 return reader_->Initialize(handle); |
| 78 } |
| 79 |
| 80 bool DeviceOrientationEventPump::SendStartMessage() { |
| 81 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling()); |
| 82 } |
| 83 |
| 84 bool DeviceOrientationEventPump::SendStopMessage() { |
| 85 return RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling()); |
| 86 } |
| 87 |
| 88 } // namespace content |
OLD | NEW |