Remove the RenderProcessHost observer and attach the WebContentsObserver earlier...
[chromium-blink-merge.git] / mojo / public / cpp / system / buffer.h
blob8b6a618f12a8a675185f857a72fff69cc827cb1f
1 // Copyright 2014 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.
5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
8 #include <assert.h>
10 #include "mojo/public/c/system/buffer.h"
11 #include "mojo/public/cpp/system/handle.h"
12 #include "mojo/public/cpp/system/macros.h"
14 namespace mojo {
16 // SharedBufferHandle ----------------------------------------------------------
18 class SharedBufferHandle : public Handle {
19 public:
20 SharedBufferHandle() {}
21 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {}
23 // Copying and assignment allowed.
26 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle),
27 "Bad size for C++ SharedBufferHandle");
29 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle;
30 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle),
31 "Bad size for C++ ScopedSharedBufferHandle");
33 inline MojoResult CreateSharedBuffer(
34 const MojoCreateSharedBufferOptions* options,
35 uint64_t num_bytes,
36 ScopedSharedBufferHandle* shared_buffer) {
37 assert(shared_buffer);
38 SharedBufferHandle handle;
39 MojoResult rv =
40 MojoCreateSharedBuffer(options, num_bytes, handle.mutable_value());
41 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
42 // will be used).
43 shared_buffer->reset(handle);
44 return rv;
47 // TODO(vtl): This (and also the functions below) are templatized to allow for
48 // future/other buffer types. A bit "safer" would be to overload this function
49 // manually. (The template enforces that the in and out handles to be of the
50 // same type.)
51 template <class BufferHandleType>
52 inline MojoResult DuplicateBuffer(
53 BufferHandleType buffer,
54 const MojoDuplicateBufferHandleOptions* options,
55 ScopedHandleBase<BufferHandleType>* new_buffer) {
56 assert(new_buffer);
57 BufferHandleType handle;
58 MojoResult rv = MojoDuplicateBufferHandle(
59 buffer.value(), options, handle.mutable_value());
60 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
61 // will be used).
62 new_buffer->reset(handle);
63 return rv;
66 template <class BufferHandleType>
67 inline MojoResult MapBuffer(BufferHandleType buffer,
68 uint64_t offset,
69 uint64_t num_bytes,
70 void** pointer,
71 MojoMapBufferFlags flags) {
72 assert(buffer.is_valid());
73 return MojoMapBuffer(buffer.value(), offset, num_bytes, pointer, flags);
76 inline MojoResult UnmapBuffer(void* pointer) {
77 assert(pointer);
78 return MojoUnmapBuffer(pointer);
81 // A wrapper class that automatically creates a shared buffer and owns the
82 // handle.
83 class SharedBuffer {
84 public:
85 explicit SharedBuffer(uint64_t num_bytes);
86 SharedBuffer(uint64_t num_bytes,
87 const MojoCreateSharedBufferOptions& options);
88 ~SharedBuffer();
90 ScopedSharedBufferHandle handle;
93 inline SharedBuffer::SharedBuffer(uint64_t num_bytes) {
94 MojoResult result MOJO_ALLOW_UNUSED =
95 CreateSharedBuffer(nullptr, num_bytes, &handle);
96 assert(result == MOJO_RESULT_OK);
99 inline SharedBuffer::SharedBuffer(
100 uint64_t num_bytes,
101 const MojoCreateSharedBufferOptions& options) {
102 MojoResult result MOJO_ALLOW_UNUSED =
103 CreateSharedBuffer(&options, num_bytes, &handle);
104 assert(result == MOJO_RESULT_OK);
107 inline SharedBuffer::~SharedBuffer() {
110 } // namespace mojo
112 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_