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