| 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/native_messaging_channel.h" | 5 #include "remoting/host/native_messaging/pipe_messaging_channel.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/callback_helpers.h" | 10 #include "base/callback_helpers.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 | 13 |
| 14 #if defined(OS_POSIX) | 14 #if defined(OS_POSIX) |
| 15 #include <unistd.h> | 15 #include <unistd.h> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 36 return base::File(result); | 36 return base::File(result); |
| 37 #else | 37 #else |
| 38 #error Not implemented. | 38 #error Not implemented. |
| 39 #endif | 39 #endif |
| 40 } | 40 } |
| 41 | 41 |
| 42 } // namespace | 42 } // namespace |
| 43 | 43 |
| 44 namespace remoting { | 44 namespace remoting { |
| 45 | 45 |
| 46 NativeMessagingChannel::NativeMessagingChannel( | 46 PipeMessagingChannel::PipeMessagingChannel( |
| 47 base::File input, | 47 base::File input, |
| 48 base::File output) | 48 base::File output) |
| 49 : native_messaging_reader_(DuplicatePlatformFile(input.Pass())), | 49 : native_messaging_reader_(DuplicatePlatformFile(input.Pass())), |
| 50 native_messaging_writer_(new NativeMessagingWriter( | 50 native_messaging_writer_(new NativeMessagingWriter( |
| 51 DuplicatePlatformFile(output.Pass()))), | 51 DuplicatePlatformFile(output.Pass()))), |
| 52 event_handler_(NULL), |
| 52 weak_factory_(this) { | 53 weak_factory_(this) { |
| 53 weak_ptr_ = weak_factory_.GetWeakPtr(); | 54 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 54 } | 55 } |
| 55 | 56 |
| 56 NativeMessagingChannel::~NativeMessagingChannel() { | 57 PipeMessagingChannel::~PipeMessagingChannel() { |
| 57 } | 58 } |
| 58 | 59 |
| 59 void NativeMessagingChannel::Start(const SendMessageCallback& received_message, | 60 void PipeMessagingChannel::Start(EventHandler* event_handler) { |
| 60 const base::Closure& quit_closure) { | |
| 61 DCHECK(CalledOnValidThread()); | 61 DCHECK(CalledOnValidThread()); |
| 62 DCHECK(received_message_.is_null()); | 62 DCHECK(NULL == event_handler_); |
| 63 DCHECK(quit_closure_.is_null()); | |
| 64 | 63 |
| 65 received_message_ = received_message; | 64 event_handler_ = event_handler; |
| 66 quit_closure_ = quit_closure; | 65 DCHECK(event_handler_); |
| 67 | 66 |
| 68 native_messaging_reader_.Start( | 67 native_messaging_reader_.Start( |
| 69 base::Bind(&NativeMessagingChannel::ProcessMessage, weak_ptr_), | 68 base::Bind(&PipeMessagingChannel::ProcessMessage, weak_ptr_), |
| 70 base::Bind(&NativeMessagingChannel::Shutdown, weak_ptr_)); | 69 base::Bind(&PipeMessagingChannel::Shutdown, weak_ptr_)); |
| 71 } | 70 } |
| 72 | 71 |
| 73 void NativeMessagingChannel::ProcessMessage(scoped_ptr<base::Value> message) { | 72 void PipeMessagingChannel::ProcessMessage(scoped_ptr<base::Value> message) { |
| 74 DCHECK(CalledOnValidThread()); | 73 DCHECK(CalledOnValidThread()); |
| 75 | 74 |
| 76 if (message->GetType() != base::Value::TYPE_DICTIONARY) { | 75 if (message->GetType() != base::Value::TYPE_DICTIONARY) { |
| 77 LOG(ERROR) << "Expected DictionaryValue"; | 76 LOG(ERROR) << "Expected DictionaryValue"; |
| 78 Shutdown(); | 77 Shutdown(); |
| 79 return; | 78 return; |
| 80 } | 79 } |
| 81 | 80 |
| 82 scoped_ptr<base::DictionaryValue> message_dict( | 81 if (event_handler_) { |
| 83 static_cast<base::DictionaryValue*>(message.release())); | 82 event_handler_->OnMessage(message.Pass()); |
| 84 received_message_.Run(message_dict.Pass()); | 83 } |
| 85 } | 84 } |
| 86 | 85 |
| 87 void NativeMessagingChannel::SendMessage( | 86 void PipeMessagingChannel::SendMessage( |
| 88 scoped_ptr<base::DictionaryValue> message) { | 87 scoped_ptr<base::Value> message) { |
| 89 DCHECK(CalledOnValidThread()); | 88 DCHECK(CalledOnValidThread()); |
| 90 | 89 |
| 91 bool success = message && native_messaging_writer_; | 90 bool success = message && native_messaging_writer_; |
| 92 if (success) | 91 if (success) |
| 93 success = native_messaging_writer_->WriteMessage(*message); | 92 success = native_messaging_writer_->WriteMessage(*message); |
| 94 | 93 |
| 95 if (!success) { | 94 if (!success) { |
| 96 // Close the write pipe so no more responses will be sent. | 95 // Close the write pipe so no more responses will be sent. |
| 97 native_messaging_writer_.reset(); | 96 native_messaging_writer_.reset(); |
| 98 Shutdown(); | 97 Shutdown(); |
| 99 } | 98 } |
| 100 } | 99 } |
| 101 | 100 |
| 102 void NativeMessagingChannel::Shutdown() { | 101 void PipeMessagingChannel::Shutdown() { |
| 103 DCHECK(CalledOnValidThread()); | 102 DCHECK(CalledOnValidThread()); |
| 104 | 103 |
| 105 if (!quit_closure_.is_null()) | 104 if (event_handler_) { |
| 106 base::ResetAndReturn(&quit_closure_).Run(); | 105 // Set event_handler_ to NULL to indicate the object is in a shutdown cycle. |
| 106 // Since event_handler->OnDisconnect() will destroy the current object, |
| 107 // |event_handler_| will become a dangling pointer after OnDisconnect() |
| 108 // returns. Therefore, we set |event_handler_| to NULL beforehand. |
| 109 EventHandler* handler = event_handler_; |
| 110 event_handler_ = NULL; |
| 111 handler->OnDisconnect(); |
| 112 } |
| 107 } | 113 } |
| 108 | 114 |
| 109 } // namespace remoting | 115 } // namespace remoting |
| OLD | NEW |