| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/host/native_messaging/pipe_messaging_channel.h" | 5 #include "remoting/host/native_messaging/pipe_messaging_channel.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 47 | 47 |
| 48 PipeMessagingChannel::PipeMessagingChannel(base::File input, base::File output) | 48 PipeMessagingChannel::PipeMessagingChannel(base::File input, base::File output) |
| 49 : native_messaging_reader_(DuplicatePlatformFile(std::move(input))), | 49 : native_messaging_reader_(DuplicatePlatformFile(std::move(input))), |
| 50 native_messaging_writer_( | 50 native_messaging_writer_( |
| 51 new NativeMessagingWriter(DuplicatePlatformFile(std::move(output)))), | 51 new NativeMessagingWriter(DuplicatePlatformFile(std::move(output)))), |
| 52 event_handler_(nullptr), | 52 event_handler_(nullptr), |
| 53 weak_factory_(this) { | 53 weak_factory_(this) { |
| 54 weak_ptr_ = weak_factory_.GetWeakPtr(); | 54 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 PipeMessagingChannel::~PipeMessagingChannel() {} | 57 PipeMessagingChannel::~PipeMessagingChannel() { |
| 58 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 59 } |
| 58 | 60 |
| 59 void PipeMessagingChannel::Start(EventHandler* event_handler) { | 61 void PipeMessagingChannel::Start(EventHandler* event_handler) { |
| 60 DCHECK(CalledOnValidThread()); | 62 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 61 DCHECK(!event_handler_); | 63 DCHECK(!event_handler_); |
| 62 | 64 |
| 63 event_handler_ = event_handler; | 65 event_handler_ = event_handler; |
| 64 DCHECK(event_handler_); | 66 DCHECK(event_handler_); |
| 65 | 67 |
| 66 native_messaging_reader_.Start( | 68 native_messaging_reader_.Start( |
| 67 base::Bind(&PipeMessagingChannel::ProcessMessage, weak_ptr_), | 69 base::Bind(&PipeMessagingChannel::ProcessMessage, weak_ptr_), |
| 68 base::Bind(&PipeMessagingChannel::Shutdown, weak_ptr_)); | 70 base::Bind(&PipeMessagingChannel::Shutdown, weak_ptr_)); |
| 69 } | 71 } |
| 70 | 72 |
| 71 void PipeMessagingChannel::ProcessMessage( | 73 void PipeMessagingChannel::ProcessMessage( |
| 72 std::unique_ptr<base::Value> message) { | 74 std::unique_ptr<base::Value> message) { |
| 73 DCHECK(CalledOnValidThread()); | 75 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 74 | 76 |
| 75 if (event_handler_) | 77 if (event_handler_) |
| 76 event_handler_->OnMessage(std::move(message)); | 78 event_handler_->OnMessage(std::move(message)); |
| 77 } | 79 } |
| 78 | 80 |
| 79 void PipeMessagingChannel::SendMessage(std::unique_ptr<base::Value> message) { | 81 void PipeMessagingChannel::SendMessage(std::unique_ptr<base::Value> message) { |
| 80 DCHECK(CalledOnValidThread()); | 82 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 81 | 83 |
| 82 bool success = message && native_messaging_writer_; | 84 bool success = message && native_messaging_writer_; |
| 83 if (success) | 85 if (success) |
| 84 success = native_messaging_writer_->WriteMessage(*message); | 86 success = native_messaging_writer_->WriteMessage(*message); |
| 85 | 87 |
| 86 if (!success) { | 88 if (!success) { |
| 87 // Close the write pipe so no more responses will be sent. | 89 // Close the write pipe so no more responses will be sent. |
| 88 native_messaging_writer_.reset(); | 90 native_messaging_writer_.reset(); |
| 89 Shutdown(); | 91 Shutdown(); |
| 90 } | 92 } |
| 91 } | 93 } |
| 92 | 94 |
| 93 void PipeMessagingChannel::Shutdown() { | 95 void PipeMessagingChannel::Shutdown() { |
| 94 DCHECK(CalledOnValidThread()); | 96 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 95 | 97 |
| 96 if (event_handler_) { | 98 if (event_handler_) { |
| 97 // Set |event_handler_| to nullptr to indicate the object is in a shutdown | 99 // Set |event_handler_| to nullptr to indicate the object is in a shutdown |
| 98 // cycle. Since event_handler->OnDisconnect() will destroy the current | 100 // cycle. Since event_handler->OnDisconnect() will destroy the current |
| 99 // object, |event_handler_| will become a dangling pointer after | 101 // object, |event_handler_| will become a dangling pointer after |
| 100 // OnDisconnect() returns. Therefore, we set |event_handler_| to nullptr | 102 // OnDisconnect() returns. Therefore, we set |event_handler_| to nullptr |
| 101 // beforehand. | 103 // beforehand. |
| 102 EventHandler* handler = event_handler_; | 104 EventHandler* handler = event_handler_; |
| 103 event_handler_ = nullptr; | 105 event_handler_ = nullptr; |
| 104 handler->OnDisconnect(); | 106 handler->OnDisconnect(); |
| 105 } | 107 } |
| 106 } | 108 } |
| 107 | 109 |
| 108 } // namespace remoting | 110 } // namespace remoting |
| OLD | NEW |