| 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 "modules/EventTargetModules.h" | 13 #include "modules/EventTargetModules.h" |
| 14 #include "modules/ModulesExport.h" | 14 #include "modules/ModulesExport.h" |
| 15 #include "modules/sensor/SensorController.h" |
| 15 #include "modules/sensor/SensorOptions.h" | 16 #include "modules/sensor/SensorOptions.h" |
| 16 #include "modules/sensor/SensorState.h" | 17 #include "modules/sensor/SensorReading.h" |
| 17 #include "platform/heap/Handle.h" | 18 #include "platform/heap/Handle.h" |
| 18 | 19 |
| 19 namespace blink { | 20 namespace blink { |
| 20 | 21 |
| 21 class ExceptionState; | 22 class ExceptionState; |
| 22 class ScriptState; | 23 class ScriptState; |
| 23 class SensorReading; | 24 class SensorReading; |
| 25 class SensorPollingStrategy; |
| 24 | 26 |
| 25 class MODULES_EXPORT Sensor | 27 class MODULES_EXPORT Sensor |
| 26 : public EventTargetWithInlineData | 28 : public EventTargetWithInlineData |
| 27 , public ActiveScriptWrappable | 29 , public ActiveScriptWrappable |
| 28 , public ActiveDOMObject | 30 , public ActiveDOMObject |
| 29 , public PlatformEventController { | 31 , public SensorController::Observer { |
| 30 USING_GARBAGE_COLLECTED_MIXIN(Sensor); | 32 USING_GARBAGE_COLLECTED_MIXIN(Sensor); |
| 31 DEFINE_WRAPPERTYPEINFO(); | 33 DEFINE_WRAPPERTYPEINFO(); |
| 32 | 34 |
| 33 public: | 35 public: |
| 34 ~Sensor() override; | 36 enum class SensorState : int { |
| 37 IDLE, |
| 38 ACTIVATING, |
| 39 ACTIVE, |
| 40 ERRORED |
| 41 }; |
| 35 | 42 |
| 36 void start(ScriptState*, ExceptionState&); | 43 virtual void start(ScriptState*, ExceptionState&); |
| 37 void stop(ScriptState*, ExceptionState&); | 44 virtual void stop(ScriptState*, ExceptionState&); |
| 38 void updateState(SensorState); | |
| 39 | 45 |
| 40 // EventTarget implementation. | 46 // EventTarget overrides. |
| 41 const AtomicString& interfaceName() const override { return EventTargetNames
::Sensor; } | 47 const AtomicString& interfaceName() const override { return EventTargetNames
::Sensor; } |
| 42 ExecutionContext* getExecutionContext() const override { return ContextLifec
ycleObserver::getExecutionContext(); } | 48 ExecutionContext* getExecutionContext() const override { return ContextLifec
ycleObserver::getExecutionContext(); } |
| 43 | 49 |
| 44 // Getters | 50 // Getters |
| 45 String state() const; | 51 String state() const; |
| 46 // TODO(riju): crbug.com/614797 . | 52 // TODO(riju): crbug.com/614797 . |
| 47 SensorReading* reading() const; | 53 SensorReading* reading() const; |
| 48 | 54 |
| 49 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); | 55 DEFINE_ATTRIBUTE_EVENT_LISTENER(error); |
| 50 DEFINE_ATTRIBUTE_EVENT_LISTENER(change); | 56 DEFINE_ATTRIBUTE_EVENT_LISTENER(change); |
| 51 DEFINE_ATTRIBUTE_EVENT_LISTENER(statechange); | 57 DEFINE_ATTRIBUTE_EVENT_LISTENER(statechange); |
| 52 | 58 |
| 53 // ActiveDOMObject implementation. | |
| 54 void suspend() override; | |
| 55 void resume() override; | |
| 56 void stop() override; | |
| 57 bool hasPendingActivity() const override; | |
| 58 | |
| 59 DECLARE_VIRTUAL_TRACE(); | 59 DECLARE_VIRTUAL_TRACE(); |
| 60 | 60 |
| 61 protected: | 61 protected: |
| 62 Sensor(ExecutionContext*, const SensorOptions&); | 62 Sensor(ExecutionContext*, const SensorOptions&, device::sensors::blink::Sens
orType type); |
| 63 void notifyStateChange(); | 63 virtual SensorReading* createSensorReading() = 0; |
| 64 virtual device::sensors::blink::SensorConfigurationPtr createSensorOptions()
= 0; |
| 64 | 65 |
| 65 SensorState m_sensorState; | 66 // Getters to be used by implementations. |
| 67 const SensorOptions& options() const { return m_sensorOptions; } |
| 68 SensorController* controller() { return m_controller; } |
| 69 |
| 70 private: |
| 71 void InitControllerIfNeeded(); |
| 72 |
| 73 // EventTargetWithInlineData overrides. |
| 74 void addedEventListener(const AtomicString& eventType, RegisteredEventListen
er&) override; |
| 75 void removedEventListener(const AtomicString& eventType, const RegisteredEve
ntListener&) override; |
| 76 |
| 77 // ActiveDOMObject overrides. |
| 78 void stop() override {} |
| 79 |
| 80 // SensorController::Observer overrides. |
| 81 void onSensorInitialized() override; |
| 82 void onSensorReadingChanged() override; |
| 83 void onSensorError() override; |
| 84 |
| 85 // ActiveScriptWrappable overrides. |
| 86 bool hasPendingActivity() const override; |
| 87 |
| 88 void onStartRequestCompleted(bool); |
| 89 void onStopRequestCompleted(bool); |
| 90 void pollForData(); |
| 91 |
| 92 void updateState(SensorState newState); |
| 93 void reportError(); |
| 94 |
| 95 void updatePollingStatus(); |
| 96 |
| 97 private: |
| 66 Member<SensorReading> m_sensorReading; | 98 Member<SensorReading> m_sensorReading; |
| 67 SensorOptions m_sensorOptions; | 99 SensorOptions m_sensorOptions; |
| 100 device::sensors::blink::SensorType m_type; |
| 101 SensorState m_state; |
| 102 WeakMember<SensorController> m_controller; |
| 103 Member<SensorPollingStrategy> m_polling; |
| 68 }; | 104 }; |
| 69 | 105 |
| 70 } // namespace blink | 106 } // namespace blink |
| 71 | 107 |
| 72 #endif // Sensor_h | 108 #endif // Sensor_h |
| OLD | NEW |