[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 1 | // 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 | |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 5 | #include "remoting/host/native_messaging/pipe_messaging_channel.h" |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 6 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 9 | #include "base/bind.h" |
| 10 | #include "base/callback.h" |
[email protected] | 8e02232 | 2013-11-28 03:49:06 | [diff] [blame] | 11 | #include "base/callback_helpers.h" |
Lambros Lambrou | ca76b6e | 2019-12-03 01:49:00 | [diff] [blame] | 12 | #include "base/files/file.h" |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 13 | #include "base/location.h" |
Carlos Caballero | 92aab29e | 2019-09-24 15:41:11 | [diff] [blame] | 14 | #include "base/process/process_info.h" |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 15 | #include "base/values.h" |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 16 | #include "build/build_config.h" |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 17 | |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 18 | namespace remoting { |
| 19 | |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 20 | PipeMessagingChannel::PipeMessagingChannel(base::File input, base::File output) |
Lambros Lambrou | ca76b6e | 2019-12-03 01:49:00 | [diff] [blame] | 21 | : native_messaging_reader_(input.Duplicate()), |
| 22 | native_messaging_writer_(new NativeMessagingWriter(output.Duplicate())), |
Jeremy Roman | 7c5cfabd | 2019-08-12 15:45:27 | [diff] [blame] | 23 | event_handler_(nullptr) { |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 24 | weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 25 | } |
| 26 | |
gab | bf77513a | 2017-06-01 14:35:34 | [diff] [blame] | 27 | PipeMessagingChannel::~PipeMessagingChannel() { |
| 28 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 29 | } |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 30 | |
Lambros Lambrou | ca76b6e | 2019-12-03 01:49:00 | [diff] [blame] | 31 | // static |
| 32 | void 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 | |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 46 | void PipeMessagingChannel::Start(EventHandler* event_handler) { |
gab | bf77513a | 2017-06-01 14:35:34 | [diff] [blame] | 47 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 48 | DCHECK(!event_handler_); |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 49 | |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 50 | event_handler_ = event_handler; |
| 51 | DCHECK(event_handler_); |
[email protected] | f2be940 | 2013-12-13 09:15:35 | [diff] [blame] | 52 | |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 53 | native_messaging_reader_.Start( |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 54 | base::Bind(&PipeMessagingChannel::ProcessMessage, weak_ptr_), |
| 55 | base::Bind(&PipeMessagingChannel::Shutdown, weak_ptr_)); |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 56 | } |
| 57 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 58 | void PipeMessagingChannel::ProcessMessage( |
| 59 | std::unique_ptr<base::Value> message) { |
gab | bf77513a | 2017-06-01 14:35:34 | [diff] [blame] | 60 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 61 | |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 62 | if (event_handler_) |
sergeyu | 1417e013 | 2015-12-23 19:01:22 | [diff] [blame] | 63 | event_handler_->OnMessage(std::move(message)); |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 64 | } |
| 65 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 66 | void PipeMessagingChannel::SendMessage(std::unique_ptr<base::Value> message) { |
gab | bf77513a | 2017-06-01 14:35:34 | [diff] [blame] | 67 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 68 | |
[email protected] | 8e02232 | 2013-11-28 03:49:06 | [diff] [blame] | 69 | bool success = message && native_messaging_writer_; |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 70 | if (success) |
[email protected] | 8e02232 | 2013-11-28 03:49:06 | [diff] [blame] | 71 | success = native_messaging_writer_->WriteMessage(*message); |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 72 | |
| 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] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 78 | } |
| 79 | |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 80 | void PipeMessagingChannel::Shutdown() { |
gab | bf77513a | 2017-06-01 14:35:34 | [diff] [blame] | 81 | DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 82 | |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 83 | if (event_handler_) { |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 84 | // 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. |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 89 | EventHandler* handler = event_handler_; |
sergeyu | c5f104b | 2015-01-09 19:33:24 | [diff] [blame] | 90 | event_handler_ = nullptr; |
kelvinp | 3a76af7 | 2014-09-20 02:45:27 | [diff] [blame] | 91 | handler->OnDisconnect(); |
| 92 | } |
[email protected] | 5535123 | 2013-10-17 12:05:12 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | } // namespace remoting |