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
|
// 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 Sensor_h
#define Sensor_h
#include "bindings/core/v8/ActiveScriptWrappable.h"
#include "bindings/core/v8/ScriptWrappable.h"
#include "core/dom/ContextLifecycleObserver.h"
#include "core/dom/SuspendableObject.h"
#include "core/frame/PlatformEventController.h"
#include "modules/EventTargetModules.h"
#include "modules/sensor/SensorOptions.h"
#include "modules/sensor/SensorProxy.h"
#include "platform/heap/Handle.h"
namespace blink {
class ExceptionState;
class ExecutionContext;
class SensorReading;
class Sensor : public EventTargetWithInlineData,
public ActiveScriptWrappable<Sensor>,
public ContextLifecycleObserver,
public SensorProxy::Observer {
USING_GARBAGE_COLLECTED_MIXIN(Sensor);
DEFINE_WRAPPERTYPEINFO();
public:
enum class SensorState { Idle, Activating, Activated, Errored };
~Sensor() override;
void start(ScriptState*, ExceptionState&);
void stop(ScriptState*, ExceptionState&);
// EventTarget overrides.
const AtomicString& interfaceName() const override {
return EventTargetNames::Sensor;
}
ExecutionContext* getExecutionContext() const override {
return ContextLifecycleObserver::getExecutionContext();
}
// Getters
String state() const;
// TODO(riju): crbug.com/614797 .
SensorReading* reading() const;
DEFINE_ATTRIBUTE_EVENT_LISTENER(error);
DEFINE_ATTRIBUTE_EVENT_LISTENER(change);
DEFINE_ATTRIBUTE_EVENT_LISTENER(activate);
// ActiveScriptWrappable overrides.
bool hasPendingActivity() const override;
DECLARE_VIRTUAL_TRACE();
protected:
Sensor(ExecutionContext*,
const SensorOptions&,
ExceptionState&,
device::mojom::blink::SensorType);
virtual std::unique_ptr<SensorReadingFactory>
createSensorReadingFactory() = 0;
using SensorConfigurationPtr = device::mojom::blink::SensorConfigurationPtr;
using SensorConfiguration = device::mojom::blink::SensorConfiguration;
// The default implementation will init frequency configuration parameter,
// concrete sensor implementations can override this method to handle other
// parameters if needed.
virtual SensorConfigurationPtr createSensorConfig();
private:
void initSensorProxyIfNeeded();
// ContextLifecycleObserver overrides.
void contextDestroyed(ExecutionContext*) override;
// SensorController::Observer overrides.
void onSensorInitialized() override;
void onSensorReadingChanged(double timestamp) override;
void onSensorError(ExceptionCode,
const String& sanitizedMessage,
const String& unsanitizedMessage) override;
void onStartRequestCompleted(bool);
void onStopRequestCompleted(bool);
void startListening();
void stopListening();
void updateState(SensorState newState);
void reportError(ExceptionCode = UnknownError,
const String& sanitizedMessage = String(),
const String& unsanitizedMessage = String());
void notifySensorReadingChanged();
void notifyOnActivate();
void notifyError(DOMException* error);
private:
SensorOptions m_sensorOptions;
device::mojom::blink::SensorType m_type;
SensorState m_state;
Member<SensorProxy> m_sensorProxy;
device::SensorReading m_storedData;
SensorConfigurationPtr m_configuration;
double m_lastUpdateTimestamp;
};
} // namespace blink
#endif // Sensor_h
|