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

Side by Side Diff: device/hid/hid_connection_mac.cc

Issue 2804313005: Make HidConnection::Read reentrancy safe (Closed)
Patch Set: Rebased. Created 3 years, 8 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/hid/hid_connection_linux.cc ('k') | device/hid/hid_connection_win.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2014 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/hid/hid_connection_mac.h" 5 #include "device/hid/hid_connection_mac.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/location.h" 8 #include "base/location.h"
9 #include "base/mac/foundation_util.h" 9 #include "base/mac/foundation_util.h"
10 #include "base/numerics/safe_math.h" 10 #include "base/numerics/safe_math.h"
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 buffer->data()[0] = 0; 145 buffer->data()[0] = 0;
146 memcpy(buffer->data() + 1, report_bytes, report_length); 146 memcpy(buffer->data() + 1, report_bytes, report_length);
147 } 147 }
148 148
149 connection->ProcessInputReport(buffer); 149 connection->ProcessInputReport(buffer);
150 } 150 }
151 151
152 void HidConnectionMac::ProcessInputReport( 152 void HidConnectionMac::ProcessInputReport(
153 scoped_refptr<net::IOBufferWithSize> buffer) { 153 scoped_refptr<net::IOBufferWithSize> buffer) {
154 DCHECK(thread_checker().CalledOnValidThread()); 154 DCHECK(thread_checker().CalledOnValidThread());
155 DCHECK_GE(buffer->size(), 1);
156
157 uint8_t report_id = buffer->data()[0];
158 if (IsReportIdProtected(report_id))
159 return;
160
155 PendingHidReport report; 161 PendingHidReport report;
156 report.buffer = buffer; 162 report.buffer = buffer;
157 report.size = buffer->size(); 163 report.size = buffer->size();
158 pending_reports_.push(report); 164 pending_reports_.push(report);
159 ProcessReadQueue(); 165 ProcessReadQueue();
160 } 166 }
161 167
162 void HidConnectionMac::ProcessReadQueue() { 168 void HidConnectionMac::ProcessReadQueue() {
163 DCHECK(thread_checker().CalledOnValidThread()); 169 DCHECK(thread_checker().CalledOnValidThread());
170
171 // Hold a reference to |this| to prevent a callback from freeing this object
172 // during the loop.
173 scoped_refptr<HidConnectionMac> self(this);
164 while (pending_reads_.size() && pending_reports_.size()) { 174 while (pending_reads_.size() && pending_reports_.size()) {
165 PendingHidRead read = pending_reads_.front(); 175 PendingHidRead read = pending_reads_.front();
166 PendingHidReport report = pending_reports_.front(); 176 PendingHidReport report = pending_reports_.front();
167 177
178 pending_reads_.pop();
168 pending_reports_.pop(); 179 pending_reports_.pop();
169 if (CompleteRead(report.buffer, report.size, read.callback)) { 180 read.callback.Run(true, report.buffer, report.size);
170 pending_reads_.pop();
171 }
172 } 181 }
173 } 182 }
174 183
175 void HidConnectionMac::GetFeatureReportAsync(uint8_t report_id, 184 void HidConnectionMac::GetFeatureReportAsync(uint8_t report_id,
176 const ReadCallback& callback) { 185 const ReadCallback& callback) {
177 scoped_refptr<net::IOBufferWithSize> buffer( 186 scoped_refptr<net::IOBufferWithSize> buffer(
178 new net::IOBufferWithSize(device_info()->max_feature_report_size() + 1)); 187 new net::IOBufferWithSize(device_info()->max_feature_report_size() + 1));
179 CFIndex report_size = buffer->size(); 188 CFIndex report_size = buffer->size();
180 189
181 // The IOHIDDevice object is shared with the UI thread and so this function 190 // The IOHIDDevice object is shared with the UI thread and so this function
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 this, 246 this,
238 base::Bind(callback, false))); 247 base::Bind(callback, false)));
239 } 248 }
240 } 249 }
241 250
242 void HidConnectionMac::ReturnAsyncResult(const base::Closure& callback) { 251 void HidConnectionMac::ReturnAsyncResult(const base::Closure& callback) {
243 callback.Run(); 252 callback.Run();
244 } 253 }
245 254
246 } // namespace device 255 } // namespace device
OLDNEW
« no previous file with comments | « device/hid/hid_connection_linux.cc ('k') | device/hid/hid_connection_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698