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/SensorProviderProxy.h" | |
| 6 | |
| 7 #include "core/frame/LocalFrame.h" | |
| 8 #include "modules/sensor/SensorProxy.h" | |
| 9 #include "platform/mojo/MojoHelper.h" | |
| 10 #include "public/platform/InterfaceProvider.h" | |
| 11 | |
| 12 using namespace device::mojom::blink; | |
|
riju_
2016/08/31 10:35:49
maybe remove this for 1 usage in getOrCreateSensor
Mikhail
2016/08/31 12:01:07
Done.
| |
| 13 | |
| 14 namespace blink { | |
| 15 | |
| 16 const char SensorProviderProxy::s_supplementKey[] = "SensorProvider"; | |
| 17 | |
| 18 // SensorProviderProxy | |
| 19 SensorProviderProxy::SensorProviderProxy(LocalFrame* frame) | |
| 20 : PageVisibilityObserver(frame->document() ? frame->document()->page() : nul lptr) | |
| 21 , m_frame(frame) | |
| 22 { | |
| 23 frame->interfaceProvider()->getInterface(mojo::GetProxy(&m_sensorProvider)); | |
| 24 m_sensorProvider.set_connection_error_handler(convertToBaseCallback(WTF::bin d(&SensorProviderProxy::handleSensorError, wrapWeakPersistent(this)))); | |
| 25 } | |
| 26 | |
| 27 SensorProviderProxy* SensorProviderProxy::getOrCreateForFrame(LocalFrame* frame) | |
| 28 { | |
| 29 DCHECK(frame); | |
| 30 SensorProviderProxy* result = static_cast<SensorProviderProxy*>(Supplement<L ocalFrame>::from(*frame, s_supplementKey)); | |
| 31 if (!result) { | |
| 32 result = new SensorProviderProxy(frame); | |
| 33 Supplement<LocalFrame>::provideTo(*frame, s_supplementKey, result); | |
| 34 } | |
| 35 return result; | |
| 36 } | |
| 37 | |
| 38 SensorProviderProxy::~SensorProviderProxy() | |
| 39 { | |
| 40 } | |
| 41 | |
| 42 DEFINE_TRACE(SensorProviderProxy) | |
| 43 { | |
| 44 visitor->trace(m_sensors); | |
| 45 visitor->trace(m_frame); | |
| 46 PageVisibilityObserver::trace(visitor); | |
| 47 Supplement<LocalFrame>::trace(visitor); | |
| 48 } | |
| 49 | |
| 50 void SensorProviderProxy::removeSensor(SensorProxy* sensor) | |
| 51 { | |
| 52 m_sensors.remove(sensor); | |
| 53 } | |
| 54 | |
| 55 SensorProxy* SensorProviderProxy::getOrCreateSensor(device::mojom::blink::Sensor Type type) | |
| 56 { | |
| 57 for (SensorProxy* sensor : m_sensors) { | |
| 58 if (sensor->type() == type) | |
| 59 return sensor; | |
| 60 } | |
| 61 SensorProxy* sensor = new SensorProxy(type, this); | |
| 62 m_sensors.add(sensor); | |
| 63 | |
| 64 return sensor; | |
| 65 } | |
| 66 | |
| 67 void SensorProviderProxy::handleSensorError() | |
| 68 { | |
| 69 SensorsSet copy(m_sensors); | |
| 70 for (SensorProxy* sensor : copy) { | |
| 71 // All sensors are invalidated. | |
| 72 sensor->handleSensorError(); | |
| 73 } | |
| 74 DCHECK(m_sensors.isEmpty()); | |
| 75 if (m_frame) | |
| 76 m_frame->removeSupplement(s_supplementKey); | |
| 77 } | |
| 78 | |
| 79 void SensorProviderProxy::pageVisibilityChanged() | |
| 80 { | |
| 81 if (!m_frame) | |
| 82 return; | |
| 83 | |
| 84 Page* page = m_frame->document()->page(); | |
| 85 DCHECK(page); | |
| 86 | |
| 87 if (page->visibilityState() != PageVisibilityStateVisible) { | |
| 88 for (SensorProxy* sensor : m_sensors) | |
| 89 sensor->suspend(); | |
| 90 } else { | |
| 91 for (SensorProxy* sensor : m_sensors) | |
| 92 sensor->resume(); | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 } // namespace blink | |
| OLD | NEW |