| 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 <cmath> | 7 #include <cmath> |
| 8 | 8 |
| 9 #include "content/common/device_sensors/device_orientation_messages.h" | 9 #include "base/logging.h" |
| 10 #include "base/process/process_handle.h" |
| 11 #include "content/public/common/service_registry.h" |
| 10 #include "content/public/renderer/render_thread.h" | 12 #include "content/public/renderer/render_thread.h" |
| 11 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eOrientationListener.h" | 13 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDevic
eOrientationListener.h" |
| 12 | 14 |
| 13 namespace content { | 15 namespace content { |
| 14 | 16 |
| 15 const double DeviceOrientationEventPump::kOrientationThreshold = 0.1; | 17 const double DeviceOrientationEventPump::kOrientationThreshold = 0.1; |
| 16 | 18 |
| 17 DeviceOrientationEventPump::DeviceOrientationEventPump(RenderThread* thread) | 19 DeviceOrientationEventPump::DeviceOrientationEventPump( |
| 18 : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread) { | 20 blink::WebDeviceOrientationListener* listener) |
| 21 : listener_(listener) { |
| 22 DCHECK(listener_); |
| 23 LOG(WARNING) << "DeviceOrientationEventPump::DeviceOrientationEventPump"; |
| 24 |
| 25 if (ServiceRegistry* registry = RenderThread::Get()->GetServiceRegistry()) { |
| 26 // registry can be null during testing. |
| 27 registry->ConnectToRemoteService(&service_); |
| 28 service_->StartPolling(static_cast<int32>(base::GetCurrentProcId()), |
| 29 base::Bind(&DeviceOrientationEventPump::OnDidStart, |
| 30 base::Unretained(this))); |
| 31 } |
| 19 } | 32 } |
| 20 | 33 |
| 21 DeviceOrientationEventPump::~DeviceOrientationEventPump() { | 34 DeviceOrientationEventPump::~DeviceOrientationEventPump() { |
| 22 } | 35 } |
| 23 | 36 |
| 24 bool DeviceOrientationEventPump::OnControlMessageReceived( | |
| 25 const IPC::Message& message) { | |
| 26 bool handled = true; | |
| 27 IPC_BEGIN_MESSAGE_MAP(DeviceOrientationEventPump, message) | |
| 28 IPC_MESSAGE_HANDLER(DeviceOrientationMsg_DidStartPolling, OnDidStart) | |
| 29 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 30 IPC_END_MESSAGE_MAP() | |
| 31 return handled; | |
| 32 } | |
| 33 | |
| 34 void DeviceOrientationEventPump::FireEvent() { | 37 void DeviceOrientationEventPump::FireEvent() { |
| 35 DCHECK(listener()); | 38 DCHECK(listener_); |
| 39 LOG(WARNING) << "DeviceOrientationEventPump::FireEvent"; |
| 36 blink::WebDeviceOrientationData data; | 40 blink::WebDeviceOrientationData data; |
| 37 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { | 41 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) { |
| 38 memcpy(&data_, &data, sizeof(data)); | 42 memcpy(&data_, &data, sizeof(data)); |
| 39 listener()->didChangeDeviceOrientation(data); | 43 listener_->didChangeDeviceOrientation(data); |
| 40 } | 44 } |
| 41 } | 45 } |
| 42 | 46 |
| 43 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, | 47 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1, |
| 44 bool hasAngle2, double angle2) { | 48 bool hasAngle2, double angle2) { |
| 45 if (hasAngle1 != hasAngle2) | 49 if (hasAngle1 != hasAngle2) |
| 46 return true; | 50 return true; |
| 47 return (hasAngle1 && std::fabs(angle1 - angle2) >= | 51 return (hasAngle1 && std::fabs(angle1 - angle2) >= |
| 48 DeviceOrientationEventPump::kOrientationThreshold); | 52 DeviceOrientationEventPump::kOrientationThreshold); |
| 49 } | 53 } |
| (...skipping 17 matching lines...) Expand all Loading... |
| 67 } | 71 } |
| 68 | 72 |
| 69 bool DeviceOrientationEventPump::InitializeReader( | 73 bool DeviceOrientationEventPump::InitializeReader( |
| 70 base::SharedMemoryHandle handle) { | 74 base::SharedMemoryHandle handle) { |
| 71 memset(&data_, 0, sizeof(data_)); | 75 memset(&data_, 0, sizeof(data_)); |
| 72 if (!reader_) | 76 if (!reader_) |
| 73 reader_.reset(new DeviceOrientationSharedMemoryReader()); | 77 reader_.reset(new DeviceOrientationSharedMemoryReader()); |
| 74 return reader_->Initialize(handle); | 78 return reader_->Initialize(handle); |
| 75 } | 79 } |
| 76 | 80 |
| 77 void DeviceOrientationEventPump::SendStartMessage() { | |
| 78 RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling()); | |
| 79 } | |
| 80 | |
| 81 void DeviceOrientationEventPump::SendStopMessage() { | |
| 82 RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling()); | |
| 83 } | |
| 84 | |
| 85 void DeviceOrientationEventPump::SendFakeDataForTesting(void* fake_data) { | 81 void DeviceOrientationEventPump::SendFakeDataForTesting(void* fake_data) { |
| 86 blink::WebDeviceOrientationData data = | 82 blink::WebDeviceOrientationData data = |
| 87 *static_cast<blink::WebDeviceOrientationData*>(fake_data); | 83 *static_cast<blink::WebDeviceOrientationData*>(fake_data); |
| 88 | 84 |
| 89 listener()->didChangeDeviceOrientation(data); | 85 listener_->didChangeDeviceOrientation(data); |
| 90 } | 86 } |
| 91 | 87 |
| 92 } // namespace content | 88 } // namespace content |
| OLD | NEW |