blob: b055d413f458e1b464277f15edcee35327461f01 [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) {
Roman Karaseva43d5b4e2017-12-21 03:06:0253 CHECK(message->IsValid());
yuhaoz9b8157d2015-08-18 22:21:3554 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"),
Roman Karaseva43d5b4e2017-12-21 03:06:0255 "MessagePipeReader::Send", message->flags(),
yuhaoz9b8157d2015-08-18 22:21:3556 TRACE_EVENT_FLAG_FLOW_OUT);
Eve Martin-Jones475e7e62018-02-13 22:57:2557 base::Optional<std::vector<mojo::native::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
rockot401fb2c2016-09-06 18:35:5763 if (!sender_)
64 return false;
rockot506f92fa22016-03-23 01:32:1865
Ken Rockot4c5bd802018-07-12 01:37:1166 sender_->Receive(MessageView(*message, std::move(handles)));
sammce4d0abd2016-03-07 22:38:0467 DVLOG(4) << "Send " << message->type() << ": " << message->size();
rockot401fb2c2016-09-06 18:35:5768 return true;
morritad68bedf42014-11-25 23:35:5769}
70
rockot7c6bf952016-07-14 00:34:1171void MessagePipeReader::GetRemoteInterface(
72 const std::string& name,
73 mojo::ScopedInterfaceEndpointHandle handle) {
rockot5a908952016-07-28 20:08:1774 if (!sender_.is_bound())
75 return;
Ken Rockot96d1b7b52017-05-13 00:29:2176 sender_->GetAssociatedInterface(
77 name, mojom::GenericInterfaceAssociatedRequest(std::move(handle)));
rockot7c6bf952016-07-14 00:34:1178}
79
rockot0e4de5f2016-07-22 21:18:0780void MessagePipeReader::SetPeerPid(int32_t peer_pid) {
sammcf810f07f2016-11-10 22:34:0781 delegate_->OnPeerPidReceived(peer_pid);
rockot0e4de5f2016-07-22 21:18:0782}
83
Ken Rockot4c5bd802018-07-12 01:37:1184void MessagePipeReader::Receive(MessageView message_view) {
85 if (!message_view.size()) {
Roman Karaseva43d5b4e2017-12-21 03:06:0286 delegate_->OnBrokenDataReceived();
87 return;
88 }
Ken Rockot4c5bd802018-07-12 01:37:1189 Message message(message_view.data(), message_view.size());
Roman Karaseva43d5b4e2017-12-21 03:06:0290 if (!message.IsValid()) {
91 delegate_->OnBrokenDataReceived();
92 return;
93 }
morritad68bedf42014-11-25 23:35:5794
sammce4d0abd2016-03-07 22:38:0495 DVLOG(4) << "Receive " << message.type() << ": " << message.size();
Ken Rockot4c5bd802018-07-12 01:37:1196 MojoResult write_result = ChannelMojo::WriteToMessageAttachmentSet(
97 message_view.TakeHandles(), &message);
morritad68bedf42014-11-25 23:35:5798 if (write_result != MOJO_RESULT_OK) {
rockot506f92fa22016-03-23 01:32:1899 OnPipeError(write_result);
morritad68bedf42014-11-25 23:35:57100 return;
101 }
morritad68bedf42014-11-25 23:35:57102
yuhaoz9b8157d2015-08-18 22:21:35103 TRACE_EVENT_WITH_FLOW0(TRACE_DISABLED_BY_DEFAULT("ipc.flow"),
rockot506f92fa22016-03-23 01:32:18104 "MessagePipeReader::Receive",
yuhaoz9b8157d2015-08-18 22:21:35105 message.flags(),
106 TRACE_EVENT_FLAG_FLOW_IN);
morritad68bedf42014-11-25 23:35:57107 delegate_->OnMessageReceived(message);
[email protected]64860882014-08-04 23:44:17108}
109
rockot7c6bf952016-07-14 00:34:11110void MessagePipeReader::GetAssociatedInterface(
yzshen24b40a32016-08-24 01:10:13111 const std::string& name,
rockot7c6bf952016-07-14 00:34:11112 mojom::GenericInterfaceAssociatedRequest request) {
113 DCHECK(thread_checker_.CalledOnValidThread());
114 if (delegate_)
115 delegate_->OnAssociatedInterfaceRequest(name, request.PassHandle());
116}
117
morritad68bedf42014-11-25 23:35:57118void MessagePipeReader::OnPipeError(MojoResult error) {
amistry0b0e7482015-09-02 18:04:22119 DCHECK(thread_checker_.CalledOnValidThread());
rockot0e4de5f2016-07-22 21:18:07120
121 Close();
122
123 // NOTE: The delegate call below may delete |this|.
sammce4d0abd2016-03-07 22:38:04124 if (delegate_)
rockot506f92fa22016-03-23 01:32:18125 delegate_->OnPipeError();
[email protected]64860882014-08-04 23:44:17126}
127
128} // namespace internal
129} // namespace IPC