File: pipe_messaging_channel.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 (73 lines) | stat: -rw-r--r-- 2,734 bytes parent folder | download | duplicates (5)
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
// Copyright 2014 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_NATIVE_MESSAGING_PIPE_MESSAGING_CHANNEL_H_
#define REMOTING_HOST_NATIVE_MESSAGING_PIPE_MESSAGING_CHANNEL_H_

#include <memory>

#include "base/files/file.h"
#include "base/functional/callback.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/sequence_checker.h"
#include "base/values.h"
#include "extensions/browser/api/messaging/native_messaging_channel.h"
#include "remoting/host/native_messaging/native_messaging_reader.h"
#include "remoting/host/native_messaging/native_messaging_writer.h"

namespace remoting {

// An implementation of extensions::NativeMessagingChannel using a pipe. It
// is used by the It2MeNativeMessagingHost and Me2MeNativeMessagingHost to
// communicate with the chrome process.
// TODO(kelvinp): Move this class to the extensions/browser/api/messaging
// directory.
class PipeMessagingChannel : public extensions::NativeMessagingChannel {
 public:
  typedef extensions::NativeMessagingChannel::EventHandler EventHandler;

  // Constructs an object taking the ownership of |input| and |output|.
  PipeMessagingChannel(base::File input, base::File output);

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

  ~PipeMessagingChannel() override;

#if BUILDFLAG(IS_POSIX)
  // Opens `stdin_file` and `stdout_file` with stdin and stdout respectively,
  // which will NOT be assigned file descriptors 0 (STDIN_FILENO) and 1
  // (STDOUT_FILENO), then points file descriptors 0 and 1 to /dev/null, so that
  // child processes can't inherit stdin and stdout from this process and
  // potentially corrupt the native messaging stream.
  static void OpenAndBlockStdio(base::File& stdin_file,
                                base::File& stdout_file);
#endif  // BUILDFLAG(IS_POSIX)

  // extensions::NativeMessagingChannel implementation.
  void Start(EventHandler* event_handler) override;
  void SendMessage(std::optional<base::ValueView> message) override;

 private:
  // Processes a message received from the client app.
  void ProcessMessage(base::Value message);

  // Initiates shutdown.
  void Shutdown();

  NativeMessagingReader native_messaging_reader_;
  std::unique_ptr<NativeMessagingWriter> native_messaging_writer_;

  raw_ptr<EventHandler> event_handler_;
  base::WeakPtr<PipeMessagingChannel> weak_ptr_;

  SEQUENCE_CHECKER(sequence_checker_);

  base::WeakPtrFactory<PipeMessagingChannel> weak_factory_{this};
};

}  // namespace remoting

#endif  // REMOTING_HOST_NATIVE_MESSAGING_PIPE_MESSAGING_CHANNEL_H_