sensor_impl.cc 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include "services/device/generic_sensor/sensor_impl.h"
  5. #include <utility>
  6. namespace device {
  7. SensorImpl::SensorImpl(scoped_refptr<PlatformSensor> sensor)
  8. : sensor_(std::move(sensor)),
  9. reading_notification_enabled_(true),
  10. suspended_(false) {
  11. sensor_->AddClient(this);
  12. }
  13. SensorImpl::~SensorImpl() {
  14. sensor_->RemoveClient(this);
  15. }
  16. mojo::PendingReceiver<mojom::SensorClient> SensorImpl::GetClient() {
  17. return client_.BindNewPipeAndPassReceiver();
  18. }
  19. void SensorImpl::AddConfiguration(
  20. const PlatformSensorConfiguration& configuration,
  21. AddConfigurationCallback callback) {
  22. // TODO(Mikhail): To avoid overflowing browser by repeated AddConfigs
  23. // (maybe limit the number of configs per client).
  24. std::move(callback).Run(sensor_->StartListening(this, configuration));
  25. }
  26. void SensorImpl::GetDefaultConfiguration(
  27. GetDefaultConfigurationCallback callback) {
  28. std::move(callback).Run(sensor_->GetDefaultConfiguration());
  29. }
  30. void SensorImpl::RemoveConfiguration(
  31. const PlatformSensorConfiguration& configuration) {
  32. sensor_->StopListening(this, configuration);
  33. }
  34. void SensorImpl::Suspend() {
  35. suspended_ = true;
  36. sensor_->UpdateSensor();
  37. }
  38. void SensorImpl::Resume() {
  39. suspended_ = false;
  40. sensor_->UpdateSensor();
  41. }
  42. void SensorImpl::ConfigureReadingChangeNotifications(bool enabled) {
  43. reading_notification_enabled_ = enabled;
  44. }
  45. void SensorImpl::OnSensorReadingChanged(mojom::SensorType type) {
  46. DCHECK(!suspended_);
  47. if (client_ && reading_notification_enabled_ &&
  48. sensor_->GetReportingMode() == mojom::ReportingMode::ON_CHANGE) {
  49. client_->SensorReadingChanged();
  50. }
  51. }
  52. void SensorImpl::OnSensorError() {
  53. DCHECK(!suspended_);
  54. if (client_)
  55. client_->RaiseError();
  56. }
  57. bool SensorImpl::IsSuspended() {
  58. return suspended_;
  59. }
  60. } // namespace device