File: file_transfer_message_handler.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 (95 lines) | stat: -rw-r--r-- 3,084 bytes parent folder | download | duplicates (6)
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 REMOTING_HOST_FILE_TRANSFER_FILE_TRANSFER_MESSAGE_HANDLER_H_
#define REMOTING_HOST_FILE_TRANSFER_FILE_TRANSFER_MESSAGE_HANDLER_H_

#include <cstdint>
#include <memory>
#include <optional>
#include <string>
#include <vector>

#include "base/containers/queue.h"
#include "base/memory/weak_ptr.h"
#include "remoting/host/file_transfer/buffered_file_writer.h"
#include "remoting/host/file_transfer/file_operations.h"
#include "remoting/protocol/file_transfer_helpers.h"
#include "remoting/protocol/named_message_pipe_handler.h"

namespace remoting {

constexpr char kFileTransferDataChannelPrefix[] = "filetransfer-";

class FileTransferMessageHandler : public protocol::NamedMessagePipeHandler {
 public:
  FileTransferMessageHandler(const std::string& name,
                             std::unique_ptr<protocol::MessagePipe> pipe,
                             std::unique_ptr<FileOperations> file_operations);
  ~FileTransferMessageHandler() override;

  // protocol::NamedMessagePipeHandler implementation.
  void OnConnected() override;
  void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message) override;
  void OnDisconnecting() override;

 private:
  enum State {
    // Initial state.
    kConnected,
    // Writing a file from the client.
    kWriting,
    // Reading a file and sending to the client.
    kReading,
    // Reading complete and waiting for confirmation from client.
    kEof,
    // End states
    // File successfully written.
    kClosed,
    // An error occured or the transfer was canceled.
    kFailed,
  };

  // Handlers for specific messages from the client.
  void OnMetadata(protocol::FileTransfer_Metadata metadata);
  void OnData(std::vector<std::uint8_t> data);
  void OnEnd();
  void OnRequestTransfer();
  void OnSuccess();
  void OnError(protocol::FileTransfer_Error error);

  // File reading callbacks.
  void OnOpenResult(FileOperations::Reader::OpenResult result);
  void OnReadResult(FileOperations::Reader::ReadResult result);
  void OnChunkSent();

  // File writing callbacks.
  void OnWritingComplete();
  void OnWriteError(protocol::FileTransfer_Error error);

  // Reads the next chunk in reading mode.
  void ReadNextChunk();

  // Cancels any current operation and transitions to failed state.
  void Cancel();

  // Sends an error message to the client.
  void SendError(protocol::FileTransfer_Error error);

  // Handles an unexpected message being received.
  void UnexpectedMessage(base::Location from_here, const char* message);

  void SetState(State state);

  State state_ = kConnected;
  std::unique_ptr<FileOperations> file_operations_;
  std::optional<BufferedFileWriter> buffered_file_writer_;
  std::unique_ptr<FileOperations::Reader> file_reader_;
  std::size_t queued_chunks_ = 0;
  base::WeakPtrFactory<FileTransferMessageHandler> weak_ptr_factory_{this};
};

}  // namespace remoting

#endif  // REMOTING_HOST_FILE_TRANSFER_FILE_TRANSFER_MESSAGE_HANDLER_H_