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

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

Issue 2144623003: [sensors] Introduce Generic Sensor API interfaces (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Check that frequency is > 0 Created 4 years, 5 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
« no previous file with comments | « device/sensors/platform_sensor.h ('k') | device/sensors/platform_sensor_configuration.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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
OLDNEW
« no previous file with comments | « device/sensors/platform_sensor.h ('k') | device/sensors/platform_sensor_configuration.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698