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/compiler_specific.h"
11 #include "base/logging.h"
12 #include "mojo/public/cpp/bindings/lib/message_builder.h"
13 #include "mojo/public/cpp/bindings/lib/serialization.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 internal::SerializationContext context;
22
23 auto params_ptr = pipe_control::RunOrClosePipeMessageParams::New();
24 params_ptr->input = std::move(input_ptr);
25
26 size_t size = internal::PrepareToSerialize<
27 pipe_control::RunOrClosePipeMessageParamsDataView>(params_ptr, &context);
28 internal::MessageBuilder builder(pipe_control::kRunOrClosePipeMessageId, 0,
29 size, 0);
30
31 pipe_control::internal::RunOrClosePipeMessageParams_Data* params = nullptr;
32 internal::Serialize<pipe_control::RunOrClosePipeMessageParamsDataView>(
33 params_ptr, builder.buffer(), ¶ms, &context);
34 builder.message()->set_interface_id(kInvalidInterfaceId);
35 return std::move(*builder.message());
36 }
37
38 } // namespace
39
PipeControlMessageProxy(MessageReceiver * receiver)40 PipeControlMessageProxy::PipeControlMessageProxy(MessageReceiver* receiver)
41 : receiver_(receiver) {}
42
NotifyPeerEndpointClosed(InterfaceId id,const base::Optional<DisconnectReason> & reason)43 void PipeControlMessageProxy::NotifyPeerEndpointClosed(
44 InterfaceId id,
45 const base::Optional<DisconnectReason>& reason) {
46 Message message(ConstructPeerEndpointClosedMessage(id, reason));
47 bool ok = receiver_->Accept(&message);
48 ALLOW_UNUSED_LOCAL(ok);
49 }
50
51 // static
ConstructPeerEndpointClosedMessage(InterfaceId id,const base::Optional<DisconnectReason> & reason)52 Message PipeControlMessageProxy::ConstructPeerEndpointClosedMessage(
53 InterfaceId id,
54 const base::Optional<DisconnectReason>& reason) {
55 auto event = pipe_control::PeerAssociatedEndpointClosedEvent::New();
56 event->id = id;
57 if (reason) {
58 event->disconnect_reason = pipe_control::DisconnectReason::New();
59 event->disconnect_reason->custom_reason = reason->custom_reason;
60 event->disconnect_reason->description = reason->description;
61 }
62
63 auto input = pipe_control::RunOrClosePipeInput::New();
64 input->set_peer_associated_endpoint_closed_event(std::move(event));
65
66 return ConstructRunOrClosePipeMessage(std::move(input));
67 }
68
69 } // namespace mojo
70