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

Unified Diff: mojo/common/data_pipe_utils.cc

Issue 694303002: Allow local file to run though content handler. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Delay running the callback in AsPath as the code doesn't support re-entrency. Created 6 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: mojo/common/data_pipe_utils.cc
diff --git a/mojo/common/data_pipe_utils.cc b/mojo/common/data_pipe_utils.cc
index b26b1946cf7ed53a8c77012152cd939d4e37f1a9..c24ea53d4696aca0c4036b593654cf36cbcc10a5 100644
--- a/mojo/common/data_pipe_utils.cc
+++ b/mojo/common/data_pipe_utils.cc
@@ -20,8 +20,8 @@ namespace {
bool BlockingCopyHelper(ScopedDataPipeConsumerHandle source,
const base::Callback<size_t(const void*, uint32_t)>& write_bytes) {
for (;;) {
- const void* buffer;
- uint32_t num_bytes;
+ const void* buffer = nullptr;
+ uint32_t num_bytes = 0;
MojoResult result = BeginReadDataRaw(
source.get(), &buffer, &num_bytes, MOJO_READ_DATA_FLAG_NONE);
if (result == MOJO_RESULT_OK) {
@@ -173,6 +173,53 @@ PeekStatus PeekNBytes(size_t bytes_length,
return PeekStatus::kKeepReading;
}
+bool BlockingCopyFromFile(const base::FilePath& source,
+ ScopedDataPipeProducerHandle destination,
+ uint32_t skip) {
+ base::File file(source, base::File::FLAG_OPEN | base::File::FLAG_READ);
+ if (!file.IsValid())
+ return false;
+ if (file.Seek(base::File::FROM_BEGIN, skip) != skip) {
+ return false;
+ }
+ for (;;) {
+ void* buffer;
+ uint32_t buffer_num_bytes;
+ MojoResult result = BeginWriteDataRaw(destination.get(),
+ &buffer,
+ &buffer_num_bytes,
+ MOJO_WRITE_DATA_FLAG_NONE);
+ if (result == MOJO_RESULT_OK) {
+ int bytes_read =
+ file.ReadAtCurrentPos(static_cast<char*>(buffer), buffer_num_bytes);
+ if (bytes_read >= 0) {
+ EndWriteDataRaw(destination.get(), bytes_read);
+ if (bytes_read == 0) {
+ // eof
+ return true;
+ }
+ } else {
+ // error
+ EndWriteDataRaw(destination.get(), 0);
+ return false;
+ }
+ } else if (result == MOJO_RESULT_SHOULD_WAIT) {
+ result = Wait(destination.get(),
+ MOJO_HANDLE_SIGNAL_WRITABLE,
+ MOJO_DEADLINE_INDEFINITE);
+ if (result != MOJO_RESULT_OK) {
+ // If the consumer handle was closed, then treat as EOF.
+ return result == MOJO_RESULT_FAILED_PRECONDITION;
+ }
+ } else {
+ // If the consumer handle was closed, then treat as EOF.
+ return result == MOJO_RESULT_FAILED_PRECONDITION;
+ }
+ }
+ NOTREACHED();
+ return false;
+}
+
} // namespace
bool BlockingPeekNBytes(DataPipeConsumerHandle source,
@@ -220,5 +267,18 @@ void CopyToFile(ScopedDataPipeConsumerHandle source,
callback);
}
+void CopyFromFile(const base::FilePath& source,
+ ScopedDataPipeProducerHandle destination,
+ uint32_t skip,
+ base::TaskRunner* task_runner,
+ const base::Callback<void(bool)>& callback) {
+ base::PostTaskAndReplyWithResult(
+ task_runner,
+ FROM_HERE,
+ base::Bind(
+ &BlockingCopyFromFile, source, base::Passed(&destination), skip),
+ callback);
+}
+
} // namespace common
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698