blob: b2309114253196c350bd7826deba6e89434ab4e1 [file] [log] [blame]
// 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 "modules/sensor/SensorProxy.h"
#include "modules/sensor/SensorReading.h"
#include "platform/mojo/MojoHelper.h"
#include "public/platform/InterfaceProvider.h"
#include "public/platform/Platform.h"
namespace blink {
// SensorProviderProxy
SensorProviderProxy::SensorProviderProxy(LocalFrame* frame) {
frame->interfaceProvider()->getInterface(mojo::GetProxy(&m_sensorProvider));
m_sensorProvider.set_connection_error_handler(convertToBaseCallback(
WTF::bind(&SensorProviderProxy::onSensorProviderConnectionError,
wrapWeakPersistent(this))));
}
const char* SensorProviderProxy::supplementName() {
return "SensorProvider";
}
SensorProviderProxy* SensorProviderProxy::from(LocalFrame* frame) {
DCHECK(frame);
SensorProviderProxy* result = static_cast<SensorProviderProxy*>(
Supplement<LocalFrame>::from(*frame, supplementName()));
if (!result) {
result = new SensorProviderProxy(frame);
Supplement<LocalFrame>::provideTo(*frame, supplementName(), result);
}
return result;
}
SensorProviderProxy::~SensorProviderProxy() {}
DEFINE_TRACE(SensorProviderProxy) {
visitor->trace(m_sensorProxies);
Supplement<LocalFrame>::trace(visitor);
}
SensorProxy* SensorProviderProxy::createSensorProxy(
device::mojom::blink::SensorType type,
Page* page,
std::unique_ptr<SensorReadingFactory> readingFactory) {
DCHECK(!getSensorProxy(type));
SensorProxy* sensor =
new SensorProxy(type, this, page, std::move(readingFactory));
m_sensorProxies.add(sensor);
return sensor;
}
SensorProxy* SensorProviderProxy::getSensorProxy(
device::mojom::blink::SensorType type) {
for (SensorProxy* sensor : m_sensorProxies) {
// TODO(Mikhail) : Hash sensors by type for efficiency.
if (sensor->type() == type)
return sensor;
}
return nullptr;
}
void SensorProviderProxy::onSensorProviderConnectionError() {
if (!Platform::current()) {
// TODO(rockot): Clean this up once renderer shutdown sequence is fixed.
return;
}
m_sensorProvider.reset();
for (SensorProxy* sensor : m_sensorProxies)
sensor->handleSensorError();
}
} // namespace blink