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; |
| 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 if (m_sensors.contains(sensor)) |
| 53 m_sensors.remove(sensor); |
| 54 } |
| 55 |
| 56 SensorProxy* SensorProviderProxy::getOrCreateSensor(device::mojom::blink::Sensor
Type type) |
| 57 { |
| 58 for (SensorProxy* sensor : m_sensors) { |
| 59 if (sensor->type() == type) |
| 60 return sensor; |
| 61 } |
| 62 SensorProxy* sensor = new SensorProxy(type, this); |
| 63 m_sensors.add(sensor); |
| 64 |
| 65 return sensor; |
| 66 } |
| 67 |
| 68 void SensorProviderProxy::handleSensorError() |
| 69 { |
| 70 SensorsSet copy(m_sensors); |
| 71 for (SensorProxy* sensor : copy) { |
| 72 // All sensors are invalidated. |
| 73 sensor->handleSensorError(); |
| 74 } |
| 75 DCHECK(m_sensors.isEmpty()); |
| 76 if (m_frame) |
| 77 m_frame->removeSupplement(s_supplementKey); |
| 78 } |
| 79 |
| 80 void SensorProviderProxy::pageVisibilityChanged() |
| 81 { |
| 82 if (!m_frame) |
| 83 return; |
| 84 |
| 85 Page* page = m_frame->document()->page(); |
| 86 DCHECK(page); |
| 87 |
| 88 if (page->visibilityState() != PageVisibilityStateVisible) { |
| 89 for (SensorProxy* sensor : m_sensors) |
| 90 sensor->suspendNotification(); |
| 91 } else { |
| 92 for (SensorProxy* sensor : m_sensors) |
| 93 sensor->resumeNotification(); |
| 94 } |
| 95 } |
| 96 |
| 97 } // namespace blink |
OLD | NEW |