blob: cd174a2539309f6da3c8785dcbcb6cd351311672 [file] [log] [blame]
[email protected]64860882014-08-04 23:44:171// Copyright 2014 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
amistryd4aa70d2016-06-23 07:52:375#include "ipc/ipc_message_pipe_reader.h"
[email protected]64860882014-08-04 23:44:176
tfarina10a5c062015-09-04 18:47:577#include <stdint.h>
rockot506f92fa22016-03-23 01:32:188
dchenge48600452015-12-28 02:24:509#include <utility>
tfarina10a5c062015-09-04 18:47:5710
[email protected]64860882014-08-04 23:44:1711#include "base/bind.h"
12#include "base/bind_helpers.h"
13#include "base/location.h"
14#include "base/logging.h"
rockot506f92fa22016-03-23 01:32:1815#include "base/macros.h"
skyostile687bdff2015-05-12 11:29:2116#include "base/single_thread_task_runner.h"
gabf08ccc02016-05-11 18:51:1117#include "base/threading/thread_task_runner_handle.h"
amistryd4aa70d2016-06-23 07:52:3718#include "ipc/ipc_channel_mojo.h"
rockot506f92fa22016-03-23 01:32:1819#include "mojo/public/cpp/bindings/message.h"
[email protected]64860882014-08-04 23:44:1720
21namespace IPC {
22namespace internal {
23
sammce4d0abd2016-03-07 22:38:0424MessagePipeReader::MessagePipeReader(
rockot506f92fa22016-03-23 01:32:1825 mojo::MessagePipeHandle pipe,
sammce4d0abd2016-03-07 22:38:0426 mojom::ChannelAssociatedPtr sender,
27 mojo::AssociatedInterfaceRequest<mojom::Channel> receiver,
28 MessagePipeReader::Delegate* delegate)
29 : delegate_(delegate),
30 sender_(std::move(sender)),
rockot401fb2c2016-09-06 18:35:5731 binding_(this, std::move(receiver)) {
sammce4d0abd2016-03-07 22:38:0432 sender_.set_connection_error_handler(
33 base::Bind(&MessagePipeReader::OnPipeError, base::Unretained(this),
34 MOJO_RESULT_FAILED_PRECONDITION));
35 binding_.set_connection_error_handler(
36 base::Bind(&MessagePipeReader::OnPipeError, base::Unretained(this),
37 MOJO_RESULT_FAILED_PRECONDITION));
38}
[email protected]64860882014-08-04 23:44:1739
40MessagePipeReader::~MessagePipeReader() {
amistry0b0e7482015-09-02 18:04:2241 DCHECK(thread_checker_.CalledOnValidThread());
morritab4472142015-04-20 21:20:1242 // The pipe should be closed before deletion.
[email protected]64860882014-08-04 23:44:1743}
44
45void MessagePipeReader::Close() {
amistry0b0e7482015-09-02 18:04:2246 DCHECK(thread_checker_.CalledOnValidThread());
sammce4d0abd2016-03-07 22:38:0447 sender_.reset();
48 if (binding_.is_bound())
49 binding_.Close();
morritab4472142015-04-20 21:20:1250}
51
danakj03de39b22016-04-23 04:21:0952bool MessagePipeReader::Send(std::unique_ptr<Message> message) {
yuhaoz9b8157d2015-08-18 22:21:3553 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"),
54 "MessagePipeReader::Send",
55 message->flags(),
56 TRACE_EVENT_FLAG_FLOW_OUT);
yzshen24b40a32016-08-24 01:10:1357 base::Optional<std::vector<mojom::SerializedHandlePtr>> handles;
morritad68bedf42014-11-25 23:35:5758 MojoResult result = MOJO_RESULT_OK;
rockot9691a7b2016-03-18 18:58:1559 result = ChannelMojo::ReadFromMessageAttachmentSet(message.get(), &handles);
rockot506f92fa22016-03-23 01:32:1860 if (result != MOJO_RESULT_OK)
morritad68bedf42014-11-25 23:35:5761 return false;
rockot506f92fa22016-03-23 01:32:1862
yzshen24b40a32016-08-24 01:10:1363 std::vector<uint8_t> data(message->size());
sammce4d0abd2016-03-07 22:38:0464 std::copy(reinterpret_cast<const uint8_t*>(message->data()),
65 reinterpret_cast<const uint8_t*>(message->data()) + message->size(),
yzshen24b40a32016-08-24 01:10:1366 data.data());
rockot506f92fa22016-03-23 01:32:1867
rockot401fb2c2016-09-06 18:35:5768 if (!sender_)
69 return false;
rockot506f92fa22016-03-23 01:32:1870
rockot401fb2c2016-09-06 18:35:5771 sender_->Receive(data, std::move(handles));
rockot506f92fa22016-03-23 01:32:1872
sammce4d0abd2016-03-07 22:38:0473 DVLOG(4) << "Send " << message->type() << ": " << message->size();
rockot401fb2c2016-09-06 18:35:5774 return true;
morritad68bedf42014-11-25 23:35:5775}
76
rockot7c6bf952016-07-14 00:34:1177void MessagePipeReader::GetRemoteInterface(
78 const std::string& name,
79 mojo::ScopedInterfaceEndpointHandle handle) {
rockot5a908952016-07-28 20:08:1780 if (!sender_.is_bound())
81 return;
Ken Rockot96d1b7b52017-05-13 00:29:2182 sender_->GetAssociatedInterface(
83 name, mojom::GenericInterfaceAssociatedRequest(std::move(handle)));
rockot7c6bf952016-07-14 00:34:1184}
85
rockot0e4de5f2016-07-22 21:18:0786void MessagePipeReader::SetPeerPid(int32_t peer_pid) {
sammcf810f07f2016-11-10 22:34:0787 delegate_->OnPeerPidReceived(peer_pid);
rockot0e4de5f2016-07-22 21:18:0788}
89
rockot9691a7b2016-03-18 18:58:1590void MessagePipeReader::Receive(
yzshen24b40a32016-08-24 01:10:1391 const std::vector<uint8_t>& data,
92 base::Optional<std::vector<mojom::SerializedHandlePtr>> handles) {
rockot9691a7b2016-03-18 18:58:1593 Message message(
yzshen24b40a32016-08-24 01:10:1394 data.empty() ? "" : reinterpret_cast<const char*>(data.data()),
rockot9691a7b2016-03-18 18:58:1595 static_cast<uint32_t>(data.size()));
morritad68bedf42014-11-25 23:35:5796
sammce4d0abd2016-03-07 22:38:0497 DVLOG(4) << "Receive " << message.type() << ": " << message.size();
rockot9691a7b2016-03-18 18:58:1598 MojoResult write_result =
99 ChannelMojo::WriteToMessageAttachmentSet(std::move(handles), &message);
morritad68bedf42014-11-25 23:35:57100 if (write_result != MOJO_RESULT_OK) {
rockot506f92fa22016-03-23 01:32:18101 OnPipeError(write_result);
morritad68bedf42014-11-25 23:35:57102 return;
103 }
morritad68bedf42014-11-25 23:35:57104
yuhaoz9b8157d2015-08-18 22:21:35105 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"),
rockot506f92fa22016-03-23 01:32:18106 "MessagePipeReader::Receive",
yuhaoz9b8157d2015-08-18 22:21:35107 message.flags(),
108 TRACE_EVENT_FLAG_FLOW_IN);
morritad68bedf42014-11-25 23:35:57109 delegate_->OnMessageReceived(message);
[email protected]64860882014-08-04 23:44:17110}
111
rockot7c6bf952016-07-14 00:34:11112void MessagePipeReader::GetAssociatedInterface(
yzshen24b40a32016-08-24 01:10:13113 const std::string& name,
rockot7c6bf952016-07-14 00:34:11114 mojom::GenericInterfaceAssociatedRequest request) {
115 DCHECK(thread_checker_.CalledOnValidThread());
116 if (delegate_)
117 delegate_->OnAssociatedInterfaceRequest(name, request.PassHandle());
118}
119
morritad68bedf42014-11-25 23:35:57120void MessagePipeReader::OnPipeError(MojoResult error) {
amistry0b0e7482015-09-02 18:04:22121 DCHECK(thread_checker_.CalledOnValidThread());
rockot0e4de5f2016-07-22 21:18:07122
123 Close();
124
125 // NOTE: The delegate call below may delete |this|.
sammce4d0abd2016-03-07 22:38:04126 if (delegate_)
rockot506f92fa22016-03-23 01:32:18127 delegate_->OnPipeError();
[email protected]64860882014-08-04 23:44:17128}
129
130} // namespace internal
131} // namespace IPC