Update V8 to version 4.6.72.
[chromium-blink-merge.git] / mojo / common / data_pipe_file_utils.cc
blob4bc26861101a897ba563b0d2ed8d98c8552efe4d
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"
13 namespace mojo {
14 namespace common {
15 namespace {
17 bool BlockingCopyFromFile(const base::FilePath& source,
18 ScopedDataPipeProducerHandle destination,
19 uint32_t skip) {
20 base::File file(source, base::File::FLAG_OPEN | base::File::FLAG_READ);
21 if (!file.IsValid())
22 return false;
23 if (file.Seek(base::File::FROM_BEGIN, skip) != skip) {
24 LOG(ERROR) << "Seek of " << skip << " in " << source.value() << " failed";
25 return false;
27 for (;;) {
28 void* buffer = nullptr;
29 uint32_t buffer_num_bytes = 0;
30 MojoResult result =
31 BeginWriteDataRaw(destination.get(), &buffer, &buffer_num_bytes,
32 MOJO_WRITE_DATA_FLAG_NONE);
33 if (result == MOJO_RESULT_OK) {
34 int bytes_read =
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) {
39 // eof
40 return true;
42 } else {
43 // error
44 EndWriteDataRaw(destination.get(), 0);
45 return false;
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;
54 } else {
55 // If the consumer handle was closed, then treat as EOF.
56 return result == MOJO_RESULT_FAILED_PRECONDITION;
59 #if !defined(OS_WIN)
60 NOTREACHED();
61 return false;
62 #endif
65 } // namespace
67 void CopyFromFile(const base::FilePath& source,
68 ScopedDataPipeProducerHandle destination,
69 uint32_t skip,
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),
75 callback);
78 } // namespace common
79 } // namespace mojo