File: named_message_pipe_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 (80 lines) | stat: -rw-r--r-- 2,640 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
// 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_PROTOCOL_NAMED_MESSAGE_PIPE_HANDLER_H_
#define REMOTING_PROTOCOL_NAMED_MESSAGE_PIPE_HANDLER_H_

#include <memory>
#include <string>

#include "base/functional/callback.h"
#include "base/task/sequenced_task_runner_helpers.h"
#include "base/threading/thread_checker.h"
#include "remoting/protocol/message_pipe.h"

namespace google {
namespace protobuf {
class MessageLite;
}  // namespace protobuf
}  // namespace google

namespace remoting {

class CompoundBuffer;

namespace protocol {

// A base class to handle data from a named MessagePipe. This class manages the
// lifetime itself: it deletes itself once the MessagePipe is closed or the
// derived instance actively calls Close() function.
class NamedMessagePipeHandler : public MessagePipe::EventHandler {
 protected:
  // The callers should create instances of derived classes instead of this
  // class. So hide the constructor.
  NamedMessagePipeHandler(const std::string& name,
                          std::unique_ptr<MessagePipe> pipe);

  ~NamedMessagePipeHandler() override;

  // Closes the channel and eventually destructs this instance. No operations
  // should be performed after executing this function.
  void Close();

  const std::string& pipe_name() const { return name_; }

  // Whether |pipe_| has been connected.
  bool connected() const { return is_connected_; }

  // Sends the message through the pipe. This function should only be called
  // once connected() returns true. See comments of
  // remoting::protocol::MessagePipe::Send() for details.
  void Send(const google::protobuf::MessageLite& message,
            base::OnceClosure done);

  // Derived classes can override these functions to receive data from the
  // connection or observe the connection state. These functions will not be
  // called unless |pipe_| has been opened.
  virtual void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message);
  virtual void OnConnected();
  virtual void OnDisconnecting();

 private:
  friend class base::DeleteHelper<NamedMessagePipeHandler>;

  // MessagePipe::EventHandler implementation.
  void OnMessagePipeOpen() override;
  void OnMessageReceived(std::unique_ptr<CompoundBuffer> message) override;
  void OnMessagePipeClosed() override;

  const std::string name_;
  std::unique_ptr<MessagePipe> pipe_;
  bool is_connected_ = false;

  THREAD_CHECKER(thread_checker_);
};

}  // namespace protocol
}  // namespace remoting

#endif  // REMOTING_PROTOCOL_NAMED_MESSAGE_PIPE_HANDLER_H_