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

Side by Side Diff: chrome/browser/extensions/api/usb/usb_device_resource.cc

Issue 10826273: Allocate a one-byte IOBuffer for transfers that are zero-length. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: "impedance". Created 8 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/usb/usb_device_resource.h" 5 #include "chrome/browser/extensions/api/usb/usb_device_resource.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 *output = input.data->size(); 104 *output = input.data->size();
105 return true; 105 return true;
106 } 106 }
107 } 107 }
108 return false; 108 return false;
109 } 109 }
110 110
111 template<class T> 111 template<class T>
112 static scoped_refptr<net::IOBuffer> CreateBufferForTransfer(const T& input) { 112 static scoped_refptr<net::IOBuffer> CreateBufferForTransfer(const T& input) {
113 size_t size = 0; 113 size_t size = 0;
114 if (!GetTransferSize(input, &size)) { 114 if (!GetTransferSize(input, &size))
115 return NULL; 115 return NULL;
116 }
117 116
118 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(size); 117 // Allocate a |size|-bytes buffer, or a one-byte buffer if |size| is 0. This
119 if (!input.data.get()) { 118 // is due to an impedance mismatch between IOBuffer and URBs. An IOBuffer
119 // cannot represent a zero-length buffer, while an URB can.
120 scoped_refptr<net::IOBuffer> buffer = new net::IOBuffer(std::max(
121 static_cast<size_t>(1), size));
122 if (!input.data.get())
120 return buffer; 123 return buffer;
121 }
122 124
123 memcpy(buffer->data(), input.data->data(), size); 125 memcpy(buffer->data(), input.data->data(), size);
124
125 return buffer; 126 return buffer;
126 } 127 }
127 128
128 static const char* ConvertTransferStatusToErrorString( 129 static const char* ConvertTransferStatusToErrorString(
129 const UsbTransferStatus status) { 130 const UsbTransferStatus status) {
130 switch (status) { 131 switch (status) {
131 case USB_TRANSFER_COMPLETED: 132 case USB_TRANSFER_COMPLETED:
132 return ""; 133 return "";
133 case USB_TRANSFER_ERROR: 134 case USB_TRANSFER_ERROR:
134 return kErrorGeneric; 135 return kErrorGeneric;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 UsbTransferStatus status) { 238 UsbTransferStatus status) {
238 if (buffer) { 239 if (buffer) {
239 base::BinaryValue* const response_buffer = 240 base::BinaryValue* const response_buffer =
240 base::BinaryValue::CreateWithCopiedBuffer(buffer->data(), length); 241 base::BinaryValue::CreateWithCopiedBuffer(buffer->data(), length);
241 event_notifier()->OnTransferComplete(status, 242 event_notifier()->OnTransferComplete(status,
242 ConvertTransferStatusToErrorString(status), response_buffer); 243 ConvertTransferStatusToErrorString(status), response_buffer);
243 } 244 }
244 } 245 }
245 246
246 } // namespace extensions 247 } // namespace extensions
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698