Chromium Code Reviews| Index: device/generic_sensor/platform_sensor.cc |
| diff --git a/device/generic_sensor/platform_sensor.cc b/device/generic_sensor/platform_sensor.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..f53ce7386cb140f2699fdf70843ed83a29cdceb8 |
| --- /dev/null |
| +++ b/device/generic_sensor/platform_sensor.cc |
| @@ -0,0 +1,92 @@ |
| +// 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 "device/generic_sensor/platform_sensor.h" |
| + |
| +#include <utility> |
| + |
| +#include "device/generic_sensor/platform_sensor_configuration.h" |
| +#include "device/generic_sensor/platform_sensor_provider.h" |
| + |
| +namespace device { |
| + |
| +PlatformSensor::PlatformSensor(mojom::SensorType type, |
| + mojo::ScopedSharedBufferMapping mapping, |
| + PlatformSensorProvider* provider) |
| + : shared_buffer_mapping_(std::move(mapping)), |
| + type_(type), |
| + provider_(provider), |
| + weak_factory_(this) {} |
| + |
| +PlatformSensor::~PlatformSensor() { |
| + provider_->RemoveSensor(GetType()); |
| +} |
| + |
| +mojom::SensorType PlatformSensor::GetType() const { |
| + return type_; |
| +} |
| + |
| +bool PlatformSensor::StartListening(Client* client, |
| + const PlatformSensorConfiguration& config) { |
| + DCHECK(clients_.HasObserver(client)); |
| + if (!CheckSensorConfiguration(config)) |
| + return false; |
| + |
| + auto& config_list = config_map_[client]; |
| + config_list.push_back(config); |
| + |
| + if (UpdateSensor(config_map_)) |
| + return true; |
| + |
| + auto iterator = std::find(config_list.begin(), config_list.end(), config); |
| + if (iterator != config_list.end()) |
| + config_list.erase(iterator); |
|
Reilly Grant (use Gerrit)
2016/08/11 20:59:37
Can this be replaced by config_list.remove(config)
Mikhail
2016/08/12 10:21:48
Absolutely, thanks for the proposal!
|
| + return false; |
| +} |
| + |
| +bool PlatformSensor::StopListening(Client* client, |
| + const PlatformSensorConfiguration& config) { |
| + DCHECK(clients_.HasObserver(client)); |
| + auto client_entry = config_map_.find(client); |
| + if (client_entry == config_map_.end()) |
| + return false; |
| + |
| + auto& config_list = client_entry->second; |
| + auto config_entry = std::find(config_list.begin(), config_list.end(), config); |
| + if (config_entry == config_list.end()) |
| + return false; |
| + |
| + config_list.erase(config_entry); |
| + |
| + return UpdateSensor(config_map_); |
| +} |
| + |
| +void PlatformSensor::UpdateSensor() { |
| + UpdateSensor(config_map_); |
| +} |
| + |
| +void PlatformSensor::AddClient(Client* client) { |
| + DCHECK(client); |
| + clients_.AddObserver(client); |
| +} |
| + |
| +void PlatformSensor::RemoveClient(Client* client) { |
| + DCHECK(client); |
| + clients_.RemoveObserver(client); |
| + auto client_entry = config_map_.find(client); |
| + if (client_entry != config_map_.end()) { |
| + config_map_.erase(client_entry); |
| + UpdateSensor(config_map_); |
| + } |
| +} |
| + |
| +void PlatformSensor::NotifySensorReadingChanged() { |
| + FOR_EACH_OBSERVER(Client, clients_, OnSensorReadingChanged()); |
| +} |
| + |
| +void PlatformSensor::NotifySensorError() { |
| + FOR_EACH_OBSERVER(Client, clients_, OnSensorError()); |
| +} |
| + |
| +} // namespace device |