• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_handler.h"
6 
7 #include "base/logging.h"
8 #include "mojo/public/cpp/bindings/lib/message_builder.h"
9 #include "mojo/public/cpp/bindings/lib/serialization.h"
10 #include "mojo/public/cpp/bindings/lib/validation_context.h"
11 #include "mojo/public/cpp/bindings/lib/validation_util.h"
12 #include "mojo/public/cpp/bindings/pipe_control_message_handler_delegate.h"
13 #include "mojo/public/interfaces/bindings/pipe_control_messages.mojom.h"
14 
15 namespace mojo {
16 
PipeControlMessageHandler(PipeControlMessageHandlerDelegate * delegate)17 PipeControlMessageHandler::PipeControlMessageHandler(
18     PipeControlMessageHandlerDelegate* delegate)
19     : delegate_(delegate) {}
20 
~PipeControlMessageHandler()21 PipeControlMessageHandler::~PipeControlMessageHandler() {}
22 
SetDescription(const std::string & description)23 void PipeControlMessageHandler::SetDescription(const std::string& description) {
24   description_ = description;
25 }
26 
27 // static
IsPipeControlMessage(const Message * message)28 bool PipeControlMessageHandler::IsPipeControlMessage(const Message* message) {
29   return !IsValidInterfaceId(message->interface_id());
30 }
31 
Accept(Message * message)32 bool PipeControlMessageHandler::Accept(Message* message) {
33   if (!Validate(message))
34     return false;
35 
36   if (message->name() == pipe_control::kRunOrClosePipeMessageId)
37     return RunOrClosePipe(message);
38 
39   NOTREACHED();
40   return false;
41 }
42 
Validate(Message * message)43 bool PipeControlMessageHandler::Validate(Message* message) {
44   internal::ValidationContext validation_context(
45       message->data(), message->data_num_bytes(), 0, message, description_);
46 
47   if (message->name() == pipe_control::kRunOrClosePipeMessageId) {
48     if (!internal::ValidateMessageIsRequestWithoutResponse(
49             message, &validation_context)) {
50       return false;
51     }
52     return internal::ValidateMessagePayload<
53         pipe_control::internal::RunOrClosePipeMessageParams_Data>(
54             message, &validation_context);
55   }
56 
57   return false;
58 }
59 
RunOrClosePipe(Message * message)60 bool PipeControlMessageHandler::RunOrClosePipe(Message* message) {
61   pipe_control::internal::RunOrClosePipeMessageParams_Data* params =
62       reinterpret_cast<
63           pipe_control::internal::RunOrClosePipeMessageParams_Data*>(
64           message->mutable_payload());
65   pipe_control::RunOrClosePipeMessageParamsPtr params_ptr;
66   internal::Deserialize<pipe_control::RunOrClosePipeMessageParamsPtr>(
67       params, &params_ptr, &context_);
68 
69   if (params_ptr->input->is_peer_associated_endpoint_closed_event()) {
70     return delegate_->OnPeerAssociatedEndpointClosed(
71         params_ptr->input->get_peer_associated_endpoint_closed_event()->id);
72   }
73   if (params_ptr->input->is_associated_endpoint_closed_before_sent_event()) {
74     return delegate_->OnAssociatedEndpointClosedBeforeSent(
75         params_ptr->input->get_associated_endpoint_closed_before_sent_event()
76             ->id);
77   }
78 
79   DVLOG(1) << "Unsupported command in a RunOrClosePipe message pipe control "
80            << "message. Closing the pipe.";
81   return false;
82 }
83 
84 }  // namespace mojo
85