summaryrefslogtreecommitdiff
path: root/chromium/content/renderer/device_sensors/device_orientation_event_pump.cc
blob: f2c7d89f96bedbbcb534888899f2f5c946b14a16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/renderer/device_sensors/device_orientation_event_pump.h"

#include <cmath>

#include "base/logging.h"
#include "content/public/renderer/render_frame.h"
#include "content/public/renderer/render_thread.h"
#include "content/renderer/render_thread_impl.h"
#include "services/device/public/mojom/sensor.mojom.h"
#include "services/service_manager/public/cpp/interface_provider.h"

namespace {

bool IsAngleDifferentThreshold(bool has_angle1,
                               double angle1,
                               bool has_angle2,
                               double angle2) {
  if (has_angle1 != has_angle2)
    return true;

  return (has_angle1 &&
          std::fabs(angle1 - angle2) >=
              content::DeviceOrientationEventPump::kOrientationThreshold);
}

bool IsSignificantlyDifferent(const device::OrientationData& data1,
                              const device::OrientationData& data2) {
  return IsAngleDifferentThreshold(data1.has_alpha, data1.alpha,
                                   data2.has_alpha, data2.alpha) ||
         IsAngleDifferentThreshold(data1.has_beta, data1.beta, data2.has_beta,
                                   data2.beta) ||
         IsAngleDifferentThreshold(data1.has_gamma, data1.gamma,
                                   data2.has_gamma, data2.gamma);
}

}  // namespace

namespace content {

template class DeviceSensorEventPump<blink::WebDeviceOrientationListener>;

const double DeviceOrientationEventPump::kOrientationThreshold = 0.1;

DeviceOrientationEventPump::DeviceOrientationEventPump(RenderThread* thread,
                                                       bool absolute)
    : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(thread),
      relative_orientation_sensor_(
          this,
          device::mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES),
      absolute_orientation_sensor_(
          this,
          device::mojom::SensorType::ABSOLUTE_ORIENTATION_EULER_ANGLES),
      absolute_(absolute),
      fall_back_to_absolute_orientation_sensor_(!absolute) {}

DeviceOrientationEventPump::~DeviceOrientationEventPump() {}

void DeviceOrientationEventPump::SendStartMessage() {
  // When running layout tests, those observers should not listen to the
  // actual hardware changes. In order to make that happen, don't connect
  // the other end of the mojo pipe to anything.
  //
  // TODO(sammc): Remove this when JS layout test support for shared buffers
  // is ready and the layout tests are converted to use that for mocking.
  // https://crbug.com/774183
  if (!RenderThreadImpl::current() ||
      RenderThreadImpl::current()->layout_test_mode()) {
    return;
  }

  SendStartMessageImpl();
}

void DeviceOrientationEventPump::SendStopMessage() {
  // SendStopMessage() gets called both when the page visibility changes and if
  // all device orientation event listeners are unregistered. Since removing
  // the event listener is more rare than the page visibility changing,
  // Sensor::Suspend() is used to optimize this case for not doing extra work.

  relative_orientation_sensor_.Stop();
  // This is needed in case we fallback to using the absolute orientation
  // sensor. In this case, the relative orientation sensor is marked as
  // SensorState::SHOULD_SUSPEND, and if the relative orientation sensor
  // is not available, the absolute orientation sensor should also be marked as
  // SensorState::SHOULD_SUSPEND, but only after the
  // absolute_orientation_sensor_.Start() is called for initializing
  // the absolute orientation sensor in
  // DeviceOrientationEventPump::DidStartIfPossible().
  if (relative_orientation_sensor_.sensor_state ==
          SensorState::SHOULD_SUSPEND &&
      fall_back_to_absolute_orientation_sensor_) {
    should_suspend_absolute_orientation_sensor_ = true;
  }

  absolute_orientation_sensor_.Stop();
}

void DeviceOrientationEventPump::SendFakeDataForTesting(void* fake_data) {
  if (!listener())
    return;

  device::OrientationData data =
      *static_cast<device::OrientationData*>(fake_data);
  listener()->DidChangeDeviceOrientation(data);
}

void DeviceOrientationEventPump::FireEvent() {
  device::OrientationData data;

  DCHECK(listener());

  GetDataFromSharedMemory(&data);

  if (ShouldFireEvent(data)) {
    data_ = data;
    listener()->DidChangeDeviceOrientation(data);
  }
}

void DeviceOrientationEventPump::DidStartIfPossible() {
  if (!absolute_ && !relative_orientation_sensor_.sensor &&
      fall_back_to_absolute_orientation_sensor_ && sensor_provider_) {
    // When relative orientation sensor is not available fall back to using
    // the absolute orientation sensor but only on the first failure.
    fall_back_to_absolute_orientation_sensor_ = false;
    absolute_orientation_sensor_.Start(sensor_provider_.get());
    if (should_suspend_absolute_orientation_sensor_) {
      // The absolute orientation sensor needs to be marked as
      // SensorState::SUSPENDED when it is successfully initialized.
      absolute_orientation_sensor_.sensor_state = SensorState::SHOULD_SUSPEND;
      should_suspend_absolute_orientation_sensor_ = false;
    }
    return;
  }
  DeviceSensorEventPump::DidStartIfPossible();
}

void DeviceOrientationEventPump::SendStartMessageImpl() {
  if (!sensor_provider_) {
    RenderFrame* const render_frame = GetRenderFrame();
    if (!render_frame)
      return;

    render_frame->GetRemoteInterfaces()->GetInterface(
        mojo::MakeRequest(&sensor_provider_));
    sensor_provider_.set_connection_error_handler(
        base::BindOnce(&DeviceSensorEventPump::HandleSensorProviderError,
                       base::Unretained(this)));
  }

  if (absolute_) {
    absolute_orientation_sensor_.Start(sensor_provider_.get());
  } else {
    fall_back_to_absolute_orientation_sensor_ = true;
    should_suspend_absolute_orientation_sensor_ = false;
    relative_orientation_sensor_.Start(sensor_provider_.get());
  }
}

bool DeviceOrientationEventPump::SensorsReadyOrErrored() const {
  if (!relative_orientation_sensor_.ReadyOrErrored() ||
      !absolute_orientation_sensor_.ReadyOrErrored()) {
    return false;
  }

  // At most one sensor can be successfully initialized.
  DCHECK(!relative_orientation_sensor_.sensor ||
         !absolute_orientation_sensor_.sensor);

  return true;
}

void DeviceOrientationEventPump::GetDataFromSharedMemory(
    device::OrientationData* data) {
  data->all_available_sensors_are_active = true;

  if (!absolute_ && relative_orientation_sensor_.SensorReadingCouldBeRead()) {
    // For DeviceOrientation Event, this provides relative orientation data.
    data->all_available_sensors_are_active =
        relative_orientation_sensor_.reading.timestamp() != 0.0;
    if (!data->all_available_sensors_are_active)
      return;
    data->alpha = relative_orientation_sensor_.reading.orientation_euler.z;
    data->beta = relative_orientation_sensor_.reading.orientation_euler.x;
    data->gamma = relative_orientation_sensor_.reading.orientation_euler.y;
    data->has_alpha = !std::isnan(
        relative_orientation_sensor_.reading.orientation_euler.z.value());
    data->has_beta = !std::isnan(
        relative_orientation_sensor_.reading.orientation_euler.x.value());
    data->has_gamma = !std::isnan(
        relative_orientation_sensor_.reading.orientation_euler.y.value());
    data->absolute = false;
  } else if (absolute_orientation_sensor_.SensorReadingCouldBeRead()) {
    // For DeviceOrientationAbsolute Event, this provides absolute orientation
    // data.
    //
    // For DeviceOrientation Event, this provides absolute orientation data if
    // relative orientation data is not available.
    data->all_available_sensors_are_active =
        absolute_orientation_sensor_.reading.timestamp() != 0.0;
    if (!data->all_available_sensors_are_active)
      return;
    data->alpha = absolute_orientation_sensor_.reading.orientation_euler.z;
    data->beta = absolute_orientation_sensor_.reading.orientation_euler.x;
    data->gamma = absolute_orientation_sensor_.reading.orientation_euler.y;
    data->has_alpha = !std::isnan(
        absolute_orientation_sensor_.reading.orientation_euler.z.value());
    data->has_beta = !std::isnan(
        absolute_orientation_sensor_.reading.orientation_euler.x.value());
    data->has_gamma = !std::isnan(
        absolute_orientation_sensor_.reading.orientation_euler.y.value());
    data->absolute = true;
  } else {
    data->absolute = absolute_;
  }
}

bool DeviceOrientationEventPump::ShouldFireEvent(
    const device::OrientationData& data) const {
  if (!data.all_available_sensors_are_active)
    return false;

  if (!data.has_alpha && !data.has_beta && !data.has_gamma) {
    // no data can be provided, this is an all-null event.
    return true;
  }

  return IsSignificantlyDifferent(data_, data);
}

}  // namespace content