blob: 3e566d42b3bdf9673493b9f5a1cf311500bde3af [file] [log] [blame]
[email protected]55351232013-10-17 12:05:121// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
kelvinp3a76af72014-09-20 02:45:275#include "remoting/host/native_messaging/pipe_messaging_channel.h"
[email protected]55351232013-10-17 12:05:126
sergeyu1417e0132015-12-23 19:01:227#include <utility>
8
[email protected]55351232013-10-17 12:05:129#include "base/bind.h"
10#include "base/callback.h"
[email protected]8e022322013-11-28 03:49:0611#include "base/callback_helpers.h"
Lambros Lambrouca76b6e2019-12-03 01:49:0012#include "base/files/file.h"
[email protected]55351232013-10-17 12:05:1213#include "base/location.h"
Carlos Caballero92aab29e2019-09-24 15:41:1114#include "base/process/process_info.h"
[email protected]55351232013-10-17 12:05:1215#include "base/values.h"
avic5960f32015-12-22 22:49:4816#include "build/build_config.h"
[email protected]55351232013-10-17 12:05:1217
[email protected]55351232013-10-17 12:05:1218namespace remoting {
19
sergeyu1417e0132015-12-23 19:01:2220PipeMessagingChannel::PipeMessagingChannel(base::File input, base::File output)
Lambros Lambrouca76b6e2019-12-03 01:49:0021 : native_messaging_reader_(input.Duplicate()),
22 native_messaging_writer_(new NativeMessagingWriter(output.Duplicate())),
Jeremy Roman7c5cfabd2019-08-12 15:45:2723 event_handler_(nullptr) {
[email protected]55351232013-10-17 12:05:1224 weak_ptr_ = weak_factory_.GetWeakPtr();
25}
26
gabbf77513a2017-06-01 14:35:3427PipeMessagingChannel::~PipeMessagingChannel() {
28 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
29}
[email protected]55351232013-10-17 12:05:1230
Lambros Lambrouca76b6e2019-12-03 01:49:0031// static
32void PipeMessagingChannel::ReopenStdinStdout() {
33#if defined(OS_POSIX)
34 base::FilePath dev_null("/dev/null");
35 int new_stdin =
36 base::File(dev_null, base::File::FLAG_OPEN | base::File::FLAG_READ)
37 .TakePlatformFile();
38 DCHECK_EQ(new_stdin, STDIN_FILENO);
39 int new_stdout =
40 base::File(dev_null, base::File::FLAG_OPEN | base::File::FLAG_WRITE)
41 .TakePlatformFile();
42 DCHECK_EQ(new_stdout, STDOUT_FILENO);
43#endif // defined(OS_POSIX)
44}
45
kelvinp3a76af72014-09-20 02:45:2746void PipeMessagingChannel::Start(EventHandler* event_handler) {
gabbf77513a2017-06-01 14:35:3447 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
kelvinp3a76af72014-09-20 02:45:2748 DCHECK(!event_handler_);
[email protected]55351232013-10-17 12:05:1249
kelvinp3a76af72014-09-20 02:45:2750 event_handler_ = event_handler;
51 DCHECK(event_handler_);
[email protected]f2be9402013-12-13 09:15:3552
[email protected]55351232013-10-17 12:05:1253 native_messaging_reader_.Start(
kelvinp3a76af72014-09-20 02:45:2754 base::Bind(&PipeMessagingChannel::ProcessMessage, weak_ptr_),
55 base::Bind(&PipeMessagingChannel::Shutdown, weak_ptr_));
[email protected]55351232013-10-17 12:05:1256}
57
dcheng0765c492016-04-06 22:41:5358void PipeMessagingChannel::ProcessMessage(
59 std::unique_ptr<base::Value> message) {
gabbf77513a2017-06-01 14:35:3460 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]55351232013-10-17 12:05:1261
kelvinp3a76af72014-09-20 02:45:2762 if (event_handler_)
sergeyu1417e0132015-12-23 19:01:2263 event_handler_->OnMessage(std::move(message));
[email protected]55351232013-10-17 12:05:1264}
65
dcheng0765c492016-04-06 22:41:5366void PipeMessagingChannel::SendMessage(std::unique_ptr<base::Value> message) {
gabbf77513a2017-06-01 14:35:3467 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]55351232013-10-17 12:05:1268
[email protected]8e022322013-11-28 03:49:0669 bool success = message && native_messaging_writer_;
[email protected]55351232013-10-17 12:05:1270 if (success)
[email protected]8e022322013-11-28 03:49:0671 success = native_messaging_writer_->WriteMessage(*message);
[email protected]55351232013-10-17 12:05:1272
73 if (!success) {
74 // Close the write pipe so no more responses will be sent.
75 native_messaging_writer_.reset();
76 Shutdown();
77 }
[email protected]55351232013-10-17 12:05:1278}
79
kelvinp3a76af72014-09-20 02:45:2780void PipeMessagingChannel::Shutdown() {
gabbf77513a2017-06-01 14:35:3481 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
[email protected]55351232013-10-17 12:05:1282
kelvinp3a76af72014-09-20 02:45:2783 if (event_handler_) {
sergeyuc5f104b2015-01-09 19:33:2484 // Set |event_handler_| to nullptr to indicate the object is in a shutdown
85 // cycle. Since event_handler->OnDisconnect() will destroy the current
86 // object, |event_handler_| will become a dangling pointer after
87 // OnDisconnect() returns. Therefore, we set |event_handler_| to nullptr
88 // beforehand.
kelvinp3a76af72014-09-20 02:45:2789 EventHandler* handler = event_handler_;
sergeyuc5f104b2015-01-09 19:33:2490 event_handler_ = nullptr;
kelvinp3a76af72014-09-20 02:45:2791 handler->OnDisconnect();
92 }
[email protected]55351232013-10-17 12:05:1293}
94
95} // namespace remoting