File: SensorProxy.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (166 lines) | stat: -rw-r--r-- 5,927 bytes parent folder | download
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
// 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.

#ifndef SensorProxy_h
#define SensorProxy_h

#include "core/dom/ExceptionCode.h"
#include "core/page/PageVisibilityObserver.h"
#include "device/generic_sensor/public/cpp/sensor_reading.h"
#include "device/generic_sensor/public/interfaces/sensor.mojom-blink.h"
#include "device/generic_sensor/public/interfaces/sensor_provider.mojom-blink.h"
#include "mojo/public/cpp/bindings/binding.h"
#include "platform/Supplementable.h"
#include "platform/heap/Handle.h"
#include "wtf/Vector.h"

namespace blink {

class SensorProviderProxy;
class SensorReading;
class SensorReadingFactory;
class SensorReadingUpdater;

// This class wraps 'Sensor' mojo interface and used by multiple
// JS sensor instances of the same type (within a single frame).
class SensorProxy final : public GarbageCollectedFinalized<SensorProxy>,
                          public device::mojom::blink::SensorClient,
                          public PageVisibilityObserver {
  USING_GARBAGE_COLLECTED_MIXIN(SensorProxy);
  USING_PRE_FINALIZER(SensorProxy, dispose);
  WTF_MAKE_NONCOPYABLE(SensorProxy);

 public:
  class Observer : public GarbageCollectedMixin {
   public:
    // Has valid 'Sensor' binding, {add, remove}Configuration()
    // methods can be called.
    virtual void onSensorInitialized() {}
    // Platfrom sensort reading has changed.
    // |timestamp| Reference timestamp in seconds of the moment when
    // sensor reading was updated from the buffer.
    // Note: |timestamp| values are only used to calculate elapsed time
    // between shared buffer readings. These values *do not* correspond
    // to sensor reading timestamps which are obtained on platform side.
    virtual void onSensorReadingChanged(double timestamp) {}
    // An error has occurred.
    virtual void onSensorError(ExceptionCode,
                               const String& sanitizedMessage,
                               const String& unsanitizedMessage) {}
  };

  ~SensorProxy();

  void dispose();

  void addObserver(Observer*);
  void removeObserver(Observer*);

  void initialize();

  bool isInitializing() const { return m_state == Initializing; }
  bool isInitialized() const { return m_state == Initialized; }

  // Is watching new reading data (initialized, not suspended and has
  // configurations added).
  bool isActive() const;

  void addConfiguration(device::mojom::blink::SensorConfigurationPtr,
                        std::unique_ptr<Function<void(bool)>>);

  void removeConfiguration(device::mojom::blink::SensorConfigurationPtr);

  void suspend();
  void resume();

  device::mojom::blink::SensorType type() const { return m_type; }
  device::mojom::blink::ReportingMode reportingMode() const { return m_mode; }

  // The |SensorReading| instance which is shared between sensor instances
  // of the same type.
  // Note: the returned value is reset after updateSensorReading() call.
  SensorReading* sensorReading() const { return m_reading; }

  const device::mojom::blink::SensorConfiguration* defaultConfig() const;

  double maximumFrequency() const { return m_maximumFrequency; }

  Document* document() const { return m_document; }
  const WTF::Vector<double>& frequenciesUsed() const {
    return m_frequenciesUsed;
  }

  DECLARE_VIRTUAL_TRACE();

 private:
  friend class SensorProviderProxy;
  friend class SensorReadingUpdaterContinuous;
  friend class SensorReadingUpdaterOnChange;
  SensorProxy(device::mojom::blink::SensorType,
              SensorProviderProxy*,
              Document*,
              std::unique_ptr<SensorReadingFactory>);

  // Updates sensor reading from shared buffer.
  void updateSensorReading();
  void notifySensorChanged(double timestamp);

  // device::mojom::blink::SensorClient overrides.
  void RaiseError() override;
  void SensorReadingChanged() override;

  // PageVisibilityObserver overrides.
  void pageVisibilityChanged() override;

  // Generic handler for a fatal error.
  // String parameters are intentionally copied.
  void handleSensorError(ExceptionCode = UnknownError,
                         String sanitizedMessage = String(),
                         String unsanitizedMessage = String());
  // mojo call callbacks.
  void onSensorCreated(device::mojom::blink::SensorInitParamsPtr,
                       device::mojom::blink::SensorClientRequest);
  void onAddConfigurationCompleted(
      double frequency,
      std::unique_ptr<Function<void(bool)>> callback,
      bool result);
  void onRemoveConfigurationCompleted(double frequency, bool result);

  bool tryReadFromBuffer(device::SensorReading& result);
  void onAnimationFrame(double timestamp);

  device::mojom::blink::SensorType m_type;
  device::mojom::blink::ReportingMode m_mode;
  Member<SensorProviderProxy> m_provider;
  using ObserversSet = HeapHashSet<WeakMember<Observer>>;
  ObserversSet m_observers;

  device::mojom::blink::SensorPtr m_sensor;
  device::mojom::blink::SensorConfigurationPtr m_defaultConfig;
  mojo::Binding<device::mojom::blink::SensorClient> m_clientBinding;

  enum State { Uninitialized, Initializing, Initialized };
  State m_state;
  mojo::ScopedSharedBufferHandle m_sharedBufferHandle;
  mojo::ScopedSharedBufferMapping m_sharedBuffer;
  bool m_suspended;
  Member<Document> m_document;
  Member<SensorReading> m_reading;
  std::unique_ptr<SensorReadingFactory> m_readingFactory;
  double m_maximumFrequency;

  Member<SensorReadingUpdater> m_readingUpdater;
  WTF::Vector<double> m_frequenciesUsed;
  double m_lastRafTimestamp;

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

}  // namespace blink

#endif  // SensorProxy_h