File: buffered_file_writer.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; 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,806; 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 (99 lines) | stat: -rw-r--r-- 3,461 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
96
97
98
99
// Copyright 2018 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef REMOTING_HOST_FILE_TRANSFER_BUFFERED_FILE_WRITER_H_
#define REMOTING_HOST_FILE_TRANSFER_BUFFERED_FILE_WRITER_H_

#include <cstdint>
#include <memory>
#include <vector>

#include "base/containers/queue.h"
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/functional/callback_forward.h"
#include "remoting/host/file_transfer/file_operations.h"
#include "remoting/proto/file_transfer.pb.h"

namespace remoting {

// Wrapper around FileOperations::WriteFile that automatically handles queuing
// operations. Write can be called immediately after start, can be called
// multiple times in sequence, and close can be called at any time. Internally,
// BufferedFileWriter will maintain a queue of written chunks and feed them to
// the Writer as the latter is ready for them.
class BufferedFileWriter {
 public:
  // Constructor.
  // |file_writer| should be in the kCreated state. |on_error| may be called at
  // any time if any operation fails. If no error occurs, |on_complete| will be
  // called after Close() has been called and all chunks have been successfully
  // written. Callbacks will never be called after BufferedFileWriter is
  // destroyed.
  BufferedFileWriter(
      std::unique_ptr<FileOperations::Writer> file_writer,
      base::OnceClosure on_complete,
      base::OnceCallback<void(protocol::FileTransfer_Error)> on_error);

  // Cancels the underlying Writer. If Close has already been called, this will
  // either do nothing (if writing the file has already completed) or cancel
  // writing out the file (if there are still chunks waiting be be written).
  // No callbacks will be invoked.
  ~BufferedFileWriter();

  // Start writing a new file using the provided FileOperations implementation.
  // Must be called exactly once before any other methods.
  void Start(const base::FilePath& filename);

  // Enqueue the provided chunk to be written to the file.
  void Write(std::vector<std::uint8_t> data);

  // Close the file. If any chunks are currently queued, they will be written
  // before the file is closed.
  void Close();

 private:
  enum State {
    // Initial state.
    kNotStarted,
    // A file operation is in progress.
    kWorking,
    // Waiting for data.
    kWaiting,
    // Close called, but file operations still pending.
    kClosing,
    // End states
    // File successfully written.
    kClosed,
    // An error occured or the transfer was canceled.
    kFailed,
  };

  void WriteNextChunk();
  void OnOperationResult(FileOperations::Writer::Result result);
  void DoClose();
  void OnCloseResult(FileOperations::Writer::Result result);
  void SetState(State state);

  // Tracks internal state.
  State state_ = kNotStarted;

  // Underlying Writer instance.
  std::unique_ptr<FileOperations::Writer> writer_;

  // Called once all writes are completed and the file is closed.
  base::OnceClosure on_complete_;

  // Called if there is an error at any stage. If this is called, on_complete_
  // won't be.
  base::OnceCallback<void(protocol::FileTransfer_Error)> on_error_;

  // Chunks that have been provided to Write but have not yet been passed to the
  // Writer instance.
  base::queue<std::vector<std::uint8_t>> chunks_;
};

}  // namespace remoting

#endif  // REMOTING_HOST_FILE_TRANSFER_BUFFERED_FILE_WRITER_H_