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 { | |
| 22 } | |
| 23 | |
| 24 SensorProxy::~SensorProxy() | |
| 25 { | |
| 26 } | |
| 27 | |
| 28 void SensorProxy::dispose() | |
| 29 { | |
| 30 m_clientBinding.Close(); | |
| 31 } | |
| 32 | |
| 33 DEFINE_TRACE(SensorProxy) | |
| 34 { | |
| 35 visitor->trace(m_observers); | |
| 36 visitor->trace(m_provider); | |
| 37 } | |
| 38 | |
| 39 void SensorProxy::addObserver(Observer* observer) | |
| 40 { | |
| 41 if (!m_observers.contains(observer)) | |
| 42 m_observers.add(observer); | |
| 43 } | |
| 44 | |
| 45 void SensorProxy::removeObserver(Observer* observer) | |
| 46 { | |
| 47 if (m_observers.contains(observer)) | |
|
timvolodine
2016/08/25 17:52:33
no need for contains
Mikhail
2016/08/26 16:42:42
Done.
| |
| 48 m_observers.remove(observer); | |
| 49 | |
| 50 if (m_observers.isEmpty()) | |
| 51 m_provider->removeSensor(this); | |
| 52 } | |
| 53 | |
| 54 void SensorProxy::initialize() | |
| 55 { | |
| 56 if (m_state != Uninitialized) | |
| 57 return; | |
| 58 m_state = Initializing; | |
| 59 auto callback = convertToBaseCallback(WTF::bind(&SensorProxy::onSensorCreate d, wrapWeakPersistent(this))); | |
| 60 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), c allback); | |
| 61 } | |
| 62 | |
| 63 void SensorProxy::addConfiguration(SensorConfigurationPtr configuration, std::un ique_ptr<Function<void(bool)>> callback) | |
| 64 { | |
| 65 DCHECK(isInitialized()); | |
| 66 m_sensor->AddConfiguration(std::move(configuration), convertToBaseCallback(s td::move(callback))); | |
| 67 } | |
| 68 | |
| 69 void SensorProxy::removeConfiguration(SensorConfigurationPtr configuration, std: :unique_ptr<Function<void(bool)>> callback) | |
| 70 { | |
| 71 DCHECK(isInitialized()); | |
| 72 m_sensor->RemoveConfiguration(std::move(configuration), convertToBaseCallbac k(std::move(callback))); | |
| 73 } | |
| 74 | |
| 75 void SensorProxy::suspend() | |
| 76 { | |
| 77 DCHECK(isInitialized()); | |
| 78 m_sensor->Suspend(); | |
| 79 } | |
| 80 | |
| 81 void SensorProxy::resume() | |
| 82 { | |
| 83 DCHECK(isInitialized()); | |
| 84 m_sensor->Resume(); | |
| 85 } | |
| 86 | |
| 87 void SensorProxy::RaiseError() | |
| 88 { | |
| 89 handleSensorError(); | |
| 90 } | |
| 91 | |
| 92 void SensorProxy::SensorReadingChanged() | |
| 93 { | |
| 94 for (Observer* observer : m_observers) | |
| 95 observer->onSensorReadingChanged(); | |
| 96 } | |
| 97 | |
| 98 void SensorProxy::handleSensorError() | |
| 99 { | |
| 100 m_state = Uninitialized; | |
| 101 | |
| 102 ObserversSet observersCopy(m_observers); | |
| 103 for (Observer* observer : observersCopy) | |
| 104 observer->onSensorError(); | |
| 105 | |
| 106 m_observers.clear(); | |
| 107 m_provider->removeSensor(this); | |
| 108 } | |
| 109 | |
| 110 void SensorProxy::onSensorCreated(SensorReadBufferPtr buffer, SensorClientReques t clientRequest) | |
| 111 { | |
| 112 if (!buffer) { | |
|
timvolodine
2016/08/25 17:52:32
DCHECK?
Mikhail
2016/08/26 16:42:42
(!buffer) just means that platform could not creat
timvolodine
2016/09/01 19:02:05
Would it be better to go through handleSensorError
Mikhail
2016/09/02 08:23:43
handleSensorError() is a connection_error_handler
| |
| 113 handleSensorError(); | |
| 114 return; | |
| 115 } | |
| 116 | |
| 117 if (buffer->offset % SensorReadBuffer::kReadBufferSize != 0) { | |
|
timvolodine
2016/08/25 17:52:32
DCHECK?
Mikhail
2016/08/26 16:42:42
we've NOTREACHED(); below + some exit logic for re
timvolodine
2016/09/01 19:02:06
still looks DCHECK to me.. in other words if the c
Mikhail
2016/09/02 08:23:43
Agreed. Done.
| |
| 118 NOTREACHED(); | |
| 119 handleSensorError(); | |
| 120 return; | |
| 121 } | |
| 122 | |
| 123 m_mode = buffer->mode; | |
| 124 | |
| 125 m_clientBinding.Bind(std::move(clientRequest)); | |
| 126 m_sensor.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Senso rProxy::handleSensorError, wrapWeakPersistent(this)))); | |
| 127 | |
| 128 m_sharedBufferHandle = std::move(buffer->memory); | |
| 129 DCHECK(!m_sharedBuffer); | |
| 130 m_sharedBuffer = m_sharedBufferHandle->MapAtOffset(buffer->offset, SensorRea dBuffer::kReadBufferSize); | |
| 131 | |
| 132 if (!m_sharedBuffer) { | |
| 133 handleSensorError(); | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 m_state = Initialized; | |
| 138 | |
| 139 for (Observer* observer : m_observers) | |
| 140 observer->onSensorInitialized(); | |
| 141 } | |
| 142 | |
| 143 } // namespace blink | |
| OLD | NEW |