OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "device/generic_sensor/sensor_impl.h" | 5 #include "device/generic_sensor/sensor_impl.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "mojo/public/cpp/bindings/strong_binding.h" | 9 #include "mojo/public/cpp/bindings/strong_binding.h" |
10 | 10 |
11 namespace device { | 11 namespace device { |
12 | 12 |
13 SensorImpl::SensorImpl(scoped_refptr<PlatformSensor> sensor) | 13 SensorImpl::SensorImpl(scoped_refptr<PlatformSensor> sensor) |
14 : sensor_(std::move(sensor)), suspended_(false) { | 14 : sensor_(std::move(sensor)), |
| 15 suspended_(false), |
| 16 suppress_on_change_events_count_(0) { |
15 sensor_->AddClient(this); | 17 sensor_->AddClient(this); |
16 } | 18 } |
17 | 19 |
18 SensorImpl::~SensorImpl() { | 20 SensorImpl::~SensorImpl() { |
19 sensor_->RemoveClient(this); | 21 sensor_->RemoveClient(this); |
20 } | 22 } |
21 | 23 |
22 mojom::SensorClientRequest SensorImpl::GetClient() { | 24 mojom::SensorClientRequest SensorImpl::GetClient() { |
23 return mojo::MakeRequest(&client_); | 25 return mojo::MakeRequest(&client_); |
24 } | 26 } |
25 | 27 |
26 void SensorImpl::AddConfiguration( | 28 void SensorImpl::AddConfiguration( |
27 const PlatformSensorConfiguration& configuration, | 29 const PlatformSensorConfiguration& configuration, |
28 AddConfigurationCallback callback) { | 30 AddConfigurationCallback callback) { |
29 // TODO(Mikhail): To avoid overflowing browser by repeated AddConfigs | 31 // TODO(Mikhail): To avoid overflowing browser by repeated AddConfigs |
30 // (maybe limit the number of configs per client). | 32 // (maybe limit the number of configs per client). |
31 std::move(callback).Run(sensor_->StartListening(this, configuration)); | 33 bool success = sensor_->StartListening(this, configuration); |
| 34 if (success && configuration.suppress_on_change_events()) |
| 35 ++suppress_on_change_events_count_; |
| 36 std::move(callback).Run(success); |
32 } | 37 } |
33 | 38 |
34 void SensorImpl::GetDefaultConfiguration( | 39 void SensorImpl::GetDefaultConfiguration( |
35 GetDefaultConfigurationCallback callback) { | 40 GetDefaultConfigurationCallback callback) { |
36 std::move(callback).Run(sensor_->GetDefaultConfiguration()); | 41 std::move(callback).Run(sensor_->GetDefaultConfiguration()); |
37 } | 42 } |
38 | 43 |
39 void SensorImpl::RemoveConfiguration( | 44 void SensorImpl::RemoveConfiguration( |
40 const PlatformSensorConfiguration& configuration, | 45 const PlatformSensorConfiguration& configuration, |
41 RemoveConfigurationCallback callback) { | 46 RemoveConfigurationCallback callback) { |
42 std::move(callback).Run(sensor_->StopListening(this, configuration)); | 47 bool success = sensor_->StopListening(this, configuration); |
| 48 if (success && configuration.suppress_on_change_events()) |
| 49 --suppress_on_change_events_count_; |
| 50 std::move(callback).Run(success); |
43 } | 51 } |
44 | 52 |
45 void SensorImpl::Suspend() { | 53 void SensorImpl::Suspend() { |
46 suspended_ = true; | 54 suspended_ = true; |
47 sensor_->UpdateSensor(); | 55 sensor_->UpdateSensor(); |
48 } | 56 } |
49 | 57 |
50 void SensorImpl::Resume() { | 58 void SensorImpl::Resume() { |
51 suspended_ = false; | 59 suspended_ = false; |
52 sensor_->UpdateSensor(); | 60 sensor_->UpdateSensor(); |
53 } | 61 } |
54 | 62 |
55 void SensorImpl::OnSensorReadingChanged() { | 63 void SensorImpl::OnSensorReadingChanged() { |
56 DCHECK(!suspended_); | 64 DCHECK(!suspended_); |
57 if (client_) | 65 if (client_ && suppress_on_change_events_count_ == 0) |
58 client_->SensorReadingChanged(); | 66 client_->SensorReadingChanged(); |
59 } | 67 } |
60 | 68 |
61 void SensorImpl::OnSensorError() { | 69 void SensorImpl::OnSensorError() { |
62 DCHECK(!suspended_); | 70 DCHECK(!suspended_); |
63 if (client_) | 71 if (client_) |
64 client_->RaiseError(); | 72 client_->RaiseError(); |
65 } | 73 } |
66 | 74 |
67 bool SensorImpl::IsNotificationSuspended() { | 75 bool SensorImpl::IsNotificationSuspended() { |
68 return suspended_; | 76 return suspended_; |
69 } | 77 } |
70 | 78 |
71 } // namespace device | 79 } // namespace device |
OLD | NEW |