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/sensors/platform_sensor.h" |
| 6 |
| 7 #include <utility> |
| 8 |
| 9 #include "device/sensors/platform_sensor_configuration.h" |
| 10 #include "device/sensors/platform_sensor_provider.h" |
| 11 |
| 12 namespace device { |
| 13 |
| 14 PlatformSensor::PlatformSensor(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 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 (UpdateSensor(config_map_)) |
| 40 return true; |
| 41 |
| 42 auto iterator = std::find(config_list.begin(), config_list.end(), config); |
| 43 if (iterator != config_list.end()) |
| 44 config_list.erase(iterator); |
| 45 return false; |
| 46 } |
| 47 |
| 48 bool PlatformSensor::StopListening(Client* client, |
| 49 const PlatformSensorConfiguration& config) { |
| 50 DCHECK(clients_.HasObserver(client)); |
| 51 auto client_entry = config_map_.find(client); |
| 52 if (client_entry == config_map_.end()) |
| 53 return false; |
| 54 |
| 55 auto& config_list = client_entry->second; |
| 56 auto config_entry = std::find(config_list.begin(), config_list.end(), config); |
| 57 if (config_entry == config_list.end()) |
| 58 return false; |
| 59 |
| 60 config_list.erase(config_entry); |
| 61 |
| 62 return UpdateSensor(config_map_); |
| 63 } |
| 64 |
| 65 void PlatformSensor::AddClient(Client* client) { |
| 66 DCHECK(client); |
| 67 clients_.AddObserver(client); |
| 68 } |
| 69 |
| 70 void PlatformSensor::RemoveClient(Client* client) { |
| 71 DCHECK(client); |
| 72 clients_.RemoveObserver(client); |
| 73 auto client_entry = config_map_.find(client); |
| 74 if (client_entry != config_map_.end()) { |
| 75 config_map_.erase(client_entry); |
| 76 UpdateSensor(config_map_); |
| 77 } |
| 78 } |
| 79 |
| 80 void PlatformSensor::NotifySensorReadingChanged() { |
| 81 FOR_EACH_OBSERVER(Client, clients_, OnSensorReadingChanged()); |
| 82 } |
| 83 |
| 84 void PlatformSensor::NotifySensorError() { |
| 85 FOR_EACH_OBSERVER(Client, clients_, OnSensorError()); |
| 86 } |
| 87 |
| 88 } // namespace device |
OLD | NEW |