• 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/lib/control_message_handler.h"
6 
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <utility>
10 
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/interface_control_messages.mojom.h"
15 
16 namespace mojo {
17 namespace internal {
18 
19 // static
IsControlMessage(const Message * message)20 bool ControlMessageHandler::IsControlMessage(const Message* message) {
21   return message->header()->name == kRunMessageId ||
22          message->header()->name == kRunOrClosePipeMessageId;
23 }
24 
ControlMessageHandler(uint32_t interface_version)25 ControlMessageHandler::ControlMessageHandler(uint32_t interface_version)
26     : interface_version_(interface_version) {
27 }
28 
~ControlMessageHandler()29 ControlMessageHandler::~ControlMessageHandler() {
30 }
31 
Accept(Message * message)32 bool ControlMessageHandler::Accept(Message* message) {
33   if (message->header()->name == kRunOrClosePipeMessageId)
34     return RunOrClosePipe(message);
35 
36   NOTREACHED();
37   return false;
38 }
39 
AcceptWithResponder(Message * message,MessageReceiverWithStatus * responder)40 bool ControlMessageHandler::AcceptWithResponder(
41     Message* message,
42     MessageReceiverWithStatus* responder) {
43   if (message->header()->name == kRunMessageId)
44     return Run(message, responder);
45 
46   NOTREACHED();
47   return false;
48 }
49 
Run(Message * message,MessageReceiverWithStatus * responder)50 bool ControlMessageHandler::Run(Message* message,
51                                 MessageReceiverWithStatus* responder) {
52   RunResponseMessageParamsPtr response_params_ptr(
53       RunResponseMessageParams::New());
54   response_params_ptr->reserved0 = 16u;
55   response_params_ptr->reserved1 = 0u;
56   response_params_ptr->query_version_result = QueryVersionResult::New();
57   response_params_ptr->query_version_result->version = interface_version_;
58 
59   size_t size = PrepareToSerialize<RunResponseMessageParamsPtr>(
60       response_params_ptr, &context_);
61   ResponseMessageBuilder builder(kRunMessageId, size, message->request_id());
62 
63   RunResponseMessageParams_Data* response_params = nullptr;
64   Serialize<RunResponseMessageParamsPtr>(response_params_ptr, builder.buffer(),
65                                          &response_params, &context_);
66   bool ok = responder->Accept(builder.message());
67   ALLOW_UNUSED_LOCAL(ok);
68   delete responder;
69 
70   return true;
71 }
72 
RunOrClosePipe(Message * message)73 bool ControlMessageHandler::RunOrClosePipe(Message* message) {
74   RunOrClosePipeMessageParams_Data* params =
75       reinterpret_cast<RunOrClosePipeMessageParams_Data*>(
76           message->mutable_payload());
77   RunOrClosePipeMessageParamsPtr params_ptr;
78   Deserialize<RunOrClosePipeMessageParamsPtr>(params, &params_ptr, &context_);
79 
80   return interface_version_ >= params_ptr->require_version->version;
81 }
82 
83 }  // namespace internal
84 }  // namespace mojo
85