blob: 63e584bd7a4cbfa47fc2c2521336952550cda658 [file] [log] [blame]
// 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 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_reader.h"
#include "services/device/public/mojom/sensor.mojom-blink.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/handle.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 GarbageCollectedFinalized<SensorProxy>,
public PageVisibilityObserver,
public FocusChangedObserver {
WTF_MAKE_NONCOPYABLE(SensorProxy);
USING_GARBAGE_COLLECTED_MIXIN(SensorProxy);
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() 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 SetReadingForInspector(const device::SensorReading&) {}
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(blink::Visitor*) 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() {}
device::mojom::blink::SensorProvider* sensor_provider() const;
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_;
using ReadingBuffer = device::SensorReadingSharedBuffer;
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(ReadingBuffer) ==
device::mojom::blink::SensorInitParams::kReadBufferSizeForTests,
"Check reading buffer size for tests");
};
} // namespace blink
#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_SENSOR_SENSOR_PROXY_H_