OLD | NEW |
(Empty) | |
| 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 #include "modules/sensor/Sensor.h" |
| 6 |
| 7 #include "core/dom/Document.h" |
| 8 #include "core/dom/ExceptionCode.h" |
| 9 #include "core/dom/ExecutionContextTask.h" |
| 10 #include "core/events/Event.h" |
| 11 |
| 12 #include "modules/sensor/SensorReading.h" |
| 13 |
| 14 namespace blink { |
| 15 |
| 16 Sensor::~Sensor() |
| 17 { |
| 18 } |
| 19 |
| 20 Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOp
tions) |
| 21 : ActiveScriptWrappable(this) |
| 22 , ActiveDOMObject(executionContext) |
| 23 , PlatformEventController(toDocument(executionContext)->page()) |
| 24 , m_sensorState(SensorState::Idle) |
| 25 , m_sensorReading(nullptr) |
| 26 , m_sensorOptions(sensorOptions) |
| 27 { |
| 28 } |
| 29 |
| 30 // Getters |
| 31 String Sensor::state() const |
| 32 { |
| 33 // TODO(riju): Validate the transitions. |
| 34 switch (m_sensorState) { |
| 35 case SensorState::Idle: |
| 36 return "idle"; |
| 37 case SensorState::Activating: |
| 38 return "activating"; |
| 39 case SensorState::Active: |
| 40 return "active"; |
| 41 case SensorState::Errored: |
| 42 return "errored"; |
| 43 } |
| 44 NOTREACHED(); |
| 45 return "idle"; |
| 46 } |
| 47 |
| 48 SensorReading* Sensor::reading() const |
| 49 { |
| 50 return m_sensorReading.get(); |
| 51 } |
| 52 |
| 53 void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) |
| 54 { |
| 55 |
| 56 if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Erro
red) { |
| 57 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens
orState is not idle or errored"); |
| 58 return; |
| 59 } |
| 60 |
| 61 updateState(SensorState::Activating); |
| 62 |
| 63 // TODO(riju) : Add Permissions stuff later. |
| 64 |
| 65 m_hasEventListener = true; |
| 66 |
| 67 // TODO(riju): verify the correct order of onstatechange(active) and the fir
st onchange(event). |
| 68 startUpdating(); |
| 69 } |
| 70 |
| 71 void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState) |
| 72 { |
| 73 if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Erro
red) { |
| 74 exceptionState.throwDOMException(InvalidStateError, "Invalid State: Sens
orState is either idle or errored"); |
| 75 return; |
| 76 } |
| 77 |
| 78 m_hasEventListener = false; |
| 79 stopUpdating(); |
| 80 |
| 81 m_sensorReading.clear(); |
| 82 updateState(SensorState::Idle); |
| 83 } |
| 84 |
| 85 void Sensor::updateState(SensorState newState) |
| 86 { |
| 87 DCHECK(isMainThread()); |
| 88 if (m_sensorState == newState) |
| 89 return; |
| 90 |
| 91 m_sensorState = newState; |
| 92 // Notify context that state changed. |
| 93 if (getExecutionContext()) |
| 94 getExecutionContext()->postTask(BLINK_FROM_HERE, createSameThreadTask(&S
ensor::notifyStateChange, this)); |
| 95 } |
| 96 |
| 97 void Sensor::notifyStateChange() |
| 98 { |
| 99 dispatchEvent(Event::create(EventTypeNames::statechange)); |
| 100 } |
| 101 |
| 102 void Sensor::suspend() |
| 103 { |
| 104 m_hasEventListener = false; |
| 105 stopUpdating(); |
| 106 } |
| 107 |
| 108 void Sensor::resume() |
| 109 { |
| 110 m_hasEventListener = true; |
| 111 startUpdating(); |
| 112 } |
| 113 |
| 114 void Sensor::stop() |
| 115 { |
| 116 m_hasEventListener = false; |
| 117 stopUpdating(); |
| 118 } |
| 119 |
| 120 bool Sensor::hasPendingActivity() const |
| 121 { |
| 122 // Prevent V8 from garbage collecting the wrapper object if there are |
| 123 // event listeners attached to it. |
| 124 return hasEventListeners(); |
| 125 } |
| 126 |
| 127 DEFINE_TRACE(Sensor) |
| 128 { |
| 129 ActiveDOMObject::trace(visitor); |
| 130 EventTargetWithInlineData::trace(visitor); |
| 131 PlatformEventController::trace(visitor); |
| 132 visitor->trace(m_sensorReading); |
| 133 } |
| 134 |
| 135 } // namespace blink |
OLD | NEW |