1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "mojo/common/data_pipe_utils.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/files/scoped_file.h"
10 #include "base/location.h"
11 #include "base/task_runner_util.h"
17 bool BlockingCopyFromFile(const base::FilePath
& source
,
18 ScopedDataPipeProducerHandle destination
,
20 base::File
file(source
, base::File::FLAG_OPEN
| base::File::FLAG_READ
);
23 if (file
.Seek(base::File::FROM_BEGIN
, skip
) != skip
) {
24 LOG(ERROR
) << "Seek of " << skip
<< " in " << source
.value() << " failed";
28 void* buffer
= nullptr;
29 uint32_t buffer_num_bytes
= 0;
31 BeginWriteDataRaw(destination
.get(), &buffer
, &buffer_num_bytes
,
32 MOJO_WRITE_DATA_FLAG_NONE
);
33 if (result
== MOJO_RESULT_OK
) {
35 file
.ReadAtCurrentPos(static_cast<char*>(buffer
), buffer_num_bytes
);
36 if (bytes_read
>= 0) {
37 EndWriteDataRaw(destination
.get(), bytes_read
);
38 if (bytes_read
== 0) {
44 EndWriteDataRaw(destination
.get(), 0);
47 } else if (result
== MOJO_RESULT_SHOULD_WAIT
) {
48 result
= Wait(destination
.get(), MOJO_HANDLE_SIGNAL_WRITABLE
,
49 MOJO_DEADLINE_INDEFINITE
, nullptr);
50 if (result
!= MOJO_RESULT_OK
) {
51 // If the consumer handle was closed, then treat as EOF.
52 return result
== MOJO_RESULT_FAILED_PRECONDITION
;
55 // If the consumer handle was closed, then treat as EOF.
56 return result
== MOJO_RESULT_FAILED_PRECONDITION
;
67 void CopyFromFile(const base::FilePath
& source
,
68 ScopedDataPipeProducerHandle destination
,
70 base::TaskRunner
* task_runner
,
71 const base::Callback
<void(bool)>& callback
) {
72 base::PostTaskAndReplyWithResult(task_runner
, FROM_HERE
,
73 base::Bind(&BlockingCopyFromFile
, source
,
74 base::Passed(&destination
), skip
),