File: file_stream_forwarder.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rw-r--r-- 3,397 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_ARC_FILEAPI_FILE_STREAM_FORWARDER_H_
#define CHROME_BROWSER_ASH_ARC_FILEAPI_FILE_STREAM_FORWARDER_H_

#include <memory>

#include "base/files/scoped_file.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "base/task/sequenced_task_runner.h"
#include "net/base/io_buffer.h"
#include "storage/browser/file_system/file_stream_reader.h"
#include "storage/browser/file_system/file_system_context.h"

namespace arc {

// FileStreamForwarder reads data of the given FileSystemURL, and writes it to
// the given FD. While all internal actions happen on the IO thread, all public
// methods should be called on the UI thread. If this object is destroyed before
// the completion, the in-flight operation may abort before finishing, and the
// callback will be called with false when aborted.
class FileStreamForwarder {
 public:
  struct DestroyHelper {
    void operator()(FileStreamForwarder* object) const { object->Destroy(); }
  };

  // |result| is true when the specified amount of data was successfully
  // forwarded.
  using ResultCallback = base::OnceCallback<void(bool result)>;

  // Starts reading the part of the file specified by |offset| and |size|, and
  // writes the data to |fd_dest|, and runs |callback| with true iff |size|
  // bytes of data were successfully read and written.
  FileStreamForwarder(scoped_refptr<storage::FileSystemContext> context,
                      const storage::FileSystemURL& url,
                      int64_t offset,
                      int64_t size,
                      base::ScopedFD fd_dest,
                      ResultCallback callback);

  FileStreamForwarder(const FileStreamForwarder&) = delete;
  FileStreamForwarder& operator=(const FileStreamForwarder&) = delete;

  // Posts a task to destruct this object on the IO thread.
  // Must not be called multiple times for the same object.
  void Destroy();

 private:
  // Use Destroy() to destruct this object.
  ~FileStreamForwarder();

  // Destructs this object.
  void DestroyOnIOThread();

  // Starts reading the data.
  void Start();

  // Does the actual reading.
  void DoRead();

  // Called when read is completed. |result| is the number of bytes read, or an
  // error code if it's negative.
  void OnReadCompleted(int result);

  // Called when write is completed.
  void OnWriteCompleted(bool result);

  // Runs the result callback on the UI thread.
  void NotifyCompleted(bool result);

  scoped_refptr<storage::FileSystemContext> context_;
  const storage::FileSystemURL url_;
  const int64_t offset_;
  int64_t remaining_size_;
  base::ScopedFD fd_dest_;
  ResultCallback callback_;
  scoped_refptr<base::SequencedTaskRunner> task_runner_;  // For blocking IO.
  scoped_refptr<net::IOBufferWithSize> buf_;
  std::unique_ptr<storage::FileStreamReader> stream_reader_;

  base::WeakPtrFactory<FileStreamForwarder> weak_ptr_factory_{this};
};

// FileStreamForwarderPtr is unique_ptr with a custom deleter. Use this to
// safely handle the ownership of FileStreamForwarder.
using FileStreamForwarderPtr =
    std::unique_ptr<FileStreamForwarder, FileStreamForwarder::DestroyHelper>;

}  // namespace arc

#endif  // CHROME_BROWSER_ASH_ARC_FILEAPI_FILE_STREAM_FORWARDER_H_