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