OLD | NEW |
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 <limits> | 7 #include <limits> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 } | 75 } |
76 | 76 |
77 int32_t FileIOResource::ReadOp::DoWork() { | 77 int32_t FileIOResource::ReadOp::DoWork() { |
78 DCHECK(!buffer_.get()); | 78 DCHECK(!buffer_.get()); |
79 buffer_.reset(new char[bytes_to_read_]); | 79 buffer_.reset(new char[bytes_to_read_]); |
80 return file_holder_->file()->Read(offset_, buffer_.get(), bytes_to_read_); | 80 return file_holder_->file()->Read(offset_, buffer_.get(), bytes_to_read_); |
81 } | 81 } |
82 | 82 |
83 FileIOResource::WriteOp::WriteOp(scoped_refptr<FileHolder> file_holder, | 83 FileIOResource::WriteOp::WriteOp(scoped_refptr<FileHolder> file_holder, |
84 int64_t offset, | 84 int64_t offset, |
85 scoped_ptr<char[]> buffer, | 85 std::unique_ptr<char[]> buffer, |
86 int32_t bytes_to_write, | 86 int32_t bytes_to_write, |
87 bool append) | 87 bool append) |
88 : file_holder_(file_holder), | 88 : file_holder_(file_holder), |
89 offset_(offset), | 89 offset_(offset), |
90 buffer_(std::move(buffer)), | 90 buffer_(std::move(buffer)), |
91 bytes_to_write_(bytes_to_write), | 91 bytes_to_write_(bytes_to_write), |
92 append_(append) {} | 92 append_(append) {} |
93 | 93 |
94 FileIOResource::WriteOp::~WriteOp() { | 94 FileIOResource::WriteOp::~WriteOp() { |
95 } | 95 } |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
294 if (max_offset > | 294 if (max_offset > |
295 static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { | 295 static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { |
296 return PP_ERROR_FAILED; // amount calculation would overflow. | 296 return PP_ERROR_FAILED; // amount calculation would overflow. |
297 } | 297 } |
298 increase = static_cast<int64_t>(max_offset) - max_written_offset_; | 298 increase = static_cast<int64_t>(max_offset) - max_written_offset_; |
299 } | 299 } |
300 | 300 |
301 if (increase > 0) { | 301 if (increase > 0) { |
302 // Request a quota reservation. This makes the Write asynchronous, so we | 302 // Request a quota reservation. This makes the Write asynchronous, so we |
303 // must copy the plugin's buffer. | 303 // must copy the plugin's buffer. |
304 scoped_ptr<char[]> copy(new char[bytes_to_write]); | 304 std::unique_ptr<char[]> copy(new char[bytes_to_write]); |
305 memcpy(copy.get(), buffer, bytes_to_write); | 305 memcpy(copy.get(), buffer, bytes_to_write); |
306 int64_t result = | 306 int64_t result = |
307 file_system_resource_->AsPPB_FileSystem_API()->RequestQuota( | 307 file_system_resource_->AsPPB_FileSystem_API()->RequestQuota( |
308 increase, | 308 increase, |
309 base::Bind(&FileIOResource::OnRequestWriteQuotaComplete, | 309 base::Bind(&FileIOResource::OnRequestWriteQuotaComplete, |
310 this, | 310 this, |
311 offset, | 311 offset, |
312 base::Passed(©), | 312 base::Passed(©), |
313 bytes_to_write, | 313 bytes_to_write, |
314 callback)); | 314 callback)); |
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
508 } | 508 } |
509 if (result < 0) | 509 if (result < 0) |
510 result = PP_ERROR_FAILED; | 510 result = PP_ERROR_FAILED; |
511 | 511 |
512 state_manager_.SetOperationFinished(); | 512 state_manager_.SetOperationFinished(); |
513 return result; | 513 return result; |
514 } | 514 } |
515 | 515 |
516 // For the non-blocking case, post a task to the file thread. We must copy the | 516 // For the non-blocking case, post a task to the file thread. We must copy the |
517 // plugin's buffer at this point. | 517 // plugin's buffer at this point. |
518 scoped_ptr<char[]> copy(new char[bytes_to_write]); | 518 std::unique_ptr<char[]> copy(new char[bytes_to_write]); |
519 memcpy(copy.get(), buffer, bytes_to_write); | 519 memcpy(copy.get(), buffer, bytes_to_write); |
520 scoped_refptr<WriteOp> write_op(new WriteOp( | 520 scoped_refptr<WriteOp> write_op(new WriteOp( |
521 file_holder_, offset, std::move(copy), bytes_to_write, append)); | 521 file_holder_, offset, std::move(copy), bytes_to_write, append)); |
522 base::PostTaskAndReplyWithResult( | 522 base::PostTaskAndReplyWithResult( |
523 PpapiGlobals::Get()->GetFileTaskRunner(), | 523 PpapiGlobals::Get()->GetFileTaskRunner(), |
524 FROM_HERE, | 524 FROM_HERE, |
525 Bind(&FileIOResource::WriteOp::DoWork, write_op), | 525 Bind(&FileIOResource::WriteOp::DoWork, write_op), |
526 RunWhileLocked(Bind(&TrackedCallback::Run, callback))); | 526 RunWhileLocked(Bind(&TrackedCallback::Run, callback))); |
527 callback->set_completion_task(Bind(&FileIOResource::OnWriteComplete, this)); | 527 callback->set_completion_task(Bind(&FileIOResource::OnWriteComplete, this)); |
528 | 528 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 } else { | 575 } else { |
576 // The read operation failed. | 576 // The read operation failed. |
577 result = PP_ERROR_FAILED; | 577 result = PP_ERROR_FAILED; |
578 } | 578 } |
579 state_manager_.SetOperationFinished(); | 579 state_manager_.SetOperationFinished(); |
580 return result; | 580 return result; |
581 } | 581 } |
582 | 582 |
583 void FileIOResource::OnRequestWriteQuotaComplete( | 583 void FileIOResource::OnRequestWriteQuotaComplete( |
584 int64_t offset, | 584 int64_t offset, |
585 scoped_ptr<char[]> buffer, | 585 std::unique_ptr<char[]> buffer, |
586 int32_t bytes_to_write, | 586 int32_t bytes_to_write, |
587 scoped_refptr<TrackedCallback> callback, | 587 scoped_refptr<TrackedCallback> callback, |
588 int64_t granted) { | 588 int64_t granted) { |
589 DCHECK(granted >= 0); | 589 DCHECK(granted >= 0); |
590 if (granted == 0) { | 590 if (granted == 0) { |
591 callback->Run(PP_ERROR_NOQUOTA); | 591 callback->Run(PP_ERROR_NOQUOTA); |
592 return; | 592 return; |
593 } | 593 } |
594 if (open_flags_ & PP_FILEOPENFLAG_APPEND) { | 594 if (open_flags_ & PP_FILEOPENFLAG_APPEND) { |
595 DCHECK_LE(bytes_to_write, granted); | 595 DCHECK_LE(bytes_to_write, granted); |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
713 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); | 713 *output_handle = IPC::PlatformFileForTransitToPlatformFile(transit_file); |
714 | 714 |
715 // End this operation now, so the user's callback can execute another FileIO | 715 // End this operation now, so the user's callback can execute another FileIO |
716 // operation, assuming there are no other pending operations. | 716 // operation, assuming there are no other pending operations. |
717 state_manager_.SetOperationFinished(); | 717 state_manager_.SetOperationFinished(); |
718 callback->Run(result); | 718 callback->Run(result); |
719 } | 719 } |
720 | 720 |
721 } // namespace proxy | 721 } // namespace proxy |
722 } // namespace ppapi | 722 } // namespace ppapi |
OLD | NEW |