Chromium Code Reviews| 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 namespace { | |
| 15 // Maximum allowed frequency is capped to 60Hz, if client requests higher | |
| 16 // sensor polling frequency, StartListening would return error. | |
| 17 bool IsAllowedFrequency(const PlatformSensorConfiguration& config) { | |
| 18 return config.frequency() <= 60.0 && config.frequency() > 0.0; | |
|
dcheng
2016/07/14 02:50:27
As mentioned in the other CL, can we move this che
| |
| 19 } | |
| 20 } | |
| 21 | |
| 22 PlatformSensor::PlatformSensor(SensorType type, | |
| 23 mojo::ScopedSharedBufferMapping mapping, | |
| 24 PlatformSensorProvider* provider) | |
| 25 : shared_buffer_mapping_(std::move(mapping)), | |
| 26 type_(type), | |
| 27 provider_(provider), | |
| 28 weak_factory_(this) {} | |
| 29 | |
| 30 PlatformSensor::~PlatformSensor() { | |
| 31 provider_->RemoveSensor(GetType()); | |
| 32 } | |
| 33 | |
| 34 SensorType PlatformSensor::GetType() const { | |
| 35 return type_; | |
| 36 } | |
| 37 | |
| 38 bool PlatformSensor::StartListening(Client* client, | |
| 39 const PlatformSensorConfiguration& config) { | |
| 40 DCHECK(clients_.HasObserver(client)); | |
| 41 if (!CheckSensorConfiguration(config) || !IsAllowedFrequency(config)) | |
| 42 return false; | |
| 43 | |
| 44 auto& config_list = config_map_[client]; | |
| 45 config_list.push_back(config); | |
| 46 | |
| 47 if (UpdateSensor(config_map_)) | |
| 48 return true; | |
| 49 | |
| 50 auto iterator = std::find(config_list.begin(), config_list.end(), config); | |
| 51 if (iterator != config_list.end()) | |
| 52 config_list.erase(iterator); | |
| 53 return false; | |
| 54 } | |
| 55 | |
| 56 bool PlatformSensor::StopListening(Client* client, | |
| 57 const PlatformSensorConfiguration& config) { | |
| 58 DCHECK(clients_.HasObserver(client)); | |
| 59 auto client_entry = config_map_.find(client); | |
| 60 if (client_entry == config_map_.end()) | |
| 61 return false; | |
| 62 | |
| 63 auto& config_list = client_entry->second; | |
| 64 auto config_entry = std::find(config_list.begin(), config_list.end(), config); | |
| 65 if (config_entry == config_list.end()) | |
| 66 return false; | |
| 67 | |
| 68 config_list.erase(config_entry); | |
| 69 | |
| 70 return UpdateSensor(config_map_); | |
| 71 } | |
| 72 | |
| 73 void PlatformSensor::AddClient(Client* client) { | |
| 74 DCHECK(client); | |
| 75 clients_.AddObserver(client); | |
| 76 } | |
| 77 | |
| 78 void PlatformSensor::RemoveClient(Client* client) { | |
| 79 DCHECK(client); | |
| 80 clients_.RemoveObserver(client); | |
| 81 auto client_entry = config_map_.find(client); | |
| 82 if (client_entry != config_map_.end()) { | |
| 83 config_map_.erase(client_entry); | |
| 84 UpdateSensor(config_map_); | |
| 85 } | |
| 86 } | |
| 87 | |
| 88 void PlatformSensor::NotifySensorReadingChanged() { | |
| 89 FOR_EACH_OBSERVER(Client, clients_, OnSensorReadingChanged()); | |
| 90 } | |
| 91 | |
| 92 void PlatformSensor::NotifySensorError() { | |
| 93 FOR_EACH_OBSERVER(Client, clients_, OnSensorError()); | |
| 94 } | |
| 95 | |
| 96 } // namespace device | |
| OLD | NEW |