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 namespace blink { | |
| 13 | |
| 14 const char SensorProviderProxy::s_supplementKey[] = "SensorProvider"; | |
|
haraken
2016/09/06 05:04:28
Use supplementName() just like what other suppleme
| |
| 15 | |
| 16 // SensorProviderProxy | |
| 17 SensorProviderProxy::SensorProviderProxy(LocalFrame* frame) | |
| 18 : PageVisibilityObserver(frame->document() ? frame->document()->page() : nul lptr) | |
| 19 , m_frame(frame) | |
| 20 { | |
| 21 frame->interfaceProvider()->getInterface(mojo::GetProxy(&m_sensorProvider)); | |
| 22 m_sensorProvider.set_connection_error_handler(convertToBaseCallback(WTF::bin d(&SensorProviderProxy::handleSensorError, wrapWeakPersistent(this)))); | |
| 23 } | |
| 24 | |
| 25 SensorProviderProxy* SensorProviderProxy::getOrCreateForFrame(LocalFrame* frame) | |
| 26 { | |
| 27 DCHECK(frame); | |
| 28 SensorProviderProxy* result = static_cast<SensorProviderProxy*>(Supplement<L ocalFrame>::from(*frame, s_supplementKey)); | |
| 29 if (!result) { | |
| 30 result = new SensorProviderProxy(frame); | |
| 31 Supplement<LocalFrame>::provideTo(*frame, s_supplementKey, result); | |
| 32 } | |
| 33 return result; | |
| 34 } | |
| 35 | |
| 36 SensorProviderProxy::~SensorProviderProxy() | |
| 37 { | |
| 38 } | |
| 39 | |
| 40 DEFINE_TRACE(SensorProviderProxy) | |
| 41 { | |
| 42 visitor->trace(m_sensors); | |
| 43 visitor->trace(m_frame); | |
| 44 PageVisibilityObserver::trace(visitor); | |
| 45 Supplement<LocalFrame>::trace(visitor); | |
| 46 } | |
| 47 | |
| 48 void SensorProviderProxy::removeSensor(SensorProxy* sensor) | |
| 49 { | |
| 50 m_sensors.remove(sensor); | |
| 51 } | |
| 52 | |
| 53 SensorProxy* SensorProviderProxy::getOrCreateSensor(device::mojom::blink::Sensor Type type) | |
| 54 { | |
| 55 for (SensorProxy* sensor : m_sensors) { | |
| 56 // TODO(Mikhail) : Hash sensors by type for efficiency. | |
| 57 if (sensor->type() == type) | |
| 58 return sensor; | |
| 59 } | |
| 60 SensorProxy* sensor = new SensorProxy(type, this); | |
| 61 m_sensors.add(sensor); | |
| 62 | |
| 63 return sensor; | |
| 64 } | |
| 65 | |
| 66 void SensorProviderProxy::handleSensorError() | |
| 67 { | |
| 68 SensorsSet copy(m_sensors); | |
|
haraken
2016/09/06 05:04:29
Use copyToVector.
| |
| 69 for (SensorProxy* sensor : copy) { | |
| 70 // All sensors are invalidated. | |
| 71 sensor->handleSensorError(); | |
| 72 } | |
| 73 DCHECK(m_sensors.isEmpty()); | |
| 74 if (m_frame) | |
| 75 m_frame->removeSupplement(s_supplementKey); | |
|
haraken
2016/09/06 05:04:28
We normally don't remove supplement objects from t
| |
| 76 } | |
| 77 | |
| 78 void SensorProviderProxy::pageVisibilityChanged() | |
| 79 { | |
| 80 if (!m_frame) | |
| 81 return; | |
| 82 | |
| 83 Page* page = m_frame->document()->page(); | |
|
haraken
2016/09/06 05:04:29
Just use PageVisibilityObserver::page().
Mikhail
2016/09/06 07:13:58
indeed, thanks
| |
| 84 DCHECK(page); | |
| 85 | |
| 86 if (page->visibilityState() != PageVisibilityStateVisible) { | |
| 87 for (SensorProxy* sensor : m_sensors) | |
| 88 sensor->suspend(); | |
| 89 } else { | |
| 90 for (SensorProxy* sensor : m_sensors) | |
| 91 sensor->resume(); | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 } // namespace blink | |
| OLD | NEW |