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

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

Issue 2878653002: Remove base::NonThreadSafe from //device/generic_sensor/ (Closed)
Patch Set: Remove base::NonThreadSafe from //device/generic_sensor/ 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
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"
11 11
12 namespace device { 12 namespace device {
13 13
14 namespace { 14 namespace {
15 15
16 const uint64_t kReadingBufferSize = sizeof(SensorReadingSharedBuffer); 16 const uint64_t kReadingBufferSize = sizeof(SensorReadingSharedBuffer);
17 const uint64_t kSharedBufferSizeInBytes = 17 const uint64_t kSharedBufferSizeInBytes =
18 kReadingBufferSize * static_cast<uint64_t>(mojom::SensorType::LAST); 18 kReadingBufferSize * static_cast<uint64_t>(mojom::SensorType::LAST);
19 19
20 } // namespace 20 } // namespace
21 21
22 PlatformSensorProviderBase::PlatformSensorProviderBase() = default; 22 PlatformSensorProviderBase::PlatformSensorProviderBase() {
23 PlatformSensorProviderBase::~PlatformSensorProviderBase() = default; 23 DETACH_FROM_THREAD(thread_checker_);
Reilly Grant (use Gerrit) 2017/05/15 15:23:35 No need to add a DETACH_FROM_THREAD here.
ke.he 2017/05/16 03:09:22 Done.
24 }
25
26 PlatformSensorProviderBase::~PlatformSensorProviderBase() {
27 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
28 }
24 29
25 void PlatformSensorProviderBase::CreateSensor( 30 void PlatformSensorProviderBase::CreateSensor(
26 mojom::SensorType type, 31 mojom::SensorType type,
27 const CreateSensorCallback& callback) { 32 const CreateSensorCallback& callback) {
28 DCHECK(CalledOnValidThread()); 33 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
29 34
30 if (!CreateSharedBufferIfNeeded()) { 35 if (!CreateSharedBufferIfNeeded()) {
31 callback.Run(nullptr); 36 callback.Run(nullptr);
32 return; 37 return;
33 } 38 }
34 39
35 mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type); 40 mojo::ScopedSharedBufferMapping mapping = MapSharedBufferForType(type);
36 if (!mapping) { 41 if (!mapping) {
37 callback.Run(nullptr); 42 callback.Run(nullptr);
38 return; 43 return;
39 } 44 }
40 45
41 auto it = requests_map_.find(type); 46 auto it = requests_map_.find(type);
42 if (it != requests_map_.end()) { 47 if (it != requests_map_.end()) {
43 it->second.push_back(callback); 48 it->second.push_back(callback);
44 } else { // This is the first CreateSensor call. 49 } else { // This is the first CreateSensor call.
45 requests_map_[type] = CallbackQueue({callback}); 50 requests_map_[type] = CallbackQueue({callback});
46 51
47 CreateSensorInternal( 52 CreateSensorInternal(
48 type, std::move(mapping), 53 type, std::move(mapping),
49 base::Bind(&PlatformSensorProviderBase::NotifySensorCreated, 54 base::Bind(&PlatformSensorProviderBase::NotifySensorCreated,
50 base::Unretained(this), type)); 55 base::Unretained(this), type));
51 } 56 }
52 } 57 }
53 58
54 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor( 59 scoped_refptr<PlatformSensor> PlatformSensorProviderBase::GetSensor(
55 mojom::SensorType type) { 60 mojom::SensorType type) {
56 DCHECK(CalledOnValidThread()); 61 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
57 62
58 auto it = sensor_map_.find(type); 63 auto it = sensor_map_.find(type);
59 if (it != sensor_map_.end()) 64 if (it != sensor_map_.end())
60 return it->second; 65 return it->second;
61 return nullptr; 66 return nullptr;
62 } 67 }
63 68
64 bool PlatformSensorProviderBase::CreateSharedBufferIfNeeded() { 69 bool PlatformSensorProviderBase::CreateSharedBufferIfNeeded() {
65 DCHECK(CalledOnValidThread()); 70 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
66 if (shared_buffer_handle_.is_valid()) 71 if (shared_buffer_handle_.is_valid())
67 return true; 72 return true;
68 73
69 shared_buffer_handle_ = 74 shared_buffer_handle_ =
70 mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes); 75 mojo::SharedBufferHandle::Create(kSharedBufferSizeInBytes);
71 return shared_buffer_handle_.is_valid(); 76 return shared_buffer_handle_.is_valid();
72 } 77 }
73 78
74 void PlatformSensorProviderBase::RemoveSensor(mojom::SensorType type) { 79 void PlatformSensorProviderBase::RemoveSensor(mojom::SensorType type) {
75 DCHECK(CalledOnValidThread()); 80 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
76 DCHECK(ContainsKey(sensor_map_, type)); 81 DCHECK(ContainsKey(sensor_map_, type));
77 sensor_map_.erase(type); 82 sensor_map_.erase(type);
78 83
79 if (sensor_map_.empty()) { 84 if (sensor_map_.empty()) {
80 AllSensorsRemoved(); 85 AllSensorsRemoved();
81 shared_buffer_handle_.reset(); 86 shared_buffer_handle_.reset();
82 } 87 }
83 } 88 }
84 89
85 mojo::ScopedSharedBufferHandle 90 mojo::ScopedSharedBufferHandle
86 PlatformSensorProviderBase::CloneSharedBufferHandle() { 91 PlatformSensorProviderBase::CloneSharedBufferHandle() {
87 DCHECK(CalledOnValidThread()); 92 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
88 CreateSharedBufferIfNeeded(); 93 CreateSharedBufferIfNeeded();
89 return shared_buffer_handle_->Clone( 94 return shared_buffer_handle_->Clone(
90 mojo::SharedBufferHandle::AccessMode::READ_ONLY); 95 mojo::SharedBufferHandle::AccessMode::READ_ONLY);
91 } 96 }
92 97
93 bool PlatformSensorProviderBase::HasSensors() const { 98 bool PlatformSensorProviderBase::HasSensors() const {
94 DCHECK(CalledOnValidThread()); 99 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
95 return !sensor_map_.empty(); 100 return !sensor_map_.empty();
96 } 101 }
97 102
98 void PlatformSensorProviderBase::NotifySensorCreated( 103 void PlatformSensorProviderBase::NotifySensorCreated(
99 mojom::SensorType type, 104 mojom::SensorType type,
100 scoped_refptr<PlatformSensor> sensor) { 105 scoped_refptr<PlatformSensor> sensor) {
101 DCHECK(CalledOnValidThread()); 106 DCHECK_CALLED_ON_VALID_THREAD(thread_checker_);
102 DCHECK(!ContainsKey(sensor_map_, type)); 107 DCHECK(!ContainsKey(sensor_map_, type));
103 DCHECK(ContainsKey(requests_map_, type)); 108 DCHECK(ContainsKey(requests_map_, type));
104 109
105 if (sensor) 110 if (sensor)
106 sensor_map_[type] = sensor.get(); 111 sensor_map_[type] = sensor.get();
107 112
108 // Inform subscribers about the sensor. 113 // Inform subscribers about the sensor.
109 // |sensor| can be nullptr here. 114 // |sensor| can be nullptr here.
110 auto it = requests_map_.find(type); 115 auto it = requests_map_.find(type);
111 for (auto& callback : it->second) 116 for (auto& callback : it->second)
(...skipping 12 matching lines...) Expand all
124 129
125 mojo::ScopedSharedBufferMapping 130 mojo::ScopedSharedBufferMapping
126 PlatformSensorProviderBase::MapSharedBufferForType(mojom::SensorType type) { 131 PlatformSensorProviderBase::MapSharedBufferForType(mojom::SensorType type) {
127 mojo::ScopedSharedBufferMapping mapping = shared_buffer_handle_->MapAtOffset( 132 mojo::ScopedSharedBufferMapping mapping = shared_buffer_handle_->MapAtOffset(
128 kReadingBufferSize, SensorReadingSharedBuffer::GetOffset(type)); 133 kReadingBufferSize, SensorReadingSharedBuffer::GetOffset(type));
129 memset(mapping.get(), 0, kReadingBufferSize); 134 memset(mapping.get(), 0, kReadingBufferSize);
130 return mapping; 135 return mapping;
131 } 136 }
132 137
133 } // namespace device 138 } // namespace device
OLDNEW
« no previous file with comments | « device/generic_sensor/platform_sensor_provider_base.h ('k') | device/generic_sensor/platform_sensor_provider_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698