1 // Copyright 2015 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
5 #include "mojo/public/cpp/bindings/pipe_control_message_proxy.h"
6
7 #include <stddef.h>
8 #include <utility>
9
10 #include "base/logging.h"
11 #include "base/macros.h"
12 #include "mojo/public/cpp/bindings/lib/serialization.h"
13 #include "mojo/public/cpp/bindings/message.h"
14 #include "mojo/public/interfaces/bindings/pipe_control_messages.mojom.h"
15
16 namespace mojo {
17 namespace {
18
ConstructRunOrClosePipeMessage(pipe_control::RunOrClosePipeInputPtr input_ptr)19 Message ConstructRunOrClosePipeMessage(
20 pipe_control::RunOrClosePipeInputPtr input_ptr) {
21 auto params_ptr = pipe_control::RunOrClosePipeMessageParams::New();
22 params_ptr->input = std::move(input_ptr);
23
24 Message message(pipe_control::kRunOrClosePipeMessageId, 0, 0, 0, nullptr);
25 internal::SerializationContext context;
26 pipe_control::internal::RunOrClosePipeMessageParams_Data::BufferWriter params;
27 internal::Serialize<pipe_control::RunOrClosePipeMessageParamsDataView>(
28 params_ptr, message.payload_buffer(), ¶ms, &context);
29 message.set_interface_id(kInvalidInterfaceId);
30 return message;
31 }
32
33 } // namespace
34
PipeControlMessageProxy(MessageReceiver * receiver)35 PipeControlMessageProxy::PipeControlMessageProxy(MessageReceiver* receiver)
36 : receiver_(receiver) {}
37
NotifyPeerEndpointClosed(InterfaceId id,const base::Optional<DisconnectReason> & reason)38 void PipeControlMessageProxy::NotifyPeerEndpointClosed(
39 InterfaceId id,
40 const base::Optional<DisconnectReason>& reason) {
41 Message message(ConstructPeerEndpointClosedMessage(id, reason));
42 ignore_result(receiver_->Accept(&message));
43 }
44
45 // static
ConstructPeerEndpointClosedMessage(InterfaceId id,const base::Optional<DisconnectReason> & reason)46 Message PipeControlMessageProxy::ConstructPeerEndpointClosedMessage(
47 InterfaceId id,
48 const base::Optional<DisconnectReason>& reason) {
49 auto event = pipe_control::PeerAssociatedEndpointClosedEvent::New();
50 event->id = id;
51 if (reason) {
52 event->disconnect_reason = pipe_control::DisconnectReason::New();
53 event->disconnect_reason->custom_reason = reason->custom_reason;
54 event->disconnect_reason->description = reason->description;
55 }
56
57 auto input = pipe_control::RunOrClosePipeInput::New();
58 input->set_peer_associated_endpoint_closed_event(std::move(event));
59
60 return ConstructRunOrClosePipeMessage(std::move(input));
61 }
62
63 } // namespace mojo
64