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

Side by Side Diff: ppapi/proxy/file_io_resource.cc

Issue 27730003: Avoid memory allocation for PPB_FileIO Read when callback is blocking. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: No mallocs for Query blocking. Created 7 years, 2 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 "ppapi/proxy/file_io_resource.h" 5 #include "ppapi/proxy/file_io_resource.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/task_runner_util.h" 8 #include "base/task_runner_util.h"
9 #include "ipc/ipc_message.h" 9 #include "ipc/ipc_message.h"
10 #include "ppapi/c/pp_errors.h" 10 #include "ppapi/c/pp_errors.h"
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 int32_t rv = state_manager_.CheckOperationState( 131 int32_t rv = state_manager_.CheckOperationState(
132 FileIOStateManager::OPERATION_EXCLUSIVE, true); 132 FileIOStateManager::OPERATION_EXCLUSIVE, true);
133 if (rv != PP_OK) 133 if (rv != PP_OK)
134 return rv; 134 return rv;
135 if (!info) 135 if (!info)
136 return PP_ERROR_BADARGUMENT; 136 return PP_ERROR_BADARGUMENT;
137 if (!FileHandleHolder::IsValid(file_handle_)) 137 if (!FileHandleHolder::IsValid(file_handle_))
138 return PP_ERROR_FAILED; 138 return PP_ERROR_FAILED;
139 139
140 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE); 140 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_EXCLUSIVE);
141 scoped_refptr<QueryOp> query_op(new QueryOp(file_handle_));
142 141
143 // If the callback is blocking, perform the task on the calling thread. 142 // If the callback is blocking, perform the task on the calling thread.
144 if (callback->is_blocking()) { 143 if (callback->is_blocking()) {
145 int32_t result; 144 int32_t result = PP_ERROR_FAILED;
145 base::PlatformFileInfo file_info;
146 // The plugin could release its reference to this instance when we release
147 // the proxy lock below.
148 scoped_refptr<FileIOResource> protect(this);
146 { 149 {
147 // Release the proxy lock while making a potentially slow file call. 150 // Release the proxy lock while making a potentially slow file call.
148 ProxyAutoUnlock unlock; 151 ProxyAutoUnlock unlock;
149 result = query_op->DoWork(); 152 if (base::GetPlatformFileInfo(file_handle_->raw_handle(), &file_info))
153 result = PP_OK;
150 } 154 }
151 return OnQueryComplete(query_op, info, result); 155 if (result == PP_OK) {
156 // This writes the file info into the plugin's PP_FileInfo struct.
157 ppapi::PlatformFileInfoToPepperFileInfo(file_info,
158 file_system_type_,
159 info);
160 }
161 state_manager_.SetOperationFinished();
162 return result;
152 } 163 }
153 164
154 // For the non-blocking case, post a task to the file thread and add a 165 // For the non-blocking case, post a task to the file thread and add a
155 // completion task to write the result. 166 // completion task to write the result.
167 scoped_refptr<QueryOp> query_op(new QueryOp(file_handle_));
156 base::PostTaskAndReplyWithResult( 168 base::PostTaskAndReplyWithResult(
157 PpapiGlobals::Get()->GetFileTaskRunner(), 169 PpapiGlobals::Get()->GetFileTaskRunner(),
158 FROM_HERE, 170 FROM_HERE,
159 Bind(&FileIOResource::QueryOp::DoWork, query_op), 171 Bind(&FileIOResource::QueryOp::DoWork, query_op),
160 RunWhileLocked(Bind(&TrackedCallback::Run, callback))); 172 RunWhileLocked(Bind(&TrackedCallback::Run, callback)));
161 callback->set_completion_task( 173 callback->set_completion_task(
162 Bind(&FileIOResource::OnQueryComplete, this, query_op, info)); 174 Bind(&FileIOResource::OnQueryComplete, this, query_op, info));
163 175
164 return PP_OK_COMPLETIONPENDING; 176 return PP_OK_COMPLETIONPENDING;
165 } 177 }
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 const PP_ArrayOutput& array_output, 321 const PP_ArrayOutput& array_output,
310 scoped_refptr<TrackedCallback> callback) { 322 scoped_refptr<TrackedCallback> callback) {
311 if (bytes_to_read < 0) 323 if (bytes_to_read < 0)
312 return PP_ERROR_FAILED; 324 return PP_ERROR_FAILED;
313 if (!FileHandleHolder::IsValid(file_handle_)) 325 if (!FileHandleHolder::IsValid(file_handle_))
314 return PP_ERROR_FAILED; 326 return PP_ERROR_FAILED;
315 327
316 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ); 328 state_manager_.SetPendingOperation(FileIOStateManager::OPERATION_READ);
317 329
318 bytes_to_read = std::min(bytes_to_read, kMaxReadSize); 330 bytes_to_read = std::min(bytes_to_read, kMaxReadSize);
319 scoped_refptr<ReadOp> read_op(
320 new ReadOp(file_handle_, offset, bytes_to_read));
321 if (callback->is_blocking()) { 331 if (callback->is_blocking()) {
322 int32_t result; 332 char* buffer = static_cast<char*>(
323 { 333 array_output.GetDataBuffer(array_output.user_data, bytes_to_read, 1));
dmichael (off chromium) 2013/10/22 18:02:58 I had to read a bunch of code to convince myself w
bbudge 2013/10/22 20:51:04 Done.
334 int32_t result = PP_ERROR_FAILED;
335 // The plugin could release its reference to this instance when we release
336 // the proxy lock below.
337 scoped_refptr<FileIOResource> protect(this);
338 if (buffer) {
324 // Release the proxy lock while making a potentially slow file call. 339 // Release the proxy lock while making a potentially slow file call.
325 ProxyAutoUnlock unlock; 340 ProxyAutoUnlock unlock;
326 result = read_op->DoWork(); 341 result = base::ReadPlatformFile(
342 file_handle_->raw_handle(), offset, buffer, bytes_to_read);
343 if (result < 0)
344 result = PP_ERROR_FAILED;
327 } 345 }
328 return OnReadComplete(read_op, array_output, result); 346 state_manager_.SetOperationFinished();
347 return result;
329 } 348 }
330 349
331 // For the non-blocking case, post a task to the file thread. 350 // For the non-blocking case, post a task to the file thread.
351 scoped_refptr<ReadOp> read_op(
352 new ReadOp(file_handle_, offset, bytes_to_read));
332 base::PostTaskAndReplyWithResult( 353 base::PostTaskAndReplyWithResult(
333 PpapiGlobals::Get()->GetFileTaskRunner(), 354 PpapiGlobals::Get()->GetFileTaskRunner(),
334 FROM_HERE, 355 FROM_HERE,
335 Bind(&FileIOResource::ReadOp::DoWork, read_op), 356 Bind(&FileIOResource::ReadOp::DoWork, read_op),
336 RunWhileLocked(Bind(&TrackedCallback::Run, callback))); 357 RunWhileLocked(Bind(&TrackedCallback::Run, callback)));
337 callback->set_completion_task( 358 callback->set_completion_task(
338 Bind(&FileIOResource::OnReadComplete, this, read_op, array_output)); 359 Bind(&FileIOResource::OnReadComplete, this, read_op, array_output));
339 360
340 return PP_OK_COMPLETIONPENDING; 361 return PP_OK_COMPLETIONPENDING;
341 } 362 }
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
431 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); 452 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file);
432 453
433 // End this operation now, so the user's callback can execute another FileIO 454 // End this operation now, so the user's callback can execute another FileIO
434 // operation, assuming there are no other pending operations. 455 // operation, assuming there are no other pending operations.
435 state_manager_.SetOperationFinished(); 456 state_manager_.SetOperationFinished();
436 callback->Run(result); 457 callback->Run(result);
437 } 458 }
438 459
439 } // namespace proxy 460 } // namespace proxy
440 } // namespace ppapi 461 } // namespace ppapi
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