File: message_for_transit.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (115 lines) | stat: -rw-r--r-- 3,545 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef MOJO_EDK_SYSTEM_MESSAGE_FOR_TRANSIT_H_
#define MOJO_EDK_SYSTEM_MESSAGE_FOR_TRANSIT_H_

#include <stdint.h>

#include <memory>

#include "base/macros.h"
#include "base/memory/ptr_util.h"
#include "mojo/edk/system/dispatcher.h"
#include "mojo/edk/system/ports_message.h"
#include "mojo/edk/system/system_impl_export.h"

namespace mojo {
namespace edk {

// MessageForTransit holds onto a PortsMessage which may be sent via
// |MojoWriteMessage()| or which may have been received on a pipe endpoint.
// Instances of this class are exposed to Mojo system API consumers via the
// opaque pointers used with |MojoCreateMessage()|, |MojoDestroyMessage()|,
// |MojoWriteMessageNew()|, and |MojoReadMessageNew()|.
class MOJO_SYSTEM_IMPL_EXPORT MessageForTransit {
 public:
#pragma pack(push, 1)
  // Header attached to every message.
  struct MessageHeader {
    // The number of serialized dispatchers included in this header.
    uint32_t num_dispatchers;

    // Total size of the header, including serialized dispatcher data.
    uint32_t header_size;
  };

  // Header for each dispatcher in a message, immediately following the message
  // header.
  struct DispatcherHeader {
    // The type of the dispatcher, correpsonding to the Dispatcher::Type enum.
    int32_t type;

    // The size of the serialized dispatcher, not including this header.
    uint32_t num_bytes;

    // The number of ports needed to deserialize this dispatcher.
    uint32_t num_ports;

    // The number of platform handles needed to deserialize this dispatcher.
    uint32_t num_platform_handles;
  };
#pragma pack(pop)

  ~MessageForTransit();

  // A static constructor for building outbound messages.
  static MojoResult Create(
      std::unique_ptr<MessageForTransit>* message,
      uint32_t num_bytes,
      const Dispatcher::DispatcherInTransit* dispatchers,
      uint32_t num_dispatchers);

  // A static constructor for wrapping inbound messages.
  static std::unique_ptr<MessageForTransit> WrapPortsMessage(
      std::unique_ptr<PortsMessage> message) {
    return base::WrapUnique(new MessageForTransit(std::move(message)));
  }

  const void* bytes() const {
    DCHECK(message_);
    return static_cast<const void*>(
        static_cast<const char*>(message_->payload_bytes()) +
            header()->header_size);
  }

  void* mutable_bytes() {
    DCHECK(message_);
    return static_cast<void*>(
        static_cast<char*>(message_->mutable_payload_bytes()) +
            header()->header_size);
  }

  size_t num_bytes() const {
    size_t header_size = header()->header_size;
    DCHECK_GE(message_->num_payload_bytes(), header_size);
    return message_->num_payload_bytes() - header_size;
  }

  size_t num_handles() const { return header()->num_dispatchers; }

  const PortsMessage& ports_message() const { return *message_; }

  std::unique_ptr<PortsMessage> TakePortsMessage() {
    return std::move(message_);
  }

 private:
  explicit MessageForTransit(std::unique_ptr<PortsMessage> message);

  const MessageForTransit::MessageHeader* header() const {
    DCHECK(message_);
    return static_cast<const MessageForTransit::MessageHeader*>(
        message_->payload_bytes());
  }

  std::unique_ptr<PortsMessage> message_;

  DISALLOW_COPY_AND_ASSIGN(MessageForTransit);
};

}  // namespace edk
}  // namespace mojo

#endif  // MOJO_EDK_SYSTEM_MESSAGE_FOR_TRANSIT_H_