1 // Copyright 2016 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. 4 5 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_SENSOR_SENSOR_PROXY_H_ 6 #define THIRD_PARTY_BLINK_RENDERER_MODULES_SENSOR_SENSOR_PROXY_H_ 7 8 #include "base/macros.h" 9 #include "services/device/public/cpp/generic_sensor/sensor_reading.h" 10 #include "services/device/public/cpp/generic_sensor/sensor_reading_shared_buffer_reader.h" 11 #include "services/device/public/mojom/sensor.mojom-blink-forward.h" 12 #include "services/device/public/mojom/sensor_provider.mojom-blink.h" 13 #include "third_party/blink/renderer/core/page/focus_changed_observer.h" 14 #include "third_party/blink/renderer/core/page/page_visibility_observer.h" 15 #include "third_party/blink/renderer/modules/modules_export.h" 16 #include "third_party/blink/renderer/platform/bindings/exception_code.h" 17 #include "third_party/blink/renderer/platform/heap/handle.h" 18 19 namespace blink { 20 21 class SensorProviderProxy; 22 23 // This class wraps 'Sensor' mojo interface and used by multiple 24 // JS sensor instances of the same type (within a single frame). 25 class MODULES_EXPORT SensorProxy : public GarbageCollected<SensorProxy>, 26 public PageVisibilityObserver, 27 public FocusChangedObserver { 28 USING_GARBAGE_COLLECTED_MIXIN(SensorProxy); 29 30 public: 31 class Observer : public GarbageCollectedMixin { 32 public: 33 // Has valid 'Sensor' binding, {add, remove}Configuration() 34 // methods can be called. OnSensorInitialized()35 virtual void OnSensorInitialized() {} 36 // Observer should update its cached reading and send 'onchange' 37 // event if needed. OnSensorReadingChanged()38 virtual void OnSensorReadingChanged() {} 39 // An error has occurred. OnSensorError(DOMExceptionCode,const String & sanitized_message,const String & unsanitized_message)40 virtual void OnSensorError(DOMExceptionCode, 41 const String& sanitized_message, 42 const String& unsanitized_message) {} 43 }; 44 45 ~SensorProxy() override; 46 47 void Dispose(); 48 49 void AddObserver(Observer*); 50 void RemoveObserver(Observer*); 51 52 // Public methods to be implemented by descendants. 53 virtual void Initialize() = 0; 54 virtual void AddConfiguration(device::mojom::blink::SensorConfigurationPtr, 55 base::OnceCallback<void(bool)>) = 0; 56 virtual void RemoveConfiguration( 57 device::mojom::blink::SensorConfigurationPtr) = 0; 58 virtual double GetDefaultFrequency() const = 0; 59 virtual std::pair<double, double> GetFrequencyLimits() const = 0; SetReadingForInspector(const device::SensorReading &)60 virtual void SetReadingForInspector(const device::SensorReading&) {} 61 62 virtual void ReportError(DOMExceptionCode code, const String& description); 63 // Getters. IsInitializing()64 bool IsInitializing() const { return state_ == kInitializing; } IsInitialized()65 bool IsInitialized() const { return state_ == kInitialized; } type()66 device::mojom::blink::SensorType type() const { return type_; } 67 // Note: do not use the stored references to the returned value 68 // outside the current call chain. 69 const device::SensorReading& GetReading(bool remapped = false) const; 70 71 // Detach from the local frame's SensorProviderProxy. 72 void Detach(); 73 74 void Trace(Visitor*) override; 75 76 static const char kDefaultErrorDescription[]; 77 78 protected: 79 SensorProxy(device::mojom::blink::SensorType, SensorProviderProxy*, Page*); 80 void UpdateSuspendedStatus(); 81 82 // Protected methods to be implemented by descendants. Suspend()83 virtual void Suspend() {} Resume()84 virtual void Resume() {} 85 86 device::mojom::blink::SensorProvider* sensor_provider() const; 87 88 device::mojom::blink::SensorType type_; 89 using ObserversSet = HeapHashSet<WeakMember<Observer>>; 90 ObserversSet observers_; 91 92 enum State { kUninitialized, kInitializing, kInitialized }; 93 State state_ = kUninitialized; 94 95 device::SensorReading reading_; 96 mutable device::SensorReading remapped_reading_; 97 98 private: 99 // PageVisibilityObserver overrides. 100 void PageVisibilityChanged() override; 101 102 // FocusChangedObserver overrides. 103 void FocusedFrameChanged() override; 104 105 // Returns true if conditions to suspend sensor reading updates are met. 106 bool ShouldSuspendUpdates() const; 107 108 Member<SensorProviderProxy> provider_; 109 bool detached_ = false; 110 111 static_assert( 112 sizeof(device::SensorReadingSharedBuffer) == 113 device::mojom::blink::SensorInitParams::kReadBufferSizeForTests, 114 "Check reading buffer size for tests"); 115 116 DISALLOW_COPY_AND_ASSIGN(SensorProxy); 117 }; 118 119 } // namespace blink 120 121 #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_SENSOR_SENSOR_PROXY_H_ 122