summaryrefslogtreecommitdiffstats
path: root/ipc/chromium/src/chrome/common/ipc_message.cc
blob: 5343c67fc0b2b1cf88d5a16b044bf30aada24c60 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
// Copyright (c) 2006-2008 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.

#include "chrome/common/ipc_message.h"

#include "base/logging.h"
#include "build/build_config.h"
#include "mojo/core/ports/event.h"

#include <utility>

#include "nsISupportsImpl.h"

namespace IPC {

//------------------------------------------------------------------------------

const mojo::core::ports::UserMessage::TypeInfo Message::kUserMessageTypeInfo{};

Message::~Message() { MOZ_COUNT_DTOR(IPC::Message); }

Message::Message(int32_t routing_id, msgid_t type, uint32_t segment_capacity,
                 HeaderFlags flags)
    : UserMessage(&kUserMessageTypeInfo),
      Pickle(sizeof(Header), segment_capacity) {
  MOZ_COUNT_CTOR(IPC::Message);
  header()->routing = routing_id;
  header()->type = type;
  header()->flags = flags;
  header()->num_handles = 0;
  header()->txid = -1;
  header()->seqno = 0;
#if defined(OS_MACOSX)
  header()->cookie = 0;
  header()->num_send_rights = 0;
#endif
  header()->event_footer_size = 0;
}

Message::Message(const char* data, int data_len)
    : UserMessage(&kUserMessageTypeInfo),
      Pickle(sizeof(Header), data, data_len) {
  MOZ_COUNT_CTOR(IPC::Message);
}

/*static*/ mozilla::UniquePtr<Message> Message::IPDLMessage(
    int32_t routing_id, msgid_t type, uint32_t segment_capacity,
    HeaderFlags flags) {
  return mozilla::MakeUnique<Message>(routing_id, type, segment_capacity,
                                      flags);
}

/*static*/ mozilla::UniquePtr<Message> Message::ForSyncDispatchError(
    NestedLevel level) {
  auto m = mozilla::MakeUnique<Message>(0, 0, 0, HeaderFlags(level));
  auto& flags = m->header()->flags;
  flags.SetSync();
  flags.SetReply();
  flags.SetReplyError();
  return m;
}

void Message::WriteFooter(const void* data, uint32_t data_len) {
  if (data_len == 0) {
    return;
  }

  WriteBytes(data, data_len);
}

bool Message::ReadFooter(void* buffer, uint32_t buffer_len, bool truncate) {
  if (buffer_len == 0) {
    return true;
  }

  if (NS_WARN_IF(AlignInt(header()->payload_size) != header()->payload_size) ||
      NS_WARN_IF(AlignInt(buffer_len) > header()->payload_size)) {
    return false;
  }

  // Seek to the start of the footer, and read it in. We read in with a
  // duplicate of the iterator so we can use it to truncate later.
  uint32_t offset = header()->payload_size - AlignInt(buffer_len);
  PickleIterator footer_iter(*this);
  if (NS_WARN_IF(!IgnoreBytes(&footer_iter, offset))) {
    return false;
  }

  PickleIterator read_iter(footer_iter);
  bool ok = ReadBytesInto(&read_iter, buffer, buffer_len);

  // If requested, truncate the buffer to the start of the footer.
  if (truncate) {
    Truncate(&footer_iter);
  }
  return ok;
}

bool Message::WriteFileHandle(mozilla::UniqueFileHandle handle) {
  uint32_t handle_index = attached_handles_.Length();
  WriteUInt32(handle_index);
  if (handle_index == MAX_DESCRIPTORS_PER_MESSAGE) {
    return false;
  }
  attached_handles_.AppendElement(std::move(handle));
  return true;
}

bool Message::ConsumeFileHandle(PickleIterator* iter,
                                mozilla::UniqueFileHandle* handle) const {
  uint32_t handle_index;
  if (!ReadUInt32(iter, &handle_index)) {
    return false;
  }
  if (handle_index >= attached_handles_.Length()) {
    return false;
  }
  // NOTE: This mutates the underlying array, replacing the handle with an
  // invalid handle.
  *handle = std::exchange(attached_handles_[handle_index], nullptr);
  return true;
}

void Message::SetAttachedFileHandles(
    nsTArray<mozilla::UniqueFileHandle> handles) {
  MOZ_DIAGNOSTIC_ASSERT(attached_handles_.IsEmpty());
  attached_handles_ = std::move(handles);
}

uint32_t Message::num_handles() const { return attached_handles_.Length(); }

void Message::WritePort(mozilla::ipc::ScopedPort port) {
  uint32_t port_index = attached_ports_.Length();
  WriteUInt32(port_index);
  attached_ports_.AppendElement(std::move(port));
}

bool Message::ConsumePort(PickleIterator* iter,
                          mozilla::ipc::ScopedPort* port) const {
  uint32_t port_index;
  if (!ReadUInt32(iter, &port_index)) {
    return false;
  }
  if (port_index >= attached_ports_.Length()) {
    return false;
  }
  // NOTE: This mutates the underlying array, replacing the port with a consumed
  // port.
  *port = std::exchange(attached_ports_[port_index], {});
  return true;
}

void Message::SetAttachedPorts(nsTArray<mozilla::ipc::ScopedPort> ports) {
  MOZ_DIAGNOSTIC_ASSERT(attached_ports_.IsEmpty());
  attached_ports_ = std::move(ports);
}

#if defined(OS_MACOSX)
bool Message::WriteMachSendRight(mozilla::UniqueMachSendRight port) {
  uint32_t index = attached_send_rights_.Length();
  WriteUInt32(index);
  if (index == MAX_DESCRIPTORS_PER_MESSAGE) {
    return false;
  }
  attached_send_rights_.AppendElement(std::move(port));
  return true;
}

bool Message::ConsumeMachSendRight(PickleIterator* iter,
                                   mozilla::UniqueMachSendRight* port) const {
  uint32_t index;
  if (!ReadUInt32(iter, &index)) {
    return false;
  }
  if (index >= attached_send_rights_.Length()) {
    return false;
  }
  // NOTE: This mutates the underlying array, replacing the send right with a
  // null right.
  *port = std::exchange(attached_send_rights_[index], nullptr);
  return true;
}

uint32_t Message::num_send_rights() const {
  return attached_send_rights_.Length();
}
#endif

bool Message::WillBeRoutedExternally(
    mojo::core::ports::UserMessageEvent& event) {
  if (!attached_ports_.IsEmpty()) {
    // Explicitly attach any ports which were attached to this Message to this
    // UserMessageEvent before we route it externally so that they can be
    // transferred correctly. These ports will be recovered if needed in
    // `GetMessage`.
    MOZ_DIAGNOSTIC_ASSERT(
        event.num_ports() == 0,
        "Must not have previously attached ports to the UserMessageEvent");
    event.ReservePorts(attached_ports_.Length());
    for (size_t i = 0; i < event.num_ports(); ++i) {
      event.ports()[i] = attached_ports_[i].Release().name();
    }
    attached_ports_.Clear();
  }
  return true;
}

void Message::AssertAsLargeAsHeader() const {
  MOZ_DIAGNOSTIC_ASSERT(size() >= sizeof(Header));
  MOZ_DIAGNOSTIC_ASSERT(CurrentSize() >= sizeof(Header));
  // Our buffers should agree with what our header specifies.
  MOZ_DIAGNOSTIC_ASSERT(size() == CurrentSize());
}

}  // namespace IPC