blob: 0e590b351637edf8e3e2659791e6730ac0f5fd00 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
[email protected]aab1b9e2012-11-06 00:29:512// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]aab1b9e2012-11-06 00:29:515#include "net/base/file_stream_context.h"
6
[email protected]aab1b9e2012-11-06 00:29:517#include <errno.h>
dchengc7eeda422015-12-26 03:56:488#include <utility>
[email protected]aab1b9e2012-11-06 00:29:519
Hans Wennborg0924470b2020-04-27 21:08:0510#include "base/check.h"
[email protected]57999812013-02-24 05:40:5211#include "base/files/file_path.h"
Avi Drissman41c4a412023-01-11 22:45:3712#include "base/functional/bind.h"
13#include "base/functional/callback.h"
14#include "base/functional/callback_helpers.h"
[email protected]aab1b9e2012-11-06 00:29:5115#include "base/location.h"
[email protected]2025d002012-11-14 20:54:3516#include "base/posix/eintr_wrapper.h"
Patrick Monette643cdf62021-10-15 19:13:4217#include "base/task/task_runner.h"
[email protected]aab1b9e2012-11-06 00:29:5118#include "net/base/io_buffer.h"
19#include "net/base/net_errors.h"
20
[email protected]aab1b9e2012-11-06 00:29:5121namespace net {
22
Gabriel Charette96d5c642020-04-09 20:31:1023FileStream::Context::Context(scoped_refptr<base::TaskRunner> task_runner)
24 : Context(base::File(), std::move(task_runner)) {}
[email protected]aab1b9e2012-11-06 00:29:5125
[email protected]a08305912014-03-21 00:41:1526FileStream::Context::Context(base::File file,
Gabriel Charette96d5c642020-04-09 20:31:1027 scoped_refptr<base::TaskRunner> task_runner)
28 : file_(std::move(file)), task_runner_(std::move(task_runner)) {}
[email protected]aab1b9e2012-11-06 00:29:5129
Chris Watkins68b15032017-12-01 03:07:1330FileStream::Context::~Context() = default;
[email protected]aab1b9e2012-11-06 00:29:5131
[email protected]633ff3b12014-06-20 23:30:1832int FileStream::Context::Read(IOBuffer* in_buf,
33 int buf_len,
Matt Menkedadd6c72018-01-30 23:07:2534 CompletionOnceCallback callback) {
Eric Roman89e0df132018-03-29 21:52:4035 DCHECK(!async_in_progress_);
[email protected]aab1b9e2012-11-06 00:29:5136
37 scoped_refptr<IOBuffer> buf = in_buf;
Sean Mahera539a1582022-11-28 18:13:4138 const bool posted = task_runner_->PostTaskAndReplyWithResult(
39 FROM_HERE,
Matt Menkedadd6c72018-01-30 23:07:2540 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]aab1b9e2012-11-06 00:29:5144 DCHECK(posted);
45
46 async_in_progress_ = true;
47 return ERR_IO_PENDING;
48}
49
[email protected]633ff3b12014-06-20 23:30:1850int FileStream::Context::Write(IOBuffer* in_buf,
51 int buf_len,
Matt Menkedadd6c72018-01-30 23:07:2552 CompletionOnceCallback callback) {
Eric Roman89e0df132018-03-29 21:52:4053 DCHECK(!async_in_progress_);
[email protected]aab1b9e2012-11-06 00:29:5154
55 scoped_refptr<IOBuffer> buf = in_buf;
Sean Mahera539a1582022-11-28 18:13:4156 const bool posted = task_runner_->PostTaskAndReplyWithResult(
57 FROM_HERE,
Matt Menkedadd6c72018-01-30 23:07:2558 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]aab1b9e2012-11-06 00:29:5162 DCHECK(posted);
63
64 async_in_progress_ = true;
65 return ERR_IO_PENDING;
66}
67
[email protected]633ff3b12014-06-20 23:30:1868FileStream::Context::IOResult FileStream::Context::SeekFileImpl(
wtc69f8ea82015-06-04 00:08:1369 int64_t offset) {
reillygaa435e72015-06-16 00:48:4870 int64_t res = file_.Seek(base::File::FROM_BEGIN, offset);
[email protected]aab1b9e2012-11-06 00:29:5171 if (res == -1)
[email protected]bfb88ec2013-02-27 20:21:3572 return IOResult::FromOSError(errno);
[email protected]aab1b9e2012-11-06 00:29:5173
[email protected]bfb88ec2013-02-27 20:21:3574 return IOResult(res, 0);
[email protected]aab1b9e2012-11-06 00:29:5175}
76
[email protected]633ff3b12014-06-20 23:30:1877void FileStream::Context::OnFileOpened() {
78}
79
[email protected]bfb88ec2013-02-27 20:21:3580FileStream::Context::IOResult FileStream::Context::ReadFileImpl(
81 scoped_refptr<IOBuffer> buf,
82 int buf_len) {
[email protected]633ff3b12014-06-20 23:30:1883 int res = file_.ReadAtCurrentPosNoBestEffort(buf->data(), buf_len);
[email protected]aab1b9e2012-11-06 00:29:5184 if (res == -1)
[email protected]bfb88ec2013-02-27 20:21:3585 return IOResult::FromOSError(errno);
[email protected]aab1b9e2012-11-06 00:29:5186
[email protected]bfb88ec2013-02-27 20:21:3587 return IOResult(res, 0);
[email protected]aab1b9e2012-11-06 00:29:5188}
89
[email protected]bfb88ec2013-02-27 20:21:3590FileStream::Context::IOResult FileStream::Context::WriteFileImpl(
91 scoped_refptr<IOBuffer> buf,
92 int buf_len) {
[email protected]633ff3b12014-06-20 23:30:1893 int res = file_.WriteAtCurrentPosNoBestEffort(buf->data(), buf_len);
[email protected]aab1b9e2012-11-06 00:29:5194 if (res == -1)
[email protected]bfb88ec2013-02-27 20:21:3595 return IOResult::FromOSError(errno);
[email protected]aab1b9e2012-11-06 00:29:5196
[email protected]bfb88ec2013-02-27 20:21:3597 return IOResult(res, 0);
[email protected]aab1b9e2012-11-06 00:29:5198}
99
[email protected]aab1b9e2012-11-06 00:29:51100} // namespace net