| 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> |
| 8 |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 10 #include "base/location.h" | 12 #include "base/location.h" |
| 11 #include "base/values.h" | 13 #include "base/values.h" |
| 12 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 13 | 15 |
| 14 #if defined(OS_POSIX) | 16 #if defined(OS_POSIX) |
| 15 #include <unistd.h> | 17 #include <unistd.h> |
| 16 #endif | 18 #endif |
| (...skipping 19 matching lines...) Expand all Loading... |
| 36 return base::File(result); | 38 return base::File(result); |
| 37 #else | 39 #else |
| 38 #error Not implemented. | 40 #error Not implemented. |
| 39 #endif | 41 #endif |
| 40 } | 42 } |
| 41 | 43 |
| 42 } // namespace | 44 } // namespace |
| 43 | 45 |
| 44 namespace remoting { | 46 namespace remoting { |
| 45 | 47 |
| 46 PipeMessagingChannel::PipeMessagingChannel( | 48 PipeMessagingChannel::PipeMessagingChannel(base::File input, base::File output) |
| 47 base::File input, | 49 : native_messaging_reader_(DuplicatePlatformFile(std::move(input))), |
| 48 base::File output) | 50 native_messaging_writer_( |
| 49 : native_messaging_reader_(DuplicatePlatformFile(input.Pass())), | 51 new NativeMessagingWriter(DuplicatePlatformFile(std::move(output)))), |
| 50 native_messaging_writer_(new NativeMessagingWriter( | |
| 51 DuplicatePlatformFile(output.Pass()))), | |
| 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 } | |
| 59 | 58 |
| 60 void PipeMessagingChannel::Start(EventHandler* event_handler) { | 59 void PipeMessagingChannel::Start(EventHandler* event_handler) { |
| 61 DCHECK(CalledOnValidThread()); | 60 DCHECK(CalledOnValidThread()); |
| 62 DCHECK(!event_handler_); | 61 DCHECK(!event_handler_); |
| 63 | 62 |
| 64 event_handler_ = event_handler; | 63 event_handler_ = event_handler; |
| 65 DCHECK(event_handler_); | 64 DCHECK(event_handler_); |
| 66 | 65 |
| 67 native_messaging_reader_.Start( | 66 native_messaging_reader_.Start( |
| 68 base::Bind(&PipeMessagingChannel::ProcessMessage, weak_ptr_), | 67 base::Bind(&PipeMessagingChannel::ProcessMessage, weak_ptr_), |
| 69 base::Bind(&PipeMessagingChannel::Shutdown, weak_ptr_)); | 68 base::Bind(&PipeMessagingChannel::Shutdown, weak_ptr_)); |
| 70 } | 69 } |
| 71 | 70 |
| 72 void PipeMessagingChannel::ProcessMessage(scoped_ptr<base::Value> message) { | 71 void PipeMessagingChannel::ProcessMessage(scoped_ptr<base::Value> message) { |
| 73 DCHECK(CalledOnValidThread()); | 72 DCHECK(CalledOnValidThread()); |
| 74 | 73 |
| 75 if (event_handler_) | 74 if (event_handler_) |
| 76 event_handler_->OnMessage(message.Pass()); | 75 event_handler_->OnMessage(std::move(message)); |
| 77 } | 76 } |
| 78 | 77 |
| 79 void PipeMessagingChannel::SendMessage( | 78 void PipeMessagingChannel::SendMessage(scoped_ptr<base::Value> message) { |
| 80 scoped_ptr<base::Value> message) { | |
| 81 DCHECK(CalledOnValidThread()); | 79 DCHECK(CalledOnValidThread()); |
| 82 | 80 |
| 83 bool success = message && native_messaging_writer_; | 81 bool success = message && native_messaging_writer_; |
| 84 if (success) | 82 if (success) |
| 85 success = native_messaging_writer_->WriteMessage(*message); | 83 success = native_messaging_writer_->WriteMessage(*message); |
| 86 | 84 |
| 87 if (!success) { | 85 if (!success) { |
| 88 // Close the write pipe so no more responses will be sent. | 86 // Close the write pipe so no more responses will be sent. |
| 89 native_messaging_writer_.reset(); | 87 native_messaging_writer_.reset(); |
| 90 Shutdown(); | 88 Shutdown(); |
| 91 } | 89 } |
| 92 } | 90 } |
| 93 | 91 |
| 94 void PipeMessagingChannel::Shutdown() { | 92 void PipeMessagingChannel::Shutdown() { |
| 95 DCHECK(CalledOnValidThread()); | 93 DCHECK(CalledOnValidThread()); |
| 96 | 94 |
| 97 if (event_handler_) { | 95 if (event_handler_) { |
| 98 // Set |event_handler_| to nullptr to indicate the object is in a shutdown | 96 // Set |event_handler_| to nullptr to indicate the object is in a shutdown |
| 99 // cycle. Since event_handler->OnDisconnect() will destroy the current | 97 // cycle. Since event_handler->OnDisconnect() will destroy the current |
| 100 // object, |event_handler_| will become a dangling pointer after | 98 // object, |event_handler_| will become a dangling pointer after |
| 101 // OnDisconnect() returns. Therefore, we set |event_handler_| to nullptr | 99 // OnDisconnect() returns. Therefore, we set |event_handler_| to nullptr |
| 102 // beforehand. | 100 // beforehand. |
| 103 EventHandler* handler = event_handler_; | 101 EventHandler* handler = event_handler_; |
| 104 event_handler_ = nullptr; | 102 event_handler_ = nullptr; |
| 105 handler->OnDisconnect(); | 103 handler->OnDisconnect(); |
| 106 } | 104 } |
| 107 } | 105 } |
| 108 | 106 |
| 109 } // namespace remoting | 107 } // namespace remoting |
| OLD | NEW |