Make USB permissions work in the new permission message system
[chromium-blink-merge.git] / content / renderer / device_sensors / device_orientation_event_pump.cc
blob093841b069f92e1d3529899200f333e4e00aad7d
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
3 // found in the LICENSE file.
5 #include "device_orientation_event_pump.h"
7 #include <cmath>
9 #include "content/common/device_sensors/device_orientation_messages.h"
10 #include "content/public/renderer/render_thread.h"
11 #include "third_party/WebKit/public/platform/modules/device_orientation/WebDeviceOrientationListener.h"
13 namespace content {
15 const double DeviceOrientationEventPump::kOrientationThreshold = 0.1;
17 DeviceOrientationEventPump::DeviceOrientationEventPump(RenderThread* thread)
18 : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread) {
21 DeviceOrientationEventPump::~DeviceOrientationEventPump() {
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;
34 void DeviceOrientationEventPump::FireEvent() {
35 DCHECK(listener());
36 blink::WebDeviceOrientationData data;
37 if (reader_->GetLatestData(&data) && ShouldFireEvent(data)) {
38 memcpy(&data_, &data, sizeof(data));
39 listener()->didChangeDeviceOrientation(data);
43 static bool IsSignificantlyDifferent(bool hasAngle1, double angle1,
44 bool hasAngle2, double angle2) {
45 if (hasAngle1 != hasAngle2)
46 return true;
47 return (hasAngle1 && std::fabs(angle1 - angle2) >=
48 DeviceOrientationEventPump::kOrientationThreshold);
51 bool DeviceOrientationEventPump::ShouldFireEvent(
52 const blink::WebDeviceOrientationData& data) const {
53 if (!data.allAvailableSensorsAreActive)
54 return false;
56 if (!data.hasAlpha && !data.hasBeta && !data.hasGamma) {
57 // no data can be provided, this is an all-null event.
58 return true;
61 return IsSignificantlyDifferent(
62 data_.hasAlpha, data_.alpha, data.hasAlpha, data.alpha) ||
63 IsSignificantlyDifferent(
64 data_.hasBeta, data_.beta, data.hasBeta, data.beta) ||
65 IsSignificantlyDifferent(
66 data_.hasGamma, data_.gamma, data.hasGamma, data.gamma);
69 bool DeviceOrientationEventPump::InitializeReader(
70 base::SharedMemoryHandle handle) {
71 memset(&data_, 0, sizeof(data_));
72 if (!reader_)
73 reader_.reset(new DeviceOrientationSharedMemoryReader());
74 return reader_->Initialize(handle);
77 void DeviceOrientationEventPump::SendStartMessage() {
78 RenderThread::Get()->Send(new DeviceOrientationHostMsg_StartPolling());
81 void DeviceOrientationEventPump::SendStopMessage() {
82 RenderThread::Get()->Send(new DeviceOrientationHostMsg_StopPolling());
85 void DeviceOrientationEventPump::SendFakeDataForTesting(void* fake_data) {
86 blink::WebDeviceOrientationData data =
87 *static_cast<blink::WebDeviceOrientationData*>(fake_data);
89 listener()->didChangeDeviceOrientation(data);
92 } // namespace content