summaryrefslogtreecommitdiffstats
path: root/ipc/chromium/src/chrome/common/ipc_message_utils.cc
blob: 49cb277e5f93e5f78820a3d6f60d8c480a4f8441 (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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "chrome/common/ipc_message_utils.h"
#include "mozilla/ipc/SharedMemory.h"
#include "mozilla/ipc/SharedMemoryBasic.h"

namespace IPC {

static uint32_t kShmemThreshold = 64 * 1024;

MessageBufferWriter::MessageBufferWriter(MessageWriter* writer,
                                         uint32_t full_len)
    : writer_(writer) {
  if (full_len > kShmemThreshold) {
    shmem_ = new mozilla::ipc::SharedMemoryBasic();
    if (!shmem_->Create(full_len)) {
      writer->FatalError("SharedMemory::Create failed!");
      return;
    }
    if (!shmem_->Map(full_len)) {
      writer->FatalError("SharedMemory::Map failed");
      return;
    }
    if (!shmem_->WriteHandle(writer)) {
      writer->FatalError("SharedMemory::WriterHandle failed");
      return;
    }
    buffer_ = reinterpret_cast<char*>(shmem_->memory());
  }
  remaining_ = full_len;
}

MessageBufferWriter::~MessageBufferWriter() {
  if (remaining_ != 0) {
    writer_->FatalError("didn't fully write message buffer");
  }
}

bool MessageBufferWriter::WriteBytes(const void* data, uint32_t len) {
  MOZ_RELEASE_ASSERT(len == remaining_ || (len % 4) == 0,
                     "all writes except for the final write must be a multiple "
                     "of 4 bytes in length due to padding");
  if (len > remaining_) {
    writer_->FatalError("MessageBufferWriter overrun");
    return false;
  }
  remaining_ -= len;
  // If we're serializing using a shared memory region, `buffer_` will be
  // initialized to point into that region.
  if (buffer_) {
    memcpy(buffer_, data, len);
    buffer_ += len;
    return true;
  }
  return writer_->WriteBytes(data, len);
}

MessageBufferReader::MessageBufferReader(MessageReader* reader,
                                         uint32_t full_len)
    : reader_(reader) {
  if (full_len > kShmemThreshold) {
    shmem_ = new mozilla::ipc::SharedMemoryBasic();
    if (!shmem_->ReadHandle(reader)) {
      reader->FatalError("SharedMemory::ReadHandle failed!");
      return;
    }
    if (!shmem_->Map(full_len)) {
      reader->FatalError("SharedMemory::Map failed");
      return;
    }
    buffer_ = reinterpret_cast<const char*>(shmem_->memory());
  }
  remaining_ = full_len;
}

MessageBufferReader::~MessageBufferReader() {
  if (remaining_ != 0) {
    reader_->FatalError("didn't fully write message buffer");
  }
}

bool MessageBufferReader::ReadBytesInto(void* data, uint32_t len) {
  MOZ_RELEASE_ASSERT(len == remaining_ || (len % 4) == 0,
                     "all reads except for the final read must be a multiple "
                     "of 4 bytes in length due to padding");
  if (len > remaining_) {
    reader_->FatalError("MessageBufferReader overrun");
    return false;
  }
  remaining_ -= len;
  // If we're serializing using a shared memory region, `buffer_` will be
  // initialized to point into that region.
  if (buffer_) {
    memcpy(data, buffer_, len);
    buffer_ += len;
    return true;
  }
  return reader_->ReadBytesInto(data, len);
}

}  // namespace IPC