File: launch_context_posix.cc

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 (125 lines) | stat: -rw-r--r-- 4,062 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/messaging/launch_context.h"

#include <memory>
#include <utility>

#include "base/command_line.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/logging.h"
#include "base/path_service.h"
#include "base/posix/eintr_wrapper.h"
#include "base/process/launch.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/common/chrome_paths.h"
#include "net/base/file_stream.h"

namespace extensions {

namespace {

base::FilePath FindManifestInDir(int dir_key, const std::string& host_name) {
  base::FilePath base_path;
  if (base::PathService::Get(dir_key, &base_path)) {
    base::FilePath path = base_path.Append(host_name + ".json");
    if (base::PathExists(path)) {
      return path;
    }
  }
  return base::FilePath();
}

}  // namespace

// static
base::FilePath LaunchContext::FindManifest(const std::string& host_name,
                                           bool allow_user_level_hosts,
                                           std::string& error_message) {
  base::FilePath result;
  if (allow_user_level_hosts) {
    result = FindManifestInDir(chrome::DIR_USER_NATIVE_MESSAGING, host_name);
  }
  if (result.empty()) {
    result = FindManifestInDir(chrome::DIR_NATIVE_MESSAGING, host_name);
  }

  if (result.empty()) {
    error_message = "Can't find native messaging host " + host_name;
  }

  return result;
}

// static
std::optional<LaunchContext::ProcessState> LaunchContext::LaunchNativeProcess(
    const base::CommandLine& command_line,
    // This is only relevant on Windows
    bool native_hosts_executables_launch_directly) {
  base::LaunchOptions options;

  int read_pipe_fds[2] = {};
  if (HANDLE_EINTR(pipe(read_pipe_fds)) != 0) {
    LOG(ERROR) << "Bad read pipe";
    return std::nullopt;
  }
  base::ScopedFD read_pipe_read_fd(read_pipe_fds[0]);
  base::ScopedFD read_pipe_write_fd(read_pipe_fds[1]);
  options.fds_to_remap.push_back(
      std::make_pair(read_pipe_write_fd.get(), STDOUT_FILENO));

  int write_pipe_fds[2] = {};
  if (HANDLE_EINTR(pipe(write_pipe_fds)) != 0) {
    LOG(ERROR) << "Bad write pipe";
    return std::nullopt;
  }
  base::ScopedFD write_pipe_read_fd(write_pipe_fds[0]);
  base::ScopedFD write_pipe_write_fd(write_pipe_fds[1]);
  options.fds_to_remap.push_back(
      std::make_pair(write_pipe_read_fd.get(), STDIN_FILENO));

  options.current_directory = command_line.GetProgram().DirName();

#if BUILDFLAG(IS_LINUX)
  // Don't use no_new_privs mode, e.g. in case the host needs to use sudo.
  options.allow_new_privs = true;
#endif

#if BUILDFLAG(IS_MAC)
  // This is executing a third-party binary, so do not associate any system
  // private data requests with Chrome.
  options.disclaim_responsibility = true;
#endif

  base::Process local_process = base::LaunchProcess(command_line, options);
  if (!local_process.IsValid()) {
    LOG(ERROR) << "Error launching process";
    return std::nullopt;
  }

  // We will not be reading from the write pipe, nor writing from the read pipe.
  write_pipe_read_fd.reset();
  read_pipe_write_fd.reset();

  return ProcessState(std::move(local_process), std::move(read_pipe_read_fd),
                      std::move(write_pipe_write_fd));
}

void LaunchContext::ConnectPipes(base::ScopedPlatformFile read_file,
                                 base::ScopedPlatformFile write_file) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  CHECK(native_process_.IsValid());

  base::PlatformFile read_file_unowned = read_file.get();
  OnSuccess(read_file_unowned,
            std::make_unique<net::FileStream>(base::File(std::move(read_file)),
                                              background_task_runner_),
            std::make_unique<net::FileStream>(base::File(std::move(write_file)),
                                              background_task_runner_));
}

}  // namespace extensions