OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef Sensor_h | 5 #ifndef Sensor_h |
6 #define Sensor_h | 6 #define Sensor_h |
7 | 7 |
8 #include "bindings/core/v8/ActiveScriptWrappable.h" | 8 #include "bindings/core/v8/ActiveScriptWrappable.h" |
9 #include "bindings/core/v8/ScriptWrappable.h" | 9 #include "bindings/core/v8/ScriptWrappable.h" |
10 #include "core/dom/ActiveDOMObject.h" | 10 #include "core/dom/ActiveDOMObject.h" |
11 #include "core/dom/ContextLifecycleObserver.h" | 11 #include "core/dom/ContextLifecycleObserver.h" |
12 #include "core/frame/PlatformEventController.h" | 12 #include "core/frame/PlatformEventController.h" |
| 13 #include "core/page/PageVisibilityObserver.h" |
13 #include "modules/EventTargetModules.h" | 14 #include "modules/EventTargetModules.h" |
14 #include "modules/ModulesExport.h" | |
15 #include "modules/sensor/SensorOptions.h" | 15 #include "modules/sensor/SensorOptions.h" |
16 #include "modules/sensor/SensorState.h" | 16 #include "modules/sensor/SensorProxy.h" |
17 #include "platform/heap/Handle.h" | 17 #include "platform/heap/Handle.h" |
18 | 18 |
19 namespace blink { | 19 namespace blink { |
20 | 20 |
21 class ExceptionState; | 21 class ExceptionState; |
22 class ScriptState; | 22 class ScriptState; |
23 class SensorReading; | 23 class SensorReading; |
| 24 class SensorPollingStrategy; |
24 | 25 |
25 class MODULES_EXPORT Sensor | 26 class Sensor |
26 : public EventTargetWithInlineData | 27 : public EventTargetWithInlineData |
27 , public ActiveScriptWrappable | 28 , public ActiveScriptWrappable |
28 , public ActiveDOMObject | 29 , public ActiveDOMObject |
29 , public PlatformEventController { | 30 , public PageVisibilityObserver |
| 31 , public SensorProxy::Observer { |
| 32 USING_PRE_FINALIZER(Sensor, dispose); |
30 USING_GARBAGE_COLLECTED_MIXIN(Sensor); | 33 USING_GARBAGE_COLLECTED_MIXIN(Sensor); |
31 DEFINE_WRAPPERTYPEINFO(); | 34 DEFINE_WRAPPERTYPEINFO(); |
32 | 35 |
33 public: | 36 public: |
34 ~Sensor() override; | 37 enum class SensorState { |
| 38 IDLE, |
| 39 ACTIVATING, |
| 40 ACTIVE, |
| 41 ERRORED |
| 42 }; |
| 43 |
| 44 void dispose(); |
35 | 45 |
36 void start(ScriptState*, ExceptionState&); | 46 void start(ScriptState*, ExceptionState&); |
37 void stop(ScriptState*, ExceptionState&); | 47 void stop(ScriptState*, ExceptionState&); |
38 void updateState(SensorState); | |
39 | 48 |
40 // EventTarget implementation. | 49 // EventTarget overrides. |
41 const AtomicString& interfaceName() const override { return EventTargetNames
::Sensor; } | 50 const AtomicString& interfaceName() const override { return EventTargetNames
::Sensor; } |
42 ExecutionContext* getExecutionContext() const override { return ContextLifec
ycleObserver::getExecutionContext(); } | 51 ExecutionContext* getExecutionContext() const override { return ContextLifec
ycleObserver::getExecutionContext(); } |
43 | 52 |
44 // Getters | 53 // Getters |
45 String state() const; | 54 String state() const; |
46 // TODO(riju): crbug.com/614797 . | 55 // TODO(riju): crbug.com/614797 . |
47 SensorReading* reading() const; | 56 SensorReading* reading() const; |
48 | 57 |
49 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); | 58 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); |
50 DEFINE_ATTRIBUTE_EVENT_LISTENER(change); | 59 DEFINE_ATTRIBUTE_EVENT_LISTENER(change); |
51 DEFINE_ATTRIBUTE_EVENT_LISTENER(statechange); | 60 DEFINE_ATTRIBUTE_EVENT_LISTENER(statechange); |
52 | 61 |
53 // ActiveDOMObject implementation. | 62 // ActiveScriptWrappable overrides. |
54 void suspend() override; | |
55 void resume() override; | |
56 void stop() override; | |
57 | |
58 // ScriptWrappable implementation. | |
59 bool hasPendingActivity() const override; | 63 bool hasPendingActivity() const override; |
60 | 64 |
61 DECLARE_VIRTUAL_TRACE(); | 65 DECLARE_VIRTUAL_TRACE(); |
62 | 66 |
63 protected: | 67 protected: |
64 Sensor(ExecutionContext*, const SensorOptions&); | 68 Sensor(ExecutionContext*, const SensorOptions&, device::mojom::blink::Sensor
Type); |
65 void notifyStateChange(); | 69 virtual SensorReading* createSensorReading(ExecutionContext*) = 0; |
66 | 70 |
67 SensorState m_sensorState; | 71 using SensorConfiguration = device::mojom::blink::SensorConfiguration; |
| 72 using SensorConfigurationPtr = device::mojom::blink::SensorConfigurationPtr; |
| 73 virtual SensorConfigurationPtr createSensorConfig(const SensorOptions&, cons
t SensorConfiguration* defaultConfig) = 0; |
| 74 |
| 75 private: |
| 76 void initSensorProxyIfNeeded(); |
| 77 |
| 78 // ActiveDOMObject overrides. |
| 79 void stop() override; |
| 80 |
| 81 // SensorController::Observer overrides. |
| 82 void onSensorInitialized() override; |
| 83 void onSensorReadingChanged() override; |
| 84 void onSensorError() override; |
| 85 |
| 86 void onStartRequestCompleted(bool); |
| 87 void onStopRequestCompleted(bool); |
| 88 |
| 89 // PageVisibilityObserver overrides. |
| 90 void pageVisibilityChanged() override; |
| 91 |
| 92 void startListening(); |
| 93 void stopListening(); |
| 94 |
| 95 // Makes sensor reading refresh its values from the shared buffer. |
| 96 void pollForData(); |
| 97 |
| 98 void updateState(SensorState newState); |
| 99 void reportError(); |
| 100 |
| 101 void updatePollingStatus(); |
| 102 |
| 103 private: |
68 Member<SensorReading> m_sensorReading; | 104 Member<SensorReading> m_sensorReading; |
69 SensorOptions m_sensorOptions; | 105 SensorOptions m_sensorOptions; |
| 106 device::mojom::blink::SensorType m_type; |
| 107 SensorState m_state; |
| 108 WeakMember<SensorProxy> m_sensorProxy; |
| 109 Member<SensorPollingStrategy> m_polling; |
| 110 SensorProxy::Reading m_storedData; |
| 111 SensorConfigurationPtr m_configuration; |
70 }; | 112 }; |
71 | 113 |
72 } // namespace blink | 114 } // namespace blink |
73 | 115 |
74 #endif // Sensor_h | 116 #endif // Sensor_h |
OLD | NEW |