Chromium Code Reviews| Index: third_party/WebKit/Source/modules/sensor/Sensor.cpp |
| diff --git a/third_party/WebKit/Source/modules/sensor/Sensor.cpp b/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..45313cbc3f5a2021f03bbab6c39419a4fa38a8d8 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp |
| @@ -0,0 +1,135 @@ |
| +// 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. |
| + |
| +#include "modules/sensor/Sensor.h" |
| + |
| +#include "core/dom/Document.h" |
| +#include "core/dom/ExceptionCode.h" |
| +#include "core/events/Event.h" |
| + |
| +#include "modules/sensor/SensorReading.h" |
| + |
| +namespace blink { |
| + |
| +Sensor::~Sensor() |
| +{ |
| +} |
| + |
| +Sensor::Sensor(ExecutionContext* executionContext, const SensorOptions& sensorOptions) |
| + : ActiveScriptWrappable(this) |
| + , ActiveDOMObject(executionContext) |
| + , PlatformEventController(toDocument(executionContext)->page()) |
| + , m_sensorState(SensorState::Idle) |
| + , m_sensorReading(nullptr) |
| + , m_sensorOptions(sensorOptions) |
| +{ |
| +} |
| + |
| +// Getters |
| +String Sensor::state() const |
| +{ |
| + switch (m_sensorState) { |
| + case SensorState::Idle: |
| + return "idle"; |
| + case SensorState::Activating: |
| + return "activating"; |
| + case SensorState::Active: |
| + return "active"; |
| + case SensorState::Errored: |
| + return "errored"; |
| + } |
| + NOTREACHED(); |
| + return "idle"; |
| +} |
| + |
| +SensorReading* Sensor::reading() const |
| +{ |
| + return m_sensorReading.get(); |
| +} |
| + |
| +void Sensor::start(ScriptState* scriptState, ExceptionState& exceptionState) |
| +{ |
| + if (m_sensorState != SensorState::Idle && m_sensorState != SensorState::Errored) { |
| + exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is not idle or errored"); |
| + |
| + // Avoid re-entrancy. sensor.start() inside 'onstatechange' event handler. |
| + m_hasEventListener = false; |
| + stopUpdating(); |
| + |
| + return; |
| + } |
| + |
| + updateState(SensorState::Activating); |
| + |
| + // TODO(riju) : Add Permissions stuff later. |
| + |
| + m_hasEventListener = true; |
| + startUpdating(); |
| + updateState(SensorState::Active); |
|
timvolodine
2016/05/26 15:49:01
is this correct? startUpdating is async, so it cou
riju_
2016/05/26 17:32:21
No, removing the active statechange from here.
timvolodine
2016/05/27 18:40:57
ok, could you pls add a TODO regarding this in ord
riju_
2016/05/30 10:15:13
Done.
|
| + |
| +} |
| + |
| +void Sensor::stop(ScriptState* scriptState, ExceptionState& exceptionState) |
| +{ |
| + if (m_sensorState == SensorState::Idle || m_sensorState == SensorState::Errored) { |
| + exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState is either idle or errored"); |
| + return; |
| + } |
| + |
| + // Avoid re-entrancy. sensor.stop() inside 'onstatechange' event handler. |
| + if (m_sensorState == SensorState::Activating) { |
|
timvolodine
2016/05/26 15:49:01
so this means that we cannot stop the sensor while
riju_
2016/05/26 17:32:20
Wrongly done , fixing it.
|
| + exceptionState.throwDOMException(InvalidStateError, "Invalid State: SensorState Activating"); |
| + return; |
| + } |
| + |
| + m_hasEventListener = false; |
| + stopUpdating(); |
| + |
| + m_sensorReading.clear(); |
| + updateState(SensorState::Idle); |
| +} |
| + |
| +void Sensor::updateState(SensorState newState) |
| +{ |
| + if (m_sensorState == newState) |
| + return; |
| + |
| + m_sensorState = newState; |
| + dispatchEvent(Event::create(EventTypeNames::statechange)); |
| +} |
| + |
| +void Sensor::suspend() |
| +{ |
| + m_hasEventListener = false; |
| + stopUpdating(); |
| +} |
| + |
| +void Sensor::resume() |
| +{ |
| + m_hasEventListener = true; |
| + startUpdating(); |
| +} |
| + |
| +void Sensor::stop() |
| +{ |
| + m_hasEventListener = false; |
| + stopUpdating(); |
| +} |
| + |
| +bool Sensor::hasPendingActivity() const |
| +{ |
| + // Prevent V8 from garbage collecting the wrapper object if there are |
| + // event listeners attached to it. |
| + return hasEventListeners(); |
| +} |
| + |
| +DEFINE_TRACE(Sensor) |
| +{ |
| + ActiveDOMObject::trace(visitor); |
| + EventTargetWithInlineData::trace(visitor); |
| + PlatformEventController::trace(visitor); |
| + visitor->trace(m_sensorReading); |
| +} |
| + |
| +} // namespace blink |