| Index: third_party/WebKit/Source/modules/sensor/SensorProviderProxy.cpp
|
| diff --git a/third_party/WebKit/Source/modules/sensor/SensorProviderProxy.cpp b/third_party/WebKit/Source/modules/sensor/SensorProviderProxy.cpp
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..82a6abeaec9f334307935cd7c5d12ef7e1076b9f
|
| --- /dev/null
|
| +++ b/third_party/WebKit/Source/modules/sensor/SensorProviderProxy.cpp
|
| @@ -0,0 +1,97 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "modules/sensor/SensorProviderProxy.h"
|
| +
|
| +#include "core/frame/LocalFrame.h"
|
| +#include "modules/sensor/SensorProxy.h"
|
| +#include "platform/mojo/MojoHelper.h"
|
| +#include "public/platform/InterfaceProvider.h"
|
| +
|
| +using namespace device::mojom::blink;
|
| +
|
| +namespace blink {
|
| +
|
| +const char SensorProviderProxy::s_supplementKey[] = "SensorProvider";
|
| +
|
| +// SensorProviderProxy
|
| +SensorProviderProxy::SensorProviderProxy(LocalFrame* frame)
|
| + : PageVisibilityObserver(frame->document() ? frame->document()->page() : nullptr)
|
| + , m_frame(frame)
|
| +{
|
| + frame->interfaceProvider()->getInterface(mojo::GetProxy(&m_sensorProvider));
|
| + m_sensorProvider.set_connection_error_handler(convertToBaseCallback(WTF::bind(&SensorProviderProxy::handleSensorError, wrapWeakPersistent(this))));
|
| +}
|
| +
|
| +SensorProviderProxy* SensorProviderProxy::getOrCreateForFrame(LocalFrame* frame)
|
| +{
|
| + DCHECK(frame);
|
| + SensorProviderProxy* result = static_cast<SensorProviderProxy*>(Supplement<LocalFrame>::from(*frame, s_supplementKey));
|
| + if (!result) {
|
| + result = new SensorProviderProxy(frame);
|
| + Supplement<LocalFrame>::provideTo(*frame, s_supplementKey, result);
|
| + }
|
| + return result;
|
| +}
|
| +
|
| +SensorProviderProxy::~SensorProviderProxy()
|
| +{
|
| +}
|
| +
|
| +DEFINE_TRACE(SensorProviderProxy)
|
| +{
|
| + visitor->trace(m_sensors);
|
| + visitor->trace(m_frame);
|
| + PageVisibilityObserver::trace(visitor);
|
| + Supplement<LocalFrame>::trace(visitor);
|
| +}
|
| +
|
| +void SensorProviderProxy::removeSensor(SensorProxy* sensor)
|
| +{
|
| + if (m_sensors.contains(sensor))
|
| + m_sensors.remove(sensor);
|
| +}
|
| +
|
| +SensorProxy* SensorProviderProxy::getOrCreateSensor(device::mojom::blink::SensorType type)
|
| +{
|
| + for (SensorProxy* sensor : m_sensors) {
|
| + if (sensor->type() == type)
|
| + return sensor;
|
| + }
|
| + SensorProxy* sensor = new SensorProxy(type, this);
|
| + m_sensors.add(sensor);
|
| +
|
| + return sensor;
|
| +}
|
| +
|
| +void SensorProviderProxy::handleSensorError()
|
| +{
|
| + SensorsSet copy(m_sensors);
|
| + for (SensorProxy* sensor : copy) {
|
| + // All sensors are invalidated.
|
| + sensor->handleSensorError();
|
| + }
|
| + DCHECK(m_sensors.isEmpty());
|
| + if (m_frame)
|
| + m_frame->removeSupplement(s_supplementKey);
|
| +}
|
| +
|
| +void SensorProviderProxy::pageVisibilityChanged()
|
| +{
|
| + if (!m_frame)
|
| + return;
|
| +
|
| + Page* page = m_frame->document()->page();
|
| + DCHECK(page);
|
| +
|
| + if (page->visibilityState() != PageVisibilityStateVisible) {
|
| + for (SensorProxy* sensor : m_sensors)
|
| + sensor->suspendNotification();
|
| + } else {
|
| + for (SensorProxy* sensor : m_sensors)
|
| + sensor->resumeNotification();
|
| + }
|
| +}
|
| +
|
| +} // namespace blink
|
|
|