summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/modules/sensor/ambient_light_sensor.cc
blob: 7d31a652c661c86f25177dd7020006654be1f3a2 (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
// Copyright 2016 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 "third_party/blink/renderer/modules/sensor/ambient_light_sensor.h"

#include "third_party/blink/public/mojom/feature_policy/feature_policy_feature.mojom-blink.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise.h"
#include "third_party/blink/renderer/bindings/core/v8/script_promise_resolver.h"

using device::mojom::blink::SensorType;

namespace blink {

namespace {

// Even though the underlying value has changed, for ALS we provide readouts to
// JS to the nearest 50 Lux.
constexpr int kAlsRoundingThreshold = 50;

// Decrease precision of ALS readouts.
// Round off to the nearest kAlsRoundingThreshold.
double RoundIlluminance(double value) {
  return kAlsRoundingThreshold * std::round(value / kAlsRoundingThreshold);
}

// Value will have to vary by at least half the rounding threshold before it has
// an effect on the output.
bool IsSignificantlyDifferent(double als_old, double als_new) {
  return std::fabs(als_old - als_new) >= kAlsRoundingThreshold / 2;
}

}  // namespace

// static
AmbientLightSensor* AmbientLightSensor::Create(
    ExecutionContext* execution_context,
    const SensorOptions* options,
    ExceptionState& exception_state) {
  return MakeGarbageCollected<AmbientLightSensor>(execution_context, options,
                                                  exception_state);
}

// static
AmbientLightSensor* AmbientLightSensor::Create(
    ExecutionContext* execution_context,
    ExceptionState& exception_state) {
  return Create(execution_context, SensorOptions::Create(), exception_state);
}

AmbientLightSensor::AmbientLightSensor(ExecutionContext* execution_context,
                                       const SensorOptions* options,
                                       ExceptionState& exception_state)
    : Sensor(execution_context,
             options,
             exception_state,
             SensorType::AMBIENT_LIGHT,
             {mojom::blink::FeaturePolicyFeature::kAmbientLightSensor}) {}

base::Optional<double> AmbientLightSensor::illuminance() const {
  if (hasReading()) {
    DCHECK(latest_reading_.has_value());
    return RoundIlluminance(latest_reading_.value());
  }
  return base::nullopt;
}

double AmbientLightSensor::illuminance(bool& is_null) const {
  INIT_IS_NULL_AND_RETURN(is_null, 0.0);
  DCHECK(latest_reading_.has_value());
  return RoundIlluminance(*latest_reading_);
}

// When the reading we get does not differ significantly from our current
// value, we discard this reading and do not emit any events. This is a privacy
// measure to avoid giving readings that are too specific.
void AmbientLightSensor::OnSensorReadingChanged() {
  // The platform sensor may start sending readings before the sensor is fully
  // activated on the Blink side. In this case, bail out early, otherwise we
  // will set |latest_reading_| and not send a "reading" event.
  if (!IsActivated())
    return;

  const double new_reading = GetReading().als.value;
  if (latest_reading_.has_value() &&
      !IsSignificantlyDifferent(*latest_reading_, new_reading)) {
    return;
  }

  latest_reading_ = new_reading;
  Sensor::OnSensorReadingChanged();
}

}  // namespace blink