Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(469)

Side by Side Diff: device/generic_sensor/platform_sensor.cc

Issue 2865263002: Move //device/generic_sensor to be part of the internal implementation of the Device Service. (Closed)
Patch Set: code rebase Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/generic_sensor/platform_sensor.h"
6
7 #include <utility>
8
9 #include "base/threading/thread_task_runner_handle.h"
10 #include "device/generic_sensor/platform_sensor_provider.h"
11 #include "device/generic_sensor/public/cpp/platform_sensor_configuration.h"
12
13 namespace device {
14
15 PlatformSensor::PlatformSensor(mojom::SensorType type,
16 mojo::ScopedSharedBufferMapping mapping,
17 PlatformSensorProvider* provider)
18 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
19 shared_buffer_mapping_(std::move(mapping)),
20 type_(type),
21 provider_(provider),
22 weak_factory_(this) {}
23
24 PlatformSensor::~PlatformSensor() {
25 provider_->RemoveSensor(GetType());
26 }
27
28 mojom::SensorType PlatformSensor::GetType() const {
29 return type_;
30 }
31
32 double PlatformSensor::GetMaximumSupportedFrequency() {
33 return GetDefaultConfiguration().frequency();
34 }
35
36 double PlatformSensor::GetMinimumSupportedFrequency() {
37 return 1.0 / (60 * 60);
38 }
39
40 bool PlatformSensor::StartListening(Client* client,
41 const PlatformSensorConfiguration& config) {
42 DCHECK(clients_.HasObserver(client));
43 if (!CheckSensorConfiguration(config))
44 return false;
45
46 auto& config_list = config_map_[client];
47 config_list.push_back(config);
48
49 if (!UpdateSensorInternal(config_map_)) {
50 config_list.pop_back();
51 return false;
52 }
53
54 return true;
55 }
56
57 bool PlatformSensor::StopListening(Client* client,
58 const PlatformSensorConfiguration& config) {
59 DCHECK(clients_.HasObserver(client));
60 auto client_entry = config_map_.find(client);
61 if (client_entry == config_map_.end())
62 return false;
63
64 auto& config_list = client_entry->second;
65 auto config_entry = std::find(config_list.begin(), config_list.end(), config);
66 if (config_entry == config_list.end())
67 return false;
68
69 config_list.erase(config_entry);
70
71 return UpdateSensorInternal(config_map_);
72 }
73
74 void PlatformSensor::UpdateSensor() {
75 UpdateSensorInternal(config_map_);
76 }
77
78 void PlatformSensor::AddClient(Client* client) {
79 DCHECK(client);
80 clients_.AddObserver(client);
81 }
82
83 void PlatformSensor::RemoveClient(Client* client) {
84 DCHECK(client);
85 clients_.RemoveObserver(client);
86 auto client_entry = config_map_.find(client);
87 if (client_entry != config_map_.end()) {
88 config_map_.erase(client_entry);
89 UpdateSensorInternal(config_map_);
90 }
91 }
92
93 void PlatformSensor::UpdateSensorReading(const SensorReading& reading,
94 bool notify_clients) {
95 ReadingBuffer* buffer =
96 static_cast<ReadingBuffer*>(shared_buffer_mapping_.get());
97 auto& seqlock = buffer->seqlock.value();
98 seqlock.WriteBegin();
99 buffer->reading = reading;
100 seqlock.WriteEnd();
101
102 if (notify_clients)
103 task_runner_->PostTask(
104 FROM_HERE, base::Bind(&PlatformSensor::NotifySensorReadingChanged,
105 weak_factory_.GetWeakPtr()));
106 }
107
108 void PlatformSensor::NotifySensorReadingChanged() {
109 for (auto& client : clients_) {
110 if (!client.IsNotificationSuspended())
111 client.OnSensorReadingChanged();
112 }
113 }
114
115 void PlatformSensor::NotifySensorError() {
116 for (auto& observer : clients_)
117 observer.OnSensorError();
118 }
119
120 bool PlatformSensor::UpdateSensorInternal(const ConfigMap& configurations) {
121 const PlatformSensorConfiguration* optimal_configuration = nullptr;
122 for (const auto& pair : configurations) {
123 if (pair.first->IsNotificationSuspended())
124 continue;
125
126 const auto& conf_list = pair.second;
127 for (const auto& configuration : conf_list) {
128 if (!optimal_configuration || configuration > *optimal_configuration) {
129 optimal_configuration = &configuration;
130 }
131 }
132 }
133
134 if (!optimal_configuration) {
135 StopSensor();
136 return true;
137 }
138
139 return StartSensor(*optimal_configuration);
140 }
141
142 } // namespace device
OLDNEW
« no previous file with comments | « device/generic_sensor/platform_sensor.h ('k') | device/generic_sensor/platform_sensor_accelerometer_mac.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698