| 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/native_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" |
| (...skipping 27 matching lines...) Expand all Loading... |
| 38 #else | 38 #else |
| 39 #error Not implemented. | 39 #error Not implemented. |
| 40 #endif | 40 #endif |
| 41 } | 41 } |
| 42 | 42 |
| 43 } // namespace | 43 } // namespace |
| 44 | 44 |
| 45 namespace remoting { | 45 namespace remoting { |
| 46 | 46 |
| 47 NativeMessagingChannel::NativeMessagingChannel( | 47 NativeMessagingChannel::NativeMessagingChannel( |
| 48 scoped_ptr<Delegate> delegate, | |
| 49 base::PlatformFile input, | 48 base::PlatformFile input, |
| 50 base::PlatformFile output) | 49 base::PlatformFile output) |
| 51 : native_messaging_reader_(DuplicatePlatformFile(input)), | 50 : native_messaging_reader_(DuplicatePlatformFile(input)), |
| 52 native_messaging_writer_(new NativeMessagingWriter( | 51 native_messaging_writer_(new NativeMessagingWriter( |
| 53 DuplicatePlatformFile(output))), | 52 DuplicatePlatformFile(output))), |
| 54 delegate_(delegate.Pass()), | |
| 55 weak_factory_(this) { | 53 weak_factory_(this) { |
| 56 weak_ptr_ = weak_factory_.GetWeakPtr(); | 54 weak_ptr_ = weak_factory_.GetWeakPtr(); |
| 57 delegate_->SetSendMessageCallback( | |
| 58 base::Bind(&NativeMessagingChannel::SendMessage, weak_ptr_)); | |
| 59 } | 55 } |
| 60 | 56 |
| 61 NativeMessagingChannel::~NativeMessagingChannel() { | 57 NativeMessagingChannel::~NativeMessagingChannel() { |
| 62 } | 58 } |
| 63 | 59 |
| 64 void NativeMessagingChannel::Start(const base::Closure& quit_closure) { | 60 void NativeMessagingChannel::Start(const SendMessageCallback& received_message, |
| 61 const base::Closure& quit_closure) { |
| 65 DCHECK(CalledOnValidThread()); | 62 DCHECK(CalledOnValidThread()); |
| 63 DCHECK(received_message_.is_null()); |
| 66 DCHECK(quit_closure_.is_null()); | 64 DCHECK(quit_closure_.is_null()); |
| 67 DCHECK(!quit_closure.is_null()); | |
| 68 | 65 |
| 66 received_message_ = received_message; |
| 69 quit_closure_ = quit_closure; | 67 quit_closure_ = quit_closure; |
| 68 |
| 70 native_messaging_reader_.Start( | 69 native_messaging_reader_.Start( |
| 71 base::Bind(&NativeMessagingChannel::ProcessMessage, weak_ptr_), | 70 base::Bind(&NativeMessagingChannel::ProcessMessage, weak_ptr_), |
| 72 base::Bind(&NativeMessagingChannel::Shutdown, weak_ptr_)); | 71 base::Bind(&NativeMessagingChannel::Shutdown, weak_ptr_)); |
| 73 } | 72 } |
| 74 | 73 |
| 75 void NativeMessagingChannel::ProcessMessage(scoped_ptr<base::Value> message) { | 74 void NativeMessagingChannel::ProcessMessage(scoped_ptr<base::Value> message) { |
| 76 DCHECK(CalledOnValidThread()); | 75 DCHECK(CalledOnValidThread()); |
| 77 | 76 |
| 78 if (message->GetType() != base::Value::TYPE_DICTIONARY) { | 77 if (message->GetType() != base::Value::TYPE_DICTIONARY) { |
| 79 LOG(ERROR) << "Expected DictionaryValue"; | 78 LOG(ERROR) << "Expected DictionaryValue"; |
| 80 Shutdown(); | 79 Shutdown(); |
| 81 return; | 80 return; |
| 82 } | 81 } |
| 83 | 82 |
| 84 scoped_ptr<base::DictionaryValue> message_dict( | 83 scoped_ptr<base::DictionaryValue> message_dict( |
| 85 static_cast<base::DictionaryValue*>(message.release())); | 84 static_cast<base::DictionaryValue*>(message.release())); |
| 86 delegate_->ProcessMessage(message_dict.Pass()); | 85 received_message_.Run(message_dict.Pass()); |
| 87 } | 86 } |
| 88 | 87 |
| 89 void NativeMessagingChannel::SendMessage( | 88 void NativeMessagingChannel::SendMessage( |
| 90 scoped_ptr<base::DictionaryValue> message) { | 89 scoped_ptr<base::DictionaryValue> message) { |
| 91 DCHECK(CalledOnValidThread()); | 90 DCHECK(CalledOnValidThread()); |
| 92 | 91 |
| 93 bool success = message && native_messaging_writer_; | 92 bool success = message && native_messaging_writer_; |
| 94 if (success) | 93 if (success) |
| 95 success = native_messaging_writer_->WriteMessage(*message); | 94 success = native_messaging_writer_->WriteMessage(*message); |
| 96 | 95 |
| 97 if (!success) { | 96 if (!success) { |
| 98 // Close the write pipe so no more responses will be sent. | 97 // Close the write pipe so no more responses will be sent. |
| 99 native_messaging_writer_.reset(); | 98 native_messaging_writer_.reset(); |
| 100 Shutdown(); | 99 Shutdown(); |
| 101 } | 100 } |
| 102 } | 101 } |
| 103 | 102 |
| 104 void NativeMessagingChannel::Shutdown() { | 103 void NativeMessagingChannel::Shutdown() { |
| 105 DCHECK(CalledOnValidThread()); | 104 DCHECK(CalledOnValidThread()); |
| 106 | 105 |
| 107 if (!quit_closure_.is_null()) | 106 if (!quit_closure_.is_null()) |
| 108 base::ResetAndReturn(&quit_closure_).Run(); | 107 base::ResetAndReturn(&quit_closure_).Run(); |
| 109 } | 108 } |
| 110 | 109 |
| 111 } // namespace remoting | 110 } // namespace remoting |
| OLD | NEW |