File: sensor_proxy.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (119 lines) | stat: -rw-r--r-- 4,488 bytes parent folder | download | duplicates (6)
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
// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_SENSOR_SENSOR_PROXY_H_
#define THIRD_PARTY_BLINK_RENDERER_MODULES_SENSOR_SENSOR_PROXY_H_

#include "services/device/public/cpp/generic_sensor/sensor_reading.h"
#include "services/device/public/cpp/generic_sensor/sensor_reading_shared_buffer.h"
#include "services/device/public/cpp/generic_sensor/sensor_reading_shared_buffer_reader.h"
#include "services/device/public/mojom/sensor.mojom-blink-forward.h"
#include "services/device/public/mojom/sensor_provider.mojom-blink.h"
#include "third_party/blink/renderer/core/page/focus_changed_observer.h"
#include "third_party/blink/renderer/core/page/page_visibility_observer.h"
#include "third_party/blink/renderer/platform/bindings/exception_code.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_set.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"

namespace blink {

class SensorProviderProxy;

// This class wraps 'Sensor' mojo interface and used by multiple
// JS sensor instances of the same type (within a single frame).
class SensorProxy : public GarbageCollected<SensorProxy>,
                    public PageVisibilityObserver,
                    public FocusChangedObserver {
 public:
  class Observer : public GarbageCollectedMixin {
   public:
    // Has valid 'Sensor' binding, {add, remove}Configuration()
    // methods can be called.
    virtual void OnSensorInitialized() {}
    // Observer should update its cached reading and send 'onchange'
    // event if needed.
    virtual void OnSensorReadingChanged() {}
    // An error has occurred.
    virtual void OnSensorError(DOMExceptionCode,
                               const String& sanitized_message,
                               const String& unsanitized_message) {}
  };

  SensorProxy(const SensorProxy&) = delete;
  SensorProxy& operator=(const SensorProxy&) = delete;

  ~SensorProxy() override;

  void Dispose();

  void AddObserver(Observer*);
  void RemoveObserver(Observer*);

  // Public methods to be implemented by descendants.
  virtual void Initialize() = 0;
  virtual void AddConfiguration(device::mojom::blink::SensorConfigurationPtr,
                                base::OnceCallback<void(bool)>) = 0;
  virtual void RemoveConfiguration(
      device::mojom::blink::SensorConfigurationPtr) = 0;
  virtual double GetDefaultFrequency() const = 0;
  virtual std::pair<double, double> GetFrequencyLimits() const = 0;

  virtual void ReportError(DOMExceptionCode code, const String& description);
  // Getters.
  bool IsInitializing() const { return state_ == kInitializing; }
  bool IsInitialized() const { return state_ == kInitialized; }
  device::mojom::blink::SensorType type() const { return type_; }
  // Note: do not use the stored references to the returned value
  // outside the current call chain.
  const device::SensorReading& GetReading(bool remapped = false) const;

  // Detach from the local frame's SensorProviderProxy.
  void Detach();

  void Trace(Visitor*) const override;

  static const char kDefaultErrorDescription[];

 protected:
  SensorProxy(device::mojom::blink::SensorType, SensorProviderProxy*, Page*);
  void UpdateSuspendedStatus();

  // Protected methods to be implemented by descendants.
  virtual void Suspend() {}
  virtual void Resume() {}

  SensorProviderProxy* sensor_provider_proxy() const { return provider_.Get(); }

  device::mojom::blink::SensorType type_;
  using ObserversSet = HeapHashSet<WeakMember<Observer>>;
  ObserversSet observers_;

  enum State { kUninitialized, kInitializing, kInitialized };
  State state_ = kUninitialized;

  device::SensorReading reading_;
  mutable device::SensorReading remapped_reading_;

 private:
  // PageVisibilityObserver overrides.
  void PageVisibilityChanged() override;

  // FocusChangedObserver overrides.
  void FocusedFrameChanged() override;

  // Returns true if conditions to suspend sensor reading updates are met.
  bool ShouldSuspendUpdates() const;

  Member<SensorProviderProxy> provider_;
  bool detached_ = false;

  static_assert(
      sizeof(device::SensorReadingSharedBuffer) ==
          device::mojom::blink::SensorInitParams::kReadBufferSizeForTests,
      "Check reading buffer size for tests");
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_MODULES_SENSOR_SENSOR_PROXY_H_