aboutsummaryrefslogtreecommitdiff
path: root/mojo/public/cpp/bindings/lib/message_buffer.h
blob: 038213149f34e953646cf43f525195babfb014eb (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
// 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_PUBLIC_CPP_BINDINGS_MESSAGE_LIB_MESSAGE_BUFFER_H_
#define MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_LIB_MESSAGE_BUFFER_H_

#include <stddef.h>
#include <stdint.h>

#include <utility>

#include "base/macros.h"
#include "mojo/public/cpp/bindings/lib/buffer.h"
#include "mojo/public/cpp/system/message.h"

namespace mojo {
namespace internal {

// A fixed-size Buffer implementation using a Mojo message object for storage.
class MessageBuffer : public Buffer {
 public:
  // Initializes this buffer to carry a fixed byte capacity and no handles.
  MessageBuffer(size_t capacity, bool zero_initialized);

  // Initializes this buffer from an existing Mojo MessageHandle.
  MessageBuffer(ScopedMessageHandle message, uint32_t num_bytes);

  ~MessageBuffer() override;

  void* data() const { return buffer_; }
  uint32_t data_num_bytes() const { return data_num_bytes_; }

  // Buffer:
  void* Allocate(size_t delta) override;

  ScopedMessageHandle TakeMessage() { return std::move(message_); }

  void NotifyBadMessage(const std::string& error);

 private:
  uint32_t data_num_bytes_ = 0;
  ScopedMessageHandle message_;
  void* buffer_;

  uint32_t bytes_claimed_ = 0;

  DISALLOW_COPY_AND_ASSIGN(MessageBuffer);
};

}  // namespace internal
}  // namespace mojo

#endif  // MOJO_PUBLIC_CPP_BINDINGS_MESSAGE_LIB_MESSAGE_BUFFER_H_