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

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

Issue 2144623003: [sensors] Introduce Generic Sensor API interfaces (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Comments from Reilly Created 4 years, 4 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 "device/generic_sensor/platform_sensor_configuration.h"
10 #include "device/generic_sensor/platform_sensor_provider.h"
11
12 namespace device {
13
14 PlatformSensor::PlatformSensor(mojom::SensorType type,
15 mojo::ScopedSharedBufferMapping mapping,
16 PlatformSensorProvider* provider)
17 : shared_buffer_mapping_(std::move(mapping)),
18 type_(type),
19 provider_(provider),
20 weak_factory_(this) {}
21
22 PlatformSensor::~PlatformSensor() {
23 provider_->RemoveSensor(GetType());
24 }
25
26 mojom::SensorType PlatformSensor::GetType() const {
27 return type_;
28 }
29
30 bool PlatformSensor::StartListening(Client* client,
31 const PlatformSensorConfiguration& config) {
32 DCHECK(clients_.HasObserver(client));
33 if (!CheckSensorConfiguration(config))
34 return false;
35
36 auto& config_list = config_map_[client];
37 config_list.push_back(config);
38
39 if (UpdateSensor(config_map_))
40 return true;
41
42 auto iterator = std::find(config_list.begin(), config_list.end(), config);
43 if (iterator != config_list.end())
44 config_list.erase(iterator);
Reilly Grant (use Gerrit) 2016/08/11 20:59:37 Can this be replaced by config_list.remove(config)
Mikhail 2016/08/12 10:21:48 Absolutely, thanks for the proposal!
45 return false;
46 }
47
48 bool PlatformSensor::StopListening(Client* client,
49 const PlatformSensorConfiguration& config) {
50 DCHECK(clients_.HasObserver(client));
51 auto client_entry = config_map_.find(client);
52 if (client_entry == config_map_.end())
53 return false;
54
55 auto& config_list = client_entry->second;
56 auto config_entry = std::find(config_list.begin(), config_list.end(), config);
57 if (config_entry == config_list.end())
58 return false;
59
60 config_list.erase(config_entry);
61
62 return UpdateSensor(config_map_);
63 }
64
65 void PlatformSensor::UpdateSensor() {
66 UpdateSensor(config_map_);
67 }
68
69 void PlatformSensor::AddClient(Client* client) {
70 DCHECK(client);
71 clients_.AddObserver(client);
72 }
73
74 void PlatformSensor::RemoveClient(Client* client) {
75 DCHECK(client);
76 clients_.RemoveObserver(client);
77 auto client_entry = config_map_.find(client);
78 if (client_entry != config_map_.end()) {
79 config_map_.erase(client_entry);
80 UpdateSensor(config_map_);
81 }
82 }
83
84 void PlatformSensor::NotifySensorReadingChanged() {
85 FOR_EACH_OBSERVER(Client, clients_, OnSensorReadingChanged());
86 }
87
88 void PlatformSensor::NotifySensorError() {
89 FOR_EACH_OBSERVER(Client, clients_, OnSensorError());
90 }
91
92 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698