Avi Drissman | 6459548 | 2022-09-14 20:52:29 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 5 | #include "net/base/file_stream_context.h" |
| 6 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 7 | #include <errno.h> |
dcheng | c7eeda42 | 2015-12-26 03:56:48 | [diff] [blame] | 8 | #include <utility> |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 9 | |
Hans Wennborg | 0924470b | 2020-04-27 21:08:05 | [diff] [blame] | 10 | #include "base/check.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 11 | #include "base/files/file_path.h" |
Avi Drissman | 41c4a41 | 2023-01-11 22:45:37 | [diff] [blame] | 12 | #include "base/functional/bind.h" |
| 13 | #include "base/functional/callback.h" |
| 14 | #include "base/functional/callback_helpers.h" |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 15 | #include "base/location.h" |
[email protected] | 2025d00 | 2012-11-14 20:54:35 | [diff] [blame] | 16 | #include "base/posix/eintr_wrapper.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 17 | #include "base/task/task_runner.h" |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 18 | #include "net/base/io_buffer.h" |
| 19 | #include "net/base/net_errors.h" |
| 20 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 21 | namespace net { |
| 22 | |
Gabriel Charette | 96d5c64 | 2020-04-09 20:31:10 | [diff] [blame] | 23 | FileStream::Context::Context(scoped_refptr<base::TaskRunner> task_runner) |
| 24 | : Context(base::File(), std::move(task_runner)) {} |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 25 | |
[email protected] | a0830591 | 2014-03-21 00:41:15 | [diff] [blame] | 26 | FileStream::Context::Context(base::File file, |
Gabriel Charette | 96d5c64 | 2020-04-09 20:31:10 | [diff] [blame] | 27 | scoped_refptr<base::TaskRunner> task_runner) |
| 28 | : file_(std::move(file)), task_runner_(std::move(task_runner)) {} |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 29 | |
Chris Watkins | 68b1503 | 2017-12-01 03:07:13 | [diff] [blame] | 30 | FileStream::Context::~Context() = default; |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 31 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 32 | int FileStream::Context::Read(IOBuffer* in_buf, |
| 33 | int buf_len, |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 34 | CompletionOnceCallback callback) { |
Eric Roman | 89e0df13 | 2018-03-29 21:52:40 | [diff] [blame] | 35 | DCHECK(!async_in_progress_); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 36 | |
| 37 | scoped_refptr<IOBuffer> buf = in_buf; |
Sean Maher | a539a158 | 2022-11-28 18:13:41 | [diff] [blame] | 38 | const bool posted = task_runner_->PostTaskAndReplyWithResult( |
| 39 | FROM_HERE, |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 40 | base::BindOnce(&Context::ReadFileImpl, base::Unretained(this), buf, |
| 41 | buf_len), |
| 42 | base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this), |
| 43 | IntToInt64(std::move(callback)))); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 44 | DCHECK(posted); |
| 45 | |
| 46 | async_in_progress_ = true; |
| 47 | return ERR_IO_PENDING; |
| 48 | } |
| 49 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 50 | int FileStream::Context::Write(IOBuffer* in_buf, |
| 51 | int buf_len, |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 52 | CompletionOnceCallback callback) { |
Eric Roman | 89e0df13 | 2018-03-29 21:52:40 | [diff] [blame] | 53 | DCHECK(!async_in_progress_); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 54 | |
| 55 | scoped_refptr<IOBuffer> buf = in_buf; |
Sean Maher | a539a158 | 2022-11-28 18:13:41 | [diff] [blame] | 56 | const bool posted = task_runner_->PostTaskAndReplyWithResult( |
| 57 | FROM_HERE, |
Matt Menke | dadd6c7 | 2018-01-30 23:07:25 | [diff] [blame] | 58 | base::BindOnce(&Context::WriteFileImpl, base::Unretained(this), buf, |
| 59 | buf_len), |
| 60 | base::BindOnce(&Context::OnAsyncCompleted, base::Unretained(this), |
| 61 | IntToInt64(std::move(callback)))); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 62 | DCHECK(posted); |
| 63 | |
| 64 | async_in_progress_ = true; |
| 65 | return ERR_IO_PENDING; |
| 66 | } |
| 67 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 68 | FileStream::Context::IOResult FileStream::Context::SeekFileImpl( |
wtc | 69f8ea8 | 2015-06-04 00:08:13 | [diff] [blame] | 69 | int64_t offset) { |
reillyg | aa435e7 | 2015-06-16 00:48:48 | [diff] [blame] | 70 | int64_t res = file_.Seek(base::File::FROM_BEGIN, offset); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 71 | if (res == -1) |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 72 | return IOResult::FromOSError(errno); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 73 | |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 74 | return IOResult(res, 0); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 75 | } |
| 76 | |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 77 | void FileStream::Context::OnFileOpened() { |
| 78 | } |
| 79 | |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 80 | FileStream::Context::IOResult FileStream::Context::ReadFileImpl( |
| 81 | scoped_refptr<IOBuffer> buf, |
| 82 | int buf_len) { |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 83 | int res = file_.ReadAtCurrentPosNoBestEffort(buf->data(), buf_len); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 84 | if (res == -1) |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 85 | return IOResult::FromOSError(errno); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 86 | |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 87 | return IOResult(res, 0); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 90 | FileStream::Context::IOResult FileStream::Context::WriteFileImpl( |
| 91 | scoped_refptr<IOBuffer> buf, |
| 92 | int buf_len) { |
[email protected] | 633ff3b1 | 2014-06-20 23:30:18 | [diff] [blame] | 93 | int res = file_.WriteAtCurrentPosNoBestEffort(buf->data(), buf_len); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 94 | if (res == -1) |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 95 | return IOResult::FromOSError(errno); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 96 | |
[email protected] | bfb88ec | 2013-02-27 20:21:35 | [diff] [blame] | 97 | return IOResult(res, 0); |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 98 | } |
| 99 | |
[email protected] | aab1b9e | 2012-11-06 00:29:51 | [diff] [blame] | 100 | } // namespace net |