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 "device/generic_sensor/platform_sensor.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "device/generic_sensor/platform_sensor_configuration.h" |
| 10 #include "device/generic_sensor/platform_sensor_provider.h" |
| 11 |
| 12 namespace device { |
| 13 |
| 14 PlatformSensor::PlatformSensor(mojom::SensorType type, |
| 15 mojo::ScopedSharedBufferMapping mapping, |
| 16 PlatformSensorProvider* provider) |
| 17 : shared_buffer_mapping_(std::move(mapping)), |
| 18 type_(type), |
| 19 provider_(provider), |
| 20 weak_factory_(this) {} |
| 21 |
| 22 PlatformSensor::~PlatformSensor() { |
| 23 provider_->RemoveSensor(GetType()); |
| 24 } |
| 25 |
| 26 mojom::SensorType PlatformSensor::GetType() const { |
| 27 return type_; |
| 28 } |
| 29 |
| 30 bool PlatformSensor::StartListening(Client* client, |
| 31 const PlatformSensorConfiguration& config) { |
| 32 DCHECK(clients_.HasObserver(client)); |
| 33 if (!CheckSensorConfiguration(config)) |
| 34 return false; |
| 35 |
| 36 auto& config_list = config_map_[client]; |
| 37 config_list.push_back(config); |
| 38 |
| 39 if (!UpdateSensorInternal(config_map_)) { |
| 40 config_list.pop_back(); |
| 41 return false; |
| 42 } |
| 43 |
| 44 return true; |
| 45 } |
| 46 |
| 47 bool PlatformSensor::StopListening(Client* client, |
| 48 const PlatformSensorConfiguration& config) { |
| 49 DCHECK(clients_.HasObserver(client)); |
| 50 auto client_entry = config_map_.find(client); |
| 51 if (client_entry == config_map_.end()) |
| 52 return false; |
| 53 |
| 54 auto& config_list = client_entry->second; |
| 55 auto config_entry = std::find(config_list.begin(), config_list.end(), config); |
| 56 if (config_entry == config_list.end()) |
| 57 return false; |
| 58 |
| 59 config_list.erase(config_entry); |
| 60 |
| 61 return UpdateSensorInternal(config_map_); |
| 62 } |
| 63 |
| 64 void PlatformSensor::UpdateSensor() { |
| 65 UpdateSensorInternal(config_map_); |
| 66 } |
| 67 |
| 68 void PlatformSensor::AddClient(Client* client) { |
| 69 DCHECK(client); |
| 70 clients_.AddObserver(client); |
| 71 } |
| 72 |
| 73 void PlatformSensor::RemoveClient(Client* client) { |
| 74 DCHECK(client); |
| 75 clients_.RemoveObserver(client); |
| 76 auto client_entry = config_map_.find(client); |
| 77 if (client_entry != config_map_.end()) { |
| 78 config_map_.erase(client_entry); |
| 79 UpdateSensorInternal(config_map_); |
| 80 } |
| 81 } |
| 82 |
| 83 void PlatformSensor::NotifySensorReadingChanged() { |
| 84 FOR_EACH_OBSERVER(Client, clients_, OnSensorReadingChanged()); |
| 85 } |
| 86 |
| 87 void PlatformSensor::NotifySensorError() { |
| 88 FOR_EACH_OBSERVER(Client, clients_, OnSensorError()); |
| 89 } |
| 90 |
| 91 } // namespace device |
OLD | NEW |