Chromium Code Reviews| 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/SensorProxy.h" | |
| 6 | |
| 7 #include "core/frame/LocalFrame.h" | |
| 8 #include "modules/sensor/SensorProviderProxy.h" | |
| 9 #include "platform/mojo/MojoHelper.h" | |
| 10 | |
| 11 using namespace device::mojom::blink; | |
| 12 | |
| 13 namespace blink { | |
| 14 | |
| 15 SensorProxy::SensorProxy(SensorType sensorType, SensorProviderProxy* provider) | |
| 16 : m_type(sensorType) | |
| 17 , m_mode(ReportingMode::CONTINUOUS) | |
| 18 , m_provider(provider) | |
| 19 , m_clientBinding(this) | |
| 20 , m_state(SensorProxy::Uninitialized) | |
| 21 , m_reading() | |
| 22 { | |
| 23 } | |
| 24 | |
| 25 SensorProxy::~SensorProxy() | |
| 26 { | |
| 27 } | |
| 28 | |
| 29 void SensorProxy::dispose() | |
| 30 { | |
| 31 m_clientBinding.Close(); | |
| 32 } | |
| 33 | |
| 34 DEFINE_TRACE(SensorProxy) | |
| 35 { | |
| 36 visitor->trace(m_observers); | |
| 37 visitor->trace(m_provider); | |
| 38 } | |
| 39 | |
| 40 void SensorProxy::addObserver(Observer* observer) | |
| 41 { | |
| 42 if (!m_observers.contains(observer)) | |
| 43 m_observers.add(observer); | |
| 44 } | |
| 45 | |
| 46 void SensorProxy::removeObserver(Observer* observer) | |
| 47 { | |
| 48 m_observers.remove(observer); | |
| 49 } | |
| 50 | |
| 51 void SensorProxy::initialize() | |
| 52 { | |
| 53 if (m_state != Uninitialized) | |
| 54 return; | |
| 55 | |
| 56 if (!m_provider->sensorProvider()) { | |
| 57 handleSensorError(); | |
| 58 return; | |
| 59 } | |
| 60 | |
| 61 m_state = Initializing; | |
| 62 auto callback = convertToBaseCallback(WTF::bind(&SensorProxy::onSensorCreate d, wrapWeakPersistent(this))); | |
| 63 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), c allback); | |
| 64 } | |
| 65 | |
| 66 void SensorProxy::addConfiguration(SensorConfigurationPtr configuration, std::un ique_ptr<Function<void(bool)>> callback) | |
| 67 { | |
| 68 DCHECK(isInitialized()); | |
| 69 m_sensor->AddConfiguration(std::move(configuration), convertToBaseCallback(s td::move(callback))); | |
| 70 } | |
| 71 | |
| 72 void SensorProxy::removeConfiguration(SensorConfigurationPtr configuration, std: :unique_ptr<Function<void(bool)>> callback) | |
| 73 { | |
| 74 DCHECK(isInitialized()); | |
| 75 m_sensor->RemoveConfiguration(std::move(configuration), convertToBaseCallbac k(std::move(callback))); | |
| 76 } | |
| 77 | |
| 78 void SensorProxy::updateInternalReading() | |
| 79 { | |
| 80 DCHECK(isInitialized()); | |
| 81 Reading* reading = static_cast<Reading*>(m_sharedBuffer.get()); | |
| 82 m_reading = *reading; | |
| 83 } | |
| 84 | |
| 85 void SensorProxy::RaiseError() | |
| 86 { | |
| 87 handleSensorError(); | |
| 88 } | |
| 89 | |
| 90 void SensorProxy::SensorReadingChanged() | |
| 91 { | |
| 92 for (Observer* observer : m_observers) | |
| 93 observer->onSensorReadingChanged(); | |
| 94 } | |
| 95 | |
| 96 void SensorProxy::handleSensorError() | |
| 97 { | |
| 98 m_state = Uninitialized; | |
| 99 m_sensor.reset(); | |
| 100 for (Observer* observer : m_observers) | |
| 101 observer->onSensorError(); | |
| 102 } | |
| 103 | |
| 104 void SensorProxy::onSensorCreated(SensorReadBufferPtr buffer, SensorClientReques t clientRequest) | |
| 105 { | |
| 106 DCHECK_EQ(Initializing, m_state); | |
| 107 if (!buffer) { | |
| 108 handleSensorError(); | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 DCHECK_EQ(0u, buffer->offset % SensorReadBuffer::kReadBufferSize); | |
| 113 | |
| 114 m_mode = buffer->mode; | |
| 115 | |
| 116 m_clientBinding.Bind(std::move(clientRequest)); | |
| 117 DCHECK(m_sensor.is_bound()); | |
| 118 m_sensor.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Senso rProxy::handleSensorError, wrapWeakPersistent(this)))); | |
|
haraken
2016/09/07 02:21:56
I'd call this immediately after setting m_sensor;
Mikhail
2016/09/07 07:55:15
Done.
| |
| 119 | |
| 120 m_sharedBufferHandle = std::move(buffer->memory); | |
| 121 DCHECK(!m_sharedBuffer); | |
| 122 m_sharedBuffer = m_sharedBufferHandle->MapAtOffset(buffer->offset, SensorRea dBuffer::kReadBufferSize); | |
| 123 | |
| 124 if (!m_sharedBuffer) { | |
| 125 handleSensorError(); | |
| 126 return; | |
| 127 } | |
| 128 | |
| 129 m_state = Initialized; | |
| 130 for (Observer* observer : m_observers) | |
| 131 observer->onSensorInitialized(); | |
| 132 } | |
| 133 | |
| 134 } // namespace blink | |
| OLD | NEW |