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_provider_base.cc

Issue 2434073002: [sensors] PlatformSensorProvider implementations are notified when there are no sensor instances (Closed)
Patch Set: rebased Created 4 years, 1 month 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
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/platform_sensor_provider_base.h" 5 #include "device/generic_sensor/platform_sensor_provider_base.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h" 10 #include "device/generic_sensor/public/interfaces/sensor_provider.mojom.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 shared_buffer_handle_ = 72 shared_buffer_handle_ =
73 mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes); 73 mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes);
74 return shared_buffer_handle_.is_valid(); 74 return shared_buffer_handle_.is_valid();
75 } 75 }
76 76
77 void PlatformSensorProviderBase::RemoveSensor(mojom::SensorType type) { 77 void PlatformSensorProviderBase::RemoveSensor(mojom::SensorType type) {
78 DCHECK(CalledOnValidThread()); 78 DCHECK(CalledOnValidThread());
79 DCHECK(ContainsKey(sensor_map_, type)); 79 DCHECK(ContainsKey(sensor_map_, type));
80 sensor_map_.erase(type); 80 sensor_map_.erase(type);
81 81
82 if (sensor_map_.empty()) 82 if (sensor_map_.empty()) {
83 AllSensorsRemoved();
83 shared_buffer_handle_.reset(); 84 shared_buffer_handle_.reset();
85 }
84 } 86 }
85 87
86 mojo::ScopedSharedBufferHandle 88 mojo::ScopedSharedBufferHandle
87 PlatformSensorProviderBase::CloneSharedBufferHandle() { 89 PlatformSensorProviderBase::CloneSharedBufferHandle() {
88 DCHECK(CalledOnValidThread()); 90 DCHECK(CalledOnValidThread());
89 CreateSharedBufferIfNeeded(); 91 CreateSharedBufferIfNeeded();
90 return shared_buffer_handle_->Clone( 92 return shared_buffer_handle_->Clone(
91 mojo::SharedBufferHandle::AccessMode::READ_ONLY); 93 mojo::SharedBufferHandle::AccessMode::READ_ONLY);
92 } 94 }
93 95
96 bool PlatformSensorProviderBase::HasSensors() const {
97 DCHECK(CalledOnValidThread());
98 return !sensor_map_.empty();
99 }
100
94 void PlatformSensorProviderBase::NotifySensorCreated( 101 void PlatformSensorProviderBase::NotifySensorCreated(
95 mojom::SensorType type, 102 mojom::SensorType type,
96 scoped_refptr<PlatformSensor> sensor) { 103 scoped_refptr<PlatformSensor> sensor) {
97 DCHECK(CalledOnValidThread()); 104 DCHECK(CalledOnValidThread());
98 DCHECK(!ContainsKey(sensor_map_, type)); 105 DCHECK(!ContainsKey(sensor_map_, type));
99 DCHECK(ContainsKey(requests_map_, type)); 106 DCHECK(ContainsKey(requests_map_, type));
100 107
101 if (sensor) 108 if (sensor)
102 sensor_map_[type] = sensor.get(); 109 sensor_map_[type] = sensor.get();
103 110
104 // Inform subscribers about the sensor. 111 // Inform subscribers about the sensor.
105 // |sensor| can be nullptr here. 112 // |sensor| can be nullptr here.
106 auto it = requests_map_.find(type); 113 auto it = requests_map_.find(type);
107 for (auto& callback : it->second) 114 for (auto& callback : it->second)
108 callback.Run(sensor); 115 callback.Run(sensor);
109 116
110 requests_map_.erase(type); 117 requests_map_.erase(type);
111 } 118 }
112 119
113 } // namespace device 120 } // namespace device
OLDNEW
« no previous file with comments | « device/generic_sensor/platform_sensor_provider_base.h ('k') | device/generic_sensor/platform_sensor_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698