OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ipc/ipc_message_pipe_reader.h" | 5 #include "ipc/ipc_message_pipe_reader.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/bind_helpers.h" | 12 #include "base/bind_helpers.h" |
13 #include "base/location.h" | 13 #include "base/location.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/single_thread_task_runner.h" | 16 #include "base/single_thread_task_runner.h" |
17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
18 #include "ipc/ipc_channel_mojo.h" | 18 #include "ipc/ipc_channel_mojo.h" |
19 #include "mojo/public/cpp/bindings/message.h" | 19 #include "mojo/public/cpp/bindings/message.h" |
20 | 20 |
21 namespace IPC { | 21 namespace IPC { |
22 namespace internal { | 22 namespace internal { |
23 | 23 |
24 namespace { | |
25 | |
26 // Used by Send() to capture a serialized Channel::Receive message. | |
27 class MessageSerializer : public mojo::MessageReceiverWithResponder { | |
28 public: | |
29 MessageSerializer() {} | |
30 ~MessageSerializer() override {} | |
31 | |
32 mojo::Message* message() { return &message_; } | |
33 | |
34 private: | |
35 // mojo::MessageReceiverWithResponder | |
36 bool Accept(mojo::Message* message) override { | |
37 message_ = std::move(*message); | |
38 return true; | |
39 } | |
40 | |
41 bool AcceptWithResponder(mojo::Message* message, | |
42 mojo::MessageReceiver* responder) override { | |
43 NOTREACHED(); | |
44 return false; | |
45 } | |
46 | |
47 mojo::Message message_; | |
48 | |
49 DISALLOW_COPY_AND_ASSIGN(MessageSerializer); | |
50 }; | |
51 | |
52 } // namespace | |
53 | |
54 MessagePipeReader::MessagePipeReader( | 24 MessagePipeReader::MessagePipeReader( |
55 mojo::MessagePipeHandle pipe, | 25 mojo::MessagePipeHandle pipe, |
56 mojom::ChannelAssociatedPtr sender, | 26 mojom::ChannelAssociatedPtr sender, |
57 mojo::AssociatedInterfaceRequest<mojom::Channel> receiver, | 27 mojo::AssociatedInterfaceRequest<mojom::Channel> receiver, |
58 MessagePipeReader::Delegate* delegate) | 28 MessagePipeReader::Delegate* delegate) |
59 : delegate_(delegate), | 29 : delegate_(delegate), |
60 sender_(std::move(sender)), | 30 sender_(std::move(sender)), |
61 binding_(this, std::move(receiver)), | 31 binding_(this, std::move(receiver)) { |
62 sender_interface_id_(sender_.interface_id()), | |
63 sender_pipe_(pipe) { | |
64 sender_.set_connection_error_handler( | 32 sender_.set_connection_error_handler( |
65 base::Bind(&MessagePipeReader::OnPipeError, base::Unretained(this), | 33 base::Bind(&MessagePipeReader::OnPipeError, base::Unretained(this), |
66 MOJO_RESULT_FAILED_PRECONDITION)); | 34 MOJO_RESULT_FAILED_PRECONDITION)); |
67 binding_.set_connection_error_handler( | 35 binding_.set_connection_error_handler( |
68 base::Bind(&MessagePipeReader::OnPipeError, base::Unretained(this), | 36 base::Bind(&MessagePipeReader::OnPipeError, base::Unretained(this), |
69 MOJO_RESULT_FAILED_PRECONDITION)); | 37 MOJO_RESULT_FAILED_PRECONDITION)); |
70 } | 38 } |
71 | 39 |
72 MessagePipeReader::~MessagePipeReader() { | 40 MessagePipeReader::~MessagePipeReader() { |
73 DCHECK(thread_checker_.CalledOnValidThread()); | 41 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 16 matching lines...) Expand all Loading... |
90 MojoResult result = MOJO_RESULT_OK; | 58 MojoResult result = MOJO_RESULT_OK; |
91 result = ChannelMojo::ReadFromMessageAttachmentSet(message.get(), &handles); | 59 result = ChannelMojo::ReadFromMessageAttachmentSet(message.get(), &handles); |
92 if (result != MOJO_RESULT_OK) | 60 if (result != MOJO_RESULT_OK) |
93 return false; | 61 return false; |
94 | 62 |
95 std::vector<uint8_t> data(message->size()); | 63 std::vector<uint8_t> data(message->size()); |
96 std::copy(reinterpret_cast<const uint8_t*>(message->data()), | 64 std::copy(reinterpret_cast<const uint8_t*>(message->data()), |
97 reinterpret_cast<const uint8_t*>(message->data()) + message->size(), | 65 reinterpret_cast<const uint8_t*>(message->data()) + message->size(), |
98 data.data()); | 66 data.data()); |
99 | 67 |
100 MessageSerializer serializer; | 68 if (!sender_) |
101 mojom::ChannelProxy proxy(&serializer); | 69 return false; |
102 proxy.Receive(data, std::move(handles)); | |
103 mojo::Message* mojo_message = serializer.message(); | |
104 | 70 |
105 size_t num_handles = mojo_message->handles()->size(); | 71 sender_->Receive(data, std::move(handles)); |
106 DCHECK_LE(num_handles, std::numeric_limits<uint32_t>::max()); | |
107 | |
108 mojo_message->set_interface_id(sender_interface_id_); | |
109 result = mojo::WriteMessageNew(sender_pipe_, mojo_message->TakeMojoMessage(), | |
110 MOJO_WRITE_MESSAGE_FLAG_NONE); | |
111 | 72 |
112 DVLOG(4) << "Send " << message->type() << ": " << message->size(); | 73 DVLOG(4) << "Send " << message->type() << ": " << message->size(); |
113 return result == MOJO_RESULT_OK; | 74 return true; |
114 } | 75 } |
115 | 76 |
116 void MessagePipeReader::GetRemoteInterface( | 77 void MessagePipeReader::GetRemoteInterface( |
117 const std::string& name, | 78 const std::string& name, |
118 mojo::ScopedInterfaceEndpointHandle handle) { | 79 mojo::ScopedInterfaceEndpointHandle handle) { |
119 if (!sender_.is_bound()) | 80 if (!sender_.is_bound()) |
120 return; | 81 return; |
121 mojom::GenericInterfaceAssociatedRequest request; | 82 mojom::GenericInterfaceAssociatedRequest request; |
122 request.Bind(std::move(handle)); | 83 request.Bind(std::move(handle)); |
123 sender_->GetAssociatedInterface(name, std::move(request)); | 84 sender_->GetAssociatedInterface(name, std::move(request)); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
165 | 126 |
166 Close(); | 127 Close(); |
167 | 128 |
168 // NOTE: The delegate call below may delete |this|. | 129 // NOTE: The delegate call below may delete |this|. |
169 if (delegate_) | 130 if (delegate_) |
170 delegate_->OnPipeError(); | 131 delegate_->OnPipeError(); |
171 } | 132 } |
172 | 133 |
173 } // namespace internal | 134 } // namespace internal |
174 } // namespace IPC | 135 } // namespace IPC |
OLD | NEW |